Chapter 1: Boot Sequence#

Sample code#

  • Directory: docs/os-on-arduino/code/01_boot

uv run pio run -d 01_boot -t upload
uv run pio device monitor -b 115200

Source code#

The sketch for this chapter. Click a file name to expand. The code is mirrored automatically from the manuscript repository, so what you see here is always current.

docs/os-on-arduino/code/01_boot#

src/main.cpp — 63 lines

Open on GitHub

 1/**
 2 * Chapter 1: Boot Sequence Check
 3 *
 4 * This sketch demonstrates the boot sequence by examining:
 5 * - BSS section (zero-initialized variables)
 6 * - DATA section (initialized variables copied from Flash)
 7 * - Vector table location
 8 * - Stack pointer
 9 */
10
11#include <Arduino.h>
12
13// Global variable in BSS section (uninitialized, should be zero)
14int bss_var;
15
16// Global variable in DATA section (initialized, copied from Flash)
17int data_var = 12345;
18
19void setup()
20{
21    Serial.begin(115200);
22    while (!Serial) { }
23    while (Serial.read() != 'S') { delay(1); }
24
25    Serial.println("=== Boot Sequence Check ===");
26    Serial.println();
27
28    // BSS section variable (should be zero-cleared)
29    Serial.print("bss_var (should be 0): ");
30    Serial.println(bss_var);
31
32    // DATA section variable (initial value should be copied)
33    Serial.print("data_var (should be 12345): ");
34    Serial.println(data_var);
35
36    Serial.println();
37    Serial.println("=== Memory Addresses ===");
38
39    // Show variable addresses
40    Serial.print("&bss_var:  0x");
41    Serial.println((uint32_t)&bss_var, HEX);
42
43    Serial.print("&data_var: 0x");
44    Serial.println((uint32_t)&data_var, HEX);
45
46    // Vector table location (VTOR register)
47    Serial.print("VTOR:      0x");
48    Serial.println(SCB->VTOR, HEX);
49
50    // Stack pointer
51    uint32_t sp;
52    __asm volatile ("MRS %0, MSP" : "=r" (sp));
53    Serial.print("MSP:       0x");
54    Serial.println(sp, HEX);
55
56    Serial.println();
57    Serial.println("If you see this, boot sequence completed!");
58}
59
60void loop()
61{
62    // Nothing to do
63}

docs/os-on-arduino/code/01_boot_vector_dump#

src/main.cpp — 60 lines

Open on GitHub

 1// vector_dump.ino
 2// 第1章 演習1-2: ベクタテーブルの内容をダンプ
 3
 4#include <Arduino.h>
 5
 6void setup()
 7{
 8    Serial.begin(115200);
 9    while (!Serial) { }
10    while (Serial.read() != 'S') { delay(1); }
11
12    Serial.println("=== Vector Table Dump ===");
13    Serial.println();
14
15    // ベクタテーブルのアドレス(VTORから取得)
16    uint32_t *vtor = (uint32_t *)SCB->VTOR;
17
18    const char *names[] = {
19        "Initial SP",
20        "Reset",
21        "NMI",
22        "HardFault",
23        "MemManage",
24        "BusFault",
25        "UsageFault",
26        "Reserved",
27        "Reserved",
28        "Reserved",
29        "Reserved",
30        "SVCall",
31        "DebugMon",
32        "Reserved",
33        "PendSV",
34        "SysTick"
35    };
36
37    for (int i = 0; i < 16; i++) {
38        Serial.print("[");
39        if (i < 10) Serial.print(" ");
40        Serial.print(i);
41        Serial.print("] ");
42
43        // アドレス
44        char hex[16];
45        snprintf(hex, sizeof(hex), "0x%08lX", (unsigned long)vtor[i]);
46        Serial.print(hex);
47
48        // 名前
49        Serial.print("  ");
50        Serial.println(names[i]);
51    }
52
53    Serial.println();
54    Serial.println("Note: Handler addresses point to Flash (0x000xxxxx)");
55    Serial.println("      Initial SP points to RAM top (0x200xxxxx)");
56}
57
58void loop()
59{
60}

On the board#

../_images/ch01_boot.gif

Nothing appears for a while after the monitor opens; everything comes out at once the moment you send S. If data_var reads 12345, copying the initialized data worked.#

Pitfalls#

(Collected from reader questions — ask via GitHub Issues.)