NHS31xx SW API
i2cbbm: I2C bit bang master driver

Detailed Description

Introduction
The i2cbbm module is used to configure and operate a I2C master through bitbanging. For this 2 SWM pins are used for the clock CLK and data DAT lines.

The Module works as follows:

Diversity
This driver supports diversity settings to adapt to different layouts: the CLK and DAT pins can be assigned - I2CBBM_CLK_PIN resp. I2CBBM_DAT_PIN, and additional pins can be put to use as pull-up resistors - I2CBBM_PULLUPS. In addition, the clock stretching leniency can be adjusted - I2CBBM_MAX_CLK_STRETCH, and the clock pulse duration - I2CBBM_PULSE_WIDTH. Check Diversity Settings for all diversity parameters
Note
This mod will use at least 2 pins. The specified clock, data, and any pull-up pins may NOT be altered until the driver deinit is called.
Usage
To communicate with an I2C slave:
Example code
const uint8_t data[4] = {0x31, 0x34, 0x61, 0x95};
uint8_t slaveAddress = 0x7F; /* 7-bit I2C address. MSBit is disregarded. */
I2cbbm_SetAddress(slaveAddress);
int written = I2cbbm_Write(data, 4);

Modules

 Diversity Settings
 

Functions

void I2cbbm_Init (void)
 
void I2cbbm_DeInit (void)
 
int I2cbbm_Write (const uint8_t *pBuf, unsigned int size)
 
int I2cbbm_Read (uint8_t *pBuf, unsigned int size)
 
int I2cbbm_WriteRead (const uint8_t *pWriteBuf, unsigned int writeSize, uint8_t *pReadBuf, unsigned int readSize)
 
void I2cbbm_SetAddress (uint8_t address)
 

Function Documentation

◆ I2cbbm_Init()

void I2cbbm_Init ( void  )

This function must be the first function to call in this module after leaving deep power down or power-off power save mode, or after calling I2cbbm_DeInit. Initializes the I2C bit bang module and resets the default I2C slave address to I2CBBM_DEFAULT_I2C_ADDRESS. When finished, the module is initialized and the other API calls may be used.

Postcondition
The pins listed as diversity settings: I2CBBM_CLK_PIN, I2CBBM_DAT_PIN and I2CBBM_PULLUPS are now reserved for exclusive use by this module.

◆ I2cbbm_DeInit()

void I2cbbm_DeInit ( void  )

Deinitializes the I2C bit bang module.

Postcondition
The module is deinitialized and the I2CBBM_CLK_PIN and I2CBBM_DAT_PIN pins - not the pins listed in I2CBBM_PULLUPS - are restored to the state they were in when the module was initiated. All pins are now free to be used by the rest of the application.

◆ I2cbbm_Write()

int I2cbbm_Write ( const uint8_t *  pBuf,
unsigned int  size 
)

Transmits data to the I2C slave.

Parameters
pBuf: Pointer to the data to be placed on the I2C bus
size: Number of bytes to transmit
Returns
When successful, the number of bytes that have been sent are returned. When the transmission fails, -1 is returned.

◆ I2cbbm_Read()

int I2cbbm_Read ( uint8_t *  pBuf,
unsigned int  size 
)

Receives data from the I2C slave.

Parameters
pBuf: Pointer to the buffer to store the received data.
size: Number of bytes to receive
Returns
When successful, the number of bytes that have been read are returned. When the transmission fails, -1 is returned.

◆ I2cbbm_WriteRead()

int I2cbbm_WriteRead ( const uint8_t *  pWriteBuf,
unsigned int  writeSize,
uint8_t *  pReadBuf,
unsigned int  readSize 
)

Transmits to and immediately receives data from the I2C slave. This is done by implementing a repeated start condition, i.e. no stop condition is generated between the write and the subsequent read operation.

Parameters
pWriteBuf: Pointer to the data to be placed on the I2C bus.
writeSize: Number of bytes to receive, may be 0.
pReadBuf: Pointer to the buffer to store the received data.
readSize: Number of bytes to receive, may be 0.
Returns
The number of received bytes or -1 if an error occurred.

◆ I2cbbm_SetAddress()

void I2cbbm_SetAddress ( uint8_t  address)

Changes the I2C slave address to communicate to. This takes effect immediately.

Parameters
address: The 7-bit I2C address to read/write from/to. The lowest 7 bits are being used, the MSBit is disregarded.