KM3NeT CLB  2.0
KM3NeT CLB v2 Embedded Software
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
i2c.h File Reference

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.

Macros

#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"
 

Typedefs

typedef uint8_t i2cAddr
 I2C address type.
 

Functions

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)
 

Detailed Description

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.

// Example initialization
errPrint(true); // print error
return;
}
// Read from device.
int devId = 0x75;
uint8_t buf[4];
if (!i2cRead(I2C1, devId, buf, sizeof(buf))) {
errPrint(true);
return;
}

Definition in file i2c.h.

Macro Definition Documentation

#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.

Function Documentation

bool i2cExists ( I2C_Device dev,
i2cAddr  addr,
bool *  canRead,
bool *  canWrite 
)

Checks whether or not an I2C address is present on the bus.

Parameters
devI2C Device
addrThe address to check.
canReadReturns whether or not the device can be read true - The device can be read false - The device can not be read
canWriteReturns whether or not the device can be written true - The device can be written false - The device can not be written
Return values
trueThere was no error (no acks are not returned as errors by this function)
falseThere 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
devThe device to initialize.
bitrateThe bitrate to set, see I2C_BITRATE_* defines.

Definition at line 122 of file i2c.c.

bool i2cRead ( I2C_Device dev,
i2cAddr  addr,
uint8_t *  bytes,
int  len 
)

Reads from the I2C device.

Parameters
devThe WB device.
addrThe address of the I2C device
byteThe buffer to write into, must be atleast the size of the no of bytes to read.
lenThe no of bytes to read.
Return values
trueWrite was succesful
falseWrite 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
devThe WB device.
addrThe address of the I2C device
regThe register to read.
byteThe buffer to write into, must be atleast the size of the no of bytes to read.
lenThe no of bytes to read.
Return values
trueWrite was succesful
falseWrite 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
devThe WB device.
addrThe address of the I2C device
reg_addrThe register address to read
answerThe buffer to write into, must be at least the size of the no of byte of the answer
lenThe no of bytes to read in the answer
Return values
trueRead was succesful
falseRead 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
devThe WB device.
addrThe address of the I2C device
cmdThe command to send
cmd_lenThe no of bytes to send in the command
answerThe buffer to write into, must be at least the size of the no of byte of the answer
answer_lenThe no of bytes to read in the answer
Return values
trueWrite was succesful
falseWrite was unsuccessful, check errCode() for the exact error.

Definition at line 287 of file i2c.c.

bool i2cWrite ( I2C_Device dev,
i2cAddr  addr,
uint8_t *  bytes,
int  len 
)

Writes to the I2C device.

Parameters
devThe WB device.
addrThe address of the I2C device
byteThe bytes to send
lenThe no of bytes to send
Return values
trueWrite was succesful
falseWrite 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
devThe WB device.
addrThe address of the I2C device
wrThe bytes to send
wrLenThe no of bytes to send
rdThe buffer to write into, must be at least the size of the no of bytes to read.
rdLenThe no of bytes to read.
Return values
trueWrite was succesful
falseWrite 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
devThe WB device.
addrThe address of the I2C device
regThe register to write.
byteThe bytes to send
lenThe no of bytes to send
Return values
trueWrite was succesful
falseWrite was unsuccessful, check errCode() for the exact error.

Definition at line 247 of file i2c.c.