49 if ((mem_fd = open(
"/dev/mem", O_RDWR)) < 0)
55 else if ((
gGpioMap = (
volatile uint32_t *)mmap(NULL,
67 else if (close(mem_fd) != OK)
75 FILE* cpuinfo = fopen(
"/proc/cpuinfo",
"r");
82 while (((linelen = getline(&line, &foo, cpuinfo)) >= 0))
84 if (strstr(line,
"Revision") == line)
86 char* rev = strstr(line,
":");
89 long revision = strtol(rev + 1, NULL, 16);
103 if (
pcbRev != pcbRevError)
110 rtn = ERROR_EXTERNAL;
121 dbgPrint(
DBG_INFO,
"can't open /proc/cpuinfo. errno: %s.", strerror(errno));
122 rtn = ERROR_EXTERNAL;
140 dbgPrint(
DBG_INFO,
"gGpioMap was NULL. Ensure gpioSetup() was called successfully.");
147 rtn = ERROR_EXTERNAL;
170 dbgPrint(
DBG_INFO,
"gGpioMap was NULL. Ensure gpioSetup() called successfully.");
174 else if (function < eFunctionMin || function >
eFunctionMax)
182 dbgPrint(
DBG_INFO,
"gpioValidatePin() failed. Ensure pin %d is valid.", gpioNumber);
192 *(
gGpioMap + (gpioNumber / 10)) |= (
function << ((gpioNumber % 10) * 3));
214 dbgPrint(
DBG_INFO,
"gGpioMap was NULL. Ensure gpioSetup() was called successfully.");
220 dbgPrint(
DBG_INFO,
"gpioValidatePin() failed. Ensure pin %d is valid.", gpioNumber);
223 else if (state ==
high)
231 else if (state ==
low)
261 dbgPrint(
DBG_INFO,
"gGpioMap was NULL. Ensure gpioSetup() was called successfully.");
265 else if (state == NULL)
273 dbgPrint(
DBG_INFO,
"gpioValidatePin() failed. Pin %d isn't valid.", gpioNumber);
305 struct timespec sleepTime;
309 dbgPrint(
DBG_INFO,
"gGpioMap was NULL. Ensure gpioSetup() was called successfully.");
315 dbgPrint(
DBG_INFO,
"gpioValidatePin() failed. Pin %d isn't valid.", gpioNumber);
318 else if (resistorOption < pullDisable || resistorOption >
pullup)
320 dbgPrint(
DBG_INFO,
"resistorOption value: %d was out of range.", resistorOption);
326 sleepTime.tv_sec = 0;
332 nanosleep(&sleepTime, NULL);
336 nanosleep(&sleepTime, NULL);
362 dbgPrint(
DBG_INFO,
"gGpioMap was NULL. Ensure gpioSetup() was called successfully.");
366 else if (gpioNumberScl == NULL)
372 else if (gpioNumberSda == NULL)
378 else if (
pcbRev == pcbRev1)
385 else if (
pcbRev == pcbRev2)
407 static const char * errorString[] = {
ERRORS };
409 if (error < 0 || error >= ERROR_MAX)
411 return "InvalidError";
416 return errorString[error];
439 int dbgPrint(FILE * stream,
const char * file,
int line,
const char * format, ...)
447 if ((tempRtn = fprintf(stream,
"[%s:%d] ", file, line)) < 0)
453 va_start(arguments, format);
454 if ((tempRtn = vfprintf(stream, format, arguments)) < 0)
461 if ((tempRtn = fprintf(stream,
"\n")) < 0)
482 errStatus rtn = ERROR_INVALID_PIN_NUMBER;
488 static uint32_t pinCnt = 0;
492 if (
pcbRev == pcbRevError)
497 else if (
pcbRev == pcbRev1)
500 memcpy(validPins, validPinsForRev1,
sizeof(validPinsForRev1));
503 else if (
pcbRev == pcbRev2)
506 memcpy(validPins, validPinsForRev2,
sizeof(validPinsForRev2));
511 for (index = 0; index < pinCnt; index++)
513 if (gpioNumber == validPins[index])
errStatus gpioReadPin(int gpioNumber, eState *state)
Reads the current state of a gpio pin.
#define REV1_PINCNT
Pin count on a PCB rev1 Raspberry Pi.
errStatus gpioSetup(void)
Maps the memory used for GPIO access. This function must be called prior to any of the other GPIO cal...
errStatus gpioSetPin(int gpioNumber, eState state)
Sets a pin to high or low.
#define REV2_SCL
The BCM2835 pin number of SCL on rev2 Raspberry Pi.
static errStatus gpioValidatePin(int gpioNumber)
Internal function which Validates that the pin gpioNumber is valid for the Raspberry Pi...
#define REV2_SDA
The BCM2835 pin number of SDA on rev2 Raspberry Pi.
errStatus
The enum of possible errors returned from gpio functions. Errors themselves are defined in the macro ...
Contains defines for gpio.c.
eResistor
The enum for possible pull resistors.
#define REV2_PINCNT
Pin count on a PCB rev2 Raspberry Pi.
eState
The enum of possible pin states in input/output modes.
#define RESISTOR_SLEEP_US
#define REV1_SDA
The BCM2835 pin number of SDA on rev1 Raspberry Pi.
const char * gpioErrToString(errStatus error)
Debug function which converts an error from errStatus to a string.
#define GPIO_GPLEV0
GPIO_GPLEV0 register.
#define GPIO_GPCLR0
GPIO_GPCLR0 register.
tPcbRev
valid PCB revision values
#define ERRORS
The list of errors which may be returned from gpio functions.
static volatile uint32_t * gGpioMap
Pointer which will be mmap'd to the GPIO memory in /dev/mem.
errStatus gpioCleanup(void)
Unmaps the memory used for the gpio pins. This function should be called when finished with the GPIO ...
errStatus gpioSetPullResistor(int gpioNumber, eResistor resistorOption)
Allows configuration of the internal resistor at a GPIO pin.
int dbgPrint(FILE *stream, const char *file, int line, const char *format,...)
Debug function wrapper for fprintf().
#define DBG_INFO
Macro which covers the first three arguments of dbgPrint.
eFunction
The enum of pin functions available.
#define GPIO_GPPUDCLK0
GPIO_GPPUDCLK0 register.
errStatus gpioGetI2cPins(int *gpioNumberScl, int *gpioNumberSda)
Get the correct I2C pins.
#define GPIO_GPSET0
GPSET_0 register.
errStatus gpioSetFunction(int gpioNumber, eFunction function)
Sets the functionality of the desired pin.
#define REV1_SCL
The BCM2835 pin number of SCL on rev1 Raspberry Pi.
static tPcbRev pcbRev
PCB revision that executable is being run on.
#define GPIO_GPPUD
GPIO_GPPUD register.