You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+32-16
Original file line number
Diff line number
Diff line change
@@ -1,24 +1,29 @@
1
-
# ESP32-C3 Direct Boot example
1
+
# Direct Boot example
2
2
3
-
This is an example of ESP32-C3 "direct boot" feature. It allows an application to be executed directly from flash, without using the 2nd stage bootloader.
3
+
Supported chips: ESP32-C3, ESP32-H2
4
+
5
+
This is an example of "direct boot" feature.
6
+
It allows an application to be executed directly from flash, without using the 2nd stage bootloader.
4
7
5
8
## Background
6
9
7
-
ESP8266 and ESP32 series of chips share the common [binary image format](https://github.com/espressif/esptool/wiki/Firmware-Image-Format). This format describes how the binary image stored in flash should be loaded into IRAM/DRAM by the ROM bootloader. In typical applications, the ROM bootloader doesn't load the application binary directly. Instead, it loads the 2nd stage bootloader into RAM. The 2nd stage bootloader then loads the application: sections which should reside in RAM are copied from flash into RAM, and cache MMU is configured to map the remaining sections, which are accessed from flash.
10
+
ESP8266 and ESP32 series of chips share the common [binary image format](https://docs.espressif.com/projects/esptool/en/latest/esp32/advanced-topics/firmware-image-format.html). This format describes how the binary image stored in flash should be loaded into IRAM/DRAM by the ROM bootloader. In typical applications, the ROM bootloader doesn't load the application binary directly. Instead, it loads the 2nd stage bootloader into RAM. The 2nd stage bootloader then loads the application: sections which should reside in RAM are copied from flash into RAM, and cache MMU is configured to map the remaining sections, which are accessed from flash.
8
11
9
12
Compared to other microcontrollers, where the program in flash is executed directly without the need for additional stages of bootloaders, this arrangement does add complexity. However, it should be noted that in most production applications the 2nd stage bootloader is required to support firmware update, rollback, and security features. Because of this, the 2nd stage bootloader is used in ESP-IDF, despite the extra complexity.
10
13
11
-
## Direct boot in ESP32-C3
14
+
## Direct boot feature
12
15
13
-
ESP32-C3 (starting from silicon revision 3) allows an application stored in flash to be executed directly, without being copied into RAM. This makes it possible to link an application with a relatively simple linker script, and produce the binary using `objcopy` command, then flash the resulting binary to the ESP32-C3.
16
+
Chips, supported in this example (for example, ESP32-C3, starting from silicon revision 3) allow an application stored in flash to be executed directly, without being copied into RAM. This makes it possible to link an application with a relatively simple linker script, and produce the binary using `objcopy` command, then flash the resulting binary to the target chip.
14
17
15
18
Direct boot feature is activated under the following conditions:
16
19
* Secure boot is disabled.
17
20
* Direct boot feature is not disabled via `EFUSE_DIS_LEGACY_SPI_BOOT` eFuse bit.
18
21
* The ROM bootloader doesn't detect a valid binary image [in the usual format](https://github.com/espressif/esptool/wiki/Firmware-Image-Format)
19
22
* The first 8 bytes in flash are `1d 04 db ae 1d 04 db ae` — that is a "magic number" 0xaedb041d repeated twice.
20
23
21
-
In this case, the ROM bootloader sets up Flash MMU to map 4 MB of Flash to addresses 0x42000000 (for code execution) and 0x3C000000 (for read-only data access). The bootloader then jumps to address 0x42000008, i.e. to the instruction at offset 8 in flash, immediately after the magic numbers.
24
+
In this case, the ROM bootloader sets up Flash MMU to map all amount of Flash then jumps to address `Flash address + 8`, i.e. to the instruction at offset 8 in flash, immediately after the magic numbers.
25
+
26
+
For example, the ROM bootloader of ESP32-C3 sets up Flash MMU to map 4 MB of Flash to addresses 0x42000000 (for code execution) and 0x3C000000 (for read-only data access).
22
27
23
28
The application entry function needs to:
24
29
1. set up global pointer register
@@ -41,7 +46,7 @@ Use it if all of the below are true:
41
46
* The code doesn't fit into RAM, therefore execution from flash is required.
42
47
* Dependency on the ESP-specific binary image format or the ESP-IDF 2nd stage bootloader is undesirable.
43
48
44
-
This feature can also be useful in an educational context to "hide" the added complexity of ESP32-C3 Flash MMU and cache configuration.
49
+
This feature can also be useful in an educational context to "hide" the added complexity of chip Flash MMU and cache configuration.
45
50
46
51
## Alternatives to direct boot
47
52
@@ -56,16 +61,17 @@ If the entire application code is small enough to fit into RAM, then the direct
56
61
This example contains the following parts:
57
62
58
63
*[common/](common/) directory with the application entrypoint, placeholder for the vector table, and a simple implementation of `_write` syscall.
59
-
*[ld/](ld/) directory with the linker scripts.
60
-
*[examples/hello_world/](examples/hello_world/) directory with the minimal example project which prints "Hello, world!" to the UART.
61
64
*[examples/blink/](examples/blink/) directory with an example project which blinks an LED.
65
+
*[examples/hello_world/](examples/hello_world/) directory with the minimal example project which prints "Hello, world!" to the UART.
66
+
*[img/](img/) directory with *.svg format diagrams which illustrate the run-time memory layout and binary image layout when direct boot is used.
67
+
*[ld/](ld/) directory with the linker scripts.
62
68
63
69
64
70
## Prerequisites
65
71
66
72
### Cross-compiler
67
73
68
-
Download and install `riscv-none-elf-gcc` toolchain, for example from the [xPack project](https://github.com/xpack-dev-tools/riscv-none-elf-gcc-xpack/releases).
74
+
Download and install `riscv-none-elf-gcc` toolchain, for example from the [xPack project](https://github.com/xpack-dev-tools/riscv-none-elf-gcc-xpack/releases).
69
75
70
76
This example has been built and tested with toolchain release `12.2.0-3`.
71
77
@@ -79,7 +85,7 @@ This example uses CMake. Make sure that CMake and your build system of choice (e
79
85
80
86
### esptool.py
81
87
82
-
To flash binaries into the ESP32-C3, [esptool.py](https://github.com/espressif/esptool) is used.
88
+
To flash binaries into the chip, [esptool.py](https://github.com/espressif/esptool) is used.
83
89
84
90
If you have Python and pip installed, you can install esptool using:
85
91
```bash
@@ -105,7 +111,7 @@ To debug the examples using JTAG and GDB, follow these steps:
105
111
riscv-none-elf-gdb -x gdbinit build/blink
106
112
```
107
113
This will use the provided gdbinit file to:
108
-
- Launch OpenOCD in pipe mode. Adjust the `gdbinit` file if you need to change OpenOCD launch configuration. You can also launch OpenOCD manually, in that case use `target extended-remote :3333` in `gdbinit`.
114
+
- Launch OpenOCD in pipe mode. Adjust the `gdbinit` file if you need to change OpenOCD launch configuration or select another target chip. You can also launch OpenOCD manually, in that case use `target extended-remote :3333` in `gdbinit`.
109
115
- Flash the program over JTAG
110
116
- Reset the target
111
117
- Set a temporary breakpoint at `main`
@@ -114,16 +120,26 @@ To debug the examples using JTAG and GDB, follow these steps:
114
120
115
121
## Memory layout
116
122
117
-
The following diagram illustrates the run-time memory layout and binary image layout when direct boot is used.
ROM bootloader maps the 0 – 4 MB region of flash to the CPU address space twice: to the "DROM" region using the data cache, and to the "IROM" region using the instruction cache.
124
130
125
131
As it is obvious from the diagram, some parts of this mapping are unnecessary. These parts are shown in gray on the right. For example, `.text` section gets mapped not only to the IROM, but also to DROM, even though code execution only happens through IROM.
126
132
127
133
Such uniform mapping was chosen simply because it is universal, and can be set up by the ROM code without any prior knowledge about the application being loaded. This mapping isn't in any way a limitation of ESP32-C3 cache hardware; for example, ESP-IDF 2nd stage bootloader maps only those regions which are necessary in the given part of the address space.
128
134
129
-
The run-time memory layout and flash binary image layout shown above are achieved in the linker script ([ld/common.ld](ld/common.ld)) by specifying the LMAs (load addresses). LMAs start at 0, and match the addresses in flash. VMAs for IROM (`entry` and `.text`) and DROM (`.rodata`) sections are set in such a way that LMA == VMA - BASE, where *BASE* is the starting address of IROM or DROM. Non-cached `.data` section is then added at the next available LMA.
135
+
The run-time memory layout and flash binary image layout shown above are achieved in the linker script ([ld/esp32c3/common.ld](ld/esp32c3/common.ld)) by specifying the LMAs (load addresses). LMAs start at 0, and match the addresses in flash. VMAs for IROM (`entry` and `.text`) and DROM (`.rodata`) sections are set in such a way that LMA == VMA - BASE, where *BASE* is the starting address of IROM or DROM. Non-cached `.data` section is then added at the next available LMA.
ROM bootloader maps the 0 – 4 MB region of flash to the CPU address space using the cache and the Flash MMU.
142
+
143
+
The memory layout can be found in liker script ([ld/esp32h2/memory.ld](ld/esp32h2/memory.ld)).
144
+
145
+
The run-time memory layout and flash binary image layout shown above are achieved in the linker script ([ld/esp32h2/common.ld](ld/esp32h2/common.ld)) by specifying the LMAs (load addresses). LMAs start at 0, and match the addresses in flash. VMAs for ROM (`entry`, `.text` and `.rodata`) section is set in such a way that LMA == VMA - BASE, where *BASE* is the starting address of ROM. Non-cached `.data` section is then added at the next available LMA.
4. The LED attached to GPIO 2 should be blinking at around 3 Hz rate.
35
+
4. The LED attached to GPIO 2 should be blinking at around 3 Hz rate for ESP32-C3 (frequency can be vary depending on the maximum frequency of the selected chip).
0 commit comments