diff --git a/src/link/at32_flash_f43xg.ld b/src/link/at32_flash_f43xg.ld index 1c93ed239b..bb54e8c3f6 100644 --- a/src/link/at32_flash_f43xg.ld +++ b/src/link/at32_flash_f43xg.ld @@ -23,13 +23,15 @@ MEM : 0x2000 0000 -- 0x2007 FFFF */ +_SRAM_SIZE = 192; + MEMORY { FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 16K FLASH_CONFIG (r) : ORIGIN = 0x08004000, LENGTH = 16K FLASH1 (rx) : ORIGIN = 0x08008000, LENGTH = 992K SYSTEM_MEMORY (rx) : ORIGIN = 0x1FFF0000, LENGTH = 16K - RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 192K + RAM (xrw) : ORIGIN = 0x20000000, LENGTH = _SRAM_SIZE * 1024 MEMORY_B1 (rx) : ORIGIN = 0x60000000, LENGTH = 0K /* external ram */ } diff --git a/src/link/at32_flash_f43xm.ld b/src/link/at32_flash_f43xm.ld index 90d88e282e..c8019596ed 100644 --- a/src/link/at32_flash_f43xm.ld +++ b/src/link/at32_flash_f43xm.ld @@ -23,13 +23,15 @@ MEM : 0x2000 0000 -- 0x2007 FFFF */ +_SRAM_SIZE = 192; + MEMORY { FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 16K FLASH_CONFIG (r) : ORIGIN = 0x08004000, LENGTH = 16K FLASH1 (rx) : ORIGIN = 0x08008000, LENGTH = 4000K SYSTEM_MEMORY (rx) : ORIGIN = 0x1FFF0000, LENGTH = 16K - RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 192K + RAM (xrw) : ORIGIN = 0x20000000, LENGTH = _SRAM_SIZE * 1024 MEMORY_B1 (rx) : ORIGIN = 0x60000000, LENGTH = 0K /* external ram */ } diff --git a/src/main/drivers/at32/system_at32f43x.c b/src/main/drivers/at32/system_at32f43x.c index 70ee28a837..35188ae848 100644 --- a/src/main/drivers/at32/system_at32f43x.c +++ b/src/main/drivers/at32/system_at32f43x.c @@ -29,6 +29,71 @@ #include "drivers/persistent.h" #include "at32f435_437_clock.h" +// See RM_AT32F435_437_EN_V2.05.pdf reference manual table 5-6 for more info. +#if 256 < TARGET_FLASH_SIZE +#define USD_EOPB0_SRAM_CONFIG_MASK 0x7 +#else +#define USD_EOPB0_SRAM_CONFIG_MASK 0x3 +#endif + +static flash_usd_eopb0_type get_sram_config(void) +{ + extern uint32_t _SRAM_SIZE; // Defined in linker file + switch ((uint32_t)&_SRAM_SIZE) { +#if 256 == TARGET_FLASH_SIZE + case 448: + return FLASH_EOPB0_SRAM_448K; + case 512: + return FLASH_EOPB0_SRAM_512K; + case 384: + default: + return FLASH_EOPB0_SRAM_384K; +#elif 448 == TARGET_FLASH_SIZE + case 256: + return FLASH_EOPB0_SRAM_256K; + case 320: + return FLASH_EOPB0_SRAM_320K; + case 384: + return FLASH_EOPB0_SRAM_384K; + case 448: + return FLASH_EOPB0_SRAM_448K; + case 512: + return FLASH_EOPB0_SRAM_512K; + case 192: + default: + return FLASH_EOPB0_SRAM_192K; +#elif 1024 <= TARGET_FLASH_SIZE + case 128: + return FLASH_EOPB0_SRAM_128K; + case 256: + return FLASH_EOPB0_SRAM_256K; + case 320: + return FLASH_EOPB0_SRAM_320K; + case 384: + return FLASH_EOPB0_SRAM_384K; + case 448: + return FLASH_EOPB0_SRAM_448K; + case 512: + return FLASH_EOPB0_SRAM_512K; + case 192: + default: + return FLASH_EOPB0_SRAM_192K; +#endif + } +} + +static void init_sram_config(void) +{ + // Make sure the SRAM config is correct + const flash_usd_eopb0_type sram_cfg = get_sram_config(); + if (((USD->eopb0) & USD_EOPB0_SRAM_CONFIG_MASK) != sram_cfg) { + flash_unlock(); + flash_user_system_data_erase(); + flash_eopb0_config(sram_cfg); + systemReset(); + } +} + void systemReset(void) { __disable_irq(); @@ -96,6 +161,8 @@ bool isMPUSoftReset(void) void systemInit(void) { + init_sram_config(); + persistentObjectInit(); checkForBootLoaderRequest();