lpcware

Missunderstanding code example (big/little endian, cast??)

Discussion created by lpcware Employee on Jun 15, 2016
Latest reply on Jun 15, 2016 by lpcware
Content originally posted in LPCWare by magicdim on Thu Jan 24 03:28:12 MST 2013
HI everyone,

I'am using LPCXpresso1769 board, and I would like to know if someone could explain the next pieces of code:

In the example "LPCX176x_cmsis2_systick" given in the IDE, we can find in leds.c:


// Function to invert current state of LED2 [COLOR=Red](soldered on P0.22)[/COLOR]
void led2_invert (void)
{
int ledstate;

// Read current state of GPIO P0_0..31, which includes LED2
ledstate = LPC_GPIO0->FIOPIN;
// Turn off LED2 if it is on
// (ANDing to ensure we only affect the LED output)
LPC_GPIO0->FIOCLR = ledstate & (1 << 22);
// Turn on LED2 if it is off
// (ANDing to ensure we only affect the LED output) dimitri
LPC_GPIO0->FIOSET = ((~ledstate) & (1 << 22));
}


ledstate = LPC_GPIO0->FIOPIN;[COLOR=red]==>reads the states of P0 to P31, OK?[/COLOR]

LPC_GPIO0->FIOCLR = ledstate & (1 << 22);[COLOR=red] ==> sets 0 to P0.22, OK?[/COLOR]


[COLOR=red]So we can assume that FIOPIN in the order 31....0 as we shift << from right to left, OK?[/COLOR]

When I see in Embedded Artists "demo" application:


uint_8t btn1;
...
btn1 = ((GPIO_ReadValue(0) >> 4) & 0x01); [COLOR=Red]assumed to read P0.4...[/COLOR]

uint32_t GPIO_ReadValue(uint8_t portNum)
{
LPC_GPIO_TypeDef *pGPIO = GPIO_GetPointer(portNum);

if (pGPIO != NULL) {
return pGPIO->FIOPIN;
}

return (0);
}

static LPC_GPIO_TypeDef *GPIO_GetPointer(uint8_t portNum)
{
LPC_GPIO_TypeDef *pGPIO = NULL;

switch (portNum) {
case 0:
pGPIO = LPC_GPIO0;
break;
......
}

return pGPIO;
}


So if I understand, GPIO_ReadValue(0) points to LPC_GPIO0->FIOPIN,[COLOR=Red] OK?[/COLOR]

Which I don't understand is (GPIO_ReadValue(0) >> 4) ==> if we point to the base adresse of FIOPIN why do we shit from left to right???

Thanks in advance!!!

Outcomes