From 974112378b5bd8e1dee12a5a09d00a426d2e1980 Mon Sep 17 00:00:00 2001 From: TianZhongXing Date: Fri, 14 Sep 2018 18:28:18 +0800 Subject: [PATCH] feature: allow .bss segment in external memory --- components/bt/CMakeLists.txt | 1 - components/esp32/CMakeLists.txt | 12 ++++++++++-- components/esp32/Kconfig | 8 ++++++++ components/esp32/component.mk | 13 ++++++++++--- components/esp32/cpu_start.c | 13 ++++++++++++- components/esp32/include/esp_attr.h | 7 +++++++ components/esp32/ld/esp32.common.ld | 16 ++++++++++++++++ components/esp32/ld/esp32.ld | 4 ++++ components/esp32/spiram.c | 13 ++++++++++--- components/lwip/apps/dhcpserver/dhcpserver.c | 18 ++++++++++++++---- 10 files changed, 91 insertions(+), 14 deletions(-) diff --git a/components/bt/CMakeLists.txt b/components/bt/CMakeLists.txt index df6fa250d4..1bcea86c4c 100644 --- a/components/bt/CMakeLists.txt +++ b/components/bt/CMakeLists.txt @@ -154,7 +154,6 @@ if(CONFIG_BT_ENABLED) "bluedroid/external/sbc/encoder/srce/sbc_enc_coeffs.c" "bluedroid/external/sbc/encoder/srce/sbc_encoder.c" "bluedroid/external/sbc/encoder/srce/sbc_packing.c" - "bluedroid/hci/buffer_allocator.c" "bluedroid/hci/hci_audio.c" "bluedroid/hci/hci_hal_h4.c" "bluedroid/hci/hci_layer.c" diff --git a/components/esp32/CMakeLists.txt b/components/esp32/CMakeLists.txt index 8577dc4340..e0ae5af7fd 100644 --- a/components/esp32/CMakeLists.txt +++ b/components/esp32/CMakeLists.txt @@ -74,9 +74,9 @@ else() target_link_libraries(esp32 coexist core espnow mesh net80211 phy pp rtc smartconfig wpa2 wpa wps) endif() target_linker_script(esp32 "${CMAKE_CURRENT_BINARY_DIR}/esp32_out.ld") + target_linker_script(esp32 "${CMAKE_CURRENT_BINARY_DIR}/esp32.common_out.ld") target_linker_script(esp32 - "ld/esp32.common.ld" "ld/esp32.rom.ld" "ld/esp32.peripherals.ld" "ld/esp32.rom.libgcc.ld" @@ -113,7 +113,15 @@ else() MAIN_DEPENDENCY ${LD_DIR}/esp32.ld ${SDKCONFIG_H} COMMENT "Generating linker script..." VERBATIM) - add_custom_target(esp32_linker_script DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/esp32_out.ld) + + # Preprocess esp32.common.ld linker script to include configuration, becomes esp32.common_out.ld + add_custom_command( + OUTPUT esp32.common_out.ld + COMMAND "${CMAKE_C_COMPILER}" -C -P -x c -E -o esp32.common_out.ld -I ${CONFIG_DIR} ${LD_DIR}/esp32.common.ld + MAIN_DEPENDENCY ${LD_DIR}/esp32.common.ld ${SDKCONFIG_H} + COMMENT "Generating linker script..." + VERBATIM) + add_custom_target(esp32_linker_script DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/esp32_out.ld ${CMAKE_CURRENT_BINARY_DIR}/esp32.common_out.ld) add_dependencies(esp32 esp32_linker_script) if(CONFIG_ESP32_PHY_INIT_DATA_IN_PARTITION) diff --git a/components/esp32/Kconfig b/components/esp32/Kconfig index aa7415f57c..a5be995c35 100644 --- a/components/esp32/Kconfig +++ b/components/esp32/Kconfig @@ -172,6 +172,14 @@ config SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY ROM code in any way (no direct calls, but also no Bluetooth/WiFi), you can try to disable this and use xTaskCreateStatic to create the tasks stack in external memory. +config SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY + bool "Allow .bss segment placed in external memory" + default n + depends on SPIRAM_SUPPORT + help + If enabled the option,and add EXT_RAM_ATTR defined your variable,then your variable will be placed + in PSRAM instead of internal memory. + endmenu config MEMMAP_TRACEMEM diff --git a/components/esp32/component.mk b/components/esp32/component.mk index 6c03587a36..9900213b68 100644 --- a/components/esp32/component.mk +++ b/components/esp32/component.mk @@ -11,7 +11,8 @@ endif #Linker scripts used to link the final application. #Warning: These linker scripts are only used when the normal app is compiled; the bootloader #specifies its own scripts. -LINKER_SCRIPTS += esp32.common.ld esp32.rom.ld esp32.peripherals.ld + +LINKER_SCRIPTS += esp32.rom.ld esp32.peripherals.ld #Force pure functions from libgcc.a to be linked from ROM LINKER_SCRIPTS += esp32.rom.libgcc.ld @@ -38,6 +39,7 @@ COMPONENT_ADD_LDFLAGS += $(COMPONENT_PATH)/libhal.a \ $(addprefix -l,$(LIBS)) \ -L $(COMPONENT_PATH)/ld \ -T esp32_out.ld \ + -T esp32.common_out.ld \ -u ld_include_panic_highint_hdl \ $(addprefix -T ,$(LINKER_SCRIPTS)) @@ -53,12 +55,17 @@ COMPONENT_ADD_LINKER_DEPS := $(ALL_LIB_FILES) $(addprefix ld/,$(LINKER_SCRIPTS)) # # The library doesn't really depend on esp32_out.ld, but it # saves us from having to add the target to a Makefile.projbuild -$(COMPONENT_LIBRARY): esp32_out.ld +$(COMPONENT_LIBRARY): esp32_out.ld esp32.common_out.ld esp32_out.ld: $(COMPONENT_PATH)/ld/esp32.ld ../include/sdkconfig.h $(CC) -I ../include -C -P -x c -E $< -o $@ -COMPONENT_EXTRA_CLEAN := esp32_out.ld +# Preprocess esp32.common.ld linker script to include configuration, becomes esp32.common_out.ld +esp32.common_out.ld: $(COMPONENT_PATH)/ld/esp32.common.ld ../include/sdkconfig.h + $(CC) -I ../include -C -P -x c -E $< -o $@ + +COMPONENT_EXTRA_CLEAN := esp32_out.ld +COMPONENT_EXTRA_CLEAN += esp32.common_out.ld # disable stack protection in files which are involved in initialization of that feature stack_check.o: CFLAGS := $(filter-out -fstack-protector%, $(CFLAGS)) diff --git a/components/esp32/cpu_start.c b/components/esp32/cpu_start.c index cfff63a2c5..6475c728f9 100644 --- a/components/esp32/cpu_start.c +++ b/components/esp32/cpu_start.c @@ -92,6 +92,10 @@ extern int _bss_start; extern int _bss_end; extern int _rtc_bss_start; extern int _rtc_bss_end; +#if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY +extern int _ext_ram_bss_start; +extern int _ext_ram_bss_end; +#endif extern int _init_start; extern void (*__init_array_start)(void); extern void (*__init_array_end)(void); @@ -153,6 +157,11 @@ void IRAM_ATTR call_start_cpu0() #if CONFIG_SPIRAM_BOOT_INIT esp_spiram_init_cache(); if (esp_spiram_init() != ESP_OK) { +#if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY + ESP_EARLY_LOGE(TAG, "Failed to init external RAM and place some block started symbol in it"); + abort(); +#endif + #if CONFIG_SPIRAM_IGNORE_NOTFOUND ESP_EARLY_LOGI(TAG, "Failed to init external RAM; continuing without it."); s_spiram_okay = false; @@ -201,7 +210,9 @@ void IRAM_ATTR call_start_cpu0() } } #endif - +#if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY + memset(&_ext_ram_bss_start, 0, (&_ext_ram_bss_end - &_ext_ram_bss_start) * sizeof(_ext_ram_bss_start)); +#endif /* Initialize heap allocator. WARNING: This *needs* to happen *after* the app cpu has booted. If the heap allocator is initialized first, it will put free memory linked list items into memory also used by the ROM. Starting the app cpu will let its ROM initialize that memory, diff --git a/components/esp32/include/esp_attr.h b/components/esp32/include/esp_attr.h index 5bf9a22926..6d6979b2ac 100644 --- a/components/esp32/include/esp_attr.h +++ b/components/esp32/include/esp_attr.h @@ -39,6 +39,13 @@ // Forces code into RTC fast memory. See "docs/deep-sleep-stub.rst" #define RTC_IRAM_ATTR __attribute__((section(".rtc.text"))) +#if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY +// Forces bss variable into external memory. " +#define EXT_RAM_ATTR __attribute__((section(".ext_ram.bss"))) +#else +#define EXT_RAM_ATTR +#endif + // Forces data into RTC slow memory. See "docs/deep-sleep-stub.rst" // Any variable marked with this attribute will keep its value // during a deep sleep / wake cycle. diff --git a/components/esp32/ld/esp32.common.ld b/components/esp32/ld/esp32.common.ld index cf1a3a7a9e..deffd3a2bc 100644 --- a/components/esp32/ld/esp32.common.ld +++ b/components/esp32/ld/esp32.common.ld @@ -1,3 +1,5 @@ +#include "sdkconfig.h" + /* Default entry point: */ ENTRY(call_start_cpu0); @@ -50,6 +52,19 @@ SECTIONS _rtc_noinit_end = ABSOLUTE(.); } > rtc_slow_seg +#ifdef CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY + /* external memory bss, from any global variable with EXT_RAM_ATTR attribute*/ + .ext_ram.bss (NOLOAD) : + { + _ext_ram_bss_start = ABSOLUTE(.); + *(.ext_ram.bss) + *libnet80211.a:(.dynsbss .sbss .sbss.* .gnu.linkonce.sb.* .scommon .sbss2.* .gnu.linkonce.sb2.* .dynbss .bss .bss.* .share.mem .gnu.linkonce.b.* COMMON) + *libpp.a:(.dynsbss .sbss .sbss.* .gnu.linkonce.sb.* .scommon .sbss2.* .gnu.linkonce.sb2.* .dynbss .bss .bss.* .share.mem .gnu.linkonce.b.* COMMON) + *libbt.a:(EXCLUDE_FILE (libbtdm_app.a) .dynsbss .sbss .sbss.* .gnu.linkonce.sb.* .scommon .sbss2.* .gnu.linkonce.sb2.* .dynbss .bss .bss.* .share.mem .gnu.linkonce.b.* COMMON) + _ext_ram_bss_end = ABSOLUTE(.); + } > extern_ram_seg +#endif + /* Send .iram0 code to iram */ .iram0.vectors : { @@ -167,6 +182,7 @@ SECTIONS { . = ALIGN (8); _bss_start = ABSOLUTE(.); + *(.ext_ram.bss) _bt_bss_start = ABSOLUTE(.); *libbt.a:(.bss .bss.* COMMON) . = ALIGN (4); diff --git a/components/esp32/ld/esp32.ld b/components/esp32/ld/esp32.ld index ee41f74793..dcca0bc685 100644 --- a/components/esp32/ld/esp32.ld +++ b/components/esp32/ld/esp32.ld @@ -68,6 +68,10 @@ MEMORY */ rtc_slow_seg(RW) : org = 0x50000000 + CONFIG_ULP_COPROC_RESERVE_MEM, len = 0x1000 - CONFIG_ULP_COPROC_RESERVE_MEM + + /* external memory ,including data and text */ + extern_ram_seg(RWX) : org = 0x3F800000, + len = 0x400000 } /* Heap ends at top of dram0_0_seg */ diff --git a/components/esp32/spiram.c b/components/esp32/spiram.c index 98effb1279..cee8129cf2 100644 --- a/components/esp32/spiram.c +++ b/components/esp32/spiram.c @@ -59,7 +59,9 @@ static const char* TAG = "spiram"; #error "FLASH speed can only be equal to or higher than SRAM speed while SRAM is enabled!" #endif - +#if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY +extern int _ext_ram_bss_start, _ext_ram_bss_end; +#endif static bool spiram_inited=false; @@ -162,11 +164,16 @@ esp_err_t esp_spiram_init() esp_err_t esp_spiram_add_to_heapalloc() -{ - ESP_EARLY_LOGI(TAG, "Adding pool of %dK of external SPI memory to heap allocator", CONFIG_SPIRAM_SIZE/1024); +{ //Add entire external RAM region to heap allocator. Heap allocator knows the capabilities of this type of memory, so there's //no need to explicitly specify them. +#if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY + ESP_EARLY_LOGI(TAG, "Adding pool of %dK of external SPI memory to heap allocator", (CONFIG_SPIRAM_SIZE - (&_ext_ram_bss_end - &_ext_ram_bss_start))/1024); + return heap_caps_add_region((intptr_t)&_ext_ram_bss_end, (intptr_t)SOC_EXTRAM_DATA_LOW + CONFIG_SPIRAM_SIZE-1); +#else + ESP_EARLY_LOGI(TAG, "Adding pool of %dK of external SPI memory to heap allocator", CONFIG_SPIRAM_SIZE/1024); return heap_caps_add_region((intptr_t)SOC_EXTRAM_DATA_LOW, (intptr_t)SOC_EXTRAM_DATA_LOW + CONFIG_SPIRAM_SIZE-1); +#endif } diff --git a/components/lwip/apps/dhcpserver/dhcpserver.c b/components/lwip/apps/dhcpserver/dhcpserver.c index 63f01a6854..7812f7425a 100644 --- a/components/lwip/apps/dhcpserver/dhcpserver.c +++ b/components/lwip/apps/dhcpserver/dhcpserver.c @@ -81,21 +81,31 @@ typedef struct _list_node { static const u32_t magic_cookie = 0x63538263; +#if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY +static struct udp_pcb EXT_RAM_ATTR *pcb_dhcps; +static ip4_addr_t EXT_RAM_ATTR broadcast_dhcps; +static ip4_addr_t EXT_RAM_ATTR server_address; +static ip4_addr_t EXT_RAM_ATTR dns_server; +static ip4_addr_t EXT_RAM_ATTR client_address; //added +static ip4_addr_t EXT_RAM_ATTR client_address_plus; +static dhcps_lease_t EXT_RAM_ATTR dhcps_poll; +static dhcps_offer_t EXT_RAM_ATTR dhcps_dns; +#else static struct udp_pcb *pcb_dhcps = NULL; static ip4_addr_t broadcast_dhcps; static ip4_addr_t server_address; static ip4_addr_t dns_server = {0}; static ip4_addr_t client_address; //added static ip4_addr_t client_address_plus; - +static dhcps_lease_t dhcps_poll; +static dhcps_offer_t dhcps_dns = 0x00; +#endif +static dhcps_cb_t dhcps_cb; static list_node *plist = NULL; static bool renew = false; -static dhcps_lease_t dhcps_poll; static dhcps_time_t dhcps_lease_time = DHCPS_LEASE_TIME_DEF; //minute static dhcps_offer_t dhcps_offer = 0xFF; -static dhcps_offer_t dhcps_dns = 0x00; -static dhcps_cb_t dhcps_cb; /****************************************************************************** * FunctionName : dhcps_option_info