Chapter #001 – How much time it takes to wait 0 micro-seconds?


1. Preview

Welcome to my the first chapter of my brand new technical blog "The Magic Corner: Practical Real-Time Optimizations". In this blog I'm going to demonstrate real-world software optimizations of real-time / embedded systems.

As we all know, real time and embedded systems (in the future I'll write on the difference between those two) typically have limited resources, such as small memory and slower micro-controller and many other limitations. Nevertheless, these systems usually need to work very fast, to be responsive in short period of times, and meet very strict timing KPIs.

In this blog I'm going to share some practical ways that will help you optimize your embedded Software (a.k.a. Firmware). Many of the optimizations I'm going to describe might seem to you simple and intuitive, yet I was surprised to find out that many Embedded-Software developers were not familiar with some of them.

Ah, and yes – this blog is focused on C programming for Embedded-Systems. If you are interested in optimizations for other programming language, look somewhere else, sorry.

So, let's jump to the first optimization.

2. "Please wait for 0 micro-seconds"

When dealing with HW devices and/or slow bus's it is common to find delays in the code. The delays can be found between sending consecutive data packets over internal fabric into a storage device. Or a delay might appear after writing some bit into a register that triggered some HW-based cryptographic flow, and the SW needs to wait before checking for the operation completion.

But how come that SW needs to wait 0 usec? (usec = micro-second for the newcomers)

This might happen when the delay itself is not known ahead of time when writing the code. The duration of the delay might be depend on the runtime flow. For example, when sending data to a slow storage device, it can be that for small buffer size the SW does not need to wait at all, but for larger buffers, a delay might be required. This is how you might get into a code that actually waits for 0 msec.

let's take for example this code snippet: