Tag Archives: GPIO

Robust C Library and Utility for GPIO sysfs Interface in Linux

by Derek Hildreth – Technologic Systems

This comprehensive and easy to read example C code is designed to work alone or included as a library for dealing with general purpose I/O via the sysfs interface in Linux.  It can easily be applied to any computer which utilizes the GPIO sysfs interface (both pro and maker boards).  Getting started is a breeze.  You just need to decide if you’re going to use it as a library (including it into your existing code) or as a stand-alone utility.  We’ll talk about  in this article, but first and foremost, here’s the source code we’ll be working with:

https://github.com/embeddedarm/gpio-sysfs-demo

To get started, download or clone this repository to your board, extract it, and change your directory to it, like so:

wget https://github.com/embeddedarm/gpio-sysfs-demo/archive/master.zip
unzip master.zip
cd gpio-sysfs-demo-master/

The two important files in this repository came from Technologic System’s GitHub pages:

The included Makefile makes quick work out of compiling the example code mentioned in the sections below, as you’ll see soon enough.

Stand-Alone Utility

As a stand-alone utility, you can easily copy the resulting gpioctl program to a directory in your current PATH and include in a script or use it for those one-off commands.

To do this, we’ll simply run sudo make install.  The Makefile will compile the standalone utility using (essentially) gcc -D CTL gpiolib.c -o gpioctl and then automatically copy it to /usr/local/bin, which is popularly included in PATH.  Then, you can run gpioctl –help for instructions on how to use it.  For example, say you want to output logical high on DIO #59.  You would first set the DIO to be an output, and then you’d set it.  Here’s an example of toggling it:

gpioctl –ddrout 59

gpioctl –setout 59

gpioctl –clrout 59

That’s it!  If, say, DIO #59 was tied to an LED, you should see the LED turn on and off again.

 

Now you can easily include these commands into a bash shell script or Python system call or, well, you get the point.

Pro Tip: Keep in mind, the LED may be active low, so you might actually see the opposite (logic high output turns the LED off, logic low output turns the LED on).

Include as Library

Okay, so if you wanted to use this as a library, giving our own C program access to the helpful functions included in gpioctl.c, we need to include it as a source when compiling our program.  You’ll find the example source code gpiodemo.c included in the repository you downloaded earlier:

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include "gpiolib.h"

int main(int argc, char **argv)  {
    int gpio_pin = 59;
    gpio_export(gpio_pin);
    gpio_direction(gpio_pin, 1);

    for(int i = 0; i < 5; i++) {
        printf(">> GPIO %d ON\n", gpio_pin);
        gpio_write(gpio_pin, 1);

        sleep(1);

        printf(">> GPIO %d OFF\n", gpio_pin);
        gpio_write(gpio_pin, 0);   

        sleep(1);
    }                                                                                                                                                      
    return 0;
}

If you simply run make, you’ll get a compiled gpiodemo binary which simply toggles DIO #59, which is connected to an LED on a breadboard.  Referring to the Makefile, you’ll see it’s compiling the example code by running gcc -o gpiodemo gpiolib.c gpiodemo.c.  Now, you can take this knowledge and example and apply it to your own project.

Bonus: Very Fast GPIO

The sysfs API allows for very simple IO access, but it is not very fast.  For example, on the TS-4900 sending out a square wave on a single CPU GPIO through the sysfs API will toggle at around 160 – 170 KHz.  The same IO accessed through registers will toggle at 1.66 – 1.74 MHz.  FPGA GPIO will always be slow at around 700hz.  If you’re needing higher performance GPIO programming in order to meet very specific timing requirements, you might want to take a look at the gpiolib-fast.c solution.  Basically, instead of going through sysfs, you’ll be using the GPIO memory registers directly.  Take care in doing so, as the operations aren’t atomic and can therefore foul things up in unexpected ways.  Kernel drivers or other processes accessing GPIO in the same bank performing a read/modify/write can rewrite the same register with old values causing unexpected results.  Source code can be found here:

Conclusion

You should now be able to control GPIO through the sysfs interface like a boss!  We hope this article was helpful in getting your project off the ground quickly.  If it made your day, please consider sharing it!  If you have questions, please use the comments below.

Low-Cost FPGA With Reconfigurable Electronics Feature

Iolinker is a cheap 64 FPGA board with a MachXO FPGA that functions as a dynamically configurable IO matrix. Its main functionality, besides IO extension, is to dynamically set up a matrix of GPIO connections, that allow direct pass-through of high-frequency signals. Circuits can thereby be configured and programmed on the fly. There are UART / SPI / I2C connections that allow for easy integration of up to 127 chips connected in parallel.

Thanks to the open source library, Iolinker allows developers to create reconfigurable, easy to self test electronics within minutes. It can be used to be an IO extender and can output PWM signals. In addition, its revolutionary “IO linking” feature allows to dynamically pass through high-speed signals between IOs, better than any microprocessor ever could.

Check this teaser about the new board:

Iolinker has the following specifications:

  • Reprogrammable FPGA board with Lattice LCMXO3L-4300E-5UWG81CTR50
  • Preprogrammed and usable out of the box as your IO interface of choice.
  • 49 GPIOs for PWM or IO extension usage, VCCIO is 3.3V.
  • Boards can be connected in parallel, to create endless IO extension.
  • IOs can be linked to each other, i.e. you tell the FPGA to connect them, and it forwards the input signal from one pin to another. (Read more about the iolinker chip function.)
  • UART, SPI or I2C interfaces are available.

In order to make the ultimate IO interface for users, the team are accepting feature requests at the contact page.

In short, the Iolinker board is easy to use and can reconfigure schematics on the fly, what makes it ideal to reduce prototyping time and jumper cable mess, and to maximize the ability of using IO extensions.

More technical details about Iolinker and its price will be announced soon at the Kickstarter campaign at Feb 14. Some special offers are for everyone who register in the website’s newsletter, so register now and stay tuned!

 

ESP8266 controlled with Android app (MIT App Inventor)

featured-image-600x337

Rui Santos from Random Nerd Tutorials tipped us with his latest tutorial on how to build an Android app using the MIT App Inventor software that allows you to control the ESP8266 GPIOs:

ESP8266 controlled with Android app (MIT App Inventor) – [Link]

How to Control ESP8266 GPIO using Android App

This video will guide you to control gpio of esp8266 from android app in details. so that you can build your first home automation project easily.

How to Control ESP8266 GPIO using Android App – [Link]