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