10000 stm32/sdram: Update MPU settings to block invalid region, change attrs. · guidebee/micropython@1470184 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1470184

Browse files
pi-anldpgeorge
authored andcommitted
stm32/sdram: Update MPU settings to block invalid region, change attrs.
Set the active MPU region to the actual size of SDRAM configured and invalidate the rest of the memory-mapped region, to prevent errors due to CPU speculation. Also update the attributes of the SDRAM region as per ST recommendations, and change region numbers to avoid conflicts elsewhere in the codebase (see eth usage).
1 parent 5357dad commit 1470184

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

ports/stm32/sdram.c

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@
3939
#define SDRAM_START_ADDRESS 0xD0000000
4040
#endif
4141

42+
// Provides the MPU_REGION_SIZE_X value when passed the size of region in bytes
43+
// "m" must be a power of 2 between 32 and 4G (2**5 and 2**32) and this formula
44+
// computes the log2 of "m", minus 1
45+
#define MPU_REGION_SIZE(m) (((m) - 1) / (((m) - 1) % 255 + 1) / 255 % 255 * 8 + 7 - 86 / (((m) - 1) % 255 + 12) - 1)
46+
47+
#define SDRAM_MPU_REGION_SIZE (MPU_REGION_SIZE(MICROPY_HW_SDRAM_SIZE))
48+
4249
#ifdef FMC_SDRAM_BANK
4350

4451
static void sdram_init_seq(SDRAM_HandleTypeDef
@@ -244,17 +251,33 @@ static void sdram_init_seq(SDRAM_HandleTypeDef
244251
/* Disable the MPU */
245252
HAL_MPU_Disable();
246253

247-
/* Configure the MPU attributes as Write-Through for External SDRAM */
254+
/* Configure the MPU attributes for External SDRAM
255+
Initially disable all access for the entire SDRAM memory space,
256+
then enable access/caching for the size used
257+
*/
248258
MPU_InitStruct.Enable = MPU_REGION_ENABLE;
259+
MPU_InitStruct.Number = MPU_REGION_NUMBER4;
249260
MPU_InitStruct.BaseAddress = SDRAM_START_ADDRESS;
250-
MPU_InitStruct.Size = MPU_REGION_SIZE_256MB;
251-
MPU_InitStruct.AccessPermission = MPU_REGION_FULL_ACCESS;
261+
MPU_InitStruct.Size = MPU_REGION_SIZE_512MB;
262+
MPU_InitStruct.AccessPermission = MPU_REGION_NO_ACCESS;
252263
MPU_InitStruct.IsBufferable = MPU_ACCESS_NOT_BUFFERABLE;
253-
MPU_InitStruct.IsCacheable = MPU_ACCESS_CACHEABLE;
264+
MPU_InitStruct.IsCacheable = MPU_ACCESS_NOT_CACHEABLE;
254265
MPU_InitStruct.IsShareable = MPU_ACCESS_NOT_SHAREABLE;
255-
MPU_InitStruct.Number = MPU_REGION_NUMBER0;
256266
MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL0;
257267
MPU_InitStruct.SubRegionDisable = 0x00;
268+
MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_DISABLE;
269+
HAL_MPU_ConfigRegion(&MPU_InitStruct);
270+
271+
MPU_InitStruct.Enable = MPU_REGION_ENABLE;
272+
MPU_InitStruct.Number = MPU_REGION_NUMBER5;
273+
MPU_InitStruct.BaseAddress = SDRAM_START_ADDRESS;
274+
MPU_InitStruct.Size = SDRAM_MPU_REGION_SIZE;
275+
MPU_InitStruct.AccessPermission = MPU_REGION_FULL_ACCESS;
276+
MPU_InitStruct.IsBufferable = MPU_ACCESS_BUFFERABLE;
277+
MPU_InitStruct.IsCacheable = MPU_ACCESS_CACHEABLE;
278+
MPU_InitStruct.IsShareable = MPU_ACCESS_NOT_SHAREABLE;
279+
MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL1;
280+
MPU_InitStruct.SubRegionDisable = 0x00;
258281
MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_ENABLE;
259282
HAL_MPU_ConfigRegion(&MPU_InitStruct);
260283

0 commit comments

Comments
 (0)
0