AnsweredAssumed Answered

When ST HAL will be fully compatible with RTOS???

Question asked by shlapunov.nicolai on Feb 22, 2018

I had some trouble with SPI before and now I have same kind of trouble with I2C. I use FreeRTOS and HAL drivers, all generated by CubeMX. Function and my fix in bold:

static HAL_StatusTypeDef I2C_WaitOnRXNEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart)
{
  while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == RESET)
  {
    /* Check if a NACK is detected */
    if(I2C_IsAcknowledgeFailed(hi2c, Timeout, Tickstart) != HAL_OK)
    {
      return HAL_ERROR;
    }

 

    /* Check if a STOPF is detected */
    if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET)
    {
       // But without any received byte. ST HAL doesn't expect
       // context switching. So, if context will be switched after
       // entering into while() cycle but before STOPF check happen
       // when we receiving last byte, STOPF will be set after this
       // byte received. When context will be switched back, this
       // code will found STOPF and return an error. For prevent that,
       // after STOPF condition detected we should check RXNE bit,
       // and if we have byte - this is not an error.
       if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == RESET)
       {
         /* Clear STOP Flag */
         __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);

 

         /* Clear Configuration Register 2 */
         I2C_RESET_CR2(hi2c);

 

         hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
         hi2c->State= HAL_I2C_STATE_READY;
         hi2c->Mode = HAL_I2C_MODE_NONE;

 

         /* Process Unlocked */
         __HAL_UNLOCK(hi2c);

 

         return HAL_ERROR;
       }
    }

So, I have same kind of issue second time! Your developers should pay more attention and start thinking "in multi-threading".

 

Thanks

Outcomes