Skip to content

Commit

Permalink
soc: xtensa: espressif_esp32: add SPIRAM heap offset and size
Browse files Browse the repository at this point in the history
The existing heap starts at the beginning of SPIRAM which is also occupied
by the WiFi stack if CONFIG_ESP32_WIFI_NET_ALLOC_SPIRAM is enabled.  This
results in memory corruption.

Update the linker description to add a _spiram_heap_start symbol and add a
KConfig size option to allow the SPIRAM stack to be correctly located and
sized.  Requires matching commit in hal_espressif.

Fixes: 61359

Signed-off-by: Eric Holmberg <eric.holmberg@northriversystems.co.nz>
  • Loading branch information
EricNRS authored and carlescufi committed Aug 22, 2023
1 parent d9fe63b commit 526ff04
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 7 deletions.
8 changes: 8 additions & 0 deletions soc/xtensa/espressif_esp32/common/Kconfig.soc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ config ESP_HEAP_SEARCH_ALL_REGIONS
regions. If the region of desired capability is exhausted,
memory will be allocated from other available region.

config ESP_SPIRAM_HEAP_SIZE
int "Size of SPIRAM heap"
default 262134 if SYS_HEAP_SMALL_ONLY
default 1048576 if !SYS_HEAP_SMALL_ONLY
depends on ESP_SPIRAM
help
Specify size of SPIRAM heap.

menu "SPI RAM config"
depends on ESP_SPIRAM

Expand Down
10 changes: 9 additions & 1 deletion soc/xtensa/espressif_esp32/esp32/default.ld
Original file line number Diff line number Diff line change
Expand Up @@ -453,9 +453,12 @@ SECTIONS
*libsubsys__net__ip.a:(.noinit .noinit.*)
*libsubsys__net.a:(.noinit .noinit.*)
#endif
_spiram_heap_start = ABSOLUTE(.);
. = . + CONFIG_ESP_SPIRAM_HEAP_SIZE;

*(.ext_ram.bss*)
_ext_ram_data_end = ABSOLUTE(.) + CONFIG_ESP_SPIRAM_SIZE;

_ext_ram_data_end = ABSOLUTE(.);
} GROUP_LINK_IN(ext_ram_seg)
#endif

Expand Down Expand Up @@ -581,3 +584,8 @@ _heap_sentry = 0x3ffe3f20;

ASSERT(((_iram_end - ORIGIN(iram0_0_seg)) <= LENGTH(iram0_0_seg)),
"IRAM0 segment data does not fit.")

#if defined(CONFIG_ESP_SPIRAM)
ASSERT(((_ext_ram_data_end - _ext_ram_data_start) <= CONFIG_ESP_SPIRAM_SIZE),
"External SPIRAM overflowed.")
#endif /* CONFIG_ESP_SPIRAM */
12 changes: 11 additions & 1 deletion soc/xtensa/espressif_esp32/esp32s2/default.ld
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ SECTIONS
#if defined(CONFIG_ESP_SPIRAM)
.ext_ram.bss (NOLOAD):
{
_ext_ram_data_start = ABSOLUTE(.);
_ext_ram_bss_start = ABSOLUTE(.);

#if defined(CONFIG_ESP32_WIFI_NET_ALLOC_SPIRAM)
Expand All @@ -342,10 +343,14 @@ SECTIONS
*libsubsys__net__ip.a:(.noinit .noinit.*)
*libsubsys__net.a:(.noinit .noinit.*)
#endif
_spiram_heap_start = ABSOLUTE(.);
. = . + CONFIG_ESP_SPIRAM_HEAP_SIZE;

*(.ext_ram.bss*)
. = ALIGN(4);
_ext_ram_bss_end = ABSOLUTE(.) + CONFIG_ESP_SPIRAM_SIZE;
_ext_ram_bss_end = ABSOLUTE(.);

_ext_ram_data_end = ABSOLUTE(.);
} > ext_ram_seg
#endif

Expand Down Expand Up @@ -555,3 +560,8 @@ ASSERT(((__bss_end - ORIGIN(dram0_0_seg)) <= LENGTH(dram0_0_seg)),

ASSERT(((_iram_end - ORIGIN(iram0_0_seg)) <= LENGTH(iram0_0_seg)),
"IRAM0 segment data does not fit.")

#if defined(CONFIG_ESP_SPIRAM)
ASSERT(((_ext_ram_data_end - _ext_ram_data_start) <= CONFIG_ESP_SPIRAM_SIZE),
"External SPIRAM overflowed.")
#endif /* CONFIG_ESP_SPIRAM */
11 changes: 8 additions & 3 deletions soc/xtensa/espressif_esp32/esp32s3/default.ld
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ SECTIONS
/* This section holds .ext_ram.bss data, and will be put in PSRAM */
.ext_ram.bss (NOLOAD) :
{
_ext_ram_data_start = ABSOLUTE(.);
_ext_ram_bss_start = ABSOLUTE(.);
*(.ext_ram.bss*)
. = ALIGN(4);
Expand All @@ -246,6 +247,10 @@ SECTIONS
*libsubsys__net__ip.a:(.noinit .noinit.*)
*libsubsys__net.a:(.noinit .noinit.*)
#endif
_spiram_heap_start = ABSOLUTE(.);
. = . + CONFIG_ESP_SPIRAM_HEAP_SIZE;

_ext_ram_data_end = ABSOLUTE(.);
} > ext_ram_seg
#endif

Expand Down Expand Up @@ -678,6 +683,6 @@ ASSERT(((_iram_end - ORIGIN(iram0_0_seg)) <= LENGTH(iram0_0_seg)),
"IRAM0 segment data does not fit.")

#if defined(CONFIG_ESP_SPIRAM)
ASSERT(((ORIGIN(ext_ram_seg)) > _image_rodata_end),
"External RAM segment does not fit.")
#endif
ASSERT(((_ext_ram_data_end - _ext_ram_data_start) <= CONFIG_ESP_SPIRAM_SIZE),
"External SPIRAM overflowed.")
#endif /* CONFIG_ESP_SPIRAM */
1 change: 0 additions & 1 deletion tests/boards/espressif_esp32/cache_coex/prj.conf
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
CONFIG_ESP_SPIRAM=y
CONFIG_ESP_HEAP_MIN_EXTRAM_THRESHOLD=2048
CONFIG_HEAP_MEM_POOL_SIZE=98304
CONFIG_ESP_HEAP_MEM_POOL_REGION_1_SIZE=100000
CONFIG_ESP_HEAP_SEARCH_ALL_REGIONS=n
CONFIG_FLASH=y
CONFIG_ENTROPY_GENERATOR=y
Expand Down
2 changes: 1 addition & 1 deletion west.yml
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ manifest:
groups:
- hal
- name: hal_espressif
revision: ec90f3c49e3826ef2c56c5381aa068c4f482b31f
revision: 80d910ca89eab9bce03f59a4ade33f1fc30ce0ad
path: modules/hal/espressif
west-commands: west/west-commands.yml
groups:
Expand Down

0 comments on commit 526ff04

Please sign in to comment.