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