Page 1 of 1

GPIO read

Posted: Thu Mar 17, 2011 11:30 am
by soumajit
I am using sub_gpio_read() function to read the status of GPIO pins. But it returns a hex value. If I want the individual pin status how do i get it? Basically I want to poll individual pins and want to know its value but since the status obtained is in hex how do i get the individual pin status..?

Re: GPIO read

Posted: Thu Mar 17, 2011 12:38 pm
by serg
Hi soumajit,

You can pick out individual pin values by using the C language "bitwise and" operator. Let's say you need to poll the pin #22.
The bitmask corresponding to the bit #22 is (2^22) or (1<<22). Next, you should apply your bitmask to the current pin values returned by the sub_gpio_read() function. The code below demonstrates this.

Code: Select all

#define PIN(x)  (1<<(x))

int curr_pin_state;
status = sub_gpio_read( h, &curr_pin_state);
if(status) goto error;
if( curr_pin_state & PIN(22))
   printf("Bit 22 is set\n");
else
   printf("Bit 22 is not set\n");

Re: GPIO read

Posted: Fri Mar 18, 2011 5:10 am
by soumajit
Thank you so much for the reply :D