Wiki » Historique » Version 49
Patrice Nadeau, 2023-09-03 16:36
| 1 | 1 | Patrice Nadeau | # Règles de codage C |
|---|---|---|---|
| 2 | |||
| 3 | Le langage C, version C99 (ISO/IEC 9899:1999) utilisé avec le compilateur [GCC](https://gcc.gnu.org/). |
||
| 4 | |||
| 5 | > `gcc` n'est pas entièrement compatible avec le standard C99 (<https://gcc.gnu.org/c99status.html>). |
||
| 6 | |||
| 7 | --- |
||
| 8 | {{toc}} |
||
| 9 | |||
| 10 | ## Style |
||
| 11 | |||
| 12 | 6 | Patrice Nadeau | Le code DOIT : |
| 13 | 5 | Patrice Nadeau | * Être dans le style [K&R](https://fr.wikipedia.org/wiki/Style_d%27indentation#Style_K&R) avec la variante *one true brace style* (1TBS): |
| 14 | 1 | Patrice Nadeau | * L’indentation est de 4 espaces |
| 15 | * Le « backslash » est utilisé pour les lignes de plus de 80 caractères |
||
| 16 | * Une instruction par ligne |
||
| 17 | * Une espace avant et après un opérateur sauf pour les opérateurs « [unaires](https://fr.wikipedia.org/wiki/Op%C3%A9ration_unaire) » |
||
| 18 | 46 | Patrice Nadeau | * Les fonctions, variables, constantes et `#define` DOIVENT être en [anglais américain](https://fr.wikipedia.org/wiki/Anglais_am%C3%A9ricain) |
| 19 | 47 | Patrice Nadeau | * Les commentaires et documentation DOIVENT être en français, avec le logiciel [Doxygen](https://www.doxygen.nl/) : |
| 20 | * Chaque objet (fonctions, variables, etc.) DOIT être commenté/documenté : |
||
| 21 | * Dans le format [Javadoc](https://www.doxygen.nl/manual/docblocks.html) (/** */) |
||
| 22 | * Avant sa déclaration |
||
| 23 | * Les « décorations » sont faites avec la syntaxe Markdown |
||
| 24 | 1 | Patrice Nadeau | |
| 25 | 46 | Patrice Nadeau | Justifications : |
| 26 | 1 | Patrice Nadeau | * [K&R](https://fr.wikipedia.org/wiki/Style_d%27indentation#Style_K&R) |
| 27 | * Prévient les erreurs lors d'ajout dans les boucles n'ayant qu'une instruction comme bloc |
||
| 28 | 46 | Patrice Nadeau | * Support ASCII 7-bits |
| 29 | * Correspondance avec la fiche technique (datasheet) |
||
| 30 | * [Loi sur la langue officielle et commune du Québec, le français](https://www.publicationsduquebec.gouv.qc.ca/fileadmin/Fichiers_client/lois_et_reglements/LoisAnnuelles/fr/2022/2022C14F.PDF) |
||
| 31 | 1 | Patrice Nadeau | |
| 32 | Exemple : |
||
| 33 | ``` c |
||
| 34 | 47 | Patrice Nadeau | /** |
| 35 | * @brief Fonction principale |
||
| 36 | * @return Une evaleur |
||
| 37 | * @remark Note non importante |
||
| 38 | * @note Note générale |
||
| 39 | * @attention Note importante |
||
| 40 | * @warning Note conséquence négative |
||
| 41 | */ |
||
| 42 | 1 | Patrice Nadeau | int fonction(void) { |
| 43 | int x; |
||
| 44 | if (var != 1) { |
||
| 45 | 14 | Patrice Nadeau | x = x + 1; |
| 46 | 1 | Patrice Nadeau | y++; |
| 47 | printf("This is a long\ |
||
| 48 | line that should be splitted"); |
||
| 49 | } else { |
||
| 50 | 16 | Patrice Nadeau | x--; |
| 51 | }; |
||
| 52 | 20 | Patrice Nadeau | return 0; |
| 53 | } |
||
| 54 | ``` |
||
| 55 | |||
| 56 | 1 | Patrice Nadeau | ## Fichiers |
| 57 | Le nom des fichiers DOIT être composé de la manière suivante : |
||
| 58 | * En minuscule |
||
| 59 | * 8 caractères maximum |
||
| 60 | * L'extension est |
||
| 61 | * `.h` pour les fichiers d’entête |
||
| 62 | * `.c` pour les fichiers sources |
||
| 63 | * Contient une section Doxygen « file » |
||
| 64 | * Les fichier d’entête contiennent en plus |
||
| 65 | * Une section Doxygen « mainpage » |
||
| 66 | * Une définition macro DOIT être faite pour éviter de ré-inclure le fichier. |
||
| 67 | |||
| 68 | Exemple : |
||
| 69 | ```c |
||
| 70 | #ifndef _test_h |
||
| 71 | #define _test_h |
||
| 72 | /** |
||
| 73 | * @file : test.h |
||
| 74 | * @brief Description |
||
| 75 | * @version 0.00.01 |
||
| 76 | * @date 2023-02-26 |
||
| 77 | * @author Patrice Nadeau <pnadeau@patricenadeau.com> |
||
| 78 | * @copyright 2023 Patrice Nadeau |
||
| 79 | */ |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @mainpage lcd |
||
| 83 | 37 | Patrice Nadeau | * @brief ATMEL AVR 8-bit C librairie |
| 84 | 1 | Patrice Nadeau | * @author Patrice Nadeau <pnadeau@patricenadeau.com> |
| 85 | * @version 0.0.02 |
||
| 86 | * @date 2023-03-27 |
||
| 87 | 37 | Patrice Nadeau | * @pre AVR supportes (testés en gras): |
| 88 | 1 | Patrice Nadeau | * - ATmega88 |
| 89 | * - ATmega168 |
||
| 90 | 15 | Patrice Nadeau | * - **ATmega328P** |
| 91 | 1 | Patrice Nadeau | * @copyright |
| 92 | 13 | Patrice Nadeau | * @include{doc} LICENSE.txt |
| 93 | 1 | Patrice Nadeau | */ |
| 94 | |||
| 95 | ... |
||
| 96 | |||
| 97 | #endif /*_usart.h*/ |
||
| 98 | ``` |
||
| 99 | |||
| 100 | ## Commentaires |
||
| 101 | Les commentaires DOIVENT : |
||
| 102 | * Être de style « C » |
||
| 103 | * Précéder l’élément à documenté |
||
| 104 | * En minuscules et commencer par une majuscule |
||
| 105 | |||
| 106 | Exemple : |
||
| 107 | ``` c |
||
| 108 | /* Une seule ligne... */ |
||
| 109 | |||
| 110 | /* |
||
| 111 | * Sur |
||
| 112 | * plusieurs |
||
| 113 | * lignes |
||
| 114 | */ |
||
| 115 | ``` |
||
| 116 | |||
| 117 | ## Convention de noms |
||
| 118 | |||
| 119 | 30 | Patrice Nadeau | En général : *librairie*\_*action*\_*sujet* |
| 120 | 1 | Patrice Nadeau | |
| 121 | 31 | Patrice Nadeau | * Comporter au maximum **31** caractères |
| 122 | * Être séparées par des traits de soulignement si comporte plusieurs mots |
||
| 123 | * Exceptions : |
||
| 124 | * Fonction et variables DOIVENT |
||
| 125 | * Être en minuscule |
||
| 126 | * Macros, constantes et `#define` DOIVENT |
||
| 127 | * Être en majuscule |
||
| 128 | 1 | Patrice Nadeau | |
| 129 | Justification : |
||
| 130 | * Linux kernel coding style : <https://www.kernel.org/doc/html/v4.10/process/coding-style.html#naming> |
||
| 131 | * GNU Coding Standards <https://www.gnu.org/prep/standards/html_node/Writing-C.html#Writing-C> |
||
| 132 | * Embedded C Coding Standard : <https://barrgroup.com/embedded-systems/books/embedded-c-coding-standard> |
||
| 133 | |||
| 134 | ## Déclarations locales |
||
| 135 | |||
| 136 | Une déclaration n’ayant qu’une visibilité locale DOIT : |
||
| 137 | * Être de classe `static` |
||
| 138 | |||
| 139 | Exemple: |
||
| 140 | ``` c |
||
| 141 | /** |
||
| 142 | * @brief Local function |
||
| 143 | **/ |
||
| 144 | 7 | Patrice Nadeau | static int local_func(void) { |
| 145 | 1 | Patrice Nadeau | ... |
| 146 | return 0; |
||
| 147 | } |
||
| 148 | ``` |
||
| 149 | |||
| 150 | ## Items déconseillés et retirés |
||
| 151 | |||
| 152 | Les fonctions et variables ne devant plus être utilisés, DOIVENT générer un message lors de la compilation (*-Wall*) si un appel est effectué. |
||
| 153 | * Les attributs`__attribute__((deprecated))` ou `__attribute__((unavailable))` DOIVENT être ajoutés à la déclaration. |
||
| 154 | * La documentation DOIT indiquer les substituts à utiliser. |
||
| 155 | |||
| 156 | Exemple : |
||
| 157 | ``` c |
||
| 158 | /** |
||
| 159 | * @brief OldFunction |
||
| 160 | * @deprecated Use NewFunction instead |
||
| 161 | * @since Version x.x.xx |
||
| 162 | */ |
||
| 163 | int OldFunction(void) __attribute__((deprecated)); |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @brief OldFunction |
||
| 167 | * @deprecated Use NewFunction instead |
||
| 168 | * @since Version x.x.xx |
||
| 169 | */ |
||
| 170 | int OldFunction(void) __attribute__((unavailable)); |
||
| 171 | ``` |
||
| 172 | |||
| 173 | ## Constantes |
||
| 174 | |||
| 175 | Utilisé au lieu d’une macro quand le type ou la visibilité de la variable doit être définis. |
||
| 176 | |||
| 177 | DOIVENT être |
||
| 178 | 21 | Patrice Nadeau | * De classe `static` ou `extern` selon le besoin |
| 179 | 1 | Patrice Nadeau | |
| 180 | Exemple : |
||
| 181 | |||
| 182 | ``` c |
||
| 183 | /** |
||
| 184 | 38 | Patrice Nadeau | * @name Liste des constantes |
| 185 | 1 | Patrice Nadeau | * @brief |
| 186 | */ |
||
| 187 | /** @{ */ |
||
| 188 | 38 | Patrice Nadeau | /** @brief La chaîne d'initialisation du projet */ |
| 189 | 1 | Patrice Nadeau | static const char INIT_STR[6] = "POWER"; |
| 190 | 38 | Patrice Nadeau | /** @brief Constante globale de la librairie `random` */ |
| 191 | 1 | Patrice Nadeau | extern int RANDOM_MAX = 25; |
| 192 | /** @} */ |
||
| 193 | |||
| 194 | 38 | Patrice Nadeau | /** @brief Constante */ |
| 195 | 1 | Patrice Nadeau | const int ANSWER 42; |
| 196 | ``` |
||
| 197 | |||
| 198 | ## Énumérations |
||
| 199 | |||
| 200 | DOIT être utilisée pour définir une série de valeurs. |
||
| 201 | |||
| 202 | Exemple : |
||
| 203 | ```c |
||
| 204 | /** |
||
| 205 | * @name List of STATUS values |
||
| 206 | * @brief |
||
| 207 | * */ |
||
| 208 | enum STATUS { |
||
| 209 | /** @brief Everything is fine */ |
||
| 210 | STATUS_OK = 0, |
||
| 211 | /** @brief Initialisation in progress */ |
||
| 212 | STATUS_INIT, |
||
| 213 | /** @brief System halted */ |
||
| 214 | STATUS_HALTED |
||
| 215 | }; |
||
| 216 | ``` |
||
| 217 | |||
| 218 | ## Typedef |
||
| 219 | |||
| 220 | Format : |
||
| 221 | * En minuscule, suivie de **_t** |
||
| 222 | |||
| 223 | Exemple : |
||
| 224 | ``` c |
||
| 225 | 39 | Patrice Nadeau | /** Type de la structure dans la librairie `ds1305` */ |
| 226 | 1 | Patrice Nadeau | typedef struct { |
| 227 | 39 | Patrice Nadeau | /** @brief Dernier deux chiffres : ≥ 00, ≤ 99 */ |
| 228 | 1 | Patrice Nadeau | uint8_t year; |
| 229 | /** @brief 01 - 12 */ |
||
| 230 | uint8_t month; |
||
| 231 | /** @brief 01 - 31 */ |
||
| 232 | uint8_t date; |
||
| 233 | /** @brief 1 - 7 */ |
||
| 234 | uint8_t day; |
||
| 235 | /** @brief 00 - 23 */ |
||
| 236 | uint8_t hours; |
||
| 237 | /** @brief 00 - 59 */ |
||
| 238 | uint8_t minutes; |
||
| 239 | /** @brief 00 - 59 */ |
||
| 240 | uint8_t seconds; |
||
| 241 | } ds1305_time_t; |
||
| 242 | ``` |
||
| 243 | |||
| 244 | ## Variables |
||
| 245 | |||
| 246 | Exemple : |
||
| 247 | ``` c |
||
| 248 | 40 | Patrice Nadeau | /** @brief Variable locale */ |
| 249 | 1 | Patrice Nadeau | static int ctr; |
| 250 | 40 | Patrice Nadeau | /** @brief Variable globale */ |
| 251 | int RANDOM_CTR; |
||
| 252 | 1 | Patrice Nadeau | ``` |
| 253 | |||
| 254 | ## Structures |
||
| 255 | |||
| 256 | Format |
||
| 257 | * En minuscule, séparé par des «underscores» si nécessaire. |
||
| 258 | |||
| 259 | Exemple : |
||
| 260 | ``` c |
||
| 261 | /** |
||
| 262 | * @brief Structure for a local menu |
||
| 263 | * @see MenuSelect |
||
| 264 | */ |
||
| 265 | struct menu { |
||
| 266 | 8 | Patrice Nadeau | /** @brief Character used for the item */ |
| 267 | char choice; |
||
| 268 | /** @brief Description of the item */ |
||
| 269 | char *item; |
||
| 270 | 1 | Patrice Nadeau | }; |
| 271 | ``` |
||
| 272 | |||
| 273 | ## Fonctions |
||
| 274 | |||
| 275 | |||
| 276 | Le nom DOIT être dans le format suivant : *Action***_***Item***_***Attribut*, où *Action* signifie : |
||
| 277 | 29 | Patrice Nadeau | * **set**, **get**, **clear** : Règle, obtient ou vide un registre |
| 278 | 1 | Patrice Nadeau | * **read**, **write** : Lis ou écris dans un fichier |
| 279 | * **init** : Fonction d’initialisation |
||
| 280 | * **is** : Vérifie un état |
||
| 281 | 36 | Patrice Nadeau | * **setup** : Fonction de configuration des ports (AVR) |
| 282 | 1 | Patrice Nadeau | |
| 283 | Exceptions |
||
| 284 | 41 | Patrice Nadeau | * Les fonctions définies dans une librairie de bas niveau pour du matériel (« driver ») devraient utiliser le nom définis dans la fiche technique. |
| 285 | 1 | Patrice Nadeau | |
| 286 | Une fonction DEVRAIT retourner une valeur. |
||
| 287 | 28 | Patrice Nadeau | * Type entier (oui/non) : |
| 288 | 1 | Patrice Nadeau | * Succès : **0** |
| 289 | * Erreur : **1** |
||
| 290 | * Type booléen (Librairie `<stdbool.h>`) |
||
| 291 | * **true** |
||
| 292 | * **false** |
||
| 293 | * Pointeur : |
||
| 294 | * **NULL** : Erreur |
||
| 295 | * Autre valeur : adresse du pointeur |
||
| 296 | |||
| 297 | Justification : |
||
| 298 | * [AVR1000b](https://ww1.microchip.com/downloads/en/Appnotes/AVR1000b-Getting-Started-Writing-C-Code-for-AVR-DS90003262B.pdf) |
||
| 299 | |||
| 300 | Exemple : |
||
| 301 | |||
| 302 | ``` c |
||
| 303 | /** |
||
| 304 | 42 | Patrice Nadeau | * @brief Vérifie si une horloge est est initialisée |
| 305 | 1 | Patrice Nadeau | * @param[in] nb Timer number. @n Possible values : |
| 306 | 24 | Patrice Nadeau | * − @arg **TIMER_1** |
| 307 | * − @arg **TIMER_2** |
||
| 308 | 1 | Patrice Nadeau | * @return |
| 309 | 42 | Patrice Nadeau | * @retval true Horloge *nb* est initialisée |
| 310 | * @retval false Horloge *nb* n'est PAS initialisée |
||
| 311 | 1 | Patrice Nadeau | * @pre init_timer |
| 312 | **/ |
||
| 313 | static bool is_timer_set(uint8_t nb); |
||
| 314 | |||
| 315 | ``` |
||
| 316 | |||
| 317 | 11 | Patrice Nadeau | ## Préprocesseur |
| 318 | Directives du préprocesseur gcc. |
||
| 319 | 1 | Patrice Nadeau | |
| 320 | ### #include |
||
| 321 | |||
| 322 | Pour inclure d’autres fichier comme les fichiers entête. |
||
| 323 | 43 | Patrice Nadeau | |
| 324 | 1 | Patrice Nadeau | |
| 325 | ### #ifdef / ifndef |
||
| 326 | |||
| 327 | Surtout utiliser pour des options de compilation sur différentes plateforme. |
||
| 328 | Utiliser une forme évitant les répétitions. |
||
| 329 | |||
| 330 | > N’est pas documenté dans Doxygen. |
||
| 331 | |||
| 332 | Exemple : |
||
| 333 | ```c |
||
| 334 | const char BLUE = |
||
| 335 | #if ENABLED(FEATURE_ONE) |
||
| 336 | '1' |
||
| 337 | #else |
||
| 338 | '0' |
||
| 339 | #endif |
||
| 340 | ; |
||
| 341 | ``` |
||
| 342 | |||
| 343 | ### Diagnostiques |
||
| 344 | |||
| 345 | Les macros `#warning` et `#error` sont utilisées pour afficher des avertissements (continue la compilation) ou des erreurs (arrête la compilation). |
||
| 346 | |||
| 347 | > Ne sont pas documentées dans Doxygen. |
||
| 348 | |||
| 349 | Exemple : |
||
| 350 | ``` c |
||
| 351 | #ifndef usart_AVR |
||
| 352 | #error "__FILE_NAME__ is not supported on this AVR !" |
||
| 353 | #endif |
||
| 354 | |||
| 355 | #ifndef __test__ |
||
| 356 | #warning "test is not defined !" |
||
| 357 | #endif |
||
| 358 | ``` |
||
| 359 | |||
| 360 | ### Définitions |
||
| 361 | |||
| 362 | Un `#define` est utilisé pour remplacer une valeur au moment de la compilation |
||
| 363 | > Pour la définition d'une valeur « integer », un `enum` DOIT être utilisé. |
||
| 364 | |||
| 365 | Exemple : |
||
| 366 | ``` c |
||
| 367 | /** |
||
| 368 | * @name Registers name |
||
| 369 | */ |
||
| 370 | /** @{ */ |
||
| 371 | /** @brief USART1 */ |
||
| 372 | #define USART1 REG1 |
||
| 373 | /** @brief USART2 */ |
||
| 374 | #define USART2 REG2 |
||
| 375 | /** @} */ |
||
| 376 | |||
| 377 | USART1 = 0x0F; |
||
| 378 | ``` |
||
| 379 | |||
| 380 | ## Atmel AVR |
||
| 381 | |||
| 382 | Particularités pour les microcontrôleurs 8 bits AVR d’Atmel. |
||
| 383 | |||
| 384 | [Atmel AVR4027: Tips and Tricks to Optimize Your C Code for 8-bit AVR Microcontrollers](https://ww1.microchip.com/downloads/en/AppNotes/doc8453.pdf) |
||
| 385 | |||
| 386 | ### Fichier d’en-têtes |
||
| 387 | |||
| 388 | 25 | Patrice Nadeau | Vérification du modèle de microcontrôleur |
| 389 | > Via l'option `-m` de [gcc](https://github.com/embecosm/avr-gcc/blob/avr-gcc-mainline/gcc/config/avr/avr-mcus.def) |
||
| 390 | |||
| 391 | 1 | Patrice Nadeau | ```c |
| 392 | 25 | Patrice Nadeau | #ifndef defined (__AVR_ATmega48__) || (__AVR_ATmega48P__) || \ |
| 393 | (__AVR_ATmega88P__) || defined (__AVR_ATmega88__) || \ |
||
| 394 | (__AVR_ATmega168__) || defined (__AVR_ATmega168P__) || \ |
||
| 395 | (__AVR_ATmega328__) || defined (__AVR_ATmega328P__) |
||
| 396 | #warning "Cette librairie n'as pas été testée sur cette famille de microcontrôleur." |
||
| 397 | #endif |
||
| 398 | 1 | Patrice Nadeau | ``` |
| 399 | |||
| 400 | ### Macros |
||
| 401 | 45 | Patrice Nadeau | |
| 402 | Définis dans le fichier `config.h` |
||
| 403 | |||
| 404 | Liste : |
||
| 405 | 1 | Patrice Nadeau | * `F_CPU` : La fréquence utilisée par l'horloge (interne ou externe) du microcontrôleur |
| 406 | |||
| 407 | > Les « fuses » doivent correspondent à la bonne source de l'horloge. |
||
| 408 | |||
| 409 | ### Types |
||
| 410 | |||
| 411 | De nouveau type d'entier sont fournis avec la librairie `<stdint.h>`. |
||
| 412 | |||
| 413 | L'utilisation de ces types DOIT être utilisé afin d'exprimer le nombre de bit d'un objet. |
||
| 414 | |||
| 415 | ### Progmem |
||
| 416 | 44 | Patrice Nadeau | |
| 417 | <https://www.avrfreaks.net/s/topic/a5C3l000000U5SFEA0/t034767> |
||
| 418 | |||
| 419 | 1 | Patrice Nadeau | Pour mettre des variables en lecture seule dans la section FLASH au lieu de SRAM avec `<avr/pgmspace.h>`. |
| 420 | > L’accès à ces variables est faite via les macros de la librairie. |
||
| 421 | |||
| 422 | Le nom de la variable DOIT être suivie de **_P** |
||
| 423 | |||
| 424 | Exemple : |
||
| 425 | ```c |
||
| 426 | #include <avr/pgmspace.h> |
||
| 427 | ... |
||
| 428 | /** @brief Variable en FLASH */ |
||
| 429 | const int Variable1_P PROGMEM = 42; |
||
| 430 | ``` |
||
| 431 | |||
| 432 | ### Fonction main |
||
| 433 | Un microcontrôleur AVR ne termine jamais la fonction `main`. |
||
| 434 | |||
| 435 | * Déclarer la fonction main avec l’attribut `noreturn` |
||
| 436 | * La boucle sans fin la plus optimisé est le `for (;;)` |
||
| 437 | |||
| 438 | Justification : [AVR035](https://ww1.microchip.com/downloads/en/AppNotes/doc1497.pdf) |
||
| 439 | |||
| 440 | Exemple : |
||
| 441 | ```c |
||
| 442 | 26 | Patrice Nadeau | #include <avr/io.h> |
| 443 | |||
| 444 | 1 | Patrice Nadeau | /** |
| 445 | * @brief Never ending loop |
||
| 446 | */ |
||
| 447 | 12 | Patrice Nadeau | void main(void) __attribute__ ((noreturn)); |
| 448 | 1 | Patrice Nadeau | |
| 449 | /* main function definition */ |
||
| 450 | 9 | Patrice Nadeau | void main(void) { |
| 451 | 1 | Patrice Nadeau | ... |
| 452 | /* never return */ |
||
| 453 | for (;;) { |
||
| 454 | }; |
||
| 455 | }; |
||
| 456 | ``` |
||
| 457 | |||
| 458 | ### Atomic |
||
| 459 | Opérations ne devant pas être interrompus comme charger un registre de 16 bits avec un registre de 8 bits. |
||
| 460 | |||
| 461 | La librairie `avr-libc` (util/atomic.h) fournit des macros permettant la gestion entre autre des interruptions. |
||
| 462 | |||
| 463 | Les instructions critiques sont insérées dans un `ATOMIC_BLOCK`. |
||
| 464 | |||
| 465 | Exemple : |
||
| 466 | ```c |
||
| 467 | ... |
||
| 468 | ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { |
||
| 469 | ... |
||
| 470 | } |
||
| 471 | ... |
||
| 472 | ``` |