PIC32MZ Time delay interference using FreeRTOS

I have written a function below which helps generate a time delay in micro-seconds. i plan to set time delays in the rage of 2-5 micro-seconds.
void Delay_Us(unsigned int Us)
{

// Convert microseconds us into how many clock ticks it will take
Us *=(SYS_CLK_FREQ / 1000000) / 2; // Core Timer updates every 2 ticks

_CP0_SET_COUNT(0); // Set Core Timer count to 0

// Wait until Core Timer count reaches the number we calculated
while (Us > _CP0_GET_COUNT());
}
I have FREERTOS configured in the code. I am wondering how the function defined will affect its total time duration if FreeRTOS functions vTaskStartScheduler() and xTaskCreate are executed independently during the small interval.
Specifications:

Compiler: XC32,
IDE: MPLAB X IDE v4.05
Chip: PIC32MZ2048EFG144

I would appreciate some guidance.

Thank you,
Kenny

I’m not sure how calling vTaskStartScheduler() could effect it because if that function has not been called then there will be no multithreading and calling Delay_Us will result in the processor sitting in Delay_Us until the delay is complete. In other words if the scheduler has not been started then FreeRTOS cannot impact the function.

After the scheduler has been started is a different matter because any task calling Delay_Us can get preempted by any higher priority task, and therefore Delay_Us will not be accurate - the delay time achieved will be either the desired microsecond delay if no context switches occur, or an unknown time if context switches do occur.

1 Like