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