How to create a delay in a task without blocking that task?

gokcedilek wrote on Monday, May 06, 2019:


My question is about implementing the functionality of HAL_Delay(), which is implemented based on the SysTick timer tick count. In my FreeRTOS project, I am using another timer, namely TIM1, therefore using HAL_Delay() doesn’t give me correct behaviour. I would like to write my own function to create a delay in a FreeRTOS task, without leaving that task, but I am not sure how to start off. Suggestions appreciated, thank you!
Note: vTaskDelay() causes the task to be blocked, and therefore does not do what I want.

Thank you very much!

richarddamon wrote on Monday, May 06, 2019:

My first comment is why? The main reason for using something like a RTOS is to get something like task switching so that while one piece of code is waiting for somethng, other parts can run.

Second is that generally (you don’t say what processor your using), functions like HAL_Delay() are fairly easy to change what timer they are using, so making HAL_Delay work should be that hard, and if you are using other parts of the HAL package, may be important to get to work.

sorter wrote on Monday, May 06, 2019:

You can obtain crude wait using xTaskGetTickCount();

Let’s say, your tick rate is 100Hz. Assuming the task you want to wait on has the highest priority, you can accomplish what you want like that,

TickType_t currentTick = xTaskGetTickCount();
while(xTaskGetTickCount() - currentTick < pdMS_TO_TICKS(1000))