ENع
←Compile, link, flash
Lesson 03 · Getting started with a microcontroller

From code to chip: compile, link and flash

12 min10 steps5 questionsBeginner
About this lesson

A microcontroller cannot read C++. It only runs machine code, numbers stored in its flash memory. Follow the Blink sketch on an Arduino Uno R3 through the toolchain on your computer: tidied into C++, compiled by avr-g++ into an object file, linked with the core library into one program, and written out as a .hex file of at most 32,256 bytes. Then watch avrdude send it down the USB cable to the bootloader, which writes it into flash 128 bytes at a time, until the LED on pin 13 blinks. The C++ basics lessons cover the language itself; here we follow what happens to it.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 3 · Interactive
01 / 10

Your code is only text

Here is Blink, a sketch that flashes the LED on pin 13. To the chip it is just letters. The chip only runs machine code: numbers in its flash memory. A toolchain, a set of programs on your computer, does the translating.

board      Arduino Uno R3  (chip: ATmega328P)
sketch     Blink.ino, plain text
the chip   runs machine code only: numbers in flash
toolchain  the programs that turn text into those numbers

// Blink, for the Arduino Uno R3: the on-board LED is on pin 13
void setup() {
  pinMode(13, OUTPUT);
}

void loop() {
  digitalWrite(13, HIGH);
  delay(1000);
  digitalWrite(13, LOW);
  delay(1000);
}
Loading figure…
or ← →
Read the lesson step by step
  1. Your code is only text

    Here is Blink, a sketch that flashes the LED on pin 13. To the chip it is just letters. The chip only runs machine code: numbers in its flash memory. A toolchain, a set of programs on your computer, does the translating.

    board      Arduino Uno R3  (chip: ATmega328P)
    sketch     Blink.ino, plain text
    the chip   runs machine code only: numbers in flash
    toolchain  the programs that turn text into those numbers
    
    // Blink, for the Arduino Uno R3: the on-board LED is on pin 13
    void setup() {
      pinMode(13, OUTPUT);
    }
    
    void loop() {
      digitalWrite(13, HIGH);
      delay(1000);
      digitalWrite(13, LOW);
      delay(1000);
    }
  2. Tidy it into C++

    First the Arduino tools make the sketch proper C++. They add #include <Arduino.h>, which declares digitalWrite and delay, and a one-line declaration (a prototype) for each of your functions. The result is Blink.ino.cpp.

    added at the top:
      #include <Arduino.h>
      void setup();
      void loop();
    saved as  Blink.ino.cpp
    prototype  a function's name and types, declared before it is used
  3. Compile: text to machine code

    The compiler, avr-g++, turns the C++ into AVR machine code, saved in an object file (.o). It runs on your laptop but writes code for a different processor, the ATmega328P: that is called a cross-compiler.

    avr-g++ -c -Os -mmcu=atmega328p Blink.ino.cpp
      -c                stop before linking: one .o per file
      -Os               make the code small
      -mmcu=atmega328p  the target chip
    out        Blink.ino.cpp.o
               machine code, but digitalWrite is still a name, not an address
  4. Link: join the pieces

    Your .o calls digitalWrite and delay but does not contain them. They live in the core library, compiled once into core.a. The linker joins the pieces, drops unused ones, and gives every function its address in flash.

    avr-gcc -Os -Wl,--gc-sections -mmcu=atmega328p
        Blink.ino.cpp.o core.a -o Blink.ino.elf
      --gc-sections   drop the code nobody calls
    out        Blink.ino.elf: one program, every call now an address
               it starts at 0x0000, where your program begins (the bootloader jumps there when it is done)
  5. Write the .hex, check the size

    avr-objcopy copies the program's bytes into a .hex file: the same bytes and addresses, written as text. Then avr-size checks the fit. On the Uno a sketch may use at most 32,256 bytes of flash.

    avr-objcopy -O ihex -R .eeprom Blink.ino.elf Blink.ino.hex
      -O ihex   Intel HEX: bytes written as hex digits
    avr-size -A Blink.ino.elf   counts the flash it needs
    limit      32,256 bytes = 32,768 - 512 for the bootloader
  6. Upload: down the USB cable

    Now avrdude, the uploader, opens the USB port and sends the bytes at 115,200 bits per second. On the board, a second small chip, the ATmega16U2, turns USB into a plain serial line for the ATmega328P.

    avrdude -p atmega328p -c arduino -b 115200
        -D -U flash:w:Blink.ino.hex:i
      -c arduino   talk to the Arduino bootloader
      -b 115200  bits per second
      -U flash:w   write this file into flash
    on the board  ATmega16U2: USB in, serial out
  7. Reset into the bootloader

    First the ATmega16U2 pulses the chip's reset line. After reset the Uno starts in the bootloader: a 512-byte program kept at the top of flash, at 0x7E00. Its one job is to receive a new program.

    DTR line   ATmega16U2 -> 100 nF capacitor -> RESET
    reset      the chip starts in the bootloader (BOOTRST fuse)
    bootloader optiboot, 512 bytes at 0x7E00 .. 0x7FFF
    it listens on the serial line for a new program
  8. Flash it, page by page

    Flash is written in pages of 128 bytes, so the bootloader collects 128 bytes, then writes one page: page 0 at 0x0000, page 1 at 0x0080, and so on. 32,768 bytes of flash make 256 pages.

    page size  64 words = 128 bytes
    page 0     -> 0x0000
    page 1     -> 0x0080
    page 2     -> 0x0100
    ...
    256 pages x 128 bytes = 32,768 bytes
    the bootloader writes flash itself (self-programming)
  9. Read it back and compare

    Then avrdude reads the flash back and compares it with the .hex file, byte by byte. If one byte differs it reports an error. Its -V option turns this check off, trading safety for speed.

    read flash  ->  compare with the .hex file
    all equal   upload done
    any differ  verification error
    -V          skip the check (faster, less safe)
  10. Run: the LED blinks

    The bootloader jumps to address 0x0000 and your program starts: setup(), then loop() again and again. Pin 13 goes HIGH, then LOW, once a second. Flash keeps the program with the power off.

    jump to    0x0000: your program
    setup()    pin 13 is an output
    loop()     HIGH, wait 1000 ms, LOW, wait 1000 ms
    power off  the program stays in flash
    flash      good for at least 10,000 rewrites

Quiz Unlocks after the last step.