Poll

 Please rate this tutorial...

Good
1 (20%)
OK
0 (0%)
Needs Revision
3 (60%)
Inappropriate
1 (20%)

Total Members Voted: 5

Author Topic: A Tutorial on using GPIO signals  (Read 329 times)

fatman2021

  • Hero Member
  • *****
  • Posts: 978
  • Lord Jesus Christ, Son of God, have mercy on us.
    • Email
A Tutorial on using GPIO signals
« on: February 25, 2013, 09:35:55 AM »
General Purpose Input/Output (GPIO) is a generic pin on a chip whose behavior (including whether it is an input or output pin) can be controlled (programmed) by the user at run time.

GPIO pins have no special purpose defined, and go unused by default. The idea is that sometimes the system integrator building a full system that uses the chip might find it useful to have a handful of additional digital control lines, and having these available from the chip can save the hassle of having to arrange additional circuitry to provide them. For example, the Realtek ALC260 chips (audio codec) have 8 GPIO pins, which go unused by default. Some system integrators (Acer Inc. laptops) employing the ALC260 use the first GPIO (GPIO0) to turn on the amplifier used for the laptop's internal speakers and external headphone jack.

GPIOs are used in:

   1) chips with pin scarcity: IC, SoC, embedded and custom hardware, programmable logic devices (e.g., FPGAs)
   2) multifunction chips: power managers, audio codecs, video cards
   3) embedded applications (e.g., Arduino) make heavy use of GPIO for reading from various environmental sensors (IR, video, temperature, 3-axis orientation, acceleration), and for writing output to dc motors (via PWM), audio, LCD displays, or LEDs for status.

GPIO capabilities may include:

    1) GPIO pins can be configured to be input or output
    2) GPIO pins can be enabled/disabled
    3) input values are readable (typically high=1, low=0)
    4) output values are writable/readable
    5) input values can often be used as IRQs (typically for wakeup events)
    6) the device can use DMA to move large quantities of data efficiently into or out of the device

GPIO peripherals vary quite widely. In some cases, they are very simple, a group of pins that can be switched as a group to either input or output. In others, each pin can be set up flexibly to accept or source different logic voltages, with configurable drive strengths and pull ups/downs. The input and output voltages are typically, though not universally limited to the supply voltage of the device with the GPIOs on, and may be damaged by greater voltages.

Some GPIOs have 5 V tolerant inputs: even where the device has a low supply voltage (such as 2 V), the device can accept 5 V without damage.

A GPIO port is a group of GPIO pins (typically 8 GPIO pins) arranged in a group, and treated as a single port.

The following summarizes the steps to use GPIO signals from a Linux application:

The sysfs directory /sys/class/gpio contains subdirectories and files that are used for configuring and using GPIO signals from a Linux application.

/sys/class/gpio (sysfs GPIO subdirectory) - Linux applications can configuration and use GPIO signals by accessing files in this subdirectory.

/sys/class/gpio/gpio<<<number>>> (Subdirectory for configuring and reading a specific GPIO signal) - Once a GPIO has been exported to user space, a new directory appears with a set of files that allow the GPIO to be configured and used by a Linux application.

/sys/class/gpio/gpio<<<number>>>/direction  (Read-write supporting values of in and out)

/sys/class/gpio/gpio<<<number>>>/value (Read-write supporting values of 0 and 1)

/sys/class/gpio/gpio<<<number>>>/edge (Read-write supporting values of rising and falling)


We need to do this in Linux root. We need to run BASIC in the root too. First we go to the root, then we load QB64 into root:

Code: [Select]
sudo -1
sudo ./qb64

Now to export the no4 GPIO pin for example, using a Shell command:

Code: [Select]
echo “4” >  /sys/class/gpio/export
Whilst QB64 can accommodate shell commands, and we can store a set of these commands (eg. to export a number of GPIO pins at the outset) as numbered statements in a file that can be loaded with the basic command LOAD “filename” and RUN (ref 2), the shell commands have to run as a separate file, as they cannot be run from within, as part of a basic program:

Now we can access the file containing the pin direction setting from BASIC

We can set GPIO pin 4 to input or to output by OPENing its pin direction file for output and writing “in” or “out” with a PRINT# command. (ref 2 )

Code: [Select]
10 OPEN  “/sys/devices/virtual/gpio/gpio4/direction” FOR OUTPUT AS #1
20 PRINT #1,”out”
30 CLOSE #1

We are now able to control the output of the gpio 4 pin from BASIC

We can set the GPIO 4 pin to 1 or to 0 by OPENing its pin value file for output and writing “1” or “0” with a PRINT# command:

Code: [Select]
40 OPEN “/sys/devices/virtual/gpio/gpio4/value” FOR OUTPUT AS #4
50 PRINT #4,”1”
60 CLOSE #4

Example of an (unstructured) BASIC program

To read the state of a switch and control the power to two LEDs connected to GPIO pins 8,7 and 4 respectively.

Program to set 2 pins as outputs and 1 pin as input and to read the input turning on two different combinations of the two outputs (ie output 0,1 or 1,0) depending on the state of the input (1 or 0).

Code: [Select]
sudo –i
echo “4” > /sys/class/gpio/export
echo “7” > /sys/class/gpio/export
echo “8” > /sys/class/gpio/export
sudo ./qb64

Code: [Select]
10 OPEN “/sys/devices/virtual/gpio/gpio4/direction” FOR OUTPUT AS #1
20 OPEN “/sys/devices/virtual/gpio/gpio7/direction” FOR OUTPUT AS #2
30 OPEN “/sys/devices/virtual/gpio/gpio8/direction” FOR OUTPUT AS #3
35 REM opens the three pin direction file.
40 PRINT #1, “out”
50 PRINT #2, “out”
60 PRINT #3, “in”
65 REM sets GPIO pins 4 and 7 as outputs and GPIO pin 8 as input.
70 CLOSE #1
80 CLOSE #2
90 CLOSE #3
95 REM closes all open files, allowing the system to perform the direction settings.
100 OPEN “/sys/devices/virtual/gpio/gpio8/value” FOR INPUT AS #8
105 REM opens the GPIO pin 8 value file
110 INPUT #8,x
115 REM reads the value of the input pin and stores the value in numerical variable x
120 CLOSE #8
125 REM closes the open file, allowing the system to read the value of the input pin and store the value in numerical variable x.
130 OPEN “/sys/devices/virtual/gpio/gpio4/value” FOR OUTPUT AS #1
140 OPEN “/sys/devices/virtual/gpio/gpio7/value” FOR OUTPUT AS #2
145 REM opens the GPIO pins 4 and value files ready for outputting 1s and 0s.
150 IF x<1 THEN GOTO 160 ELSE GOTO 190
155 REM tests the state of the switch (1 or0) and directs the program to generate the appropriate outputs
160 PRINT #1,”1”
170 PRINT #2,”0”
180 GOTO 210
190  PRINT#1,"0"
200 PRINT #2,"1"
210 CLOSE #1
220 CLOSE #2
225 REM Closes the files and allows the outputs to light the LED
230 END

When all is done, we should unexport the GPIO pins, to leave the R-Pi as we found it:

Code: [Select]
echo “4” > /sys/class/gpio/unexport
echo “7” > /sys/class/gpio/unexport
echo “8” > /sys/class/gpio/unexport
« Last Edit: February 25, 2013, 01:23:30 PM by fatman2021 »
Woe to those who call evil good, and good evil;
Who put darkness for light, and light for darkness;
Who put bitter for sweet, and sweet for bitter!

Isaiah 5:20