1.1.11.4 I2CSMBx_WriteRead Function

C

/* x = I2C SMBUS peripheral instance number */

bool I2CSMBx_WriteRead(uint16_t address, uint8_t* wdata, size_t wlength, uint8_t* rdata, size_t rlength)

Summary

Write and Read data from I2C Slave.

Description

This function writes data from the wdata buffer to the slave of the given address and then reads data from the slave and stores the received in the rdata. The function generates a Start condition on the bus and will then send wlength number of bytes contained in wdata buffer. The function will then insert a Repeated start condition and proceed to read rlength number of bytes from the slave. The received bytes are stored in rdata buffer. A Stop condition is generated after the last byte has been received. If the slave NAKs the request or a bus error was encountered on the bus, the transfer is terminated. The application can call I2CSMBx_ErrorGet function to know the cause of the error. The function is non-blocking. It initiates the bus activity and returns immediately. The transfer is then completed in the peripheral interrupt. A transfer request cannot be placed when another transfer is in progress. Calling this function when another function is already in progress will cause the function to return false. The library will call the registered callback function when the transfer has completed.

Precondition

I2CSMBx_Initialize must have been called for the associated I2C instance.

Parameters

Param Description
address 7-bit / 10-bit slave address.
wdata pointer to write data buffer
wlength write data length in bytes.
rdata pointer to read data buffer.
rlength read data length in bytes.

Returns

true - The request was placed successfully and the bus activity was initiated

false - The request fails, if there was already a transfer in progress when this function was called

Example

uint8_t myTxData [NUM_BYTES] = {'1', '0', ' ', 'B', 'Y', 'T', 'E', 'S'};
uint8_t myRxData [NUM_BYTES] = {0};
void MyI2CCallback(uintptr_t context)
{
    // This function will be called when the transfer completes. Note
    // that this function executes in the context of the I2C interrupt.
}

I2CSMB0_CallbackRegister(MyI2CCallback, NULL);

if(!I2CSMB0_WriteRead( SLAVE_ADDR, &myTxData[0], NUM_BYTES, myRxData, NUM_BYTES))
{
    // error handling
}

Remarks

Calling this function is not the same as calling the I2CSMBx_Write() function and then calling the I2CSMBx_Read() function. The I2CSMBx_WriteRead function will insert a Repeated Start condition between the Write and the Read stages. The I2CSMBx_Write() and the I2CSMBx_Read() function insert a stop condition after the write and the read has completed.