cancel
Showing results for 
Search instead for 
Did you mean: 

HAL_I2C_Slave_Receive_IT() with varying length

craig239955_stm1_st
Associate II
Posted on April 26, 2016 at 17:00

I've got an STM32F0 based I2C slave device which I'd like to send simple commands to, as well as updates containing additional bytes of data. 

HAL_I2C_Slave_Receive_IT() seems to require you to directly set the number of bytes you are waiting for however, so all received data must be the same length. Is there any way around this using the HAL system?

#!i2c #i2c-slave #no-hablo-hal #!stm32
12 REPLIES 12
ayarema
Associate II
Posted on August 30, 2017 at 20:38

Not sure if you still need an answer but why not do 2 transfers? First transfer is always say 2 bytes and that will be the size. Second transfer will be right after with the real data and now the size from transfer 1.

Posted on August 31, 2017 at 02:20

Since the two busses are intended to be separate by means of the two distinct interfaces, you either make them transparent (by having the local I2C controller duplicate the function on the slave bus, or by taking a bus transceiver/switch and making the local bus electrically equivalent to the incoming bus.

The bus switch has the ability to buffer, so that the loading of the slave devices will not load the system bus.  You'd then have to have a command to the local processor to switch from remote to local control of that bus. 

If you're willing to encapsulate a command to the slave devices on the board, then you can do this with one interface and a well defined packet structure. 

With that kind of structure, you have one packet format for local devices, the content is the unique command to that device, data is encapsulated and sent off board, and your board doesn't care who addressed what, because it's handling all the communications on and off board.

That might be an acceptable solution.  You'll need a handler for each device type, but you'd need that anyway.

Sandro G
Associate II
Posted on May 09, 2018 at 11:20

Use this:

void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c){

    // not enough bytes received causes an error, process the data anyway

    HAL_I2C_SlaveRxCpltCallback( hi2c );

}

I don't know how to find out how much data was actually received though. I use the first byte as a length indicator, so I don't need to know. Also, I guess this Error Callback could be triggered by other errors, so you probably should find out if the 'Error' is a stop condition.