(mainly from Gemini, sources linked)
Driver, Firmware and OS
Feature | Driver | Firmware | OS |
Level of Abstraction | High-level | Low-level | High-level |
Primary Role | Facilitate communication between hardware and OS | Control basic hardware functions | Manage system resources and run applications |
Location | Stored on storage media (e.g., hard drive) | Embedded in hardware | Stored on storage media |
While these three terms are often used in the context of computing, they have distinct roles and functions.
1. Driver:
- Purpose: A software program that enables communication between a hardware device and the operating system.
-
Functionality: It translates requests from the OS into commands that the hardware can understand, and vice versa. (source 1)
Example: A graphics card driver allows the OS to control the graphics card and display images on the screen.
2. Firmware:
- Purpose: Low-level software embedded directly into hardware devices.
What is firmware?
Firmware, also known as ‘software for hardware,’ is code that’s embedded in a hardware device—such as a computer system or mobile device—to help it function.
Firmware delivers the instructions for how a device should start up, interact with other devices (such as routers or remote controls), and execute input/output (I/O) tasks. It can be regularly updated to fix problems, add features, or increase compatibility.
The term ‘firmware’ was coined by the American scientist Ascher Opler in 1967 to describe a program that he said was ‘in-between hardware and software1.’ Today, many popular devices, such as TVs, smartphones and cameras, need firmware to function.
What is software?
Software is a set of instructions that guide the key operations of a computing device.
Software can be written in many different languages, or programming codes, including the popular Python, Java and Structured Language Query (SQL). Software has many purposes but is generally grouped into two large categories—software that is written for operating systems (OS) and software that is written for applications.
Firmware vs. software: Key differences
Perhaps the most important difference between firmware and software is that while firmware helps devices boot up and communicate, software is focused more on user interaction.
Manufacturers frequently update firmware to add new features, enhance security and protect devices against vulnerabilities. As any user of an Android or iOS phone knows, firmware version updates need to be downloaded and installed regularly or they will affect device performance.
Software, by comparison, is installed on electronic devices to improve user experience on activities like browsing the internet, checking email, word processing, watching videos, streaming music and much more. Here are some of the most important differences to consider when assessing the technologies.
Functionality: It controls the basic operations of a device, often without requiring user intervention.
Example: BIOS (Basic Input/Output System) firmware controls the boot process of a computer.
3. Operating System (OS):
- Purpose: System software that manages hardware and software resources.
https://en.wikipedia.org/wiki/Operating_system#:~:text=An%20operating%20system%20(OS)%20is,common%20services%20for%20computer%20programs.
-
Functionality: It provides a user interface, manages memory, handles input/output operations, and runs applications
-
Example: Windows, macOS, and Linux are popular operating systems.
Relationship:
- Firmware and Drivers: Firmware often includes drivers for specific hardware components.
- OS and Drivers: The OS relies on drivers to interact with hardware devices.
In essence, firmware is the foundational software that brings a hardware device to life, drivers enable the OS to communicate with that device, and the OS provides the platform for running applications.
Bare Metal
- Refers to the direct access to a computer's hardware resources without the mediation of an operating system.
- Involves programming directly to the hardware, bypassing the OS layer.
- Provides maximum control and performance but requires a deep understanding of the hardware.
In short, operate a device directly with only firmware (if the device is a CPU/MCU or need one to function, then you also need to write driver for its peripherals).
C on Bare Metal Machine
C is a popular choice for programming bare metal systems due to its low-level access to hardware and its efficiency.
Direct Hardware Access in C | Five EmbedDev
Here's how it works:
- Compiler: You need a C compiler that can generate machine code for your specific target architecture. Compilers like GCC and Clang can be configured to produce bare-metal code.
-
Linker: The linker combines the compiled object files into a single executable image that can be loaded directly into the target's memory.
-
Standard Library: While the full C standard library might not be available, you can use a reduced version like Newlib. 1 Newlib provides basic functions like memory allocation, input/output, and string manipulation that are essential for bare-metal programming.
-
Hardware Abstraction Layer (HAL): You'll need to write a HAL to interface with the specific hardware of your target system. This layer provides a set of functions that abstract away the hardware details, making it easier to write portable C code.
example implementation of HAL
#include <stdint.h> #define GPIO_PORTA_BASE 0x40004000 #define GPIO_PORTA_DATA_REG *(volatile uint32_t *)(GPIO_PORTA_BASE + 0x0) #define GPIO_PORTA_DIR_REG *(volatile uint32_t *)(GPIO_PORTA_BASE + 0x4) void hal_init_gpio(uint8_t pin, uint8_t direction) { if (direction == GPIO_OUTPUT) { GPIO_PORTA_DIR_REG |= (1 << pin); } else { GPIO_PORTA_DIR_REG &= ~(1 << pin); } } void hal_set_gpio_pin(uint8_t pin) { GPIO_PORTA_DATA_REG |= (1 << pin); } void hal_clear_gpio_pin(uint8_t pin) { GPIO_PORTA_DATA_REG &= ~(1 << pin); } ... int main() { hal_init_gpio(5, GPIO_OUTPUT); // Initialize pin 5 as output while (1) { hal_set_gpio_pin(5); // Set pin 5 high // ... do something ... hal_clear_gpio_pin(5); // Clear pin 5 low // ... do something ... } }
Explanation:
This is a simplified example of a Hardware Abstraction Layer (HAL) for a hypothetical microcontroller with a GPIO port. The HAL provides functions to initialize a GPIO pin as input or output, and to set or clear the pin's value.
Key points:
- Hardware-specific addresses: The
GPIO_PORTA_BASE
and register offsets are specific to the target hardware.- Volatile keyword: The
volatile
keyword is used to prevent the compiler from optimizing away memory accesses to hardware registers.
- quite common for hardware control, since we often modify a register value to control hardware, without using the register as a variable.
Key Considerations:
- Memory Management: You'll need to manage memory manually, without the help of an operating system. This involves allocating and deallocating memory blocks as needed.
- Interrupts: You'll need to configure and handle interrupts directly. This involves writing interrupt service routines (ISRs) in C to respond to hardware events.
-
Device Drivers: You'll need to write device drivers to control the various hardware peripherals on your system. This involves understanding the hardware specifications and writing code to interact with the devices.