Introduction
This sample code uses the Live USB image from the IoT Development Kit to run a host system that can control an Intel® Galileo board using Yocto Application Development Tools and Eclipse*. This sample assumes the Intel Galileo board is booted using the SD Image, which is also available in the IoT Development Kit. It is possible to use this code directly on the board, but that’s a topic for a future paper.
The Intel Galileo board is an Arduino* certified board that offers endless possibilities for creating projects and enabling ideas for the Internet of Things. The following diagram shows how to access the controls for the digital GPIOs from the operating system.
The logical representation of each GPIO is available at the /sys/class/gpio directory. Sergey’s Blog has excellent coverage of the modules and mapping of the GPIOs, including the mapping above that explains the hardware underlying the GPIOs in the system.
For this example, we will use the pin numbers 5, 6, and 7 since they are directly mapped into the hardware and do not have any other inputs (unlike pin #3, for instance). We have one LED connected to each one of these pins. Since each LED has its own required input voltage, we have added resistance to adjust to that. The schematic looks like this:
Following is the sample code that provides the steps needed to manipulate the GPIOs:
(1) Export the GPIO number to the /sys/class/gpio/export file
(2) Set the direction: in for input, out for output
These steps are implemented using the openGPIO function, which opens the corresponding file and returns this file identifier for future reading or writing, depending on the direction declared.
int openGPIO(int gpio, int direction )
{
char buffer[256];
int fileHandle;
int fileMode;
fileHandle = open("/sys/class/gpio/export", O_WRONLY);
if(ERROR == fileHandle)
{
puts("Error: Unable to opening /sys/class/gpio/export");
return(-1);
}
sprintf(buffer, "%d", gpio);
write(fileHandle, buffer, strlen(buffer));
close(fileHandle);
sprintf(buffer, "/sys/class/gpio/gpio%d/direction", gpio);
fileHandle = open(buffer, O_WRONLY);
if(ERROR == fileHandle)
{
puts("Unable to open file:");
puts(buffer);
return(-1);
}
if (direction == GPIO_DIRECTION_OUT)
{
write(fileHandle, "out", 3);
fileMode = O_WRONLY;
}
else
{
write(fileHandle, "in", 2);
fileMode = O_RDONLY;
}
close(fileHandle);
sprintf(buffer, "/sys/class/gpio/gpio%d/value", gpio);
fileHandle = open(buffer, fileMode);
if(ERROR == fileHandle)
{
puts("Unable to open file:");
puts(buffer);
return(-1);
}
return(fileHandle); }
In the main function, we call openGPIO for each pin we want to manipulate:
fileHandleGPIO_5 = openGPIO(GP_5, GPIO_DIRECTION_OUT);
fileHandleGPIO_6 = openGPIO(GP_6, GPIO_DIRECTION_OUT);
fileHandleGPIO_7 = openGPIO(GP_7, GPIO_DIRECTION_OUT);
Once we have verified that there are no errors, we go to the fun part, which is changing the GPIO value to 0 or 1, effectively enabling or disabling the voltage in the port and making the LEDs blink:
for(i=0; i< 10; i++)
{
writeGPIO(fileHandleGPIO_LED, 1);
writeGPIO(fileHandleGPIO_5, 1);
writeGPIO(fileHandleGPIO_6, 1);
writeGPIO(fileHandleGPIO_7, 1);
sleep(BLINK_TIME_SEC);
writeGPIO(fileHandleGPIO_LED, 0);
writeGPIO(fileHandleGPIO_5, 0);
writeGPIO(fileHandleGPIO_6, 0);
writeGPIO(fileHandleGPIO_7, 0);
sleep(BLINK_TIME_SEC);
}
The full code can be found here. To execute it, open the Hello World sample that comes with the Live USB image in the IoT Development Kit and replace the code there with the code for BlinkingLEDs.c file. It should run without any problems.
Intel and the Intel logo are trademarks of Intel Corporation in the U.S. and/or other countries.
Copyright © 2014 Intel Corporation. All rights reserved.
*Other names and brands may be claimed as the property of others.