Electronic – C programming syntaxes for NXP LPC1768

cmicrocontrollernxpprogramming

My question may seem weird, but I am very new in nxp micro-controllers. It seems to me that people use different syntax in C programming. For example, in order to set a pin as an output we have:

LPC_GPIO1->FIODIR |= (1<<29);

while there is another syntax which does the same thing and it looks like this:

FIO1DIR |= (1<<29);

And they both have similar libraries. As I am learning the second type, I wanted to know if there is a reference for this type of coding?

Best Answer

it is all driven by the header files used. the first approach used a struct (pointer) to the gpio base address to address all the registers related to that port. the 2nd approach used a pointer to the exactly same address.

end of the day, it is the same thing, different packaging.

edit: here is the gist of it.

typedef struct {
    uint32_t PIN;           //data register
    uint32_t SET;
    uint32_t DIR;
    uint32_t CLR;
} GPIO_TypeDef;             //gpio structure

#define GPIO                ((GPIO_TypeDef *)&IOPIN)

int main(void) {
    while (1) {
        GPIO->DIR ^=0xffff; //flip the lowest 16 bits
        IODIR     ^=0xffff; //doing the same
    }
}

This particular chip has four registers associated with its GPIO, IOPIN, IOSET, IODIR and IOCLR; one way to address it, in this case, IODIR, is to use the macro defined in the header file, as shown in the main loop.

alternatively, you can define a struct, GPIO_TypeDef that includes the four registers in the right order (as per the compiler). you then "fix" the GPIO as a pointer to that struct but anchored to the same address starting with IOPIN.

So the two approaches of flipping the lowest 16 bits in the IODIR registers will achieve the same goal.

This approach requires a few things: 1. the port structure is the same from port to port, chip to chip; 2. the endianness is known.

most newer chips (with the exception of msp430 and pic) follow that. so you will the struct approach used widely nowadays.