Atmel AVR » Historique » Version 12
Patrice Nadeau, 2024-01-27 16:35
| 1 | 1 | Patrice Nadeau | # Atmel AVR |
|---|---|---|---|
| 2 | |||
| 3 | Particularités pour les microcontrôleurs 8 bits AVR d’Atmel. |
||
| 4 | |||
| 5 | [Atmel AVR4027: Tips and Tricks to Optimize Your C Code for 8-bit AVR Microcontrollers](https://ww1.microchip.com/downloads/en/AppNotes/doc8453.pdf) |
||
| 6 | |||
| 7 | 2 | Patrice Nadeau | ## Règles |
| 8 | 1. Macros définis dans le fichier config.h |
||
| 9 | 12 | Patrice Nadeau | 1. `F_CPU` : La fréquence utilisée par l’horloge (interne ou externe) du microcontrôleur, en Hz |
| 10 | 2 | Patrice Nadeau | |
| 11 | > Les « fuses » doivent correspondent à la bonne source de l’horloge. |
||
| 12 | 12 | Patrice Nadeau | 1. De nouveau type d’entier sont fournis avec la librairie `stdint.h`. L’utilisation de ces types DOIT être utilisé afin d’exprimer le nombre de bit d’un objet. |
| 13 | 2 | Patrice Nadeau | 1. Progmem : Pour mettre des variables en lecture seule dans la section FLASH au lieu de SRAM. |
| 14 | 1. Inclue la librairie avr/pgmspace.h |
||
| 15 | 12 | Patrice Nadeau | 1. Le nom de la variable DOIT être suivie de **_P** |
| 16 | 2 | Patrice Nadeau | 1. Utiliser les macros de la librairie pour accéder à ces variables |
| 17 | 1. Fonction main : Un microcontrôleur AVR ne termine jamais la fonction main. |
||
| 18 | 12 | Patrice Nadeau | 1. Déclarer la fonction main avec l’attribut `noreturn` |
| 19 | 1 | Patrice Nadeau | 1. Utiliser une boucle sans fin for (;;) |
| 20 | 12 | Patrice Nadeau | 1. Opérations atomiques (opérations ne devant pas être interrompus) |
| 21 | 2 | Patrice Nadeau | 1. Inclure la librairie `util/atomic.h` |
| 22 | 1 | Patrice Nadeau | 1. Insérer les instructions dans une section `ATOMIC_BLOCK` |
| 23 | 6. Interruptions |
||
| 24 | 12 | Patrice Nadeau | 1. TODO |
| 25 | 1 | Patrice Nadeau | |
| 26 | 12 | Patrice Nadeau | ## Exemple |
| 27 | 1 | Patrice Nadeau | ```c |
| 28 | #ifndef defined (__AVR_ATmega48__) || (__AVR_ATmega48P__) || \ |
||
| 29 | (__AVR_ATmega88P__) || defined (__AVR_ATmega88__) || \ |
||
| 30 | (__AVR_ATmega168__) || defined (__AVR_ATmega168P__) || \ |
||
| 31 | (__AVR_ATmega328__) || defined (__AVR_ATmega328P__) |
||
| 32 | #warning "Cette librairie n'as pas été testée sur cette famille de microcontrôleur." |
||
| 33 | #endif |
||
| 34 | |||
| 35 | 7 | Patrice Nadeau | #include <avr/io.h> |
| 36 | 1 | Patrice Nadeau | #include <avr/pgmspace.h> |
| 37 | 8 | Patrice Nadeau | #include <util/atomic.h> |
| 38 | |||
| 39 | 1 | Patrice Nadeau | ... |
| 40 | /** @brief Variable en FLASH */ |
||
| 41 | const int Variable1_P PROGMEM = 42; |
||
| 42 | 8 | Patrice Nadeau | |
| 43 | ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { |
||
| 44 | ... |
||
| 45 | } |
||
| 46 | 1 | Patrice Nadeau | |
| 47 | |||
| 48 | /** |
||
| 49 | * @brief Never ending loop |
||
| 50 | */ |
||
| 51 | void main(void) __attribute__((noreturn)); |
||
| 52 | |||
| 53 | /* main function definition */ |
||
| 54 | void main(void) { |
||
| 55 | ... |
||
| 56 | /* never return */ |
||
| 57 | for (;;) { |
||
| 58 | }; |
||
| 59 | }; |
||
| 60 | ``` |
||
| 61 | 11 | Patrice Nadeau | |
| 62 | ## Justifications |
||
| 63 | * [Atmel AVR4027 : Tips and Tricks to Optimize Your C Code for 8-bit AVR Microcon-trollers](https://ww1.microchip.com/downloads/en/AppNotes/doc8453.pdf) |
||
| 64 | * [AVR035 : Efficient C Coding for AVR6](https://ww1.microchip.com/downloads/en/AppNotes/doc1497.pdf) |
||
| 65 | * [Vérification du modèle de microcontrôleur via l’option -m de gcc](https://github.com/embecosm/avr-gcc/blob/avr-gcc-mainline/gcc/config/avr/avr-mcus.def) |