How to receive an unknown quantity of I2C data as a slave on STM32F105?

Viewed 466

I'm using ST's HAL to receive I2C data as a slave on an STM32F105.

The data is read with the code:

// Called when an initial I2C address packet is found.
void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, uint16_t AddrMatchCode)
{
    // Check I2C communication is addressed to the slave.
    if(AddrMatchCode == I2C_USERSPACE_ADDR || AddrMatchCode == I2C_KERNELSPACE_ADDR) {
        i2c_last_activity = HAL_GetTick();
        switch(TransferDirection) {
            case I2C_DIRECTION_TRANSMIT:
                // Get all data written to slave.
                // If it is a write to register command from the Master this will grab all data that comes in
                // to be later polled when the slave_data_available flag is set.
                // If it is a read from register command from the Master then this will grab the register being
                // addressed by the Master.
                wr_AddrMatchCode = AddrMatchCode;
                if (HAL_I2C_Slave_Seq_Receive_IT(&hi2c1, rx_buffer, RX_BUFFER_SIZE, I2C_FIRST_FRAME) != HAL_OK) {
                    Error_Handler();
                }
            break;

(The Error_Handler() function above is not called. The error below is triggered by an signal generated by the I2C STOP.)

Provided that I send exactly RX_BUFFER_SIZE bytes, everything is fine.

If I send less, an error HAL_I2C_ERROR_AF (== 4) is generated at the point that the STM32 receives the I2C STOP.

HAL_I2C_ErrorCallback() at stm32f1xx_hal_i2c.c:4,997 0x800447e  
I2C_ITError() at stm32f1xx_hal_i2c.c:6,278 0x8005510    
I2C_Slave_STOPF() at stm32f1xx_hal_i2c.c:6,037 0x80052ba    
HAL_I2C_EV_IRQHandler() at stm32f1xx_hal_i2c.c:4,729 0x800439c  
I2C1_EV_IRQHandler() at stm32f1xx_it.c:261 0x8001e9a    
<signal handler called>() at 0xfffffff9 
HAL_GetTick() at stm32f1xx_hal.c:307 0x8002174  
HAL_Delay() at stm32f1xx_hal.c:371 0x80021ac    
main() at main.c:192 0x800162c  

Note: There is no non-HAL function in this stack trace, apart from main().

How can I receive an unknown amount of I2C data which is less than RX_BUFFER_SIZE?

0 Answers
Related