Projet

Général

Profil

Atmel AVR » Historique » Version 16

Patrice Nadeau, 2024-01-27 16:40

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 15 Patrice Nadeau
1. Fonction `main` : Un microcontrôleur AVR ne termine jamais cette fonction
16 12 Patrice Nadeau
    1. Déclarer la fonction main avec 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
#ifndef defined (__AVR_ATmega48__) || (__AVR_ATmega48P__) || \
27
	(__AVR_ATmega88P__) || defined (__AVR_ATmega88__) || \
28
	(__AVR_ATmega168__) || defined (__AVR_ATmega168P__) || \
29
	(__AVR_ATmega328__) || defined (__AVR_ATmega328P__)
30
#warning "Cette librairie n'as pas été testée sur cette famille de microcontrôleur."
31
#endif
32
33 7 Patrice Nadeau
#include <avr/io.h>
34 1 Patrice Nadeau
#include <avr/pgmspace.h>
35 8 Patrice Nadeau
#include <util/atomic.h>
36
37 1 Patrice Nadeau
...
38
/** @brief Variable en FLASH */
39
const int Variable1_P PROGMEM = 42;
40 8 Patrice Nadeau
41
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
42
    ...
43
}
44 1 Patrice Nadeau
45
46
/** 
47
 * @brief Never ending loop
48
*/
49
void main(void) __attribute__((noreturn));
50
51
/* main function definition */
52
void main(void) {
53
    ...
54
    /* never return */
55
    for (;;) {
56
    };
57
};
58
```
59 11 Patrice Nadeau
60
## Justifications
61
* [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)
62
* [AVR035 : Efficient C Coding for AVR6](https://ww1.microchip.com/downloads/en/AppNotes/doc1497.pdf)
63
* [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)