GPIO(General purpose Input/Output):
A "General Purpose Input/Output" (GPIO) is a flexible software-controlled digital signal. On a given board each GPIO is used for one specific purpose like driving a LED, sensing a switch and so-on.
Note: On Board GPIO_LEDS : PC13,PC17,PC19.
Let us try and test "GPIO" using a basic application.
Step 1: Create a gpio_test.c File
Step 2: Write a code to test a Basic GPIO Application:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<fcntl.h>
#include <unistd.h>
char export_path[100] = "/sys/class/gpio/export";
char direction_path[100] = "/sys/class/gpio/";
char value_path[100] = "/sys/class/gpio/";
int main(int argc,char **argv)
{
int fd;
int value;
fd = open(export_path,O_WRONLY);
perror("open");
value = atoi(argv[1]);
write(fd,argv[1],strlen(argv[1])+1);
perror("write");
close(fd);
if(value>=0 && value <=31)
{
sprintf(direction_path+16,"%s%d%s","PA",value,"/direction");
sprintf(value_path+16,"%s%d%s","PA",value,"/value");
}
else if(value>=32 && value <=63)
{
sprintf(direction_path+16,"%s%d%s","PB",value-32,"/direction");
sprintf(value_path+16,"%s%d%s","PB",value-32,"/value");
}
else if(value>=64 && value <=95)
{
sprintf(direction_path+16,"%s%d%s","PC",value-64,"/direction");
sprintf(value_path+16,"%s%d%s","PC",value-64,"/value");
}
else
{
sprintf(direction_path+16,"%s%d%s","PD",value-96,"/direction");
sprintf(value_path+16,"%s%d%s","PD",value-96,"/value");
}
printf("%s\n",direction_path);
printf("%s\n",value_path);
fd = open(direction_path,O_WRONLY);
perror("open");
write(fd,"out",4);
perror("write");
close(fd);
fd = open(value_path,O_WRONLY);
perror("open");
while(1)
{
write(fd,"1",2);
perror("write");
sleep(5);
write(fd,"0",2);
perror("write");
sleep(5);
}
close(fd);
}
Step 3: Open your Terminal and navigate to your Toolchain folder on your HOST PC
Step 4: Run the "env.sh" file to enable the Toolchain using . env.sh
To check if the Toolchain is running type in "arm" and hit the <TAB> key twice.
Step 5: Create a binary named "gpiotest" from gpio_test.c arm-linux-gnueabi-gcc gpio_test.c -o gpiotest
Step 6: Transfer the compiled binary to the board using TFTP or SD card.
Step 7: Run the binary file ./gpiotest 77
on the Board's Terminal.
Last updated
Was this helpful?