IRAM 优化
优化措施
1 IRAM 优化方法:
-
简单方法,既可以优化 IRAM 有可以优化 flash 。推荐使用该方法。
make menuconfig-->Compiler options → Optimization Level -->(X) Optimize for size (-Os)
方法原理:修改 GCC 配置,可以有效调整编译后的固件大小。
2 第二个方法修改 ld 文件与配置文件:
components/esp32/ld/esp32.ld
文件修改,IRAM 增加 8K:
/* IRAM for PRO cpu. Not sure if happy with this, this is MMU area... */
iram0_0_seg (RX) : org = 0x40080000, len = 0x20000
修改为:
iram0_0_seg (RX) : org = 0x40080000, len = 0x22000
components/soc/esp32/include/soc/soc.h
文件修改:
-
IRAM 增加 8K:
#define SOC_IRAM_HIGH 0x400A0000
修改为:
#define SOC_IRAM_HIGH 0x400A2000
-
DRAM 减少 8K:
#define SOC_DIRAM_IRAM_LOW 0x400A0000
修改为:
#define SOC_DIRAM_IRAM_LOW 0x400A2000
测试 demo
/* Hello World Example
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include <stdio.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_spi_flash.h"
#ifdef CONFIG_IDF_TARGET_ESP32
#define CHIP_NAME "ESP32"
#endif
#ifdef CONFIG_IDF_TARGET_ESP32S2BETA
#define CHIP_NAME "ESP32-S2 Beta"
#endif
#define data_len 23000
IRAM_ATTR int test_data[data_len] = {0};
void app_main(void)
{
printf("Hello world!\n");
/* Print chip information */
esp_chip_info_t chip_info;
esp_chip_info(&chip_info);
printf("This is %s chip with %d CPU cores, WiFi%s%s, ",
CHIP_NAME,
chip_info.cores,
(chip_info.features & CHIP_FEATURE_BT) ? "/BT" : "",
(chip_info.features & CHIP_FEATURE_BLE) ? "/BLE" : "");
printf("silicon revision %d\n ", chip_info.revision);
printf("MALLOC_CAP_EXEC %x\n", heap_caps_get_free_size( MALLOC_CAP_EXEC ));
printf("MALLOC_CAP_32BIT %x\n", heap_caps_get_free_size( MALLOC_CAP_32BIT ));
printf("MALLOC_CAP_INTERNAL %x\n", heap_caps_get_free_size( MALLOC_CAP_INTERNAL ));
printf("MALLOC_CAP_DEFAULT %x\n", heap_caps_get_free_size( MALLOC_CAP_DEFAULT ));
for(int i = 0; i< data_len; i++){
test_data[i] = data_len;
}
printf("%dMB %s flash\n", spi_flash_get_chip_size() / (1024 * 1024),
(chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external");
for (int i = 10; i >= 0; i--) {
printf("Restarting in %d seconds...\n", i);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
printf("Restarting now.\n");
fflush(stdout);
esp_restart();
}