Skip to content

Commit 252d34d

Browse files
Merge branch 'feature/relinker_support_flash_suspend' into 'master'
feat(relinker): Support ESP32-C3 platform and SRAM optimization for flash-suspend See merge request ae_group/esp-iot-solution!839
2 parents ce1e2dd + 371c556 commit 252d34d

File tree

31 files changed

+2470
-203
lines changed

31 files changed

+2470
-203
lines changed

tools/cmake_utilities/CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1+
## v1.1.0 - 2025-01-16
2+
3+
* relinker: support ESP32-C3 and SRAM optimization for flash-suspend
4+
* relinker: add IDF v5.3.x support
5+
16
## v0.5.3 - 2023-09-15
27

38
* fix `add_dependencies called with incorrect number of arguments` in `relinker.cmake`
49
* `include(cmake_utilities)` is not suggested now, to avoid cmake_utilities dependency issue
510

611
## v0.5.2 - 2023-09-15
712

8-
* Support work on older ESP-IDF, eg: 4.3.x
13+
* Support work on older ESP-IDF, eg: 4.3.x
914

1015
## v0.5.1 - 2023-08-22
1116

tools/cmake_utilities/Kconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ menu "CMake Utilities"
1414
"Enable this option to print error information instead of
1515
throwing exception when missing function"
1616

17+
config CU_RELINKER_LINK_SPECIFIC_FUNCTIONS_TO_IRAM
18+
bool "Link specific functions to IRAM regardless of ESP-IDF's behavior"
19+
default n
20+
depends on SPI_FLASH_AUTO_SUSPEND
21+
help
22+
"Enable this option, although functions' attribution
23+
are IRAM_ATTR, or configured to be noflash by link.lf, they will
24+
not be linked into IRAM. Only functions specified by relinker
25+
configuration file will be linked into IRAM."
26+
27+
1728
config CU_RELINKER_ENABLE_CUSTOMIZED_CONFIGURATION_FILES
1829
bool "Enable customized relinker configuration files"
1930
default n

tools/cmake_utilities/docs/relinker.md

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
In ESP-IDF, some functions are put in SRAM when link stage, the reason is that some functions are critical, we need to put them in SRAM to speed up the program, or the functions will be executed when the cache is disabled. But actually, some functions can be put into Flash, here, we provide a script to let the user set the functions which are located in SRAM by default to put them into Flash, in order to save more SRAM which can be used as heap region later. This happens in the linker stage, so we call it as relinker.
44

5+
Some SoCs(ESP32-C2 and ESP32-C3) support hardware auto flash-suspend, meaning when reading, writing, or erasing the flash stage, the CPU hardware can switch to run code laying in the flash automatically without any software assist, so that we can link more functions from IRAM to flash.
6+
57
## Use
68

79
In order to use this feature, you need to include the needed CMake file in your project's CMakeLists.txt after `project(XXXX)`.
@@ -14,28 +16,52 @@ include(relinker)
1416

1517
The relinker feature is disabled by default, in order to use it, you need to enable the option `CU_RELINKER_ENABLE` in menuconfig.
1618

17-
Here are the default configuration files in the folder `cmake_utilities/scripts/relinker/examples/esp32c2`, it's just used as a reference. If you would like to use your own configuration files, please enable option `CU_RELINKER_ENABLE_CUSTOMIZED_CONFIGURATION_FILES` and set the path of your configuration files as following, this path is evaluated relative to the project root directory:
19+
Here are the default configuration files in the folder `cmake_utilities/scripts/relinker/examples/flash_suspend/XXX (XXX=esp32c2, esp32c3...)` for specific SoC platform when flash_suspend and relinker feature are enabled, please note that the configuration files may be slightly different for different ESP-IDF versions, it's just used as a reference. If you would like to use your own configuration files, please enable option `CU_RELINKER_ENABLE_CUSTOMIZED_CONFIGURATION_FILES` and set the path of your configuration files as following, this path is evaluated relative to the project root directory:
1820

1921
```
2022
[*] Enable customized relinker configuration files
2123
(path of your configuration files) Customized relinker configuration files path
2224
```
2325

