Tag Archives: C

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.

How to Write and Run a C Program on the Raspberry Pi

In this tutorial, circuitbasics.com discuss what a C program is, what C programming is used for, and finally, how to write and run a C program on the Raspberry Pi.

The C programming language is one of the most widely used programming languages of all time. It is computationally faster and more powerful than Python. C is a middle level programming language because of its low level of abstraction to assembly language.

How to Write and Run a C Program on the Raspberry Pi – [Link]

8 Channel RS485 Relay Board

8-channel-rs485-driven-relay-board-img

This project helps you control 8 relays using RS485 link with the help of a PC. This kit can effectively convert  a standard PC to a powerful   PLC ( programmable logic controller ). At the PC end , you will need to use our RS232 to RS485 Interface – C020 or any other suitable RS232 to RS485 Converter.

The project can offer a low-cost serial relay contact interface, easy to use with Visual Basic, Basic, C, Labview, Testpoint, or other high level languages that allow access to a serial port.

You can simply use the accompanying software provided with this project or issue control commands using any popular Terminal Port software available on the net.

The Serial port on the computer should be configured to 2400 baud – 8 bit words – 1 stop bit – no parity

8 Channel RS485 Relay Board – [Link]

SunDuino – Run your C application using solar power

BB25E_350T

SunDuino is a Single Board Computer with integrated Battery Charger, Voltage Regulators, I2C, Digital and Analog IO. It’s main benefit is that it can run a compiled C app for years on a small battery or forever using built in solar charger. A background RTOS provides SLEEP functions for reducing operating current to 100ua while providing 125ms periodic wakeups. Sunduino comes in 25W and 10W versions to better suit your application. Take a look at the manual and Datashseet. Also the schematic and PCB layout is available for free.

Key benefits of SunDuino:

  1. Battery charging logic is optimized for long battery life using temperature monitor. The SunDuino is a software defined charger, it supports many battery chemistries and sizes.
  2. Low current operation provides long battery life and runtime. An internal RTOS keeps battery monitoring, power event monitoring, user C Application and SLEEP mode all operating on a 100ua drain. Small batteries can run for years.
  3. Regulated output voltages of +5. +3.3 and +/-12 for the powering of external hardware. Radios, other processors, relays and LEDs are examples of external hardware which requires regulated voltages.
  4. Runs compiled C Applications and various library function for complete user control of power operation. Greatly simplifies system integration.

SunDuino – Run your C application using solar power – [Link]

RELATED POSTS

It’s the end of C as we know it!

04-TIOBE

beningo.com discuss about the future of C language and it’s use in embedded systems.

The C programming language has been with embedded software developers since its creation in 1972. Ever since then C has been a blazing constant, surviving even the big push in the late 90’s and early 21st century to move to C++ or other object oriented languages. Undoubtedly, C will continue to be a foundational language for embedded systems but over the last year, the language has begun to see a decrease in popularity.

It’s the end of C as we know it! – [Link]

DIY Mini Quadcopter with 3D-Printed Frame and Custom Firmware

20160516_194627

Simon D. Levy designed and build a mini quadcopter with 3D printed frame and custom C++ firmware. See the full project notes including bill of materials and more photos on the link below, and check out Simon’s blog on the C++ firmware here.

I wanted to try my hand at a true “DIY” project, designing and building my own micro quadcotper frame and writing my own firmware in C++.  In this post I will describe the frame and build; in my next post I will talk about the firmware.

DIY Mini Quadcopter with 3D-Printed Frame and Custom Firmware – [Link]

ulibSD – a library for use SD cards in SPI mode with uControllers

SD_Card_SPI

electronictechnician.wordpress.com has published a library for use SD cards in SPI completely written in C.

It’s a library for use SD cards in SPI mode with uControllers, entirely written in C. This library can work with SD cards and also has the possibility to emulate the behavior in a PC file (GNU/Linux) using the macro _M_IX86. It’s for debugging purposes. The data transfer is oriented to 512 byte size, remember this.

ulibSD – a library for use SD cards in SPI mode with uControllers – [Link]

Serial Port Communication in C#

50e1ce8bce395fb62b000000

Maurizio tipped us with his latest article on how to use Serial Port in C#. The article does cover the basic code needed to achieve serial communication.

The serial port of the PC is a very important resource both in industrial environment and in home-made electronics, due to the wide popularity of the UART interface which is to be found on many microcontrollers or on many test and design instrumentation (programmable power supplies, multi-meters, oscilloscopes etc).

Serial Port Communication in C# – [Link]