Skip to content

Commit 52ab3d0

Browse files
IsaacDynamoSkptakjasonpcarroll
authored
MPU assert for ARM_CM3_MPU (#952)
* Add runtime check to see if the target even has a MPU * Add missing extern symbols for __ARMCC_VERSION support * Add default for configTOTAL_MPU_REGIONS and change a runtime assert to compile time error * Simplify check and link to reference documentation Co-authored-by: Soren Ptak <[email protected]> --------- Co-authored-by: Soren Ptak <[email protected]> Co-authored-by: jasonpcarroll <[email protected]>
1 parent aa07289 commit 52ab3d0

File tree

2 files changed

+47
-10
lines changed

2 files changed

+47
-10
lines changed

portable/GCC/ARM_CM3_MPU/port.c

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,12 +1095,28 @@ __attribute__( ( weak ) ) void vPortSetupTimerInterrupt( void )
10951095

10961096
static void prvSetupMPU( void )
10971097
{
1098-
extern uint32_t __privileged_functions_start__[];
1099-
extern uint32_t __privileged_functions_end__[];
1100-
extern uint32_t __FLASH_segment_start__[];
1101-
extern uint32_t __FLASH_segment_end__[];
1102-
extern uint32_t __privileged_data_start__[];
1103-
extern uint32_t __privileged_data_end__[];
1098+
#if defined( __ARMCC_VERSION )
1099+
1100+
/* Declaration when these variable are defined in code instead of being
1101+
* exported from linker scripts. */
1102+
extern uint32_t * __privileged_functions_start__;
1103+
extern uint32_t * __privileged_functions_end__;
1104+
extern uint32_t * __FLASH_segment_start__;
1105+
extern uint32_t * __FLASH_segment_end__;
1106+
extern uint32_t * __privileged_data_start__;
1107+
extern uint32_t * __privileged_data_end__;
1108+
#else
1109+
/* Declaration when these variable are exported from linker scripts. */
1110+
extern uint32_t __privileged_functions_start__[];
1111+
extern uint32_t __privileged_functions_end__[];
1112+
extern uint32_t __FLASH_segment_start__[];
1113+
extern uint32_t __FLASH_segment_end__[];
1114+
extern uint32_t __privileged_data_start__[];
1115+
extern uint32_t __privileged_data_end__[];
1116+
#endif /* if defined( __ARMCC_VERSION ) */
1117+
1118+
/* Ensure that the device has the expected MPU type */
1119+
configASSERT( portMPU_TYPE_REG == portEXPECTED_MPU_TYPE_VALUE );
11041120

11051121
/* Check the expected MPU is present. */
11061122
if( portMPU_TYPE_REG == portEXPECTED_MPU_TYPE_VALUE )
@@ -1229,10 +1245,22 @@ void vPortStoreTaskMPUSettings( xMPU_SETTINGS * xMPUSettings,
12291245
StackType_t * pxBottomOfStack,
12301246
uint32_t ulStackDepth )
12311247
{
1232-
extern uint32_t __SRAM_segment_start__[];
1233-
extern uint32_t __SRAM_segment_end__[];
1234-
extern uint32_t __privileged_data_start__[];
1235-
extern uint32_t __privileged_data_end__[];
1248+
#if defined( __ARMCC_VERSION )
1249+
1250+
/* Declaration when these variable are defined in code instead of being
1251+
* exported from linker scripts. */
1252+
extern uint32_t * __SRAM_segment_start__;
1253+
extern uint32_t * __SRAM_segment_end__;
1254+
extern uint32_t * __privileged_data_start__;
1255+
extern uint32_t * __privileged_data_end__;
1256+
#else
1257+
/* Declaration when these variable are exported from linker scripts. */
1258+
extern uint32_t __SRAM_segment_start__[];
1259+
extern uint32_t __SRAM_segment_end__[];
1260+
extern uint32_t __privileged_data_start__[];
1261+
extern uint32_t __privileged_data_end__[];
1262+
#endif /* if defined( __ARMCC_VERSION ) */
1263+
12361264
int32_t lIndex;
12371265
uint32_t ul;
12381266

portable/GCC/ARM_CM3_MPU/portmacro.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,15 @@ typedef unsigned long UBaseType_t;
8686
#define portMPU_REGION_CACHEABLE_BUFFERABLE ( 0x07UL << 16UL )
8787
#define portMPU_REGION_EXECUTE_NEVER ( 0x01UL << 28UL )
8888

89+
/* MPU settings that can be overriden in FreeRTOSConfig.h. */
90+
#ifndef configTOTAL_MPU_REGIONS
91+
/* Define to 8 for backward compatibility. */
92+
#define configTOTAL_MPU_REGIONS ( 8UL )
93+
#elif( configTOTAL_MPU_REGIONS != 8UL )
94+
/* The Cortex M3 only supports 8 MPU regions. For more information refer to:
95+
* https://developer.arm.com/documentation/dui0552/a/cortex-m3-peripherals/optional-memory-protection-unit */
96+
#error configTOTAL_MPU_REGIONS must be 8 for this port.
97+
#endif /* configTOTAL_MPU_REGIONS Check */
8998
#define portSTACK_REGION ( 3UL )
9099
#define portGENERAL_PERIPHERALS_REGION ( 4UL )
91100
#define portUNPRIVILEGED_FLASH_REGION ( 5UL )

0 commit comments

Comments
 (0)