Friday, April 19, 2024

Accessing GPIOs using SYSfs interface of Linux

This will remove a ‘gpio19’ node (folder).

Now a user can access GPIO using its attributes. To see its attributes, type the below-mentioned commands. (Fig. 3 shows an example of gpio4.)

 [stextbox id=”grey”]$ cd gpio19
$ ls[/stextbox]

After the ‘ls’ command, you will see below-mentioned attributes:

direction. Reads as either ‘in’ or ‘out’ to declare pin as input or output.

- Advertisement -

value. Reads as either ‘0’ (low) or ‘1’ (high). If the GPIO is configured as an output, this value may be written; any non-zero value is treated as high.

edge. Reads as either ‘none,’ ‘rising,’ ‘falling’ or ‘both.’ Write these strings to select the signal edge(s).

active_low. Reads as either ‘0’ (false) or ‘1’ (true). Write any non-zero value to invert the value attribute both for reading and writing.

- Advertisement -

Now connect the LED as shown in Fig. 5 and follow the steps below to access the GPIO pins and enable the LED to blink (refer Fig. 6 for the steps). We will only use two attributes: direction and value.

1. Start the terminal window on Raspberry Pi and go to cd /sys/class/gpio/ directory.
2. Login as root (as we are accessing kernel space) by ‘su’ command and type the root password when prompted.
3. Export GPIO4 using ‘echo 4 > export’ command. By this statement you reserve GPIO for your application so other modules will not interfere.
4. New directory named ‘gpio4’ will be created.
5. Enter the directory by typing ‘cd gpio4’ to see and edit attributes of GPIO4.
6. Change attributes as: echo “out” > direction.
7. To switch the LED on use ‘echo 1 > value’ and to switch LED off use ‘echo 0 > value’
8. To see attribute’s value, use cat command: cat value or cat direction.
9. For disabling the GPIO, first write ‘cd ..’ to come one step back and then use ‘echo 4 > unexport’ command. The gpio4 folder will be deleted automatically.

By using these steps, you can enable the LED to blink manually. But what if you want it to blink automatically? For that you will use shell scripting.

Shell is a program that runs in background of a terminal. Terminal accepts command from keyboard and sends it to shell, which finds the meaning and executes the command. User enters commands one by one and they are executed in a similar manner. These commands can be stored as a sequence of commands to a file and shell can execute this file instead of entering the commands one by one in the future. This file is known as a shell script. Shell script is a series of commands written in plain text file. Shell script is just like the MS-DOS batch file.

Create a new file by typing ‘sudo nano firstscript’ in the terminal and enter the code given below in it:

 [stextbox id=”grey”]

#! /bin/bash

cd /sys/class/gpio
echo 4 > export
cd gpio4
echo “out” > direction

for i in {1..10}
do
echo 0 > value
sleep 1
echo 1 > value
sleep 1
done

cd ..
echo 4 > unexport

[/stextbox]

Now save the script by pressing ‘ctrl + o’ to save and ‘ctrl + x’ to exit.

To make the file executable, write the below-mentioned command in the terminal window:

 [stextbox id=”grey”]$ chmod 777 firstscript
Now execute the script as follows
$ sudo ./firstscript[/stextbox]

Accessing GPIO using SYSfs interface from application
The above steps are for controlling or accessing one GPIO but we can also handle more GPIOs at the same time by using an application. The application (‘c’ program) that we have discussed here also controls one GPIO, but the code can be easily modified to handle more GPIOs. Code accepts GPIO number from user and controls it.

Create another file with command ‘sudo nano led.c’ for the code of the application program and copy below code in it.

 [stextbox id=”grey”]

————- led.c ————–
#include
#include
#include
#include
#include
#include

int gpio_export(unsigned int );
int gpio_unexport(unsigned int);
int gpio_set_dir(unsigned int ,
unsigned int );
int gpio_set_value(unsigned int ,
unsigned int );

#define SYSFS_GPIO_DIR “/sys/class/
gpio”
#define MAX_BUF 64

int main()
{
int x;
printf(“select the gpio “);
scanf(“%d”,&x); // accept GPIO number
from user

gpio_export(x); // export that gpio
to user space
gpio_set_dir(x,1); // set
direction of gpio as 1=output

while(1) //loop for blinking the led
{
gpio_set_value(x,1); // glowing led
connected to gpio x.
printf(“LED ON\n”);
sleep(1); // delay of 1 second
gpio_set_value(x,0); // led off
printf(“LED OFF”);
sleep(1);
}
// making GPIO free for other kernel
modules if loop is finite
gpio_unexport(x);
return 0;
}

/*********Export function*********/
int gpio_export(unsigned int gpio)
{
int fd, len;
char buf[MAX_BUF];

//opening EXPORT file as Write Only
fd = open(SYSFS_GPIO_DIR “/export”,
O_WRONLY);
if (fd < 0) // error in opening file
{
perror(“gpio/export”);
return fd;
}

len = snprintf(buf, sizeof(buf),
“%d”, gpio);
write(fd, buf, len);
close(fd);

return 0;
}

/********** gpio_unexport ********/
int gpio_unexport(unsigned int gpio)
{
int fd, len;
char buf[MAX_BUF];

fd = open(SYSFS_GPIO_DIR “/unexport”,
O_WRONLY);
if (fd < 0)
{
perror(“gpio/unexport”);
return fd;
}

len = snprintf(buf, sizeof(buf),
“%d”, gpio);
write(fd, buf, len);
close(fd);
return 0;
}

/******* gpio_set_direction *******/
int gpio_set_dir(unsigned int gpio,
unsigned int dir)
{
int fd,len;
char buf[MAX_BUF];

len=snprintf(buf, sizeof(buf), SYSFS_
GPIO_DIR “/gpio%d/direction”, gpio);

fd = open(buf, O_WRONLY);
if (fd < 0)
{
perror(“gpio/direction”);
return fd;
}

if (dir) // 1=output and 0=input
write(fd, “out”, 4);
else
write(fd, “in”, 3);

close(fd);
return 0;
}

/********* gpio_set_value *********/
int gpio_set_value(unsigned int gpio,
unsigned int val)
{
int fd;
char buf[MAX_BUF];

len=snprintf(buf, sizeof(buf),
SYSFS_GPIO_DIR “/gpio%d/value”,
gpio);

fd = open(buf, O_WRONLY);
if (fd < 0)
{
perror(“gpio/set-value”);
return fd;
}

if (val)
write(fd, “1”, 2);
else
write(fd, “0”, 2);

close(fd);
return 0;
}

[/stextbox]

After copying the code, use ‘ctrl+o’ to save and ‘ctrl+x’ to exit. To compile the code, use in-built gcc compiler in Linux and execute the code that will use SYSfs interface to get the LED blinking connected at a GPIO.

To compile the code:

 [stextbox id=”grey”]$ gcc -o sysled led.c[/stextbox]

To execute the code:

 [stextbox id=”grey”]$ sudo ./sysled[/stextbox]

where sysled is the output filename.


Onkar Ashok Mate is pursuing M.Tech in Electronics from Walchand College of Engineering, Sangli, Maharashtra and V.B. Dharmadhikari is an associate professor at Department of Electronics, Walchand College of Engineering, Sangli, Maharashtra

SHARE YOUR THOUGHTS & COMMENTS

Unique DIY Projects

Electronics News

Truly Innovative Tech

MOst Popular Videos

Electronics Components

Calculators