1.11.12.4 1.12.12.4 1.13.11.4 1.15.11.4 1.16.9.4 1.17.9.4 1.18.10.4 1.19.9.4 1.20.9.4 1.21.10.4 1.22.14.4 1.23.13.4 1.24.11.4 I2Cx_Write Function

C

/* x = I2C instance number */

/* I2C master mode */
bool I2Cx_Write(uint16_t address, uint8_t *pdata, size_t length)

Summary

Writes data to the slave.

Description

This function writes data to a slave on the bus. The function will attempt to write length number of bytes from pdata buffer to a slave whose address is specified by address. The I2C Master will generate a Start condition, write the data and then generate a Stop Condition. If the slave NAKs the request or a bus error was encountered on the bus, the transfer is terminated. The application can call the I2Cx_ErrorGet function to know the cause of the error. The function is non-blocking. It initiates 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 the write function when another function is already in progress will cause the function to return false. If a callback was registered, the library will call the registered callback function when the transfer has completed.

Precondition

I2Cx_Initialize must have been called for the associated I2C instance.

Parameters

Param Description
address 7-bit / 10-bit slave address.
data pointer to source data buffer that contains the data to betransmitted.
length length of data buffer in number of bytes. Also the number of bytesto be written.

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 myData [NUM_BYTES];
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.
}

I2C1_Initialize();
I2C1_CallbackRegister(MyI2CCallback, NULL);

if(!I2C1_Write( SLAVE_ADDR, &myData[0], NUM_BYTES ))
{
    // error handling
}

Remarks

None.