Como ler o valor da porta gpio de um microcontrolador de braço?
Pergunta
Como obtenha o valor da porta do microcontrolador de braço em uma variável de 32 bits.
Estou usando o microcontrolador LPC2378.
Solução
Você precisa acessar os registros GPIO como faria com qualquer outro registro de função especial no chip. Os documentos LPC2378 mostram estes detalhes:
#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
Eu gosto de usar esta macro para acessar registros mapeados de memória:
#define mmioReg(a) (*(volatile unsigned long *)(a))
Em seguida, o código para ler a porta é assim:
unsigned long port0 = mmioReg(IOPIN0); // Read port 0
unsigned long port1 = mmioReg(IOPIN1); // Read port 1
A mesma macro trabalha para acessar os registros de conjunto/clear/direção. Exemplos:
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
Em um sistema real, normalmente eu escreveria algumas macros ou funções para essas operações, para reduzir os números mágicos.
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow