Low Level Programming: Some Basic Concepts

(mainly from Gemini, sources linked)

Driver, Firmware and OS

FeatureDriverFirmwareOS
Level of AbstractionHigh-levelLow-levelHigh-level
Primary RoleFacilitate communication between hardware and OSControl basic hardware functions

Manage system resources and run applications

LocationStored 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:

2. Firmware:

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 PythonJava 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

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:

  1. 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. 

    Guide: Bare-Metal Programming using gcc - CS107e

  2. Linker: The linker combines the compiled object files into a single executable image that can be loaded directly into the target's memory. 

    How to execute an object file: Part 1 - The Cloudflare Blog

  3. 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. 

    Newlib - Wikipedia

  4. 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. 

    What is a Hardware Abstraction Layer and How Does it Work?

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.  

    cpq/bare-metal-programming-guide - GitHub

  • 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.  

    Bare Metal Firmware Development: What, When and

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值