STM32F767ZI: Hard fault with FreeRTOS mutexes + DMA I2C
My setting is a Nucleo F767ZI board with a IKS01A2 shield. I created a FreeRTOS project that periodically queries the temperature sensor, calculates an average of temperature data, and prints the temperature on the UART. It has three threads that work as follows:
- The first thread performs a HAL_I2C_Mem_Read_DMA to read temperature from the sensor on the shield, stops on a FreeRTOS event (that is fired by the DMA completion callback), then acquires mutex A, puts the temperature data in a shared circular buffer, releases A, and then sleeps (period: 100 ms).
- The second thread acquires mutex A, calculates the average of the temperatures stored in the shared circular buffer, releases A, then acquires mutex B, puts the average in a shared variable, releases B, and then sleeps (period: 500 ms).
- The third thread acquires mutex B, reads the shared variable with the average temperature, releases B, prints on the UART and then sleeps (period: 1000 ms).
The application fails in few seconds with a hard fault. What I learned from some experiments is that the hard fault does not happen if I disable the DMA I2C read code. When this code is enabled, the only way to avoid the hard fault is disallowing the threads to share the mutexes, e,g, by disabling the second thread.
Am I failing something?
Best,
Pietro
