6.5.5. I2C

6.5.5.1. Functions

UINT8_t I2C_SW_init(UINT8_t busId)

Does the initialization of I2C interface. In current hardware version (WISENSE_HW_REV_6), port 3 is used for I2C purpose. This function is usually called during platform initialization.

Parameters:
  • busId – Identity of the bus, which should typically be PLFRM_I2C_SW_BUS_1_ID.
Returns:

If successful, 1 is returned. If any error occurs, 0 is returned.

UINT8_t I2C_SW_start(UINT8_t busId)

Any read or write on the I2C bus should commence with a start condition. Start condition is a high-to-low transition of SDA with SCL high.

Parameters:
  • busId – Identity of the bus, which should typically be PLFRM_I2C_SW_BUS_1_ID.
Returns:

If successful, 1 is returned. If any error occurs, 0 is returned.

UINT8_t I2C_SW_stop(UINT8_t busId)

Any read or write on the I2C bus should end with a stop condition. Stop condition is a low-to-high transition of SDA with SCL high.

Parameters:
  • busId – Identity of the bus, which should typically be PLFRM_I2C_SW_BUS_1_ID.
Returns:

If successful, 1 is returned. If any error occurs, 0 is returned.

UINT8_t I2C_SW_sendDevAddr_7(UINT8_t busId, UINT8_t devAddr, UINT8_t opn)

Each device connected to the I2C bus has a unique address. Hence, to perform any read or write operation the device has to be addressed. This is a 7-bit address. The eighth bit is set based on the requested operation: 0 for write, 1 for read.

Parameters:
  • busId – Identity of the bus, which should typically be PLFRM_I2C_SW_BUS_1_ID.
  • devAddr – This should contain the 7-bit address of the slave device.
  • opn – This can either be I2C_OPN_TYPE_READ or I2C_OPN_TYPE_WRITE.
Returns:

If successful, 1 is returned. If any error occurs, 0 is returned.

UINT8_t I2C_SW_wrByte(UINT8_t busId, UINT8_t data)

Writes a single byte to the I2C bus.

Parameters:
  • busId – Identity of the bus, which should typically be PLFRM_I2C_SW_BUS_1_ID.
  • data – The byte to be written.
Returns:

If successful, 1 is returned. If any error occurs, 0 is returned.

Example (EEPROM AT24C10C):

UINT8_t busId = PLFRM_I2C_SW_BUS_1_ID;
UINT8_t i2cAddr = AT24C10C_I2C_ADDR_PREFIX | PLTFRM_AT24C01C_1_I2C_ADDR;
UINT8_t offset = PLTFRM_AT24C01C_1_OFF_MAC_EXT_ADDR;

// Write a single byte of value 0xAA
// but need to address the device first
I2C_SW_start(busId);
I2C_SW_sendDevAddr_7(busId, i2cAddr, I2C_OPN_TYPE_WRITE);
I2C_SW_wrByte(busId, offset);
I2C_SW_wrByte(busId, 0xAA);
I2C_SW_stop(busId);
UINT8_t I2C_SW_rdByte(UINT8_t busId, UINT8_t *buff_p, UINT8_t ackCntrl)

Reads a single byte from the I2C bus.

Parameters:
  • busId – Identity of the bus, which should typically be PLFRM_I2C_SW_BUS_1_ID.
  • buff_p – Points to location where the byte read is to be stored.
  • ackCntrl – This can be set to I2C_ACK or I2C_NO_ACK.
Returns:

If successful, 1 is returned. If any error occurs, 0 is returned.

Example (EEPROM AT24C10C):

UINT8_t busId = PLFRM_I2C_SW_BUS_1_ID;
UINT8_t i2cAddr = AT24C10C_I2C_ADDR_PREFIX | PLTFRM_AT24C01C_1_I2C_ADDR;
UINT8_t offset = PLTFRM_AT24C01C_1_OFF_MAC_EXT_ADDR;
UINT8_t data[2]; // to store the data read

// Read two bytes but need to address the device first
// and write the location to read.
I2C_SW_start(busId);
I2C_SW_sendDevAddr_7(busId, i2cAddr, I2C_OPN_TYPE_WRITE);
I2C_SW_wrByte(busId, offset);
I2C_SW_start(busId);
I2C_SW_sendDevAddr_7(busId, i2cAddr, I2C_OPN_TYPE_READ);
I2C_SW_rdByte(busId, data, I2C_ACK);
I2C_SW_rdByte(busId, data+1, I2C_NO_ACK);
I2C_SW_stop(busId);

6.5.5.2. Macros

#define I2C_OPN_TYPE_READ   0x1
#define I2C_OPN_TYPE_WRITE  0x2

#define I2C_ACK      0x1
#define I2C_NO_ACK   0x2

6.5.5.3. Type Definitions

typedef struct
{
   volatile UINT8_t *dirReg_p;
   volatile UINT8_t *outReg_p;
   volatile UINT8_t *inReg_p;
   UINT8_t sclBitNr;
   UINT8_t sdaBitNr;
} I2C_SW_devInfo_s;