Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 37 ยท Getting Started

The Embedded Development Workflow

Understand how firmware gets from source code to a running chip: cross-compiling, linking, flashing, bootloaders and on-chip debugging, with runnable simulations.

On a desktop you write code, press run, and the same machine compiles and executes it. In embedded development, the computer you write code on (the host) is not the computer that runs it (the target). Your laptop has an x86-64 or ARM64 processor with an operating system; the target is, say, a 32-bit Cortex-M chip with 64 KB of flash and no OS at all. Everything in the workflow follows from that gap: you build for another machine, copy the program onto it, and then watch it from the outside.

This lesson walks the whole path: edit, cross-compile, link, flash, run, debug. You will also run small simulations of the two ideas that surprise newcomers most: how flash memory is written, and how a bootloader checks an image before running it.

The big picture

flowchart LR
    A["Source<br/>main.c, drivers"] --> B["Cross-compiler<br/>arm-none-eabi-gcc"]
    B --> C["Object files<br/>.o"]
    C --> D["Linker<br/>+ linker script"]
    D --> E["firmware.elf<br/>(symbols, debug info)"]
    E --> F["objcopy"]
    F --> G["firmware.bin / .hex"]
    G --> H["Programmer<br/>(SWD / JTAG / USB bootloader)"]
    H --> I["Target MCU<br/>flash memory"]
    I -. "debug probe" .-> E

Source becomes an ELF file for debugging and a raw binary or hex file for flashing.

Cross-compilation

A cross-compiler runs on the host but produces machine code for the target. Its name encodes the target, for example:

Toolchain prefix Target
arm-none-eabi-gcc ARM Cortex-M/R, no operating system ("none"), standard EABI
avr-gcc 8-bit AVR (Arduino Uno and relatives)
riscv32-unknown-elf-gcc 32-bit RISC-V
xtensa-esp32-elf-gcc ESP32 family (Xtensa cores)

A typical command for a Cortex-M4 looks like this. It is shown for reference (it needs the Arm toolchain installed, so it cannot run here):

Terminal
arm-none-eabi-gcc -mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 \
    -Os -ffunction-sections -fdata-sections -Wall -Wextra \
    -c main.c -o main.o

Notice a few embedded habits: -Os optimizes for size (flash is scarce), -ffunction-sections plus a later --gc-sections lets the linker drop unused functions, and -mcpu/-mthumb select the exact instruction set the chip understands. Without -mfloat-abi=hard the compiler would not use the chip's floating-point unit.

Linking and the linker script

Linking joins object files and decides where in memory everything goes. On a desktop the operating system loader picks addresses. On a microcontroller you tell the linker with a linker script that describes the memory map: where flash starts, where RAM starts, and how big each is. Code and constants go to flash; variables go to RAM. You study this in depth in Startup Code, Linker Sections and the Stack.

The result is an ELF file: code, data, symbol names and debug information. ELF is for your tools. The chip needs only the raw bytes, so objcopy strips the file down to a .bin (raw image) or .hex (text format with addresses and checksums).

After linking, check how much room you used with the size tool:

TEXT
   text    data     bss     dec     hex filename
  18364     212    1580   20156    4ebc firmware.elf

text (code and constants) and data (initialized variables) both occupy flash, because the initial values must be stored somewhere permanent. data and bss (zero-initialized variables) occupy RAM. Flash usage is therefore text + data; RAM usage is data + bss plus the stack and heap.

C A tiny memory-budget report

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Flashing: getting the image onto the chip

Program memory is flash: non-volatile, so it survives power loss. You write it with one of these routes:

  • Debug probe over SWD or JTAG. A small adapter (ST-Link, J-Link, CMSIS-DAP, Raspberry Pi Debug Probe) talks to a dedicated debug port on the chip. It can flash, halt the CPU, read registers and set breakpoints. This is what you use during development.
  • Serial or USB bootloader. A small program, already in a protected part of flash, receives a new image over UART, USB, CAN or the network and writes it. Arduino boards and the ESP32 work this way; so do field updates.
  • Mass-storage bootloader. The board shows up as a USB drive, and you drag a .uf2 file onto it (Raspberry Pi Pico).
  • In-system production programming. A factory fixture flashes thousands of boards.

How flash actually works

Flash has a rule that surprises people: you can only change a bit from 1 to 0 by writing. To get a 1 back you must erase a whole block (a page or sector, commonly 1 to 128 KB), which sets every bit in the block to 1. Erased flash reads as 0xFF. Erasing is slow (milliseconds) and each block survives only a limited number of erase cycles (typically 10,000 to 100,000).

The simulation below models one 16-byte page. Writing is a bitwise AND with the existing content, so you can see what happens when you try to overwrite without erasing.

C Simulating flash: write can only clear bits

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The second write produces 0x24, not 0x3C, because the bits already cleared by 0xA5 stay cleared. This is why firmware that stores settings in flash must erase a page before rewriting it, and why such code spreads writes around to wear-level the cells.

Bootloaders and image checks

Most products keep a small, trusted bootloader at the start of flash. On reset it decides whether to run the application or to accept a new one. A good bootloader verifies the application before jumping to it, usually with a checksum or a CRC stored in the image header. The next program implements a CRC-32 (the same polynomial used by Ethernet and ZIP) and shows how one flipped bit gets caught.

C A bootloader-style image check

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

For the standard test string "123456789" this CRC-32 gives CBF43926, the well-known check value, so you can validate your own implementation against it.

Debugging from the outside

You cannot attach a normal debugger to a chip with no operating system. Instead, a debug probe connects to the chip's debug interface. Software on the host (GDB with OpenOCD, or a vendor IDE) can then:

  • halt and resume the CPU, single-step, set hardware breakpoints (the chip has only a handful) and watchpoints on memory addresses;
  • read and write registers and memory, including peripheral registers;
  • print text through a UART (serial) port, semihosting or ITM/SWO trace channels.

The simplest debugging tool is still blinking an LED or toggling a pin and watching it with a logic analyzer or oscilloscope. Chapter 8 returns to this in detail.

Simulators and emulators

You can learn a great deal without hardware. Emulators such as QEMU model complete boards; Renesas, Microchip and ST ship simulators; online tools (Wokwi, Tinkercad Circuits, Proteus) simulate popular boards. And as in this course, plain C on your PC can simulate registers, ring buffers and schedulers. The logic is identical; only the final register addresses differ.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why is a cross-compiler needed for embedded development?

    The host (your PC) and target (the MCU) have different instruction sets, so the compiler must generate code for the target.
  2. 2. You write 0x3C into a flash byte that currently holds 0xA5 without erasing. What does the byte hold afterwards?

    Flash writes can only clear bits, so the result is 0xA5 AND 0x3C = 0x24. An erase is needed first.
  3. 3. Which sections of a firmware image occupy flash?

    Code and constants (text) and the initial values of initialized variables (data) must be stored in flash. bss only needs RAM.
  4. 4. What does a bootloader's CRC check protect against?

    A CRC detects accidental corruption such as an interrupted update or flipped bits. It is not a security signature.

Summary

  • Embedded development is cross-development: you build on a host for a different target.
  • The linker script places code and data in the target's memory map; the ELF file is for tools and the BIN/HEX file is for flashing.
  • Flash writes can only clear bits; erasing a whole page sets them back to 1 and wears the cells.
  • Bootloaders verify images (CRC or signatures) before running them.
  • Debug probes (SWD/JTAG), UART output, LEDs and logic analyzers are your window into a running chip.

Next: Choosing a Board and Toolchain