Pregunta

Cómo obtener el valor del puerto del microcontrolador ARM en una variable de 32 bits.

Estoy usando el microcontrolador LPC2378.

¿Fue útil?

Solución

Debe acceder a los registros GPIO como lo haría con cualquier otro registro de función especial en el chip. Los documentos LPC2378 muestran estos detalles:

#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

Me gusta usar esta macro para acceder a los registros asignados en memoria:

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

Luego, el código para leer el puerto se ve así:

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

La misma macro funciona para acceder a los registros set / clear / direction. Ejemplos:

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

En un sistema real, normalmente escribo algunas macros o funciones para esas operaciones, para reducir los números mágicos.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top