Raspberry Pi GPIO Library  0.3
Library allowing for control of the Raspberry Pi's GPIO pins.
Macros | Enumerations | Functions
rpiGpio.h File Reference

The main header file of the GPIO library. More...

#include "bcm2835_gpio.h"
#include <stdio.h>
#include <stdint.h>

Go to the source code of this file.

Macros

#define CORE_CLK_HZ   250000000
 Speed of the core clock core_clk.
 
#define ERRORS
 The list of errors which may be returned from gpio functions. More...
 
#define ERROR(x)   x,
 Redefining to replace the macro with x.
 
#define I2C_CLOCK_FREQ_MIN   10000
 Minimum I2C frequency (Hertz)
 
#define I2C_CLOCK_FREQ_MAX   400000
 Maximum I2C frequency (Hertz)
 
#define DBG_INFO   stderr,__FILE__,__LINE__
 Macro which covers the first three arguments of dbgPrint.
 
#define REV1_PINCNT   17
 Pin count on a PCB rev1 Raspberry Pi.
 
#define REV2_PINCNT   17
 Pin count on a PCB rev2 Raspberry Pi.
 
#define REV1_PINS   {0, 1, 4, 7, 8, 9, 10, 11, 14, 15, 17, 18, 21, 22, 23, 24, 25}
 
#define REV2_PINS   {2, 3, 4, 7, 8, 9, 10, 11, 14, 15, 17, 18, 22, 23, 24, 25, 27}
 
#define REV1_SDA   0
 The BCM2835 pin number of SDA on rev1 Raspberry Pi.
 
#define REV1_SCL   1
 The BCM2835 pin number of SCL on rev1 Raspberry Pi.
 
#define REV2_SDA   2
 The BCM2835 pin number of SDA on rev2 Raspberry Pi.
 
#define REV2_SCL   3
 The BCM2835 pin number of SCL on rev2 Raspberry Pi.
 

Enumerations

enum  errStatus { ERROR_MAX }
 The enum of possible errors returned from gpio functions. Errors themselves are defined in the macro ERRORS.
 
enum  eState { low = 0x0, high = 0x1 }
 The enum of possible pin states in input/output modes. More...
 
enum  eResistor { pullDisable = GPPUD_DISABLE, pulldown = GPPUD_PULLDOWN, pullup = GPPUD_PULLUP }
 The enum for possible pull resistors. More...
 
enum  eFunction {
  input = GPFSEL_INPUT, output = GPFSEL_OUTPUT, alt0 = GPFSEL_ALT0, alt1 = GPFSEL_ALT1,
  alt2 = GPFSEL_ALT2, alt3 = GPFSEL_ALT3, alt4 = GPFSEL_ALT4, alt5 = GPFSEL_ALT5,
  eFunctionMin = GPFSEL_INPUT, eFunctionMax = GPFSEL_ALT3
}
 The enum of pin functions available. More...
 
enum  tPcbRev { pcbRevError = 0, pcbRev1 = 1, pcbRev2 = 2 }
 valid PCB revision values
 

Functions

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 resistor)
 Allows configuration of the internal resistor at a GPIO pin. More...
 
errStatus gpioGetI2cPins (int *gpioNumberScl, int *gpioNumberSda)
 Get the correct I2C pins. More...
 
errStatus gpioI2cSetup (void)
 Initial setup of I2C functionality. More...
 
errStatus gpioI2cCleanup (void)
 Disables the I2C controller and unmaps the memory used for the i2c functionality. This function should be called when finished with the I2C module. More...
 
errStatus gpioI2cSetClock (int frequency)
 Sets the I2C Clock Frequency. More...
 
errStatus gpioI2cSet7BitSlave (uint8_t slaveAddress)
 Sets the 7-bit slave address to communicate with. More...
 
errStatus gpioI2cWriteData (const uint8_t *data, uint16_t dataLength)
 Writes data to the address previously specified by gpioI2cSet7BitSlave(). More...
 
errStatus gpioI2cReadData (uint8_t *buffer, uint16_t bytesToRead)
 Read a number of bytes from I2C. The slave address should have been previously set with gpioI2cSet7BitSlave(). 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...
 

Detailed Description

The main header file of the GPIO library.

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 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 rpiGpio.h.

Macro Definition Documentation

◆ ERRORS

