|
5 | 5 |
|
6 | 6 | #include <ch.h>
|
7 | 7 | #include "hal.h"
|
8 |
| -#include "fsmc_sdram_lld.h" |
| 8 | +#include <fsmc_sdram_lld.h> |
| 9 | +#include <stm32f7xx_hal.h> |
9 | 10 |
|
10 | 11 | // SDRAM Mode definition register defines
|
11 | 12 | #define FMC_SDCMR_MRD_BURST_LENGTH_1 ((uint16_t)0x0000)
|
@@ -95,3 +96,46 @@ void Target_ExternalMemoryInit()
|
95 | 96 | fsmcSdramInit();
|
96 | 97 | fsmcSdramStart(&SDRAMD, &sdram_cfg);
|
97 | 98 | }
|
| 99 | + |
| 100 | +void Target_ExternalMemoryConfigMPU() |
| 101 | +{ |
| 102 | + // ARM: STM32F7: hard fault caused by unaligned Memory Access |
| 103 | + // reference https://www.keil.com/support/docs/3777%20%20.htm |
| 104 | + // SYMPTOM |
| 105 | + // If you use an STM32F7xx microcontroller with an external SDRAM, |
| 106 | + // the Cortex-M7 core may unexpectedly run into the hard fault handler because of unaligned access. |
| 107 | + // This may happen for example, when the frame buffer of an LCD, a RAM filesystem or any other data is |
| 108 | + // located into the SDRAM address range 0xC0000000 - 0xC03FFFFF (max. 4MB). |
| 109 | + // The hard fault is executed although the bit UNALIGN_TRP (bit 3) in the CCR register is not enabled. |
| 110 | + |
| 111 | + // CAUSE |
| 112 | + // In general, RAM accesses on Cortex-M7 based devices do not have to be aligned in any way. |
| 113 | + // The Cortex-M7 core can handle unaligned accesses by hardware. |
| 114 | + // Usually, variables should be naturally aligned because these accesses are slightly faster than unaligned |
| 115 | + // accesses. |
| 116 | + |
| 117 | + // STM32F7xx devices have the external SDRAM mapped to the |
| 118 | + // address range 0xC0000000 - 0xC03FFFFF (max. 4MB). |
| 119 | + // According to the ARMv7-M Architecture Reference Manual chapter B3.1 (table B3-1), |
| 120 | + // the area 0xC0000000-0xDFFFFFFF (32MB) is specified as Device Memory Type. |
| 121 | + // According to chapter A3.2.1, all accesses to Device Memory Types must be naturally aligned. |
| 122 | + // If they are not, a hard fault will execute no matter if the bit UNALIGN_TRP (bit 3) in the CCR register is |
| 123 | + // enabled or not. |
| 124 | + |
| 125 | + MPU_Region_InitTypeDef MPU_InitStruct; |
| 126 | + |
| 127 | + // Configure the MPU attributes for SDRAM |
| 128 | + MPU_InitStruct.Enable = MPU_REGION_ENABLE; |
| 129 | + MPU_InitStruct.BaseAddress = 0xD0000000; |
| 130 | + MPU_InitStruct.Size = MPU_REGION_SIZE_8MB; |
| 131 | + MPU_InitStruct.AccessPermission = MPU_REGION_FULL_ACCESS; |
| 132 | + MPU_InitStruct.IsBufferable = MPU_ACCESS_NOT_BUFFERABLE; |
| 133 | + MPU_InitStruct.IsCacheable = MPU_ACCESS_NOT_CACHEABLE; |
| 134 | + MPU_InitStruct.IsShareable = MPU_ACCESS_NOT_SHAREABLE; |
| 135 | + MPU_InitStruct.Number = MPU_REGION_NUMBER0; |
| 136 | + MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL1; |
| 137 | + MPU_InitStruct.SubRegionDisable = 0x00; |
| 138 | + MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_DISABLE; |
| 139 | + |
| 140 | + HAL_MPU_ConfigRegion(&MPU_InitStruct); |
| 141 | +} |
0 commit comments