Raspberry Pi GPIO Library  0.3
Library allowing for control of the Raspberry Pi's GPIO pins.
Macros | Functions | Variables
gpio.c File Reference

Contains source for the GPIO functionality. More...

#include "gpio.h"

Go to the source code of this file.

Macros

#define ERROR(x)   #x,
 

Functions

static errStatus gpioValidatePin (int gpioNumber)
 Internal function which Validates that the pin gpioNumber is valid for the Raspberry Pi. More...
 
errStatus gpioSetup (void)
 Maps the memory used for GPIO access. This function must be called prior to any of the other GPIO calls. More...
 
errStatus gpioCleanup (void)
 Unmaps the memory used for the gpio pins. This function should be called when finished with the GPIO pins. More...
 
errStatus gpioSetFunction (int gpioNumber, eFunction function)
 Sets the functionality of the desired pin. More...
 
errStatus gpioSetPin (int gpioNumber, eState state)
 Sets a pin to high or low. More...
 
errStatus gpioReadPin (int gpioNumber, eState *state)
 Reads the current state of a gpio pin. More...
 
errStatus gpioSetPullResistor (int gpioNumber, eResistor resistorOption)
 Allows configuration of the internal resistor at a GPIO pin. More...
 
errStatus gpioGetI2cPins (int *gpioNumberScl, int *gpioNumberSda)
 Get the correct I2C pins. More...
 
const char * gpioErrToString (errStatus error)
 Debug function which converts an error from errStatus to a string. More...
 
int dbgPrint (FILE *stream, const char *file, int line, const char *format,...)
 Debug function wrapper for fprintf(). More...
 

Variables

static volatile uint32_t * gGpioMap = NULL
 Pointer which will be mmap'd to the GPIO memory in /dev/mem.
 
static tPcbRev pcbRev = pcbRevError
 PCB revision that executable is being run on.
 

Detailed Description

Contains source for the GPIO functionality.

This is is part of https://github.com/alanbarr/RaspberryPi-GPIO a C library for basic control of the Raspberry Pi's GPIO pins. Copyright (C) Alan Barr 2012

This code was loosely based on the example code provided by Dom and Gert found at: http://elinux.org/RPi_Low-level_peripherals

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file gpio.c.

Macro Definition Documentation

◆ ERROR

#define ERROR (   x)    #x,

Redefining to replace macro with x as a string, i.e. "x". For use in gpioErrToString()

Definition at line 399 of file gpio.c.

Function Documentation

◆ dbgPrint()

int dbgPrint ( FILE *  stream,
const char *  file,
int  line,
const char *  format,
  ... 
)

Debug function wrapper for fprintf().

Allows file and line information to be added easier to output strings. DBG_INFO is a macro which is useful to call as the "first" parameter to this function. Note this function will add on a newline to the end of a format string so one is generally not required in format.

Parameters
[in]streamOutput stream for strings, e.g. stderr, stdout.
[in]fileName of file to be printed. Should be retrieved with FILE.
lineLine number to print. Should be retrieved with LINE.
[in]formatFormatted string in the format which printf() would accept.
...Additional arguments - to fill in placeholders in parameter format.
Returns
This function uses the printf() family functions and the returned integer is what is returned from these calls: If successful the number or characters printed is returned, if unsuccessful a negative value.
Examples:
gpio_example_input.c, gpio_example_output.c, i2c_example_bitexpander.c, i2c_example_eeprom.c, and i2c_example_temp_sensor.c.

Definition at line 439 of file gpio.c.

◆ gpioCleanup()

errStatus gpioCleanup ( void  )

Unmaps the memory used for the gpio pins. This function should be called when finished with the GPIO pins.

Returns
An error from errStatus.
Examples:
gpio_example_input.c, gpio_example_output.c, i2c_example_bitexpander.c, i2c_example_eeprom.c, and i2c_example_temp_sensor.c.

Definition at line 134 of file gpio.c.

◆ gpioErrToString()

const char* gpioErrToString ( errStatus  error)

Debug function which converts an error from errStatus to a string.

Parameters
errorError from errStatus.
Returns
String representation of errStatus parameter error.

Definition at line 405 of file gpio.c.

◆ gpioGetI2cPins()

errStatus gpioGetI2cPins ( int *  gpioNumberScl,
int *  gpioNumberSda 
)

Get the correct I2C pins.

The different revisions of the PI have their I2C ports on different GPIO pins which require different BSC modules.

Parameters
[out]gpioNumberSclInteger to be populated with scl gpio number.
[out]gpioNumberSdaInteger to be populated with sda gpio number.
Todo:
TODO Does this need to be public or internal only?
Returns
An error from errStatus.

Definition at line 356 of file gpio.c.

◆ gpioReadPin()

errStatus gpioReadPin ( int  gpioNumber,
eState state 
)

Reads the current state of a gpio pin.

Parameters
gpioNumberThe number of the GPIO pin to read.
[out]statePointer to the variable in which the GPIO pin state is returned.
Returns
An error from errStatus.
Examples:
gpio_example_input.c.

Definition at line 255 of file gpio.c.

◆ gpioSetFunction()

errStatus gpioSetFunction ( int  gpioNumber,
eFunction  function 
)

Sets the functionality of the desired pin.

Parameters
gpioNumberThe gpio pin number to change.
functionThe desired functionality for the pin.
Returns
An error from errStatus.
Examples:
gpio_example_input.c, and gpio_example_output.c.

Definition at line 164 of file gpio.c.

◆ gpioSetPin()

errStatus gpioSetPin ( int  gpioNumber,
eState  state 
)

Sets a pin to high or low.

The pin should be configured as an ouput with gpioSetFunction() prior to this.

Parameters
gpioNumberThe pin to set.
stateThe desired state of the pin.
Returns
An error from errStatus.
Examples:
gpio_example_output.c.

Definition at line 208 of file gpio.c.

◆ gpioSetPullResistor()

errStatus gpioSetPullResistor ( int  gpioNumber,
eResistor  resistorOption 
)

Allows configuration of the internal resistor at a GPIO pin.

The GPIO pins on the BCM2835 have the option of configuring a pullup, pulldown or no resistor at the pin.

Parameters
gpioNumberThe GPIO pin to configure.
resistorOptionThe available resistor options.
Returns
An error from errStatus.
Examples:
gpio_example_input.c.

Definition at line 302 of file gpio.c.

◆ gpioSetup()

errStatus gpioSetup ( void  )

Maps the memory used for GPIO access. This function must be called prior to any of the other GPIO calls.

Returns
An error from errStatus.
Examples:
gpio_example_input.c, gpio_example_output.c, i2c_example_bitexpander.c, i2c_example_eeprom.c, and i2c_example_temp_sensor.c.

Definition at line 44 of file gpio.c.

◆ gpioValidatePin()

static errStatus gpioValidatePin ( int  gpioNumber)
static

Internal function which Validates that the pin gpioNumber is valid for the Raspberry Pi.

The first time this function is called it will perform some basic initalisation on internal variables.

Parameters
gpioNumberThe pin number to check.
Returns
An error from errStatus.

Definition at line 480 of file gpio.c.