24-
> Note: Currently only esp32c2 is supported.
26+
If you would like to save more RAM, you can try flash-suspend feature, enable options as following:
27+
28+
```
29+
Component config --->
30+
SPI Flash driver --->
31+
[ ] Use esp_flash implementation in ROM
32+
[*] Auto suspend long erase/write operations (READ DOCS FIRST)
33+
34+
CMake Utilities --->
35+
[*] Enable relinker
36+
[*] Link specific functions to IRAM regardless of ESP-IDF's behavior
37+
```
38+
39+
> Note: Currently supported platform: esp32c2 and esp32c3.
40+
> Note: Before enable flash-suspend, please check if ESP-IDF supports this flash's flash-suspend feature.
2541
2642
## Configuration Files
2743

28-
You can refer to the files in the directory of `cmake_utilities/scripts/relinker/examples/esp32c2`:
44+
You can refer to the files in the directory of `cmake_utilities/scripts/relinker/examples/$ACTION/XXX ($ACTION=iram_strip, flash_suspend) (XXX=esp32c2, esp32c3...)`:
2945

3046
- library.csv
3147
- object.csv
3248
- function.csv
3349

34-
For example, if you want to link function `__getreent` from SRAM to Flash, firstly you should add it to `function.csv` file as following:
50+
1. iram_strip
3551

36-
```
37-
libfreertos.a,tasks.c.obj,__getreent,
38-
```
52+
For `example/iram_strip`, it includes the functions which you want to relink into Flash after enable the option `CU_RELINKER_ENABLE` and disable the option `CU_RELINKER_LINK_SPECIFIC_FUNCTIONS_TO_IRAM` in menuconfig. If you want to link function `__getreent` from SRAM to Flash, firstly you should add it to `function.csv` file as following:
53+
54+
```
55+
libfreertos.a,tasks.c.obj,__getreent,
56+
```
57+
58+
2. flash_suspend
59+
60+
For `example/flash_suspend`, it includes the functions which you want to relink into SRAM after enable the options: `SPI_FLASH_AUTO_SUSPEND`, `CU_RELINKER_ENABLE`, `CU_RELINKER_LINK_SPECIFIC_FUNCTIONS_TO_IRAM` in menuconfig. If you want to link function `__getreent` into SRAM no matter its original locations, firstly you should add it to `function.csv` file as following:
61+
62+
```
63+
libfreertos.a,tasks.c.obj,__getreent,
64+
```
3965
4066
This means function `__getreent` is in object file `tasks.c.obj`, and object file `tasks.c.obj` is in library `libfreertos.a`.
4167
@@ -63,4 +89,14 @@ libfreertos.a,./esp-idf/freertos/libfreertos.a
6389
6490
This means library `libfreertos.a`'s location is `./esp-idf/freertos/libfreertos.a` relative to `build`.
6591
66-
If above related data has exists in corresponding files, please don't add this repeatedly.
92+
If above related data has exists in corresponding files, please don't add this repeatedly.
93+
94+
## Test Results Reference
95+
96+
| Chip | example | | SRAM used (Bytes) | |
97+
| -------- | --------------- | ----------------- | ----------------- | --------------------------------- |
98+
| | | Default options | Enable relinker | Enable relinker and flash-suspend |
99+
| ESP32-C2 | power_save | 101408 | 91728 | 51360 |
100+
| ESP32-C3 | power_save | 118728 | 99864 | 58312 |
101+
102+
All of the above examples are compiled on ESP-IDF Tag/v5.3.2 and version v1.1.0 of `cmake_utilities` component, you can view the memory type usage summary by using `idf.py size`, the above data is for reference only.

tools/cmake_utilities/idf_component.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: "1.0.0"
1+
version: "1.1.0"
22
description: A collection of useful cmake utilities
33
url: https://github.com/espressif/esp-iot-solution/tree/master/tools/cmake_utilities
44
documentation: https://docs.espressif.com/projects/esp-iot-solution/zh_CN/latest/basic/cmake_utilities.html

tools/cmake_utilities/relinker.cmake

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@ if(CONFIG_CU_RELINKER_ENABLE)
55
message(STATUS "Relinker is enabled.")
66
if(CONFIG_IDF_TARGET_ESP32C2)
77
set(target "esp32c2")
8+
elseif(CONFIG_IDF_TARGET_ESP32C3)
9+
set(target "esp32c3")
810
else()
911
message(FATAL_ERROR "Other targets are not supported.")
1012
endif()
1113

