Receiving arbitrary number of bytes as I2C slave

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Receiving arbitrary number of bytes as I2C slave

Jump to solution
749 Views
kevinlfw
Contributor III

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?

Labels (1)
Tags (3)
0 Kudos
1 Solution
526 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi, Kevin,

If you do not know the byte number the I2C slave device should be received, could you use only interrupt mode? once the I2C s slave receives a byte, an interrupt is triggered, you can read the char in ISR of slave I2C, this is the triditional method.

You can refer to the example  in SDK2.0:

C:\Freescale\SDK2.0_FRDM_K64F\boards\frdmk64f\driver_examples\i2c\interrupt

Hope it can help  you

BR

Xiangjun Rong

View solution in original post

2 Replies
527 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi, Kevin,

If you do not know the byte number the I2C slave device should be received, could you use only interrupt mode? once the I2C s slave receives a byte, an interrupt is triggered, you can read the char in ISR of slave I2C, this is the triditional method.

You can refer to the example  in SDK2.0:

C:\Freescale\SDK2.0_FRDM_K64F\boards\frdmk64f\driver_examples\i2c\interrupt

Hope it can help  you

BR

Xiangjun Rong

527 Views
kevinlfw
Contributor III

I ended up changing the protocol so that my device will know how many bytes it should receive.  The suggestion you made, and the change I made (providing the device with the number of bytes to receive), I'm assuming are the only possibilities available.  Thanks!

0 Kudos