Wiki » Historique » Version 158
Patrice Nadeau, 2024-01-20 21:21
| 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 | 155 | Patrice Nadeau | [[Commentaires Doxygen]] |
| 15 | 90 | Patrice Nadeau | |
| 16 | 156 | Patrice Nadeau | [[Fichiers]] |
| 17 | 104 | Patrice Nadeau | |
| 18 | 98 | Patrice Nadeau | |
| 19 | 85 | Patrice Nadeau | --- |
| 20 | 82 | Patrice Nadeau | |
| 21 | 157 | Patrice Nadeau | [[Objets et macros]] |
| 22 | 1 | Patrice Nadeau | |
| 23 | 158 | Patrice Nadeau | [[Préprocesseur]] |
| 24 | 1 | Patrice Nadeau | |
| 25 | |||
| 26 | ## Atmel AVR |
||
| 27 | |||
| 28 | Particularités pour les microcontrôleurs 8 bits AVR d’Atmel. |
||
| 29 | |||
| 30 | [Atmel AVR4027: Tips and Tricks to Optimize Your C Code for 8-bit AVR Microcontrollers](https://ww1.microchip.com/downloads/en/AppNotes/doc8453.pdf) |
||
| 31 | |||
| 32 | ### Fichier d’en-têtes |
||
| 33 | 25 | Patrice Nadeau | |
| 34 | Vérification du modèle de microcontrôleur |
||
| 35 | > Via l'option `-m` de [gcc](https://github.com/embecosm/avr-gcc/blob/avr-gcc-mainline/gcc/config/avr/avr-mcus.def) |
||
| 36 | 1 | Patrice Nadeau | |
| 37 | 25 | Patrice Nadeau | ```c |
| 38 | #ifndef defined (__AVR_ATmega48__) || (__AVR_ATmega48P__) || \ |
||
| 39 | (__AVR_ATmega88P__) || defined (__AVR_ATmega88__) || \ |
||
| 40 | (__AVR_ATmega168__) || defined (__AVR_ATmega168P__) || \ |
||
| 41 | (__AVR_ATmega328__) || defined (__AVR_ATmega328P__) |
||
| 42 | #warning "Cette librairie n'as pas été testée sur cette famille de microcontrôleur." |
||
| 43 | 1 | Patrice Nadeau | #endif |
| 44 | ``` |
||
| 45 | |||
| 46 | 45 | Patrice Nadeau | ### Macros |
| 47 | |||
| 48 | Définis dans le fichier `config.h` |
||
| 49 | |||
| 50 | 1 | Patrice Nadeau | Liste : |
| 51 | * `F_CPU` : La fréquence utilisée par l'horloge (interne ou externe) du microcontrôleur |
||
| 52 | |||
| 53 | > Les « fuses » doivent correspondent à la bonne source de l'horloge. |
||
| 54 | |||
| 55 | ### Types |
||
| 56 | |||
| 57 | De nouveau type d'entier sont fournis avec la librairie `<stdint.h>`. |
||
| 58 | |||
| 59 | L'utilisation de ces types DOIT être utilisé afin d'exprimer le nombre de bit d'un objet. |
||
| 60 | |||
| 61 | 44 | Patrice Nadeau | ### Progmem |
| 62 | |||
| 63 | <https://www.avrfreaks.net/s/topic/a5C3l000000U5SFEA0/t034767> |
||
| 64 | 1 | Patrice Nadeau | |
| 65 | Pour mettre des variables en lecture seule dans la section FLASH au lieu de SRAM avec `<avr/pgmspace.h>`. |
||
| 66 | > L’accès à ces variables est faite via les macros de la librairie. |
||
| 67 | |||
| 68 | Le nom de la variable DOIT être suivie de **_P** |
||
| 69 | |||
| 70 | Exemple : |
||
| 71 | ```c |
||
| 72 | #include <avr/pgmspace.h> |
||
| 73 | ... |
||
| 74 | /** @brief Variable en FLASH */ |
||
| 75 | const int Variable1_P PROGMEM = 42; |
||
| 76 | ``` |
||
| 77 | |||
| 78 | ### Fonction main |
||
| 79 | Un microcontrôleur AVR ne termine jamais la fonction `main`. |
||
| 80 | |||
| 81 | * Déclarer la fonction main avec l’attribut `noreturn` |
||
| 82 | * La boucle sans fin la plus optimisé est le `for (;;)` |
||
| 83 | |||
| 84 | Justification : [AVR035](https://ww1.microchip.com/downloads/en/AppNotes/doc1497.pdf) |
||
| 85 | |||
| 86 | Exemple : |
||
| 87 | 26 | Patrice Nadeau | ```c |
| 88 | #include <avr/io.h> |
||
| 89 | 1 | Patrice Nadeau | |
| 90 | /** |
||
| 91 | * @brief Never ending loop |
||
| 92 | 83 | Patrice Nadeau | */ |
| 93 | 1 | Patrice Nadeau | void main(void) __attribute__((noreturn)); |
| 94 | |||
| 95 | 9 | Patrice Nadeau | /* main function definition */ |
| 96 | 1 | Patrice Nadeau | void main(void) { |
| 97 | ... |
||
| 98 | /* never return */ |
||
| 99 | for (;;) { |
||
| 100 | }; |
||
| 101 | }; |
||
| 102 | ``` |
||
| 103 | 70 | Patrice Nadeau | |
| 104 | 113 | Patrice Nadeau | ### Opérations « atomiques » |
| 105 | 1 | Patrice Nadeau | Opérations ne devant pas être interrompus (Ex. : charger un registre de 16 bits avec un registre de 8 bits). |
| 106 | |||
| 107 | La librairie `avr-libc` (util/atomic.h) fournit des macros permettant la gestion entre autre des interruptions. |
||
| 108 | |||
| 109 | Les instructions critiques sont insérées dans un `ATOMIC_BLOCK`. |
||
| 110 | |||
| 111 | Exemple : |
||
| 112 | 72 | Patrice Nadeau | ```c |
| 113 | 1 | Patrice Nadeau | #include <util/atomic.h> |
| 114 | ... |
||
| 115 | ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { |
||
| 116 | ... |
||
| 117 | } |
||
| 118 | ... |
||
| 119 | ``` |