6969#define SDRAM_SIZE (8 * 1024 * 1024)
7070#define SDRAM_START ((void *)FSMC_Bank6_MAP_BASE)
7171
72- void SetupDeviceMemoryToEliminateUnalignedAccess ();
73-
7472// SDRAM driver configuration structure.
7573static const SDRAMConfig sdram_cfg = {
7674 .sdcr = (uint32_t )FMC_ColumnBits_Number_8b | FMC_RowBits_Number_12b | FMC_SDMemory_Width_16b |
@@ -94,12 +92,11 @@ static const SDRAMConfig sdram_cfg = {
9492
9593void Target_ExternalMemoryInit ()
9694{
97- SetupDeviceMemoryToEliminateUnalignedAccess ();
9895 fsmcSdramInit ();
9996 fsmcSdramStart (& SDRAMD , & sdram_cfg );
10097}
10198
102- void SetupDeviceMemoryToEliminateUnalignedAccess ()
99+ void Target_ExternalMemoryConfigMPU ()
103100{
104101 // ARM: STM32F7: hard fault caused by unaligned Memory Access
105102 // reference https://www.keil.com/support/docs/3777%20%20.htm
@@ -124,37 +121,20 @@ void SetupDeviceMemoryToEliminateUnalignedAccess()
124121 // If they are not, a hard fault will execute no matter if the bit UNALIGN_TRP (bit 3) in the CCR register is
125122 // enabled or not.
126123
127- // Solution recommended by KEIL
128-
129- #define MPU_REGION_ENABLE ((uint8_t)0x01U)
130- #define MPU_REGION_SIZE_8MB ((uint8_t)0x16U)
131- #define MPU_REGION_FULL_ACCESS ((uint8_t)0x03U)
132- #define MPU_ACCESS_NOT_BUFFERABLE ((uint8_t)0x00U)
133- #define MPU_ACCESS_NOT_CACHEABLE ((uint8_t)0x00U)
134- #define MPU_ACCESS_NOT_SHAREABLE ((uint8_t)0x00U)
135- #define MPU_ACCESS_NOT_SHAREABLE ((uint8_t)0x00U)
136- #define MPU_REGION_NUMBER0 ((uint8_t)0x00U)
137- #define MPU_TEX_LEVEL1 ((uint8_t)0x01U)
138- #define MPU_INSTRUCTION_ACCESS_DISABLE ((uint8_t)0x01U)
139- #define MPU_PRIVILEGED_DEFAULT ((uint32_t)0x00000004U)
140-
141- // Disable the MPU
142- __DMB ();
143- SCB -> SHCSR &= ~SCB_SHCSR_MEMFAULTENA_Msk ;
144- MPU -> CTRL = 0 ;
145-
146- // Configure the region
147- MPU -> RNR = MPU_REGION_NUMBER0 ;
148- MPU -> RBAR = 0xC0000000 ;
149- MPU -> RASR = (MPU_INSTRUCTION_ACCESS_DISABLE << MPU_RASR_XN_Pos ) | (MPU_REGION_FULL_ACCESS << MPU_RASR_AP_Pos ) |
150- (MPU_TEX_LEVEL1 << MPU_RASR_TEX_Pos ) | (MPU_ACCESS_NOT_SHAREABLE << MPU_RASR_S_Pos ) |
151- (MPU_ACCESS_NOT_CACHEABLE << MPU_RASR_C_Pos ) | (MPU_ACCESS_NOT_BUFFERABLE << MPU_RASR_B_Pos ) |
152- (0x00 << MPU_RASR_SRD_Pos ) | (MPU_REGION_SIZE_8MB << MPU_RASR_SIZE_Pos ) |
153- (MPU_REGION_ENABLE << MPU_RASR_ENABLE_Pos );
154-
155- // Enable the MPU
156- MPU -> CTRL = MPU_PRIVILEGED_DEFAULT | MPU_CTRL_ENABLE_Msk ;
157- SCB -> SHCSR |= SCB_SHCSR_MEMFAULTENA_Msk ;
158- __DSB ();
159- __ISB ();
124+ MPU_Region_InitTypeDef MPU_InitStruct ;
125+
126+ // Configure the MPU attributes for SDRAM
127+ MPU_InitStruct .Enable = MPU_REGION_ENABLE ;
128+ MPU_InitStruct .BaseAddress = 0xD0000000 ;
129+ MPU_InitStruct .Size = MPU_REGION_SIZE_8MB ;
130+ MPU_InitStruct .AccessPermission = MPU_REGION_FULL_ACCESS ;
131+ MPU_InitStruct .IsBufferable = MPU_ACCESS_NOT_BUFFERABLE ;
132+ MPU_InitStruct .IsCacheable = MPU_ACCESS_NOT_CACHEABLE ;
133+ MPU_InitStruct .IsShareable = MPU_ACCESS_NOT_SHAREABLE ;
134+ MPU_InitStruct .Number = MPU_REGION_NUMBER0 ;
135+ MPU_InitStruct .TypeExtField = MPU_TEX_LEVEL1 ;
136+ MPU_InitStruct .SubRegionDisable = 0x00 ;
137+ MPU_InitStruct .DisableExec = MPU_INSTRUCTION_ACCESS_DISABLE ;
138+
139+ HAL_MPU_ConfigRegion (& MPU_InitStruct );
160140}
0 commit comments