OpenCores I2C device driver.
More...
#include <stdint.h>
#include <stdbool.h>
#include <errorcode.h>
#include <kernel/err.h>
#include <lm32soc/dev_soc.h>
Go to the source code of this file.
|
#define | I2C_BITRATE_SLOW 10000 |
| I2C normal speed.
|
|
#define | I2C_BITRATE_NORMAL 100000 |
| I2C normal speed.
|
|
#define | I2C_BITRATE_FAST 400000 |
| I2C fast speed.
|
|
#define | I2C_BITRATE_HIGHSPEED 3500000 |
| I2C high speed.
|
|
#define | E_I2C_INIT ( E_I2C + 1 ) |
| I2C WB device failed to initialize properly.
|
|
#define | E_I2C_INIT_DESCR "Initialization error" |
|
#define | E_I2C_ARB_LOST ( E_I2C + 2 ) |
| Arbitration on the I2C bus lost.
|
|
#define | E_I2C_ARB_LOST_DESCR "Arbitration Lost" |
|
#define | E_I2C_NO_ACK ( E_I2C + 3 ) |
| I2C device did not acknowledge TX.
|
|
#define | E_I2C_NO_ACK_DESCR "No acknowledge" |
|
#define | E_I2C_TIME_OUT ( E_I2C + 4 ) |
| I2C device timed out. More...
|
|
#define | E_I2C_TIME_OUT_DESCR "Transmission time out" |
|
#define | E_I2C_IC_PROTO ( E_I2C + 10 ) |
| Protocol error. Driver error if I2C device does not respond as expected.
|
|
#define | E_I2C_IC_PROTO_DESCR "IC did not respond corrctly" |
|
|
typedef uint8_t | i2cAddr |
| I2C address type.
|
|
|
bool | i2cInit (I2C_Device *dev, uint32_t bitrate) |
| Initializes the I2C device with the specified bitrate. More...
|
|
bool | i2cWrite (I2C_Device *dev, i2cAddr addr, uint8_t *bytes, int len) |
| Writes to the I2C device. More...
|
|
bool | i2cRead (I2C_Device *dev, i2cAddr addr, uint8_t *bytes, int len) |
| Reads from the I2C device. More...
|
|
bool | i2cWriteRead (I2C_Device *dev, i2cAddr addr, uint8_t *wr, int wrLen, uint8_t *rd, int rdLen) |
| Writes and Reads To and from the I2C device. More...
|
|
bool | i2cReadReg (I2C_Device *dev, i2cAddr addr, uint8_t regNo, uint8_t *rd, int rdLen) |
| Reads from the I2C device register. More...
|
|
bool | i2cWriteReg (I2C_Device *dev, i2cAddr addr, uint8_t regNo, uint8_t *wr, int wrLen) |
| Writes to the I2C device register. More...
|
|
bool | i2cSendCmdAlt (I2C_Device *dev, i2cAddr addr, uint8_t *cmd, int cmd_len, uint8_t *answer, int answer_len) |
| Writes a command to the I2C device and receive the answer. More...
|
|
bool | i2cReadRegAlt (I2C_Device *dev, i2cAddr addr, uint8_t reg_addr, uint8_t *answer, int len) |
| Reads a register from a I2C device. More...
|
|
bool | i2cExists (I2C_Device *dev, i2cAddr addr, bool *canRead, bool *canWrite) |
| Checks whether or not an I2C address is present on the bus. More...
|
|
void | i2cDebug (bool enable) |
|
OpenCores I2C device driver.
Because there are multiple I2C devices in the memory, you will need to provide the device pointer for each one.
- See Also
- dev_soc.h
Sample code for fictive device providing 4 bytes of data.
return;
}
int devId = 0x75;
uint8_t buf[4];
return;
}
Definition in file i2c.h.
#define E_I2C_TIME_OUT ( E_I2C + 4 ) |
I2C device timed out.
(for example: clock stretching indefinitely or pull up/down issue)
Definition at line 74 of file i2c.h.
Checks whether or not an I2C address is present on the bus.
- Parameters
-
dev | I2C Device |
addr | The address to check. |
canRead | Returns whether or not the device can be read true - The device can be read false - The device can not be read |
canWrite | Returns whether or not the device can be written true - The device can be written false - The device can not be written |
- Return values
-
true | There was no error (no acks are not returned as errors by this function) |
false | There was an error. Check error module for exact error. |
Definition at line 326 of file i2c.c.
bool i2cInit |
( |
I2C_Device * |
dev, |
|
|
uint32_t |
bitrate |
|
) |
| |
Initializes the I2C device with the specified bitrate.
- Parameters
-
dev | The device to initialize. |
bitrate | The bitrate to set, see I2C_BITRATE_* defines. |
Definition at line 122 of file i2c.c.
Reads from the I2C device.
- Parameters
-
dev | The WB device. |
addr | The address of the I2C device |
byte | The buffer to write into, must be atleast the size of the no of bytes to read. |
len | The no of bytes to read. |
- Return values
-
true | Write was succesful |
false | Write was unsuccessful, check errCode() for the exact error. |
Definition at line 161 of file i2c.c.
bool i2cReadReg |
( |
I2C_Device * |
dev, |
|
|
i2cAddr |
addr, |
|
|
uint8_t |
regNo, |
|
|
uint8_t * |
rd, |
|
|
int |
rdLen |
|
) |
| |
Reads from the I2C device register.
- Parameters
-
dev | The WB device. |
addr | The address of the I2C device |
reg | The register to read. |
byte | The buffer to write into, must be atleast the size of the no of bytes to read. |
len | The no of bytes to read. |
- Return values
-
true | Write was succesful |
false | Write was unsuccessful, check errCode() for the exact error. |
Definition at line 188 of file i2c.c.
bool i2cReadRegAlt |
( |
I2C_Device * |
dev, |
|
|
i2cAddr |
addr, |
|
|
uint8_t |
reg_addr, |
|
|
uint8_t * |
answer, |
|
|
int |
len |
|
) |
| |
Reads a register from a I2C device.
!! Alternative protocol mode. Not standard I2C !!
- Parameters
-
dev | The WB device. |
addr | The address of the I2C device |
reg_addr | The register address to read |
answer | The buffer to write into, must be at least the size of the no of byte of the answer |
len | The no of bytes to read in the answer |
- Return values
-
true | Read was succesful |
false | Read was unsuccessful, check errCode() for the exact error. |
Definition at line 356 of file i2c.c.
bool i2cSendCmdAlt |
( |
I2C_Device * |
dev, |
|
|
i2cAddr |
addr, |
|
|
uint8_t * |
cmd, |
|
|
int |
cmd_len, |
|
|
uint8_t * |
answer, |
|
|
int |
answer_len |
|
) |
| |
Writes a command to the I2C device and receive the answer.
!! alternative protocol mode. Not standard I2C !!
- Parameters
-
dev | The WB device. |
addr | The address of the I2C device |
cmd | The command to send |
cmd_len | The no of bytes to send in the command |
answer | The buffer to write into, must be at least the size of the no of byte of the answer |
answer_len | The no of bytes to read in the answer |
- Return values
-
true | Write was succesful |
false | Write was unsuccessful, check errCode() for the exact error. |
Definition at line 287 of file i2c.c.
Writes to the I2C device.
- Parameters
-
dev | The WB device. |
addr | The address of the I2C device |
byte | The bytes to send |
len | The no of bytes to send |
- Return values
-
true | Write was succesful |
false | Write was unsuccessful, check errCode() for the exact error. |
Definition at line 219 of file i2c.c.
bool i2cWriteRead |
( |
I2C_Device * |
dev, |
|
|
i2cAddr |
addr, |
|
|
uint8_t * |
wr, |
|
|
int |
wrLen, |
|
|
uint8_t * |
rd, |
|
|
int |
rdLen |
|
) |
| |
Writes and Reads To and from the I2C device.
- Parameters
-
dev | The WB device. |
addr | The address of the I2C device |
wr | The bytes to send |
wrLen | The no of bytes to send |
rd | The buffer to write into, must be at least the size of the no of bytes to read. |
rdLen | The no of bytes to read. |
- Return values
-
true | Write was succesful |
false | Write was unsuccessful, check errCode() for the exact error. |
Definition at line 280 of file i2c.c.
bool i2cWriteReg |
( |
I2C_Device * |
dev, |
|
|
i2cAddr |
addr, |
|
|
uint8_t |
regNo, |
|
|
uint8_t * |
wr, |
|
|
int |
wrLen |
|
) |
| |
Writes to the I2C device register.
- Parameters
-
dev | The WB device. |
addr | The address of the I2C device |
reg | The register to write. |
byte | The bytes to send |
len | The no of bytes to send |
- Return values
-
true | Write was succesful |
false | Write was unsuccessful, check errCode() for the exact error. |
Definition at line 247 of file i2c.c.