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