Wiki » Historique » Version 54
Patrice Nadeau, 2023-09-03 16:51
| 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 | ## Convention de noms |
||
| 105 | |||
| 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 | ## Items déconseillés et retirés |
||
| 138 | |||
| 139 | 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é. |
||
| 140 | * Les attributs`__attribute__((deprecated))` ou `__attribute__((unavailable))` DOIVENT être ajoutés à la déclaration. |
||
| 141 | * La documentation DOIT indiquer les substituts à utiliser. |
||
| 142 | |||
| 143 | Exemple : |
||
| 144 | ``` c |
||
| 145 | /** |
||
| 146 | * @brief OldFunction |
||
| 147 | * @deprecated Use NewFunction instead |
||
| 148 | * @since Version x.x.xx |
||
| 149 | */ |
||
| 150 | int OldFunction(void) __attribute__((deprecated)); |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @brief OldFunction |
||
| 154 | * @deprecated Use NewFunction instead |
||
| 155 | * @since Version x.x.xx |
||
| 156 | */ |
||
| 157 | int OldFunction(void) __attribute__((unavailable)); |
||
| 158 | ``` |
||
| 159 | |||
| 160 | ## Constantes |
||
| 161 | |||
| 162 | Utilisé au lieu d’une macro quand le type ou la visibilité de la variable doit être définis. |
||
| 163 | |||
| 164 | DOIVENT être |
||
| 165 | 21 | Patrice Nadeau | * De classe `static` ou `extern` selon le besoin |
| 166 | 1 | Patrice Nadeau | |
| 167 | Exemple : |
||
| 168 | |||
| 169 | ``` c |
||
| 170 | /** |
||
| 171 | 38 | Patrice Nadeau | * @name Liste des constantes |
| 172 | 1 | Patrice Nadeau | * @brief |
| 173 | */ |
||
| 174 | /** @{ */ |
||
| 175 | 38 | Patrice Nadeau | /** @brief La chaîne d'initialisation du projet */ |
| 176 | 1 | Patrice Nadeau | static const char INIT_STR[6] = "POWER"; |
| 177 | 38 | Patrice Nadeau | /** @brief Constante globale de la librairie `random` */ |
| 178 | 1 | Patrice Nadeau | extern int RANDOM_MAX = 25; |
| 179 | /** @} */ |
||
| 180 | |||
| 181 | 38 | Patrice Nadeau | /** @brief Constante */ |
| 182 | 1 | Patrice Nadeau | const int ANSWER 42; |
| 183 | ``` |
||
| 184 | |||
| 185 | ## Énumérations |
||
| 186 | |||
| 187 | DOIT être utilisée pour définir une série de valeurs. |
||
| 188 | |||
| 189 | Exemple : |
||
| 190 | ```c |
||
| 191 | /** |
||
| 192 | * @name List of STATUS values |
||
| 193 | * @brief |
||
| 194 | * */ |
||
| 195 | enum STATUS { |
||
| 196 | /** @brief Everything is fine */ |
||
| 197 | STATUS_OK = 0, |
||
| 198 | /** @brief Initialisation in progress */ |
||
| 199 | STATUS_INIT, |
||
| 200 | /** @brief System halted */ |
||
| 201 | STATUS_HALTED |
||
| 202 | }; |
||
| 203 | ``` |
||
| 204 | |||
| 205 | ## Typedef |
||
| 206 | |||
| 207 | Format : |
||
| 208 | * En minuscule, suivie de **_t** |
||
| 209 | |||
| 210 | Exemple : |
||
| 211 | ``` c |
||
| 212 | 39 | Patrice Nadeau | /** Type de la structure dans la librairie `ds1305` */ |
| 213 | 1 | Patrice Nadeau | typedef struct { |
| 214 | 39 | Patrice Nadeau | /** @brief Dernier deux chiffres : ≥ 00, ≤ 99 */ |
| 215 | 1 | Patrice Nadeau | uint8_t year; |
| 216 | /** @brief 01 - 12 */ |
||
| 217 | uint8_t month; |
||
| 218 | /** @brief 01 - 31 */ |
||
| 219 | uint8_t date; |
||
| 220 | /** @brief 1 - 7 */ |
||
| 221 | uint8_t day; |
||
| 222 | /** @brief 00 - 23 */ |
||
| 223 | uint8_t hours; |
||
| 224 | /** @brief 00 - 59 */ |
||
| 225 | uint8_t minutes; |
||
| 226 | /** @brief 00 - 59 */ |
||
| 227 | uint8_t seconds; |
||
| 228 | } ds1305_time_t; |
||
| 229 | ``` |
||
| 230 | |||
| 231 | ## Variables |
||
| 232 | |||
| 233 | Exemple : |
||
| 234 | ``` c |
||
| 235 | 40 | Patrice Nadeau | /** @brief Variable locale */ |
| 236 | 1 | Patrice Nadeau | static int ctr; |
| 237 | 40 | Patrice Nadeau | /** @brief Variable globale */ |
| 238 | int RANDOM_CTR; |
||
| 239 | 1 | Patrice Nadeau | ``` |
| 240 | |||
| 241 | ## Structures |
||
| 242 | |||
| 243 | Format |
||
| 244 | * En minuscule, séparé par des «underscores» si nécessaire. |
||
| 245 | |||
| 246 | Exemple : |
||
| 247 | ``` c |
||
| 248 | /** |
||
| 249 | * @brief Structure for a local menu |
||
| 250 | * @see MenuSelect |
||
| 251 | */ |
||
| 252 | struct menu { |
||
| 253 | 8 | Patrice Nadeau | /** @brief Character used for the item */ |
| 254 | char choice; |
||
| 255 | /** @brief Description of the item */ |
||
| 256 | char *item; |
||
| 257 | 1 | Patrice Nadeau | }; |
| 258 | ``` |
||
| 259 | |||
| 260 | ## Fonctions |
||
| 261 | |||
| 262 | |||
| 263 | Le nom DOIT être dans le format suivant : *Action***_***Item***_***Attribut*, où *Action* signifie : |
||
| 264 | 29 | Patrice Nadeau | * **set**, **get**, **clear** : Règle, obtient ou vide un registre |
| 265 | 1 | Patrice Nadeau | * **read**, **write** : Lis ou écris dans un fichier |
| 266 | * **init** : Fonction d’initialisation |
||
| 267 | * **is** : Vérifie un état |
||
| 268 | 36 | Patrice Nadeau | * **setup** : Fonction de configuration des ports (AVR) |
| 269 | 1 | Patrice Nadeau | |
| 270 | Exceptions |
||
| 271 | 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. |
| 272 | 1 | Patrice Nadeau | |
| 273 | Une fonction DEVRAIT retourner une valeur. |
||
| 274 | 28 | Patrice Nadeau | * Type entier (oui/non) : |
| 275 | 1 | Patrice Nadeau | * Succès : **0** |
| 276 | * Erreur : **1** |
||
| 277 | * Type booléen (Librairie `<stdbool.h>`) |
||
| 278 | * **true** |
||
| 279 | * **false** |
||
| 280 | * Pointeur : |
||
| 281 | * **NULL** : Erreur |
||
| 282 | * Autre valeur : adresse du pointeur |
||
| 283 | |||
| 284 | Justification : |
||
| 285 | * [AVR1000b](https://ww1.microchip.com/downloads/en/Appnotes/AVR1000b-Getting-Started-Writing-C-Code-for-AVR-DS90003262B.pdf) |
||
| 286 | |||
| 287 | Exemple : |
||
| 288 | |||
| 289 | ``` c |
||
| 290 | /** |
||
| 291 | 42 | Patrice Nadeau | * @brief Vérifie si une horloge est est initialisée |
| 292 | 1 | Patrice Nadeau | * @param[in] nb Timer number. @n Possible values : |
| 293 | 24 | Patrice Nadeau | * − @arg **TIMER_1** |
| 294 | * − @arg **TIMER_2** |
||
| 295 | 1 | Patrice Nadeau | * @return |
| 296 | 42 | Patrice Nadeau | * @retval true Horloge *nb* est initialisée |
| 297 | * @retval false Horloge *nb* n'est PAS initialisée |
||
| 298 | 1 | Patrice Nadeau | * @pre init_timer |
| 299 | **/ |
||
| 300 | static bool is_timer_set(uint8_t nb); |
||
| 301 | |||
| 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 | ``` |