What is better to program a Linux program running in a board or a bare metal application? Nowadays embedded Linux systems are everywhere, literally EVERYWHERE. For example, smartphones (Android), TVs, vending machines, dataphones, GPSs, domotic houses, etc But… is embedded Linux always the best option for electronic devices? What are the advantages or drawbacks of embedded Linux?
First, let’s define what is “bare metal” application. Bare metal programming refers to run a program or a software directly on hardware level without the support of any operating system. In other words, program a microcontroller directly with a bunch of lines of C or Assembler-code, as traditionally it was made in electronics and we learn with Assembler.
It is a low-level method of programming that is specific to the hardware (GPIO) used.
All the small microcontrollers or classical Arduinos use bare metal program.
Normally, most of the bare-metal projects do not start from scratch, and it is used some already tested libraries. This would be a hybrid modality: “bare-metal + libraries”. These libraries contain basic functions and they could be developed by the same programmer (or team) or by a third-party. This boost the debugging time and therefore the developing time. The time-to-market may be important in many cases.
In embedded Linux, the applications run over or supported by the kernel, acting as a operating system (OS). These applications programmed for a used-defined purpose are called “user space applications”.
They have intermediate layers until they access the hardware. For that purpose, they need to go through the kernel space to access to the HW, as user space is just a chunk of memory and over the kernel.
The bigger advantage is that they are hardware-independent.
The kernel of Linux consists in various modules, which interact directly with the underlying hardware. The kernel provides the required abstraction to hide low level hardware details to system or application programs and therefore made it hardware-independent. The kernel major activities or responsibilities are: Process management, memory management, timers, inter-process-communications (IPC), device drivers for all hardware resources or power management among others.
An application running in userspace can have scheduling and threading managed by the kernel (multi-threading advantage), but on the other hand, also can be interrupted any time when the OS needs to manage other calls and processes. Therefore, applications with timing sensitive access to GPIO are not suitable or need special treatments.
Linux is generally not suitable for Hard Real-time tasks because user-space applications can always be interrupted by the kernel. Linux has the possibility of using hard real-time extensions for this task, which add extra layer between the hardware and the kernel. For example, RTLinux, RTAI or Xenomai. But it has also limits, and therefore any aeroplane or nuclear plant are based on Linux.
Bare Metal advantages:
- Better performance with the same HW (100 to 1.000 faster than w. Linux)
- Easy and fast for small applications
Bare Metal drawbacks:
- Complexity increase exponentially with the system size and functionalities
- Basic and standard functions have to be programmed and adapted to the specific HW
- No multi-threading possibilities in one core
- Easily scalable
- HW-independent (Portability)
- Multi-threading and priorities
- Free prebuilt drivers available (faster development)
- Less bugs (mature OS code rather than new own code)
- Community support
- Complex for small applications
- steeper learning curve than bare metal
- Require a minimum (powerful) hardware to run the Kernel
- Security protection needed
- Updates of the system may be needed