From 3b4fe9a40ea50764c57274a82a70c8f239a2d1f7 Mon Sep 17 00:00:00 2001 From: aineoae86-sys Date: Sun, 5 Jul 2026 19:49:58 +0800 Subject: [PATCH] fix: avoid repeated SDRAM allocator setup Track the SDRAM malloc block after it is registered so begin() does not add the same global allocator region again. Repeating the same start address is treated as an idempotent call, while changing it after allocator setup fails. Fixes #1105 Generated-by: OpenAI Codex Signed-off-by: aineoae86-sys --- libraries/Portenta_SDRAM/src/SDRAM.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/libraries/Portenta_SDRAM/src/SDRAM.cpp b/libraries/Portenta_SDRAM/src/SDRAM.cpp index aff4dad88..8f84b5265 100644 --- a/libraries/Portenta_SDRAM/src/SDRAM.cpp +++ b/libraries/Portenta_SDRAM/src/SDRAM.cpp @@ -29,6 +29,7 @@ static void MPU_Config() { } int SDRAMClass::begin(uint32_t start_address) { + static uint32_t malloc_start_address = 0; if (FMC_SDRAM_DEVICE->SDCMR == 0x00000000U) { bool ret = sdram_init(); @@ -51,7 +52,13 @@ int SDRAMClass::begin(uint32_t start_address) { } if (start_address) { - malloc_addblock((void*)start_address, SDRAM_END_ADDRESS - start_address); + if (malloc_start_address && malloc_start_address != start_address) { + return 0; + } + if (!malloc_start_address) { + malloc_addblock((void*)start_address, SDRAM_END_ADDRESS - start_address); + malloc_start_address = start_address; + } } return 1;