User Tools

Site Tools

Trace: bare_metal

What is 'Bare Metal'?

Your usual PC runs an operating system (OS). Often used OSs include Linux, BSD, OS X, Android, Windows and many others. Main point of an OS is to cover hardware abstraction. As a result, an application no longer tells the hardware which bits to change to what, it simply tells the OS to write a file. Advantage of this is, the 'write to file' command is always the same for a given OS, no matter which hardware the application runs on.

'Bare metal' means that there is no operating system, no standard library, no user management, much less a user privilege system. Whatever is done to hardware, the application does it directly (and has to know on how to do it).

Advantages

Having no OS has a number of drawbacks, but also advantages important for controllers:

  • Abstraction needs time, so it reduces performance. When running at 20 or 100 MHz, this makes quite a difference.
  • Abstraction needs memory. An OS has to be prepared for all possible usages, so all the neccessary code has to be stored somewhere. Bare metal code needs only what's actually used, so it's much smaller.
  • Timing is predictable. Operating systems run all kinds of background tasks. Doing so interrupts the main applications for short times. Not noticeable in a word processor, a killer for smooth timings of a stepper motor.
  • Applications can talk to hardware directly. There is no other code trying to talk to the same hardware, so no conflicts possible.
  • A bare metal application has total ownership of the hardware.
  • Bare metal applications can be optimized much better at compilation time. Because the compiler knows exactly what's needed and what not, all the unneeded stuff can be dropped, often resulting in much better performance. Operating systems can't do this.

Disadvantages

  • Applications have to be custom-built for each hardware. To support 20 different MCUs, you have to build and provide 20 different binaries … or to let the end user build the binary for his hardware.
  • One CPU or MCU can usually run one application, only. There are no instruments to switch between applications, so the one application starts at boot time and runs forever, until power is cut.
  • Applications have to deal with every bit in hardware. There is no OS providing a easily usable function, the application has to deal with this its self.

Strategies

People typically create a bare-metal application on a PC with a cross-compiler such as a compiler for arm, and then transfer the executable to the target hardware using a bootloader such as the LPC1114 Bootloader.

Compilers usually support building for bare metal, but need additional parameters, like –no-std-inc and/or –no-std-lib.

There are libraries which provide OS-like functionality, e.g. functions to write a file to a storage medium. Using such libraries can provide a programming experience similar to when coding for an OS. Still these libraries have to be linked in directly.

Precompiler-optimisations. It's a good idea to let the precompiler do one-time calculations as much as possible. Then only the result is stored in the binary, saving the calculation effort at runtime. Teacup Firmware uses this strategy a lot, making it about double as fast and half as big as compareable printing firmwares.

bare_metal.txt · Last modified: 2018/05/27 16:10 (external edit)