Domanda

Come ottenere il valore della porta del microcontrollore ARM in una variabile a 32 bit.

Sto usando il microcontrollore LPC2378.

È stato utile?

Soluzione

Devi accedere ai registri GPIO proprio come faresti con qualsiasi altro registro di funzioni speciali nel chip. I documenti LPC2378 mostrano questi dettagli:

#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

Mi piace usare questa macro per accedere ai registri mappati in memoria:

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

Quindi il codice per leggere la porta è simile al seguente:

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

La stessa macro funziona per accedere ai registri set / clear / direction. Esempi:

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

In un sistema reale, normalmente scriverei alcune macro o funzioni per quelle operazioni, per ridurre i numeri magici.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top