GPIO pointer strange issue

Discussion created by lpcware Employee on Jun 15, 2016
Latest reply on Jun 15, 2016 by lpcware
Content originally posted in LPCWare by MSalvati on Thu Feb 12 07:24:24 MST 2015
Hello everybody, first of all, thank you for reading :). I've been looking into this forum for a long time and it's been more than helpful !
Now after days of debbuging I've come to this situation:

I have a hard fault which, after looking at memory, I found that it's caused by a pointer which is pointing a wrong zone (typical). It seems to be a very very basic mistake but I just can't figure out what's wrong :(.
Project has a main.c, gpio.c, gpio.h  and gpio.h is included in both main and gpio source codes. LPC11xx.h is included in both as well.
The pointer, defined in gpio.h it is:

static LPC_GPIO_TypeDef (* const LPC_GPIO[4]) = { LPC_GPIO0, LPC_GPIO1, LPC_GPIO2, LPC_GPIO3 };

LPC_GPIOn is the position in memory 0x500n8000, as noted in UM10398.

So by now, I just want to access LPC_GPIO[0]->DIR and store the number 0x08. The function in gpio.c which does this is:
void GPIOSetDir( uint32_t portNum, uint32_t bitPosi, uint32_t dir )

Which stores in LPC_GPIOn->DIR the correct value

When I look at the Expressions window, value of LPC_GPIO[0] outside this function points, sometimes, to 0x010002000 (which curiously is where RAM starts, and I think it would be actually better to think that pointer outside has no sense) and, if I am not wrong, when it gets inside this function it should point to 0x50008000. This doesn't happens.
When it gets into GPIOSetDir() pointer keeps pointing elsewhere (0x46bd50d1, for example) and then, expectedly, hard faults.

This happens in the very first lines of main.c, there is no other function call before, except for the SystemInit() function (which I assume you're familiar with, the one that sets PLL clocking etc, from LPC library) which remains unchanged.

Any help is welcome! If you need more data, please ask me, I'll post as soon as possible.

Kind regards,