ruggedBOARD
Tutorial Using Python
Tutorial Using Python
  • Overview
  • Quick Start with RB
    • Pre-Requisite
      • Required Hardware and Software Components
      • Host Setup
      • Linux Commands
    • Power Up your RB
    • Fiddle with RB Command-line
  • Lib-Mraa & UPM Sensor Libraries (Python)
    • First Python Program
    • Know Your Libraries
      • LibMRAA
        • Python Programming using MRAA (SPI)
        • Python Programming using MRAA (PWM)
        • Python Programming using MRAA (gpio_blink)
        • Python Programming using MRAA (Aio)
      • LibUPM
    • Example Programs for Sensors
      • Procedure to interface phyNODE with RB
      • Accelerometer
      • Temperature Sensor
      • Pressure Sensor
      • Humidity Sensor
      • Magnetometer
      • PIR Sensor
      • Ultrasonic Sensor
      • Gas Sensor (MQ5)
    • mikroBUS UPM Sensor
    • Example programs for Communication Protocols
      • GPIO
      • I2C
      • UART
  • Hello World in C language
    • Setting up Environment
    • Transferring Files from Host to Board
    • Write your first C program
  • IoT Implementation
    • What is IoT?
    • IoT Protocols
    • MQTT Setup
  • Peripherals Functionality Testing using C Language
    • Interfaces
      • GPIO(General purpose Input/Output):
      • UART(Universal Asynchronous Receiver-Transmitter)
      • RS232
      • RS485
      • CAN
      • MikroBUS
      • mPCIe
  • Advance RB
    • Preparing SD card in case of OS failure
Powered by GitBook
On this page

Was this helpful?

  1. Peripherals Functionality Testing using C Language
  2. Interfaces

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 77on the Board's Terminal.

PreviousInterfacesNextUART(Universal Asynchronous Receiver-Transmitter)

Last updated 6 years ago

Was this helpful?