HAL cooperative mode feature request
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. */
}
}