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

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

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

515 Views
lpcware
NXP Employee
NXP Employee
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!!!
0 Kudos
6 Replies

488 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by magicdim on Thu Jan 24 08:23:29 MST 2013
OK, thanks very much Zero, I understand!
I think I posted my last reply a bit too fast before explaining what I understood!

Thanks for your help!! :)
0 Kudos

488 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Thu Jan 24 06:57:56 MST 2013

Quote: magicdim
Ok, I think I understand... shifting 4 bits to FIOPIN register (LPC_GPIO0->FIOPIN >> 4) results in pushing its bit number 3 at the end (number 0), and ANDing this result with 0x01 gives the state of this bit number 3 (pin P0.4 in this case)



No, P0.4 is bit number 5 = 0b10000 :eek:

Right shifting 4 positions is moving this bit from position 4 (value:2^4) to 0 (value:2^0) =  0b00001 :p
0 Kudos

488 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by magicdim on Thu Jan 24 06:19:51 MST 2013

Quote: Zero
No, you are ANDing 0b10000000000000000000000 ;) Result is either (1<<22) or (0<<22) :eek:

LPC_GPIO0->FIOPIN is an uint32_t (see LPC17xx.h), what's not clear there?



Yes, I agree, this one wasn't my main issue, I was disturbed by the following...


Quote: Zero
No, you are right shifting (4 bits) a 32bit register, the result is still a 32 bit register :p



Ok, I think I understand... shifting 4 bits to FIOPIN register (LPC_GPIO0->FIOPIN >> 4) results in pushing its bit number 3 at the end (number 0), and ANDing this result with 0x01 gives the state of this bit number 3 (pin P0.4 in this case)

Am I right??

P.S: sorry for my inaccurate writings...
0 Kudos

488 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Thu Jan 24 05:34:52 MST 2013

Quote: magicdim
LPC_GPIO0->FIOPIN & (1<<22)
(ANDING FIOPIN with 10000000000000000000000 right?, so FIOPIN is in the order 31->0 and not 0->31??)



No, you are ANDing 0b10000000000000000000000 Result is either (1<<22) or (0<<22) :eek:

LPC_GPIO0->FIOPIN is an uint32_t (see LPC17xx.h), what's not clear there?


Quote: magicdim

LPC_GPIO0->FIOPIN >> 4 
(we are pointing to bit 0 of FIOPIN is it correct? so how can we shift 4 bits to the right???)



No, you are right shifting (4 bits) a 32bit register, the result is still a 32 bit register
0 Kudos

488 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by magicdim on Thu Jan 24 04:41:51 MST 2013
Woops, sorry for this typing error!

Thanks for answering Zero.

Could you (or someone else!) explain to me what is the difference between:

LPC_GPIO0->FIOPIN & (1<<22)

(ANDING FIOPIN with 10000000000000000000000 right?, so FIOPIN is in the order 31->0 and not 0->31??)

and
LPC_GPIO0->FIOPIN >> 4 

(we are pointing to bit 0 of FIOPIN is it correct? so how can we shift 4 bits to the right???)
0 Kudos

488 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Thu Jan 24 03:43:58 MST 2013

Quote: magicdim
.. why do we [COLOR=Red]shit[/COLOR] from left to right???



:eek:

Please don't sh.. here at all :rolleyes:

Right shift is used here to AND bit 4 with 0x01, so the result (btn1) is 0 or 1 ;)

And yes, this registers are 32-bit, so you can shift them from 0-31 :)
0 Kudos