I am working with a KL17 CPU and I have a I2C question. The KL17 in my application is a slave I2C device that talks to another embedded CPU that is the I2C master. I am using the “Transcational” I2C API since I want to use non blocking transfers. I have the I2C working for a fixed message size of 2 bytes but my question is what is the recommended method of handling variable messages lengths? For example, if the master CPU sends or requests a 2 byte message for the KL17, the I2C bus would have the following (ignoring the start/stop and repeat start bits):
[slave address] [register] [slave address] [byte1] [byte 2]
My callback is shown below:
static void i2c_slave_callback(I2C_Type *base, i2c_slave_transfer_t *xfer, void *userData)
{
switch (xfer->event)
{
/* Transmit request */
case kI2C_SlaveTransmitEvent:
/* Update information for transmit process */
xfer->data = g_I2C_buff;
xfer->dataSize = 3;
break;
/* Receive request */
case kI2C_SlaveReceiveEvent:
/* Update information for received process */
xfer->data = g_I2C_buff;
xfer->dataSize = 3;
break;
/* Transfer done */
case kI2C_SlaveCompletionEvent:
g_SlaveCompletionFlag = true;
break;
default:
g_SlaveCompletionFlag = true;
break;
}
}
Since a write or read of two bytes would also needs to handle the [register] I found that xfer->dataSize = 3; gave the desired results. My question is what is the recommended method of sending or receiving different length messages based on the [register] value?
For example say register = 0x02 should be a two byte read and register = 0x03 is a one byte read. The Kl17 is at slave address 0x28.
[0x28] [0x02] [0x28] [byte1] [byte2] 0x02 is read 2 bytes
[0x28] [0x03] [0x28] [byte1] 0x01 is read 1 byte
I won’t know how to set the transfer up till I get the get the [register] value. What is the recommended method for doing this?
Thanks,
Doug