Introduction. Who Is in Charge After Power-On?
A thermostat can take a temperature reading and switch on the heat moments after power returns. What starts its program? Who tells it when to take the next reading? On a laptop, an operating system such as Linux starts applications and manages their access to hardware. A small controller can do its entire job with one program that runs directly on its processor. Writing software for that arrangement is called bare-metal programming.
In our previous Gate & Kernel lesson, pressing a button on an Arduino Uno R3 turned on a small light. Start that circuit again, press the button, and release it. You have just seen three jobs that every controller designer must assign somewhere: observe an input, decide what it means, and act on an output. This time we will look at the whole system that makes those jobs possible, from the first instruction to the choice between software and a dedicated circuit. Try the button circuit from the previous lesson.
1. How Does Bare-Metal Programming Work?
The part of a chip that executes instructions is its central processing unit, or CPU. Reset puts the CPU into a defined starting state. The hardware determines where execution begins. From there, a small sequence of startup instructions prepares working memory and transfers control to the application. A common application configures the hardware once, then repeats a sequence of jobs for as long as the device has power.
You can see this structure in the classic Uno R3. Open Arduino’s main.cpp and find setup();. That line calls the setup function once. Just below it, for (;;) begins an indefinitely repeating block containing loop();. Arduino supplied these lines. They become part of the program installed on the board. The CPU runs both the support code and the code you wrote. See the calls in Arduino’s AVR core.
2. Bare Metal vs. an Operating System
Imagine two programs that both turn on a light. In a Linux application, a request to control an output typically passes into the kernel, the central part of the operating system. A device driver translates the request into the operations the hardware requires. Other applications can run while the first one waits. The kernel decides which program gets processor time and can enforce rules about who may use a device.
In bare-metal software, the application can call a driver included in its own program. It decides when the call happens and what work follows. That short path suits a device with a fixed job. It also gives the programmer a responsibility that Linux usually handles: if two parts of the application want to change the same output, the program itself must coordinate them.
3. Main Components of a Bare-Metal System
A microcontroller combines a CPU, memory, and hardware for interacting with the outside world on one chip. On the Uno R3 that chip is the ATmega328P. Follow one button reading through it and each component has a distinct job.
- Program storage. Flash memory retains the instructions when power is removed. On the Uno, the CPU fetches program instructions from flash.
- Working memory. RAM holds changing data while the program runs. It also provides space for the stack, which keeps track of unfinished function calls and their return information.
- Clock and peripherals. A clock supplies timing pulses. A peripheral is a hardware block with a particular job. A timer counts pulses, while a general-purpose input/output block, or GPIO, reads and drives pins.
- Firmware and build tools. Firmware is the program installed on the device. A compiler translates source code into machine instructions. A linker combines the pieces and places code and data at addresses that suit the chip.
Changing the button rule changes the firmware. Moving the wire to a different pin changes which hardware input the firmware must read. Both details have to agree. ATmega328P datasheet.
4. What Is Memory-Mapped I/O?
An address is a number the CPU uses to select a location. Some addresses select RAM. Chip designers assign others to peripheral registers, locations through which software observes or controls hardware. When a processor can use memory-access instructions to reach those registers, the arrangement is called memory-mapped input/output, or MMIO.
For example, the Uno’s digital pin 13 corresponds to bit 5 of port B. A port is a group of pins, and a bit is one binary digit. In the ATmega328P data address space, address 0x24 selects the direction register for port B and 0x25 selects its output register. The prefix 0x marks a number written in base sixteen. Set bit 5 in the first register to enable output, then set bit 5 in the second to drive the pin high. The output circuit holds that level while the CPU does other work. AVR also provides special instructions for frequently used I/O registers, so the compiler has more than one way to reach the hardware.
5. Bare-Metal Programming in C
C lets us express those two operations using names supplied for the chip. Here is a complete small C program for the ATmega328P on the Uno R3. Build it with the AVR C tools for that device. Its main is the application entry function reached by the C startup code.
#include <avr/io.h>
int main(void) {
DDRB |= (1u << DDB5);
PORTB |= (1u << PORTB5);
for (;;) {
}
}
DDRB names the direction register. PORTB names the output register. The shift 1u << DDB5 produces a value with just the selected bit set. The |= operation sets that bit while preserving the other bits read from the register. The lamp stays on throughout the empty loop. The chip header marks these hardware accesses volatile, telling the compiler that the accesses themselves matter. Coordinating simultaneous updates from different parts of a program still requires separate care.
6. Bare-Metal Programming in Rust
Rust can target the same kind of machine. The setting #![no_std] allows a program to use Rust’s core language library without depending on its full standard library. A runtime for the chosen target provides startup support. A hardware abstraction layer, or HAL, provides operations such as configuring an output pin. Once a suitable HAL has configured an active-high LED output named led, the application might contain this fragment:
led.set_high().unwrap();
loop {
core::hint::spin_loop();
}
Here set_high() requests a high output. With an output-pin interface that returns a result, unwrap() continues on success and invokes the program’s panic behavior on error. The fragment assumes the board setup, output-pin interface, and panic handler are already in place. A HAL can represent an input pin and an output pin as different types. With such an interface, trying to drive a pin still configured as an input can produce a compiler error before the firmware reaches the board. The eventual electrical action is the same one we saw in C. The Embedded Rust Book on bare-metal environments.
7. Bare Metal vs. an RTOS
Now give the thermostat three jobs: take a reading, refresh a display, and send a status message. A bare-metal main loop can perform a small step of each job in turn. If sending a message waits indefinitely for a reply, everything later in that loop waits too. The designer can split communication into short steps and return to it on the next pass.
Another option is a real-time operating system, or RTOS. It lets the programmer organize jobs into independently scheduled units called tasks. Its scheduler chooses a ready task according to rules such as priority. With FreeRTOS, for example, a communication task can block while waiting for data, leaving a ready measurement task free to run. This adds task state, stack space, and rules for sharing devices. The decision turns on how many jobs must wait or progress independently. FreeRTOS scheduling.
8. Interrupts in Bare-Metal Systems
Repeatedly asking whether an input has changed is called polling. Hardware can also signal an event that needs attention. That signal is an interrupt. When the interrupt is enabled and accepted, the CPU suspends its normal sequence and executes a designated function called an interrupt handler. After the handler finishes, execution can resume where it left off. All of this can happen on a board running bare-metal firmware.
A timer interrupt might announce that the next temperature sample is due. A short handler can acknowledge the event and record that work is pending. The main loop can then collect the reading and update the display. A single pending flag is sufficient if only the latest request matters. If every tick must be counted, two ticks arriving before the flag is checked could look like one. A counter or queue, accessed safely by both the handler and the main code, preserves more information.
9. Bare-Metal Systems and Real-Time Performance
A deadline is the latest time by which a response must be complete. A system has a real-time requirement when meeting that deadline is part of doing the job correctly. Latency is the time between an event and its response. Jitter describes how much the timing varies across repetitions.
Suppose a controller must set an output within one millisecond of an input change. If a display update keeps the main loop away from that input for three milliseconds, a change just after a poll can already miss the deadline. A fast average loop time tells us little about that particular press. An interrupt can shorten detection time, but time spent with interrupts disabled and work waiting after the handler also count. To measure the whole response, capture the input and output on two channels of a logic analyzer, an instrument that records digital transitions. Measure the gap with the display busy as well as idle.
10. Bootloaders in Bare-Metal Systems
How did the application get into flash in the first place? One option is a bootloader, a small program that runs early and can select, receive, or start application firmware. The standard Uno R3 has a bootloader that supports uploading a sketch over the board’s programming connection. When it transfers control to the installed application, that application’s own startup code prepares its execution environment.
These are separate jobs. A manufacturer can use an external programmer to install firmware and configure a suitable chip to start the application directly. Another product may need a bootloader that checks an update and chooses between two firmware images. Both products can run a bare-metal application. A practical design question is how the owner will update or recover the device after it leaves the factory. Arduino’s Optiboot source.
11. Bare-Metal Debugging
Suppose the LED never lights. Begin with a test that gives two possible explanations different outcomes. In the previous Uno simulation, keep the original wiring, press the button, and inspect the value returned by digitalRead(2). With the internal pull-up enabled and the button wired to ground, released should read HIGH and pressed should read LOW. If the input changes correctly, follow the output decision. If it stays HIGH, check the button connections and pin setup first.
A debugger lets you inspect a running program. A breakpoint pauses it at a chosen instruction. On a supported target, a hardware debug probe connects the development computer to the chip so you can stop at startup, step through code, and inspect registers. Without a probe, the Uno example can report readings over its serial connection. Stopping a CPU or printing a message changes timing, so use the two-channel measurement from Chapter 9 when the fault appears only at full speed.
12. Bare-Metal Programming on Arm
An instruction set architecture defines the instructions and processor behavior software can rely on. Arm processors appear in many kinds of systems. For a concrete microcontroller example, consider an Arm Cortex-M4. At reset it obtains two values from the beginning of its vector table, a table used to locate startup and event-handling code. The first gives the initial stack pointer, which identifies the position of the working stack. The second gives the address of the reset handler, where startup execution begins.
Open a Cortex-M startup file and look for those first two entries. Farther down are the handlers for other events. This reveals which details come from the processor and which come from the chip vendor. Two Cortex-M4 chips follow the same processor startup convention, yet their GPIO addresses and clock settings may differ. Moving a C program between them can preserve its decision logic while requiring new pin initialization. Arm CMSIS-Core startup file guide.
13. Bare-Metal Programming on RISC-V
RISC-V, pronounced “risk five,” is an open instruction set architecture. Consider a small RISC-V controller running in machine mode, the processor’s most privileged execution mode. On reset the processor starts at an address defined by the implementation. Startup software typically establishes a stack and prepares memory before entering the application. A processor register called mtvec specifies where machine-mode execution is directed for events called traps, which include interrupts and exceptions.
For a new board, consult two documents. The RISC-V specification explains what mtvec means. The chip manual or board support package tells you where RAM and GPIO live and how the board routes interrupts. Knowing the processor speaks RISC-V is enough to choose an instruction family. Lighting a particular LED also requires the board’s wiring and peripheral map. RISC-V machine-level specification.
14. Bare-Metal Programming in FPGA Systems
A field-programmable gate array, or FPGA, contains logic and connections that a designer can configure into digital circuits. In our FPGA series, a button signal could pass through a circuit straight to an LED output. That design can work without a CPU fetching any instructions. Bare-metal software enters the picture when the system includes a processor.
One option is a soft processor, a CPU built from configurable logic, such as AMD MicroBlaze. Another is a hard processor, a CPU manufactured into the chip alongside the programmable logic, as in a Zynq-7000 device. Imagine adding a hardware counter that records input pulses. The FPGA circuit can count while the CPU is busy. The designer gives the counter a readable register at an assigned address, and the firmware reads the total when needed. This is the memory-mapped I/O from Chapter 4, with one important freedom: the FPGA designer can create the peripheral and choose where it appears in the system’s address map. Example of a configurable memory-mapped GPIO peripheral.
Conclusion. What Takes the Operating System’s Place?
On the Uno, you can point to the code that calls the application, the register that drives the light, and the loop that decides when to read the button again. A larger controller distributes those responsibilities differently. A timer can announce when a sample is due. An RTOS can schedule independent tasks. An FPGA circuit can count events before software ever reads them.
When you pick up an unfamiliar board, start with three questions. Where does the CPU begin after reset? Which register connects my program to the hardware I care about? How long can an event wait before my code responds? Answer those, and you can trace a bare-metal system from its first instruction to an observable result.
