bloodline
Posts: 76
Joined: Sun Jun 10, 2012 8:44 pm
Location: London - England

GPIO access in C++

Sun Sep 08, 2013 5:19 pm

Hi all,

I have written a simple GPIO class to allow easy access of the 17 GPIO pins exposed on the Raspi. So far, setting a pin high or low works and I also have software PWM working on any pin too.

A couple of issues I want to sort out before I throw this online for those that might find it useful.

I can't figure out how to read a GPIO pin yet, any clues please. I'm hitting the hardware not using the Kernel interface. :)

And my PWM is based on the nanosleep timer... unfortunately this is only stable to about 10us, so it fine for speed control and LED brightness... but does make servos a bit twitchy... do we have any realtime timers Linux?

User avatar
joan
Posts: 14887
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: GPIO access in C++

Sun Sep 08, 2013 6:40 pm

To read gpios.

Code: Select all

...
/* uint32_t  *gpioReg points to base address of gpio registers */
...
#define GPLEV0    13
#define GPLEV1    14
...
int gpioRead(unsigned gpio)
{
   if ((*(gpioReg + GPLEV0 + (gpio>>5)) & (1<<(gpio&0x1F))) != 0) return 1;
   else return 0;
}
...
You'll need to use the DMA hardware to time your pulses if you want accuracy.

bloodline
Posts: 76
Joined: Sun Jun 10, 2012 8:44 pm
Location: London - England

Re: GPIO access in C++

Sun Sep 08, 2013 7:16 pm

Cheers!

Return to “C/C++”