1.3.2.2 Using The Library

The I2C bit bang library supports the following I2C transfers:

  • Master Write: The master writes a block of data to the slave

  • Master Read: The master reads a block of data from the slave

  • Master Write/Read: The master writes and then reads back a block of data from slave.

The I2C Bit bang library can be directly used by application or can be used with I2C driver to perform read and write operations

Example application to write using callback method

#define APP_SLAVE_ADDR 0x0057
#define NUM_BYTES      10

uint8_t myWriteData [NUM_BYTES] = {'1', '0', ' ', 'B', 'Y', 'T', 'E', 'S', '!', '!',};

void I2C_BB_Callback(uintptr_t context)
{
    if(I2C_BB_ErrorGet() == I2CBB_ERROR_NONE)
    {
        //Transfer is completed successfully
    }
    else
    {
        //Error occurred during transfer.
    }
}

int main(void)
{
    /* Register Callback function */
    I2C_BB_CallbackRegister(I2C_BB_Callback, (uintptr_t)NULL);

    /* Submit Write Request */
    I2C_BB_Write(APP_SLAVE_ADDR, &myWriteData[0], NUM_BYTES);

    /* Perform other tasks. The I2C_BB_Callback will be called when the write transfer is complete */
}