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