Projet

Général

Profil

Atmel AVR » Historique » Version 22

Patrice Nadeau, 2024-03-09 14:33

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