Skip to main content
Mikk Leini
Senior
April 20, 2026
Question

HAL cooperative mode feature request

  • April 20, 2026
  • 1 reply
  • 135 views

This is an improvement request post.

STM32 HAL on every family has tons of following code constructs:

tickstart = HAL_GetTick();
while (!condition) 
{
 if ((HAL_GetTick() - tickstart) > TIMEOUT_VALUE)
 {
 return HAL_TIMEOUT;
 }
}

When using HAL with (Free)RTOS or interrupt based scheduler, then these while loops delay same priority tasks (until time-slicing kicks in), block everything that has lower preemption priority, waste energy (crucial for battery powered devices) and decrease responsiveness.

There are multiple ways to improve it. One of the simplest is to supplement non-critical* loops with optional user-definable yield macros.

__weak void HAL_Yield(void)
{
}

tickstart = HAL_GetTick();
while (!condition) 
{
 if ((HAL_GetTick() - tickstart) > TIMEOUT_VALUE)
 {
 return HAL_TIMEOUT;
 }
 HAL_Yield();
}

Now user can implement something like this when using CMSIS OS v2 API:

#include "cmsis_os2.h"

void HAL_Yield(void)
{
 /* Kernel running and call not from ISR? */
 if ((osKernelGetState() == osKernelRunning) && (__get_IPSR() == 0U))
 {
 /* Pass execution to same or higher priority tasks: */
 osThreadYield();
 /* OR */
 /* Pass execution to any pending task (and save power): */
 osDelay(1);
 }
 else
 {
 /* RTOS not yet/anymore running or HAL called from ISR.
 * Do not pass execution to anything else. */
 }
}
Further improved solution would give estimated waiting time argument to yield function so it can plan its return better.
 
* critical loops are the clock stabilization waiting loops and maybe something else like that.

1 reply

Technical Moderator
April 21, 2026

Hello @Mikk Leini 

Thank you for bringing this issue to our attention.

I reported this internally.

Internal ticket number: CDM0061982 (This is an internal tracking number and is not accessible or usable by customers).

"To give better visibility on the answered topics, please click on ""Accept as Solution"" on the reply which solved your issue or answered your question.Saket_Om"