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