Hello @SavitaDhiman
Although osDelay() is relatively straightforward to utilise, it is possible for certain errors to arise, which may result in the delivery of incorrect delays or even system crashes.
The following are some typical reasons why osDelay() may not function as anticipated:
- Invalid system timer configuration – If the system timer is not set up properly or disabled, osDelay() will be unpredictable.
- If you disable interrupts, the SysTick interrupt can't decrement the delay counter, so delays are much longer.
- If the processor is overwhelmed with interrupt requests, the SysTick interrupt may not fire consistently, affecting osDelay().
- Incorrect configuration of RTOS can result in issues such as priority inversion or resource starvation, which can subsequently affect thread scheduling and timing.
- You can't call osDelay() from an ISR or with interrupts disabled. This can lock up the system.
- If the delay period is too long, the SysTick counter can roll over, which can cause delays to be incorrect.
- The use of multiple timing sources may result in a conflict between the timers employed by some ISRs and SysTick, potentially affecting the accuracy of osDelay().
Debugging delays will help you identify the issue:
- Check the SysTick configuration. Make sure the settings match your system and RTOS needs.
- Verify interrupts enabled – Set a breakpoint at the start of osDelay() to check if interrupts are already disabled before it is called.
- Inspect interrupt handler – Step through the SysTick handler code to ensure it is behaving correctly and decrementing the counter.
- Measure execution times – Use profiling or an oscilloscope to find ISRs or code segments that are taking too long to execute and fix accordingly.
- It is possible to alter the thread priorities by increasing the priority of threads and ISRs using the osDelay() function. This will prevent them from being blocked for an excessive period of time.
- Confirm units Make sure the delay is specified in milliseconds. A common mistake is using microseconds instead.
- It is advisable to ascertain the speed of the timer in order to determine the length of any delays. It is likely that a 1 MHz SysTick timer will prove inadequate in this context. It would be prudent to select a timer with a faster speed.
- Aware of timer resolution – There is only 1 tick per millisecond, so delays below 10 ms may measure as 0 ms.
- Inspect compiler optimizations – Aggressive optimizations may eliminate or alter the delay loop code.
- It is recommended that the RTOS scheduler be verified. It may be the case that short delays are simply skipped if the RTOS reschedules a thread immediately.
I hope my answer has helped you. When your question is answered please close this topic by marking as Best the reply that answered you, it will help others find that answer faster.
Thanks
Kholdoun