ESP32 優化筆記(四)IRAM 優化

IRAM 優化

優化措施

1 IRAM 優化方法:

  1. 簡單方法,既可以優化 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 文件修改:

  1. IRAM 增加 8K:

    #define SOC_IRAM_HIGH           0x400A0000
    

    修改爲:

    #define SOC_IRAM_HIGH           0x400A2000
    
  2. 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();
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章