What is better, an embedded Linux program running in a board or a bare metal application?
What are the advantages or drawbacks of embedded Linux?
Nowadays embedded Linux systems are everywhere, literally EVERYWHERE. For example, smartphones (Android), TVs, vending machines, dataphones, GPSs, domotic houses, photo cameras, cars, etc But…
Is embedded Linux always the best option?
First, let’s define what is “bare metal” application.
It is a low-level method of programming and it is specific to the hardware used. For example, the GPIO, ADCs, hardware multipliers or adders, memories, communication modules (SPI, I2C, UART…), PWMs, DACs, etc
All the small microcontrollers or the classical Arduinos use bare metal programming.
Normally, most of the bare-metal projects do not start from scratch (at all). It would cost a lot to start every project from zero again and again. We use instead some already tested libraries.
We can consider this 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. The use of libraries boost the developing time, because the debugging is reduced –> better time-to-market.
Normal user-defined applications are called “User Space Applications” and 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. In User space is just a chunk of memory and over the kernel.
The bigger difference (and advantage) is that the user space applications are hardware-independent.
What does the Linux “Kernel”?
The kernel of Linux consists of various modules, which interact directly with the underlying hardware.
It 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.
Real-time in Linux?
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 an 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