8000 stm32/qspi: Support common flash sizes in MPU configuration. · micropython/micropython@b288394 · GitHub
[go: up one dir, main page]

Skip to content

Commit b288394

Browse files
iabdalkaderdpgeorge
authored andcommitted
stm32/qspi: Support common flash sizes in MPU configuration.
Add MPU configuration for common flash sizes up to 256MiB.
1 parent 0e68738 commit b288394

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

ports/stm32/qspi.c

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@
5252
#define MICROPY_HW_QSPI_CS_HIGH_CYCLES 2 // nCS stays high for 2 cycles
5353
#endif
5454

55+
#ifndef MICROPY_HW_QSPI_MPU_REGION_SIZE
56+
#define MICROPY_HW_QSPI_MPU_REGION_SIZE ((1 << (MICROPY_HW_QSPIFLASH_SIZE_BITS_LOG2 - 3)) >> 20)
57+
#endif
58+
5559
#if (MICROPY_HW_QSPIFLASH_SIZE_BITS_LOG2 - 3 - 1) >= 24
5660
#define QSPI_CMD 0xec
5761
#define QSPI_ADSIZE 3
@@ -74,11 +78,37 @@ static inline void qspi_mpu_enable_mapped(void) {
7478
// for the memory-mapped region, so 3 MPU regions are used to disable access
7579
// to everything except the valid address space, using holes in the bottom
7680
// of the regions and nesting them.
77-
// At the moment this is hard-coded to 2MiB of QSPI address space.
81+
// Note: Disabling a subregion (by setting its corresponding SRD bit to 1)
82+
// means another region overlapping the disabled range matches instead. If no
83+
// other enabled region overlaps the disabled subregion, and the access is
84+
// unprivileged or the background region is disabled, the MPU issues a fault.
7885
uint32_t irq_state = mpu_config_start();
86+
#if MICROPY_HW_QSPI_MPU_REGION_SIZE > 128
87+
mpu_config_region(MPU_REGION_QSPI1, QSPI_MAP_ADDR, MPU_CONFIG_DISABLE(0xFF, MPU_REGION_SIZE_256MB));
88+
#elif MICROPY_HW_QSPI_MPU_REGION_SIZE > 64
89+
mpu_config_region(MPU_REGION_QSPI1, QSPI_MAP_ADDR, MPU_CONFIG_DISABLE(0x0F, MPU_REGION_SIZE_256MB));
90+
#elif MICROPY_HW_QSPI_MPU_REGION_SIZE > 32
91+
mpu_config_region(MPU_REGION_QSPI1, QSPI_MAP_ADDR, MPU_CONFIG_DISABLE(0x03, MPU_REGION_SIZE_256MB));
92+
#elif MICROPY_HW_QSPI_MPU_REGION_SIZE > 16
93+
mpu_config_region(MPU_REGION_QSPI1, QSPI_MAP_ADDR, MPU_CONFIG_DISABLE(0x01, MPU_REGION_SIZE_256MB));
94+
#elif MICROPY_HW_QSPI_MPU_REGION_SIZE > 8
95+
mpu_config_region(MPU_REGION_QSPI1, QSPI_MAP_ADDR, MPU_CONFIG_DISABLE(0x01, MPU_REGION_SIZE_256MB));
96+
mpu_config_region(MPU_REGION_QSPI2, QSPI_MAP_ADDR, MPU_CONFIG_DISABLE(0x0F, MPU_REGION_SIZE_32MB));
97+
#elif MICROPY_HW_QSPI_MPU_REGION_SIZE > 4
7998
mpu_config_region(MPU_REGION_QSPI1, QSPI_MAP_ADDR, MPU_CONFIG_DISABLE(0x01, MPU_REGION_SIZE_256MB));
80-
mpu_config_region(MPU_REGION_QSPI2, QSPI_MAP_ADDR, MPU_CONFIG_DISABLE(0x0f, MPU_REGION_SIZE_32MB));
99+
mpu_config_region(MPU_REGION_QSPI2, QSPI_MAP_ADDR, MPU_CONFIG_DISABLE(0x03, MPU_REGION_SIZE_32MB));
100+
#elif MICROPY_HW_QSPI_MPU_REGION_SIZE > 2
101+
mpu_config_region(MPU_REGION_QSPI1, QSPI_MAP_ADDR, MPU_CONFIG_DISABLE(0x01, MPU_REGION_SIZE_256MB));
102+
mpu_config_region(MPU_REGION_QSPI2, QSPI_MAP_ADDR, MPU_CONFIG_DISABLE(0x01, MPU_REGION_SIZE_32MB));
103+
#elif MICROPY_HW_QSPI_MPU_REGION_SIZE > 1
104+
mpu_config_region(MPU_REGION_QSPI1, QSPI_MAP_ADDR, MPU_CONFIG_DISABLE(0x01, MPU_REGION_SIZE_256MB));
105+
mpu_config_region(MPU_REGION_QSPI2, QSPI_MAP_ADDR, MPU_CONFIG_DISABLE(0x0F, MPU_REGION_SIZE_32MB));
81106
mpu_config_region(MPU_REGION_QSPI3, QSPI_MAP_ADDR, MPU_CONFIG_DISABLE(0x01, MPU_REGION_SIZE_16MB));
107+
#else
108+
mpu_config_region(MPU_REGION_QSPI1, QSPI_MAP_ADDR, MPU_CONFIG_DISABLE(0x01, MPU_REGION_SIZE_256MB));
109+
mpu_config_region(MPU_REGION_QSPI2, QSPI_MAP_ADDR, MPU_CONFIG_DISABLE(0x01, MPU_REGION_SIZE_32MB));
110+
mpu_config_region(MPU_REGION_QSPI3, QSPI_MAP_ADDR, MPU_CONFIG_DISABLE(0x03, MPU_REGION_SIZE_4MB));
111+
#endif
82112
mpu_config_end(irq_state);
83113
}
84114

0 commit comments

Comments
 (0)
0