Skip to content

Commit 968fa47

Browse files
pi-anldpgeorge
authored andcommitted
stm32/sdram: On F7 MCUs enable MPU on external SDRAM.
This prevents hard-faults on non-aligned accesses. Reference: http://www.keil.com/support/docs/3777.htm
1 parent a1db150 commit 968fa47

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

ports/stm32/sdram.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,35 @@ static void sdram_init_seq(SDRAM_HandleTypeDef
211211
*/
212212
#define REFRESH_COUNT (MICROPY_HW_SDRAM_REFRESH_RATE * 90000 / 8192 - 20)
213213
HAL_SDRAM_ProgramRefreshRate(hsdram, REFRESH_COUNT);
214+
215+
#if defined(STM32F7)
216+
/* Enable MPU for the SDRAM Memory Region to allow non-aligned
217+
accesses (hard-fault otherwise)
218+
*/
219+
220+
MPU_Region_InitTypeDef MPU_InitStruct;
221+
222+
/* Disable the MPU */
223+
HAL_MPU_Disable();
224+
225+
/* Configure the MPU attributes for SDRAM */
226+
MPU_InitStruct.Enable = MPU_REGION_ENABLE;
227+
MPU_InitStruct.BaseAddress = SDRAM_START_ADDRESS;
228+
MPU_InitStruct.Size = MPU_REGION_SIZE_4MB;
229+
MPU_InitStruct.AccessPermission = MPU_REGION_FULL_ACCESS;
230+
MPU_InitStruct.IsBufferable = MPU_ACCESS_NOT_BUFFERABLE;
231+
MPU_InitStruct.IsCacheable = MPU_ACCESS_NOT_CACHEABLE;
232+
MPU_InitStruct.IsShareable = MPU_ACCESS_NOT_SHAREABLE;
233+
MPU_InitStruct.Number = MPU_REGION_NUMBER0;
234+
MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL1;
235+
MPU_InitStruct.SubRegionDisable = 0x00;
236+
MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_DISABLE;
237+
238+
HAL_MPU_ConfigRegion(&MPU_InitStruct);
239+
240+
/* Enable the MPU */
241+
HAL_MPU_Enable(MPU_PRIVILEGED_DEFAULT);
242+
#endif
214243
}
215244

216245
bool __attribute__((optimize("O0"))) sdram_test(bool fast) {

0 commit comments

Comments
 (0)