cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 G4 HAL i2c not clearing interrupts

photon
Associate
**Problem:**

- When the I2C is enabled, the part can hit the WWDG.
- It looks like the pointer to the function `XferISR` is getting set to NULL somehow.
  - When this happens, the interrupts are not cleared, and it continually fires the interrupt because it is never cleared.
  - This is hard to troubleshoot because, in my system, there are many higher priority interrupts, making it hard to know that this is the problem.
- On another note, it looks like the interrupts are not always cleared in accordance with **RM0440 Table 394**:
  - In the error section, I2C_FLAG_PECERR, I2C_FLAG_TIMEOUT, and I2C_FLAG_ALERT are never cleared.
  - Also in I2C_Master_ISR_IT.

**Background:**

- Using an STM32G4 with HAL v1.2.3.
- Using DMA or IT (different builds).
- Connected to many I2C sensors.
- It is in a pretty noisy environment.
- All calls are from the main loop (except what was generated by HAL); all reading of buffers is in the main loop.
- If I have an I2C error, I re-init the I2C to clear out problems.

**Recommended Solutions:**

```c
void HAL_I2C_EV_IRQHandler(I2C_HandleTypeDef *hi2c) /* Derogation MISRAC2012-Rule-8.13 */
{
  /* Get current IT Flags and IT sources value */
  uint32_t itflags   = READ_REG(hi2c->Instance->ISR);
  uint32_t itsources = READ_REG(hi2c->Instance->CR1);

  /* I2C events treatment -------------------------------------*/
  if (hi2c->XferISR != NULL)
  {
    hi2c->XferISR(hi2c, itflags, itsources);
  }
  /* Add this */
  else
  {
    /* if you are very confident in the HAL */
    ASSERT();
    /* else */
    ClearAllI2cInterrupts(hi2c);
  }
  /* End Add */
}
```

- When there is a problem, you want to bring that to the attention of the developer to deal with it.
- I would think clearing all I2C interrupts would be more palatable.
- Also, where `XferISR = NULL`, you could set `XferISR = ClearAllI2cInterrupts;` this way, it better covers the bases.

**Questions:**

- Have you seen `XferISR` set to NULL while getting interrupts?
- Do you have any ideas why this might happen?

**Final Remarks:**

- I am going to try this out. Maybe I am smashing memory and setting `XferISR = NULL`. If after initialization it should never be NULL and then it got set to NULL, then likely I did something wrong...
0 REPLIES 0