#define ERRORS
Value:
ERROR(OK) \
ERROR(ERROR_DEFAULT) \
ERROR(ERROR_INVALID_PIN_NUMBER) \
ERROR(ERROR_RANGE) \
ERROR(ERROR_NULL) \
ERROR(ERROR_EXTERNAL) \
ERROR(ERROR_NOT_INITIALISED) \
ERROR(ERROR_ALREADY_INITIALISED) \
ERROR(ERROR_I2C_NACK) \
ERROR(ERROR_I2C) \
ERROR(ERROR_I2C_CLK_TIMEOUT) \
ERROR(ERROR_INVALID_BSC) \
#define ERROR(x)
Redefining to replace the macro with x.
Definition: rpiGpio.h:52

The list of errors which may be returned from gpio functions.

Errors are defined within ERROR(x).

Definition at line 35 of file rpiGpio.h.

◆ REV1_PINS

#define REV1_PINS   {0, 1, 4, 7, 8, 9, 10, 11, 14, 15, 17, 18, 21, 22, 23, 24, 25}

@ brief List of all BCM2835 pins available through the rev1 Raspberry Pi header

Definition at line 129 of file rpiGpio.h.

◆ REV2_PINS

#define REV2_PINS   {2, 3, 4, 7, 8, 9, 10, 11, 14, 15, 17, 18, 22, 23, 24, 25, 27}

@ brief List of all BCM2835 pins available through the rev2 Raspberry Pi header

Definition at line 131 of file rpiGpio.h.

Enumeration Type Documentation

◆ eFunction

enum eFunction

The enum of pin functions available.

The enum values are equivalent to those in the data sheet.

Note
Currently only pins configured for input and output have been tested.
Enumerator
input 

Set pin to input

output 

Set pin to output

alt0 

Set pin to alternative function 0

alt1 

Set pin to alternative function 1

alt2 

Set pin to alternative function 2

alt3 

Set pin to alternative function 3

alt4 

Set pin to alternative function 4

alt5 

Set pin to alternative function 5

eFunctionMin 

Minimum valid value for enum

eFunctionMax 

Maximum valid value for enum

Definition at line 85 of file rpiGpio.h.

◆ eResistor

enum eResistor

The enum for possible pull resistors.

Enumerator
pullDisable 

No resistor

pulldown 

Pulldown resistor

pullup 

Pullup resistor

Definition at line 75 of file rpiGpio.h.

◆ eState

enum eState

The enum of possible pin states in input/output modes.

Enumerator
low 

Pin low

high 

Pin high

Definition at line 69 of file rpiGpio.h.

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.

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.

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.

◆ gpioI2cCleanup()

errStatus gpioI2cCleanup ( void  )

Disables the I2C controller and unmaps the memory used for the i2c functionality. This function should be called when finished with the I2C module.

Returns
An error from errStatus.

Definition at line 156 of file i2c.c.

◆ gpioI2cReadData()

errStatus gpioI2cReadData ( uint8_t *  buffer,
uint16_t  bytesToRead 
)

Read a number of bytes from I2C. The slave address should have been previously set with gpioI2cSet7BitSlave().

Parameters
[out]bufferA pointer to a user defined buffer which will store the bytes.
bytesToReadThe number of bytes to read.
Returns
An error from errStatus.

Definition at line 348 of file i2c.c.

◆ gpioI2cSet7BitSlave()

errStatus gpioI2cSet7BitSlave ( uint8_t  slaveAddress)

Sets the 7-bit slave address to communicate with.

This value can be set once and left if communicating with the same device.

Parameters
slaveAddress7-bit slave address.
Returns
An error from errStatus.

Definition at line 217 of file i2c.c.

◆ gpioI2cSetClock()

errStatus gpioI2cSetClock ( int  frequency)

Sets the I2C Clock Frequency.

Note
The desired frequency should be in the range: I2C_CLOCK_FREQ_MIN <= frequency <= I2C_CLOCK_FREQ_MAX.
Parameters
frequencyDesired frequency in Hertz.
Returns
An error from errStatus

Definition at line 465 of file i2c.c.

◆ gpioI2cSetup()

errStatus gpioI2cSetup ( void  )

Initial setup of I2C functionality.

gpioSetup() should be called prior to this.

Returns
An error from errStatus.

Definition at line 36 of file i2c.c.

◆ gpioI2cWriteData()

errStatus gpioI2cWriteData ( const uint8_t *  data,
uint16_t  dataLength 
)

Writes data to the address previously specified by gpioI2cSet7BitSlave().

Parameters
[in]dataPointer to the start of data to transmit.
dataLengthThe length of data.
Returns
errStatus An error from errStatus

Definition at line 243 of file i2c.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.

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.

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.

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.

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.

Definition at line 44 of file gpio.c.