From code to chip: compile, link and flash
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
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.
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);
}اقرأ الدرس خطوة بخطوة
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); }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 usedCompile: 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 addressLink: 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)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 bootloaderUpload: 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 outReset 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 programFlash 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)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)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
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01Why can't the microcontroller run your sketch's text directly?
The chip's processor carries out machine code instructions. The toolchain on your computer turns the text into those numbers.
02Your object file calls digitalWrite, but does not contain it. Which step brings digitalWrite in?
The linker joins your .o file with core.a, the already compiled core library, and gives every function its address in flash.
03In what order does the toolchain make these files?
Prepare writes Blink.ino.cpp, the compiler makes Blink.ino.cpp.o, the linker makes Blink.ino.elf, and avr-objcopy writes Blink.ino.hex.
04On the Uno, what happens right after the USB chip resets the ATmega328P for an upload?
A fuse makes the chip start in the bootloader after every reset. The bootloader receives the bytes and writes them into flash.
05The ATmega328P's flash is written in pages. How big is one page?
The datasheet gives 64 words of 2 bytes per page: 128 bytes, and 256 pages in 32,768 bytes of flash.
المسرد
- Toolchain
- The set of programs on your computer that turn source code into a program a chip can run: compiler, linker and helpers.
- Machine code
- The numbers a processor reads as instructions. It is what ends up in a microcontroller's flash.
- Cross-compiler
- A compiler that runs on one kind of computer and makes machine code for a different processor.
- Object file (.o)
- The compiler's output for one source file: machine code whose calls to other code are still names, not addresses.
- Linker
- The program that joins object files and libraries into one program and gives every piece its address.
- Intel HEX (.hex)
- A text file format that lists a program's bytes as hex digits, ready to send to a chip.
- Bootloader
- A small program kept in flash that runs after reset and can receive a new program and write it into flash.
- Flash page
- The block that flash is written in. On the ATmega328P one page is 128 bytes.
الملاحظات والمصادر
- The card shows only loop(), with the pin written as 13. The real example writes LED_BUILTIN, which is pin 13 on the Uno, and setup() calls pinMode(LED_BUILTIN, OUTPUT).
- The commands are shortened. The Arduino tools also pass warnings, include paths and link-time optimisation (-flto), compile the core's own files, and run each step for you when you press Upload.
- The ATmega16U2's DTR line reaches the reset pin through a 100 nF capacitor, so only the change of the line makes a short reset pulse. A fuse set at the factory (BOOTRST) makes the chip start in the bootloader after every reset. When the upload ends, optiboot actually lets the watchdog timer reset the chip, then jumps to your program at 0x0000.
- Other boards use the same idea with different tools. An ESP32 starts from a bootloader in ROM that cannot be changed, and the esptool program writes your binary into its flash over the serial port with write-flash, erasing 4096-byte sectors first.
- Each flash page survives at least 10,000 write and erase cycles (datasheet), plenty for uploading during development.
- Arduino CLI: Sketch build process
- Arduino AVR core: platform.txt (compile, link, objcopy and upload recipes)
- Arduino AVR core: boards.txt (Uno upload size, speed and bootloader)
- GCC manual: Options Controlling the Kind of Output
- Microchip ATmega328P datasheet (flash pages, boot loader support, reset)
- AVRDUDE manual: command line options
- Optiboot bootloader
- Arduino UNO Rev3 board page: programming and automatic reset
- Arduino UNO R3 product datasheet (A000066)
- Arduino examples: Blink.ino
- ESP-IDF: Application startup flow (ESP32)
- esptool: Basic commands (write-flash)
اختبار يتاح بعد الخطوة الأخيرة.