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