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