diff --git a/src/main/drivers/system_at32f43x.c b/src/main/drivers/system_at32f43x.c index 340058e183b..eebd27ec78f 100644 --- a/src/main/drivers/system_at32f43x.c +++ b/src/main/drivers/system_at32f43x.c @@ -28,6 +28,79 @@ #include "target/system.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(); +// NVIC_SystemReset(); +// } + + + void SetSysClock(void); void enableGPIOPowerUsageAndNoiseReductions(void) @@ -116,6 +189,7 @@ uint32_t systemBootloaderAddress(void) void systemInit(void) { + init_sram_config(); //config system clock to 288mhz usb 48mhz system_clock_config(); // Configure NVIC preempt/priority groups diff --git a/src/main/target/BLUEBERRYF435WING/target.h b/src/main/target/BLUEBERRYF435WING/target.h index 1dbfedf8a6b..61590ea6d16 100644 --- a/src/main/target/BLUEBERRYF435WING/target.h +++ b/src/main/target/BLUEBERRYF435WING/target.h @@ -38,7 +38,7 @@ * PH0 HEXT IN * PH1 HEXT OUT */ - +#define TARGET_FLASH_SIZE 1024 #define LED0 PC13 #define LED1 PC14 #define LED0_INVERTED diff --git a/src/main/target/link/at32_flash_f43xG.ld b/src/main/target/link/at32_flash_f43xG.ld index 7d43eae4e35..51bd26ec7a6 100644 --- a/src/main/target/link/at32_flash_f43xG.ld +++ b/src/main/target/link/at32_flash_f43xG.ld @@ -25,7 +25,7 @@ */ - +_SRAM_SIZE = 192; MEMORY { diff --git a/src/main/target/link/at32_flash_f43xM.ld b/src/main/target/link/at32_flash_f43xM.ld index 2c8d11d0ba5..2aac413cdad 100644 --- a/src/main/target/link/at32_flash_f43xM.ld +++ b/src/main/target/link/at32_flash_f43xM.ld @@ -25,7 +25,7 @@ */ - +_SRAM_SIZE = 192; MEMORY { diff --git a/src/main/target/link/at32_flash_f43xM_bl.ld b/src/main/target/link/at32_flash_f43xM_bl.ld index f0be9d4a56f..7008306d5e6 100644 --- a/src/main/target/link/at32_flash_f43xM_bl.ld +++ b/src/main/target/link/at32_flash_f43xM_bl.ld @@ -25,7 +25,7 @@ */ - +_SRAM_SIZE = 192; MEMORY { diff --git a/src/main/target/link/at32_flash_f43xM_for_bl.ld b/src/main/target/link/at32_flash_f43xM_for_bl.ld index 1f83b7f38fe..cfadd3db16f 100644 --- a/src/main/target/link/at32_flash_f43xM_for_bl.ld +++ b/src/main/target/link/at32_flash_f43xM_for_bl.ld @@ -25,7 +25,7 @@ */ - +_SRAM_SIZE = 192; MEMORY {