How to read value from GPIO port of ARM microcontroller?

How to get the value of the port of an ARM microcontroller into a 32-bit variable.

I am using the LPC2378 microcontroller.

+3
source share
1 answer

You need to access the GPIO registers in the same way as any other special functions registered in the chip. LPC2378 documents show this data:

#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

I like to use this macro to access the mappable registers:

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

Then the code for reading the port is as follows:

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

The same macro works to access the set / clear / direction registers. Examples:

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 a real system, I usually write some macros or functions for these operations to reduce magic numbers.

+9

Source: https://habr.com/ru/post/1724139/


All Articles