Question

Comment obtenir la valeur du port du microcontrôleur ARM dans une variable 32 bits.

J'utilise le microcontrôleur LPC2378.

Était-ce utile?

La solution

Vous devez accéder aux registres GPIO comme à tout autre registre de fonction spéciale de la puce. La documentation LPC2378 montre ces détails:

#define GPIO_BASE  0xE0028000
#define IOPIN0     (GPIO_BASE + 0x00) // Port 0 value
#define IOSET0     (GPIO_BASE + 0x04) // Port 0 set 
#define IODIR0     (GPIO_BASE + 0x08) // Port 0 direction
#define IOCLR0     (GPIO_BASE + 0x0C) // Port 0 clear
#define IOPIN1     (GPIO_BASE + 0x10) // Port 1 value
#define IOSET1     (GPIO_BASE + 0x14) // Port 1 set
#define IODIR1     (GPIO_BASE + 0x18) // Port 1 direction
#define IOCLR1     (GPIO_BASE + 0x1C) // Port 1 clear

J'aime utiliser cette macro pour accéder aux registres mappés en mémoire:

#define mmioReg(a) (*(volatile unsigned long *)(a))

Le code permettant de lire le port ressemble à ceci:

unsigned long port0 = mmioReg(IOPIN0); // Read port 0
unsigned long port1 = mmioReg(IOPIN1); // Read port 1

La même macro fonctionne pour accéder aux registres set / clear / direction. Exemples:

mmioReg(IOSET1) = (1UL << 3);   // set bit 3 of port 1
mmioReg(IOCLR0) = (1UL << 2);   // clear bit 2 of port 0
mmioReg(IODIR0) |= (1UL << 4);  // make bit 4 of port 0 an output
mmioReg(IODIR1) &= ~(1UL << 7); // make bit 7 of port 1 an input

Dans un système réel, j'écrirais normalement des macros ou des fonctions pour ces opérations afin de réduire les nombres magiques.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top