2024-11-19 11:30 AM - edited 2024-11-20 08:53 AM
2025-01-07 06:12 AM
Hello @photon
These flags "I2C_FLAG_PECERR, I2C_FLAG_TIMEOUT, and I2C_FLAG_ALERT" cannot be cleared by software. They are cleared by hardware when disabling the peripheral.
2025-01-07 01:20 PM - edited 2025-01-07 01:23 PM
#define NVIC_PRI_WWDG (1UL)
#define NVIC_PRI_PWR (1UL)
...
#define NVIC_PRI_SYS_TICK (3UL)
#define NVIC_PRI_EXTI (4UL)
#define NVIC_PRI_DMA_I2C_RX (4UL)
#define NVIC_PRI_DMA_I2C_TX (4UL)
#define NVIC_PRI_DMA_I2C_RXERR (4UL)
#define NVIC_PRI_I2C2_EV_IRQn (11UL)
#define NVIC_PRI_I2C2_ER_IRQn (11UL)
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);
}
}
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);
HAL_StatusTypeDef(*pFoo)(struct __I2C_HandleTypeDef *hi2c, uint32_t ITFlags, uint32_t ITSources);
pFoo = hi2c->XferISR;
/* I2C events treatment -------------------------------------*/
if (NULL != pFoo)
{
pFoo(hi2c, itflags & I2C_EV_MASK_IT, itsources);
}
}
#define I2C_EV_MASK_IT ( I2C_FLAG_TXE | I2C_FLAG_TXIS | I2C_FLAG_RXNE | I2C_FLAG_ADDR \
| I2C_FLAG_AF | I2C_FLAG_STOPF | I2C_FLAG_TC | I2C_FLAG_TCR )
#define I2C_ER_MASK_IT ( I2C_FLAG_BERR | I2C_FLAG_ARLO | I2C_FLAG_OVR \
| I2C_FLAG_PECERR | I2C_FLAG_TIMEOUT | I2C_FLAG_ALERT )
HAL_StatusTypeDef I2C_Null_IT(struct __I2C_HandleTypeDef *hi2c, uint32_t ITFlags,
uint32_t ITSources)
{
__HAL_LOCK(hi2c);
/* Disable interupts */
__HAL_I2C_DISABLE_IT(hi2c, ITFlags & (I2C_EV_MASK_IT | I2C_ER_MASK_IT ));
__HAL_I2C_CLEAR_FLAG(hi2c, ITFlags & (I2C_EV_MASK_IT | I2C_ER_MASK_IT ));
__HAL_UNLOCK(hi2c);
return (HAL_OK);
}
2025-01-09 06:37 AM
Hello @photon
For the moment, it is up to the user to manage the race condition on HAL access. This can be done using a software semaphore. By implementing a software semaphore, you can control access to the HAL, ensuring that only one process or thread can access it at a time, thus preventing race conditions.