C
lgpio (local /dev/gpiochip I/F)
lgpio provides all the standard Linux GPIO features.
- reading and writing GPIO singly and in groups
- software timed PWM and waves
- GPIO callbacks
- pipe notification of GPIO alerts
- I2C wrapper
- SPI wrapper
- serial link wrapper
- a simple interface to start and stop new threads
lgpio uses the new /dev/gpiochip interface rather than the deprecated syfs interface.
C documentation: http://abyz.me.uk/lg/lgpio.html
Download and install the lg archive: http://abyz.me.uk/lg/download.html
Example pulse.c
#include <stdio.h>
#include <lgpio.h>
#define IN 20
#define OUT1 21
#define OUT2 22
#define ALERT 23
/* callback function */
void cbf(int e, lgGpioAlert_p evt, void *data)
{
int i;
int secs, nanos;
for (i=0; i<e; i++)
{
secs = evt[i].report.timestamp / 1000000000L;
nanos = evt[i].report.timestamp % 1000000000L;
printf("chip=%d gpio=%d level=%d time=%d.%09d\n",
evt[i].report.chip, evt[i].report.gpio, evt[i].report.level,
secs, nanos);
}
}
int main(int argc, char *argv[])
{
int h;
int old_v, v;
int lFlags = 0; /* default line flags */
/* get a handle to the GPIO */
h = lgGpiochipOpen(0);
/* claim a GPIO for INPUT */
lgGpioClaimInput(h, lFlags, IN);
/* claim some GPIO for OUTPUT */
lgGpioClaimOutput(h, lFlags, OUT1, 0); /* initial level 0 */
lgGpioClaimOutput(h, lFlags, OUT2, 1); /* initial level 1 */
/* claim a GPIO for ALERTS */
lgGpioClaimAlert(h, lFlags, LG_BOTH_EDGES, ALERT, -1);
/* set up a callback for the alerts */
lgGpioSetAlertsFunc(h, ALERT, cbf, NULL);
/* output 1000 cycles of PWM: frequency=20 dutycycle=35 */
lgTxPwm(h, OUT2, 20.0, 35.0, 0, 1000);
old_v =-1;
while (1)
{
v = lgGpioRead(h, IN);
if (v != old_v)
{
lgGpioWrite(h, OUT1, v);
printf("GPIO %d now %d\n", OUT1, v);
old_v = v;
}
lguSleep(0.1); /* sleep for 0.1 seconds */
}
}
Compile
gcc -o pulse pulse.c -llgpio
Run
./pulse
rgpio (local & remote /dev/gpiochip I/F)
rgpio provides all the standard Linux GPIO features.
- reading and writing GPIO singly and in groups
- software timed PWM and waves
- GPIO callbacks
- I2C wrapper
- SPI wrapper
- serial link wrapper
- simple file handling
- creating and running scripts on the rgpiod daemon
- a simple interface to start and stop new threads
rgpio uses the new /dev/gpiochip interface rather than the deprecated syfs interface.
C documentation: http://abyz.me.uk/lg/rgpio.html
Download and install the lg archive: http://abyz.me.uk/lg/download.html
Example pulse.c
#include <stdio.h>
#include <stdlib.h>
#include <lgpio.h>
#include <rgpio.h>
#define IN 20
#define OUT1 21
#define OUT2 22
#define ALERT 23
/* callback function */
void cbf(
int sbc,
int chip,
int gpio,
int level,
uint64_t timestamp,
void *userdata)
{
int i;
int secs, nanos;
secs = timestamp / 1000000000L;
nanos = timestamp % 1000000000L;
printf("chip=%d gpio=%d level=%d time=%d.%09d\n",
chip, gpio, level, secs, nanos);
}
int main(int argc, char *argv[])
{
int sbc;
int h;
int cb_id;
int old_v, v;
int lFlags = 0; /* default line flags */
sbc = rgpiod_start(NULL, NULL);
if (sbc < 0)
{
printf("connection failed\n");
exit(-1);
}
/* get a handle to the GPIO */
h = gpiochip_open(sbc, 0);
/* claim a GPIO for INPUT */
gpio_claim_input(sbc, h, lFlags, IN);
/* claim some GPIO for OUTPUT */
gpio_claim_output(sbc, h, lFlags, OUT1, 0); /* initial level 0 */
gpio_claim_output(sbc, h, lFlags, OUT2, 1); /* initial level 1 */
/* claim a GPIO for ALERTS */
gpio_claim_alert(sbc, h, lFlags, LG_BOTH_EDGES, ALERT, -1);
/* set up a callback for the alerts */
cb_id = callback(sbc, h, ALERT, LG_BOTH_EDGES, cbf, NULL);
/* output 1000 cycles of PWM: frequency=20 dutycycle=35 */
tx_pwm(sbc, h, OUT2, 20.0, 35.0, 0, 1000);
old_v =-1;
while (1)
{
v = gpio_read(sbc, h, IN);
if (v != old_v)
{
gpio_write(sbc, h, OUT1, v);
printf("GPIO %d now %d\n", OUT1, v);
old_v = v;
}
lgu_sleep(0.1); /* sleep for 0.1 seconds */
}
}
Compile
gcc -o pulse pulse.c -lrgpio
Run
./pulse