AnsweredAssumed Answered

Receiving arbitrary number of bytes as I2C slave

Question asked by Kevin Luty on Dec 15, 2016
Latest reply on Dec 22, 2016 by Kevin Luty

I'm implementing a "passthrough" functionality with the K22 acting as a slave.  Essentially I take some USB data in, strip the protocol extras, and pass the payload over to the I2C line.  In return, the Master will send data back to the K22 slave, and that will be packetized, and send back over USB.

 

My question is, since I don't necessarily know how many bytes the master is going to send back to me, what is the best method to receive an arbitrary number of bytes from a master? 

 

I do know the maximum amount of bytes the master can send to me.  So would the best method be something like this:

 

uint32_t currTimeMs;
uint32_t remainingRx;
uint32_t numUsbTx;
uint32_t timeout = OSA_TimeGetMsec() + I2C_RX_TX_TIMEOUT;

i2cStatus = I2C_DRV_SlaveReceiveData(I2C_RTOS_SLAVE_INSTANCE, usbTxBuff, MAX_PAYLOAD_SIZE);
do
{
  i2cStatus = I2C_DRV_SlaveGetTransmitStatus(I2C_RTOS_SLAVE_INSTANCE, &remainingRx);
  currTimeMs = OSA_TimeGetMsec();
}
while(i2cStatus != kStatus_I2C_Success && timeout > currTimeMs);

numUsbTx = MAX_PAYLOAD_SIZE - remainingRx;
Usb_Send(usbTxBuff, numUsbTx);

 

 

Essentially, wait for a timeout and and send only the number of bytes that were received.  Are there any other ways?  Would this be easier with the ReceiveDataBlocking call?

Outcomes