Skip to main content
Visitor II
October 20, 2020
Question

STM32F0xx_HAL_I2c.c: I2C_RequestMemoryRead not interrupt based

  • October 20, 2020
  • 2 replies
  • 1361 views

Hello,

I am currently using a STM32F030CCT6 with the STM32F0xx_HAL_DRIVER generated by CubeMx. The version of the driver/cube is STM32Cube_FW_F0_V1.10.0.

In my program, I have a low priority task that calls HAL_I2C_Mem_Read_IT. This function is interrupt based and therefore I should have no problem completing the I2C request. However, once in a while I get a timeout. I investigated that timeout and found out that my task get switch during the call to HAL_I2C_Mem_Read_IT.

Going into HAL_I2C_Mem_Read_IT, I see that this function is called I2C_RequestMemoryRead. This function is responsible to send the device address and the memory address. When looking into the implementation I found that this function is not interrupt based!

I get a timeout when the task switch during I2C_RequestMemoryRead!

I was wondering if there was a particular reason why the function I2C_RequestMemoryRead is not interrupt based? Is this wanted? Is there a driver that implements I2C_RequestMemoryRead with interrupts?

Thank you!

    This topic has been closed for replies.

    2 replies

    Super User
    October 20, 2020

    HAL support for I2C in interrupt mode just isn't well implemented. Probably partly because the I2C peripheral and protocol is relatively complicated compared to UART/SPI.

    The F4 library seems to not have this drawback at a glance. You could try porting:

    https://github.com/STMicroelectronics/STM32CubeF4/blob/9f5ea7421d22c724516d00218b350d1e09a54a2c/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c.c#L2873

    In the CubeF4 repo, HAL_I2C_Mem_Read_DMA seems to rely on I2C_RequestMemoryRead, but HAL_I2C_Mem_Read_IT does not.

    No idea how common those two peripherals are.

    VLock.1Author
    Visitor II
    October 21, 2020

    Thank you for the quick answer!

    I added a critical section for the call to "HAL_I2C_Mem_Read_IT" to prevent task switching it did the job. Would of like a cleaner solution but I am not willing at this moment to modify the hal driver to have everything in interrupts.

    However, the point that you bring is a good one and anyone could try and implement the F4 driver into the F1. The driver is quite different. I can see the the "I2C_HandleTypeDef *hi2c" as new members suchs as: "Devaddress", "Memaddress","MemaddSize". But I am guessing that the architecture could be reproduce into the F0 and make " HAL_I2C_Mem_Read_IT" completely in interrupt.

    In the mean time, I also notice that the function "HAL_I2C_IsDeviceReady" is not interrupt based. It is not declared at so, but if you want to have everything in interrupt this function should probably also be modified.