Raspberry Pi GPIO Library  0.3
Library allowing for control of the Raspberry Pi's GPIO pins.
rpiGpio.h
Go to the documentation of this file.
1 
23 #ifndef _RPI_GPIO_H_
24 #define _RPI_GPIO_H_
25 
26 #include "bcm2835_gpio.h"
27 #include <stdio.h>
28 #include <stdint.h>
29 
31 #define CORE_CLK_HZ 250000000
32 
35 #define ERRORS \
36  ERROR(OK) \
37  ERROR(ERROR_DEFAULT) \
38  ERROR(ERROR_INVALID_PIN_NUMBER) \
39  ERROR(ERROR_RANGE) \
40  ERROR(ERROR_NULL) \
41  ERROR(ERROR_EXTERNAL) \
42  ERROR(ERROR_NOT_INITIALISED) \
43  ERROR(ERROR_ALREADY_INITIALISED) \
44  ERROR(ERROR_I2C_NACK) \
45  ERROR(ERROR_I2C) \
46  ERROR(ERROR_I2C_CLK_TIMEOUT) \
47  ERROR(ERROR_INVALID_BSC) \
48 
49 
50 #undef ERROR
51 
52 #define ERROR(x) x,
53 
54 
56 #define I2C_CLOCK_FREQ_MIN 10000
57 
59 #define I2C_CLOCK_FREQ_MAX 400000
60 
63 typedef enum {
64  ERRORS
65  ERROR_MAX
66 } errStatus;
67 
69 typedef enum {
70  low = 0x0,
71  high = 0x1
72 } eState;
73 
75 typedef enum {
79 } eResistor;
80 
85 typedef enum {
96 } eFunction;
97 
98 /* Function Prototypes */
99 errStatus gpioSetup(void);
100 errStatus gpioCleanup(void);
101 errStatus gpioSetFunction(int gpioNumber, eFunction function);
102 errStatus gpioSetPin(int gpioNumber, eState state);
103 errStatus gpioReadPin(int gpioNumber, eState * state);
104 errStatus gpioSetPullResistor(int gpioNumber, eResistor resistor);
105 errStatus gpioGetI2cPins(int * gpioNumberScl, int * gpioNumberSda);
106 
107 errStatus gpioI2cSetup(void);
109 errStatus gpioI2cSetClock(int frequency);
110 errStatus gpioI2cSet7BitSlave(uint8_t slaveAddress);
111 errStatus gpioI2cWriteData(const uint8_t * data, uint16_t dataLength);
112 errStatus gpioI2cReadData(uint8_t * buffer, uint16_t bytesToRead);
113 
114 const char * gpioErrToString(errStatus error);
115 int dbgPrint(FILE * stream, const char * file, int line, const char * format, ...);
116 
118 #define DBG_INFO stderr,__FILE__,__LINE__
119 
120 
121 /* Revision specific TODO not sure if it should be public maybe private revisions
122  * header?*/
124 #define REV1_PINCNT 17
125 
126 #define REV2_PINCNT 17
127 
129 #define REV1_PINS {0, 1, 4, 7, 8, 9, 10, 11, 14, 15, 17, 18, 21, 22, 23, 24, 25}
130 
131 #define REV2_PINS {2, 3, 4, 7, 8, 9, 10, 11, 14, 15, 17, 18, 22, 23, 24, 25, 27}
132 
134 #define REV1_SDA 0
135 
136 #define REV1_SCL 1
137 
138 #define REV2_SDA 2
139 
140 #define REV2_SCL 3
141 
143 typedef enum {
144  pcbRevError = 0,
145  pcbRev1 = 1,
146  pcbRev2 = 2,
147 } tPcbRev;
148 
149 
150 #endif /* _RPI_GPIO_H_ */
#define GPFSEL_ALT5
Definition: bcm2835_gpio.h:128
#define GPFSEL_ALT0
Definition: bcm2835_gpio.h:123
errStatus gpioGetI2cPins(int *gpioNumberScl, int *gpioNumberSda)
Get the correct I2C pins.
Definition: gpio.c:356
errStatus gpioSetup(void)
Maps the memory used for GPIO access. This function must be called prior to any of the other GPIO cal...
Definition: gpio.c:44
errStatus
The enum of possible errors returned from gpio functions. Errors themselves are defined in the macro ...
Definition: rpiGpio.h:63
errStatus gpioI2cSetup(void)
Initial setup of I2C functionality.
Definition: i2c.c:36
errStatus gpioSetPullResistor(int gpioNumber, eResistor resistor)
Allows configuration of the internal resistor at a GPIO pin.
Definition: gpio.c:302
Definition: rpiGpio.h:90
errStatus gpioI2cReadData(uint8_t *buffer, uint16_t bytesToRead)
Read a number of bytes from I2C. The slave address should have been previously set with gpioI2cSet7Bi...
Definition: i2c.c:348
Definition: rpiGpio.h:87
#define GPPUD_DISABLE
Definition: bcm2835_gpio.h:132
errStatus gpioReadPin(int gpioNumber, eState *state)
Reads the current state of a gpio pin.
Definition: gpio.c:255
#define GPFSEL_ALT2
Definition: bcm2835_gpio.h:125
#define GPPUD_PULLUP
Definition: bcm2835_gpio.h:134
Definition: rpiGpio.h:70
eResistor
The enum for possible pull resistors.
Definition: rpiGpio.h:75
#define GPPUD_PULLDOWN
Definition: bcm2835_gpio.h:133
#define GPFSEL_OUTPUT
Definition: bcm2835_gpio.h:122
#define GPFSEL_INPUT
Definition: bcm2835_gpio.h:121
#define GPFSEL_ALT1
Definition: bcm2835_gpio.h:124
errStatus gpioI2cWriteData(const uint8_t *data, uint16_t dataLength)
Writes data to the address previously specified by gpioI2cSet7BitSlave().
Definition: i2c.c:243
errStatus gpioSetFunction(int gpioNumber, eFunction function)
Sets the functionality of the desired pin.
Definition: gpio.c:164
eState
The enum of possible pin states in input/output modes.
Definition: rpiGpio.h:69
errStatus gpioI2cSet7BitSlave(uint8_t slaveAddress)
Sets the 7-bit slave address to communicate with.
Definition: i2c.c:217
Definition: rpiGpio.h:93
Definition: rpiGpio.h:78
#define GPFSEL_ALT3
Definition: bcm2835_gpio.h:126
Definition: rpiGpio.h:92
tPcbRev
valid PCB revision values
Definition: rpiGpio.h:143
errStatus gpioCleanup(void)
Unmaps the memory used for the gpio pins. This function should be called when finished with the GPIO ...
Definition: gpio.c:134
Definition: rpiGpio.h:88
#define ERRORS
The list of errors which may be returned from gpio functions.
Definition: rpiGpio.h:35
Definition: rpiGpio.h:71
eFunction
The enum of pin functions available.
Definition: rpiGpio.h:85
Header file for BCM2835 GPIO registers.
errStatus gpioSetPin(int gpioNumber, eState state)
Sets a pin to high or low.
Definition: gpio.c:208
Definition: rpiGpio.h:86
errStatus gpioI2cCleanup(void)
Disables the I2C controller and unmaps the memory used for the i2c functionality. This function shoul...
Definition: i2c.c:156
const char * gpioErrToString(errStatus error)
Debug function which converts an error from errStatus to a string.
Definition: gpio.c:405
Definition: rpiGpio.h:89
int dbgPrint(FILE *stream, const char *file, int line, const char *format,...)
Debug function wrapper for fprintf().
Definition: gpio.c:439
#define GPFSEL_ALT4
Definition: bcm2835_gpio.h:127
errStatus gpioI2cSetClock(int frequency)
Sets the I2C Clock Frequency.
Definition: i2c.c:465
Definition: rpiGpio.h:91