27 static volatile uint32_t *
gI2cMap = NULL;
60 rtn = ERROR_INVALID_PIN_NUMBER;
71 rtn = ERROR_ALREADY_INITIALISED;
74 else if ((mem_fd = open(
"/dev/mem", O_RDWR)) < 0)
81 else if ((
gI2cMap = (
volatile uint32_t *)mmap(NULL,
86 bscBase)) == MAP_FAILED)
93 else if (close(mem_fd) != OK)
164 dbgPrint(
DBG_INFO,
"gI2cMap was NULL. Ensure gpioI2cSetup() was called successfully.");
165 rtn = ERROR_NOT_INITIALISED;
197 rtn = ERROR_EXTERNAL;
223 dbgPrint(
DBG_INFO,
"gI2cMap was NULL. Ensure gpioI2cSetup() was called successfully.");
224 rtn = ERROR_NOT_INITIALISED;
229 I2C_A = slaveAddress;
246 uint16_t dataIndex = 0;
247 uint16_t dataRemaining = dataLength;
248 struct timespec sleepTime;
252 dbgPrint(
DBG_INFO,
"gI2cMap was NULL. Ensure gpioI2cSetup() was called successfully.");
253 rtn = ERROR_NOT_INITIALISED;
256 else if (data == NULL)
264 sleepTime.tv_sec = 0;
302 nanosleep(&sleepTime, NULL);
310 rtn = ERROR_I2C_NACK;
318 rtn = ERROR_I2C_CLK_TIMEOUT;
321 else if (dataRemaining)
323 dbgPrint(
DBG_INFO,
"BSC signaled done but %d data remained.", dataRemaining);
351 uint16_t bufferIndex = 0;
352 uint16_t dataRemaining = bytesToRead;
353 struct timespec sleepTime;
357 dbgPrint(
DBG_INFO,
"gI2cMap was NULL. Ensure gpioI2cSetup() was called successfully.");
358 rtn = ERROR_NOT_INITIALISED;
361 else if (buffer == NULL)
369 sleepTime.tv_sec = 0;
412 nanosleep(&sleepTime, NULL);
428 rtn = ERROR_I2C_NACK;
436 rtn = ERROR_I2C_CLK_TIMEOUT;
439 else if (dataRemaining)
#define CORE_CLK_HZ
Speed of the core clock core_clk.
static int i2cByteTxTime_ns
The time it takes ideally transmit 1 byte with current I2C clock.
#define I2C_DLEN
BSC_DLEN register.
#define REV2_SCL
The BCM2835 pin number of SCL on rev2 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 ...
#define I2C_MAP_SIZE
The size the I2C mapping is required to be.
static volatile uint32_t * gI2cMap
Pointer which will be mmap'd to the I2C memory in /dev/mem.
#define I2C_A
BSC_A register.
errStatus gpioI2cSet7BitSlave(uint8_t slaveAddress)
Sets the 7-bit slave address to communicate with.
#define I2C_S
BSC_S register.
#define I2C_CLOCK_FREQ_MAX
Maximum I2C frequency (Hertz)
#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 NSEC_IN_SEC
nano seconds in a second
#define I2C_DIV
BSC_DIV register.
#define I2C_C
BSC_C register.
errStatus gpioI2cCleanup(void)
Disables the I2C controller and unmaps the memory used for the i2c functionality. This function shoul...
errStatus gpioI2cSetClock(int frequency)
Sets the I2C Clock Frequency.
#define I2C_FIFO
BSC_FIFO register.
errStatus gpioI2cSetup(void)
Initial setup of I2C functionality.
errStatus gpioSetPullResistor(int gpioNumber, eResistor resistorOption)
Allows configuration of the internal resistor at a GPIO pin.
#define CLOCKS_PER_BYTE
Clock pulses per I2C byte - 8 bits + ACK.
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.
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...
errStatus gpioI2cWriteData(const uint8_t *data, uint16_t dataLength)
Writes data to the address previously specified by gpioI2cSet7BitSlave().
errStatus gpioGetI2cPins(int *gpioNumberScl, int *gpioNumberSda)
Get the correct I2C pins.
#define I2C_DEFAULT_FREQ_HZ
Default I2C clock frequency (Hertz)
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.
Contains defines for i2c.c.