title: FreeRTOS: osDelay vs HAL_delay
body: <p>While creating <strong>FreeRTOS</strong> application project with <strong>STM32CubeMx</strong>, there are two ways you can use to introduce delay, namely <strong><em>osDelay</em></strong> and <strong><em>HAL_Delay</em></strong>.  </p>
  
  <p>What's the difference among them and which one should be preferred?</p>
  
  <p><strong>osDelay</strong> Code:</p>
  
  <pre><code>/*********************** Generic Wait Functions *******************************/
  /**
  * @brief   Wait for Timeout (Time Delay)
  * @param   millisec      time delay value
  * @retval  status code that indicates the execution status of the function.
  */
  osStatus osDelay (uint32_t millisec)
  {
  #if INCLUDE_vTaskDelay
    TickType_t ticks = millisec / portTICK_PERIOD_MS;
  
    vTaskDelay(ticks ? ticks : 1);          /* Minimum delay = 1 tick */
  
    return osOK;
  #else
    (void) millisec;
  
    return osErrorResource;
  #endif
  }
  </code></pre>
  
  <p><strong>HAL_Delay</strong> Code: </p>
  
  <pre><code>/**
  * @brief This function provides accurate delay (in milliseconds) based 
  *        on variable incremented.
  * @note In the default implementation , SysTick timer is the source of time base.
  *       It is used to generate interrupts at regular time intervals where uwTick
  *       is incremented.
  * @note ThiS function is declared as __weak to be overwritten in case of other
  *       implementations in user file.
  * @param Delay: specifies the delay time length, in milliseconds.
  * @retval None
  */
  __weak void HAL_Delay(__IO uint32_t Delay)
  {
    uint32_t tickstart = 0;
    tickstart = HAL_GetTick();
    while((HAL_GetTick() - tickstart) &lt; Delay)
    {
    }
  }
  </code></pre>