14+
set(idf_version "${CONFIG_IDF_INIT_VERSION}")
15+
string(REGEX MATCH "^([0-9]+\\.[0-9]+)" version_prefix "${idf_version}")
16+
1217
if(CONFIG_CU_RELINKER_ENABLE_CUSTOMIZED_CONFIGURATION_FILES)
1318
idf_build_get_property(project_dir PROJECT_DIR)
1419
get_filename_component(cfg_file_path "${CONFIG_CU_RELINKER_CUSTOMIZED_CONFIGURATION_FILES_PATH}"
@@ -20,7 +25,15 @@ if(CONFIG_CU_RELINKER_ENABLE)
2025
else()
2126
set(cfg_file_path ${PROJECT_DIR}/relinker/${target})
2227
if(NOT EXISTS ${cfg_file_path})
23-
set(cfg_file_path ${CMAKE_CURRENT_LIST_DIR}/scripts/relinker/examples/${target})
28+
if(version_prefix STREQUAL "5.0" OR version_prefix STREQUAL "5.3")
29+
if(CONFIG_CU_RELINKER_LINK_SPECIFIC_FUNCTIONS_TO_IRAM)
30+
set(cfg_file_path ${CMAKE_CURRENT_LIST_DIR}/scripts/relinker/examples/flash_suspend/${target}/${version_prefix})
31+
else()
32+
set(cfg_file_path ${CMAKE_CURRENT_LIST_DIR}/scripts/relinker/examples/iram_strip/${target}/${version_prefix})
33+
endif()
34+
else()
35+
message(FATAL_ERROR "There is no configuration file corresponding to esp-idf/v${version_prefix}, please make a request in github issue")
36+
endif()
2437
endif()
2538
endif()
2639

@@ -37,33 +50,39 @@ if(CONFIG_CU_RELINKER_ENABLE)
3750
set(link_dst_file "${link_path}/customer_sections.ld")
3851

3952
set(relinker_opts --input ${link_src_file}
40-
--output ${link_dst_file}
41-
--library ${library_file}
42-
--object ${object_file}
43-
--function ${function_file}
44-
--sdkconfig ${sdkconfig}
45-
--objdump ${cmake_objdump})
53+
--output ${link_dst_file}
54+
--library ${library_file}
55+
--object ${object_file}
56+
--function ${function_file}
57+
--sdkconfig ${sdkconfig}
58+
--target ${target}
59+
--version ${version_prefix}
60+
--objdump ${cmake_objdump})
4661

4762
if(CONFIG_CU_RELINKER_ENABLE_PRINT_ERROR_INFO_WHEN_MISSING_FUNCTION)
48-
list(APPEND relinker_opts --missing_function_info True)
63+
list(APPEND relinker_opts --missing_function_info)
64+
endif()
65+
66+
if(CONFIG_CU_RELINKER_LINK_SPECIFIC_FUNCTIONS_TO_IRAM)
67+
list(APPEND relinker_opts --link_to_iram)
4968
endif()
5069

5170
idf_build_get_property(link_depends __LINK_DEPENDS)
5271

53-
add_custom_command(OUTPUT ${link_dst_file}
54-
COMMAND ${python} -B ${relinker_script}
55-
${relinker_opts}
56-
COMMAND ${CMAKE_COMMAND} -E copy
57-
${link_dst_file}
58-
${link_src_file}
59-
COMMAND ${CMAKE_COMMAND} -E echo
60-
/*relinker*/ >>
61-
${link_dst_file}
62-
DEPENDS "${link_depends}"
63-
"${library_file}"
64-
"${object_file}"
65-
"${function_file}"
66-
VERBATIM)
72+
add_custom_command(OUTPUT ${link_dst_file}
73+
COMMAND ${python} -B ${relinker_script}
74+
${relinker_opts}
75+
COMMAND ${CMAKE_COMMAND} -E copy
76+
${link_dst_file}
77+
${link_src_file}
78+
COMMAND ${CMAKE_COMMAND} -E echo
79+
/*relinker*/ >>
80+
${link_dst_file}
81+
DEPENDS "${link_depends}"
82+
"${library_file}"
83+
"${object_file}"
84+
"${function_file}"
85+
VERBATIM)
6786

6887
add_custom_target(customer_sections DEPENDS ${link_dst_file})
6988
add_dependencies(${project_elf} customer_sections)

0 commit comments

Comments
 (0)