Skip to content

Commit d464509

Browse files
committed
Rework MPU config for unaligned access to external memory
- Add MPU config to ORGPAL3 and ORGPALX to allow unaligned access to external memory. - Remove MPU config for non cached memory in ChibiOS mcu config for ORGPAL3 and ORGPALX. Now are in Target_ConfigNonCacheableMemory(). - Rework call to MPU config in ORGPAL3 and ORGPALX. - Migrate ST_STM32F769I_DISCOVERY MPU config to use ST HAL API.
1 parent 9566cb2 commit d464509

File tree

14 files changed

+190
-88
lines changed

14 files changed

+190
-88
lines changed

targets/ChibiOS/ORGPAL_PALTHREE/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ nf_setup_target_build(
1818
CLR_EXTRA_SOURCE_FILES
1919
# the next one is required is the target implements and it's using external memory
2020
${CMAKE_CURRENT_SOURCE_DIR}/target_external_memory.c
21-
21+
${CMAKE_CURRENT_SOURCE_DIR}/target_mpu_config.c
22+
2223
BOOTER_EXTRA_COMPILE_DEFINITIONS
2324
USBH_DEBUG_MULTI_HOST=0
2425

targets/ChibiOS/ORGPAL_PALTHREE/nanoCLR/main.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
extern int32_t hal_lfs_config();
2121
extern void hal_lfs_mount();
22+
extern void Target_ConfigMPU();
2223

2324
// need to declare the Receiver thread here
2425
osThreadDef(ReceiverThread, osPriorityHigh, 2048, "ReceiverThread");
@@ -93,6 +94,9 @@ int main(void)
9394
crcStart(NULL);
9495
#endif
9596

97+
// MPU configuration
98+
Target_ConfigMPU();
99+
96100
// config and init external memory
97101
// this has to be called after osKernelInitialize, otherwise an hard fault will occur
98102
Target_ExternalMemoryInit();

targets/ChibiOS/ORGPAL_PALTHREE/nanoCLR/mcuconf.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,7 @@
2929
/*
3030
* Memory attributes settings.
3131
*/
32-
#define STM32_NOCACHE_ENABLE TRUE
33-
#define STM32_NOCACHE_MPU_REGION MPU_REGION_0
34-
#define STM32_NOCACHE_RBAR 0x20000000U
35-
#define STM32_NOCACHE_RASR MPU_RASR_SIZE_128K
32+
#define STM32_NOCACHE_ENABLE FALSE
3633

3734
/*
3835
* HAL driver system settings.

targets/ChibiOS/ORGPAL_PALTHREE/stm32f7xx_hal_conf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ extern "C"
3434
// #define HAL_NAND_MODULE_ENABLED
3535
// #define HAL_NOR_MODULE_ENABLED
3636
// #define HAL_SRAM_MODULE_ENABLED
37-
// #define HAL_SDRAM_MODULE_ENABLED
37+
#define HAL_SDRAM_MODULE_ENABLED
3838
// #define HAL_HASH_MODULE_ENABLED
3939
// #define HAL_GPIO_MODULE_ENABLED
4040
// #define HAL_I2C_MODULE_ENABLED

targets/ChibiOS/ORGPAL_PALTHREE/target_external_memory.c

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
#include <ch.h>
77
#include "hal.h"
8-
#include "fsmc_sdram_lld.h"
8+
#include <stm32f7xx_hal.h>
99

1010
// SDRAM Mode definition register defines
1111
#define FMC_SDCMR_MRD_BURST_LENGTH_1 ((uint16_t)0x0000)
@@ -95,3 +95,46 @@ void Target_ExternalMemoryInit()
9595
fsmcSdramInit();
9696
fsmcSdramStart(&SDRAMD, &sdram_cfg);
9797
}
98+
99+
void Target_ExternalMemoryConfigMPU()
100+
{
101+
// ARM: STM32F7: hard fault caused by unaligned Memory Access
102+
// reference https://www.keil.com/support/docs/3777%20%20.htm
103+
// SYMPTOM
104+
// If you use an STM32F7xx microcontroller with an external SDRAM,
105+
// the Cortex-M7 core may unexpectedly run into the hard fault handler because of unaligned access.
106+
// This may happen for example, when the frame buffer of an LCD, a RAM filesystem or any other data is
107+
// located into the SDRAM address range 0xC0000000 - 0xC03FFFFF (max. 4MB).
108+
// The hard fault is executed although the bit UNALIGN_TRP (bit 3) in the CCR register is not enabled.
109+
110+
// CAUSE
111+
// In general, RAM accesses on Cortex-M7 based devices do not have to be aligned in any way.
112+
// The Cortex-M7 core can handle unaligned accesses by hardware.
113+
// Usually, variables should be naturally aligned because these accesses are slightly faster than unaligned
114+
// accesses.
115+
116+
// STM32F7xx devices have the external SDRAM mapped to the
117+
// address range 0xC0000000 - 0xC03FFFFF (max. 4MB).
118+
// According to the ARMv7-M Architecture Reference Manual chapter B3.1 (table B3-1),
119+
// the area 0xC0000000-0xDFFFFFFF (32MB) is specified as Device Memory Type.
120+
// According to chapter A3.2.1, all accesses to Device Memory Types must be naturally aligned.
121+
// If they are not, a hard fault will execute no matter if the bit UNALIGN_TRP (bit 3) in the CCR register is
122+
// enabled or not.
123+
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);
140+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//
2+
// Copyright (c) .NET Foundation and Contributors
3+
// See LICENSE file in the project root for full license information.
4+
//
5+
6+
#include <ch.h>
7+
#include <hal.h>
8+
#include <stm32_registry.h>
9+
#include <hal_nf_community.h>
10+
11+
extern void Target_ExternalMemoryConfigMPU();
12+
13+
// SRAM1 base address
14+
#define SRAM1_BASE 0x20000000U
15+
#define SRAM1_SIZE_128K (1UL << 16) // 2^17 bytes
16+
#define MPU_REGION_SRAM1 MPU_REGION_1
17+
18+
void Target_ConfigNonCacheableMemory()
19+
{
20+
// region
21+
MPU->RNR = MPU_REGION_SRAM1;
22+
23+
// base address
24+
MPU->RBAR = SRAM1_BASE;
25+
26+
// size and other configs
27+
MPU->RASR =
28+
((uint32_t)MPU_RASR_ATTR_AP_RW_RW | MPU_RASR_ATTR_NON_CACHEABLE | MPU_RASR_ATTR_S | MPU_RASR_SIZE_128K |
29+
MPU_RASR_ENABLE);
30+
}
31+
32+
void Target_ConfigMPU()
33+
{
34+
// disable MPU
35+
HAL_MPU_Disable();
36+
37+
// config MPU for external memory
38+
Target_ExternalMemoryConfigMPU();
39+
40+
// config MPU for non cacheable memory
41+
Target_ConfigNonCacheableMemory();
42+
43+
// enable MPU
44+
HAL_MPU_Enable(MPU_PRIVILEGED_DEFAULT);
45+
}

targets/ChibiOS/ORGPAL_PALX/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ nf_setup_target_build(
1818
CLR_EXTRA_SOURCE_FILES
1919
# the next one is required is the target implements and it's using external memory
2020
${CMAKE_CURRENT_SOURCE_DIR}/target_external_memory.c
21+
${CMAKE_CURRENT_SOURCE_DIR}/target_mpu_config.c
2122

2223
BOOTER_EXTRA_COMPILE_DEFINITIONS
2324
-DUSBH_DEBUG_MULTI_HOST=0

targets/ChibiOS/ORGPAL_PALX/nanoCLR/main.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
extern int32_t hal_lfs_config();
2121
extern void hal_lfs_mount();
22+
extern void Target_ConfigMPU();
2223

2324
// need to declare the Receiver thread here
2425
osThreadDef(ReceiverThread, osPriorityHigh, 2048, "ReceiverThread");
@@ -93,6 +94,9 @@ int main(void)
9394
crcStart(NULL);
9495
#endif
9596

97+
// MPU configuration
98+
Target_ConfigMPU();
99+
96100
// config and init external memory
97101
// this has to be called after osKernelInitialize, otherwise an hard fault will occur
98102
Target_ExternalMemoryInit();

targets/ChibiOS/ORGPAL_PALX/nanoCLR/mcuconf.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@
2727
/*
2828
* Memory attributes settings.
2929
*/
30-
#define STM32_NOCACHE_ENABLE TRUE
31-
#define STM32_NOCACHE_MPU_REGION MPU_REGION_0
32-
#define STM32_NOCACHE_RBAR 0x20000000U
33-
#define STM32_NOCACHE_RASR MPU_RASR_SIZE_128K
30+
#define STM32_NOCACHE_ENABLE FALSE
3431

3532
/*
3633
* HAL driver system settings.

targets/ChibiOS/ORGPAL_PALX/stm32f7xx_hal_conf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ extern "C"
3333
// #define HAL_FLASH_MODULE_ENABLED
3434
// #define HAL_NAND_MODULE_ENABLED
3535
// #define HAL_NOR_MODULE_ENABLED
36-
// #define HAL_SRAM_MODULE_ENABLED
36+
#define HAL_SDRAM_MODULE_ENABLED
3737
// #define HAL_SDRAM_MODULE_ENABLED
3838
// #define HAL_HASH_MODULE_ENABLED
3939
// #define HAL_GPIO_MODULE_ENABLED

0 commit comments

Comments
 (0)