Skip to content

Software: Pin Access

justinjessada edited this page Oct 31, 2013 · 15 revisions

The software contains several functions to directly access pins on the BeagleBone. These functions are kept in separate files, and are called by the actuator and sensor threads whenever a pin must be read (for input) or a pin must be written (for output). Accessing pins on the BeagleBone is done through the sysfs filesystem. Briefly, each pin can be accessed through a filepath in the Linux OS of the BeagleBone. The GPIO paths have the following formats:

  • "/sys/class/gpio%s/export"
  • "/sys/class/gpio%s/unexport"
  • "/sys/class/gpio%s/gpio%d/direction"
  • "/sys/class/gpio%s/gpio%d/value"

with similar paths for PWM and ADC modules (as defined in bbb_pin_defines.h). These files can be opened, closed and written/read by the program to achieve the desired operation.

The general process of accessing a pin is similar for GPIO, PWM and ADC modules. There are four steps; steps 1 and 2 only happen upon initialisation of the server, and step 4 only happens upon shutdown:

    1. the pin must be exported
    1. the pin direction is set (read/write)
    1. the pin is read/written
    1. the pin is unexported

This file defines all of the GPIO names, ADC names and PWM module names. The filepaths of these pins are also defined as well as the number of pins.

This file initialises a lookup table from the actual pin number to the GPIO number so that pins can be used in a relatively readable way by the program. Indexes can also be mapped to GPIO numbers and a lookup table exists for the PWM modules.

Declares all the required functions for accessing pins/PWM/ADCs. Also contains code to silence the compiler when compiling on a system separate from the BeagleBone.

Implements all of the required functions for accessing pins/PWM/ADCs. Structures are used to store information about each pin (whether it is initalised, the value file descriptor and the direction file descriptor, plus duty/period file pointers for PWM) so this does not have to be recreated on each read/write operation. There are separate arrays of GPIO pin, ADC pins and PWM pins. The functions are as follows:

  • GPIO_Export: export the pin
  • GPIO_Unexport: unexport the pin
  • PWM_Export, PWM_Unexport, ADC_Export, PWM_Unexport: as above
  • GPIO_Set: set a pin to 1 or 0
  • GPIO_Read: read a value from a pin
  • PWM_Set: set a PWM module to the required signal (polarity, duty percentage, period)
  • PWM_Stop: stop a PWM module
  • ADC_Read: read a value from an analog-to-digital converter

For more detailed comments and the actual code, see [bbb_pin.c] (https://github.com/szmoore/MCTX3420/blob/master/server/bbb_pin.c), [bbb_pin.h] (https://github.com/szmoore/MCTX3420/blob/master/server/bbb_pin.h), bbb_pin_defines.c and [bbb_pin_defines.h] (https://github.com/szmoore/MCTX3420/blob/master/server/bbb_pin_defines.h).

For more information on PWM control, see PWM Control.