Skip to content

Commit 3033161

Browse files
author
Andrew Leech
committed
stm32/mpu: Reduce flash use with non-inline mpu_config_region.
Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
1 parent 36bf7e9 commit 3033161

File tree

3 files changed

+81
-40
lines changed

3 files changed

+81
-40
lines changed

ports/stm32/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ SRC_C += \
278278
adc.c \
279279
sdio.c \
280280
subghz.c \
281+
mpu.c \
281282
$(wildcard $(BOARD_DIR)/*.c)
282283

283284
SRC_O += \

ports/stm32/mpu.c

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2019 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include <stdint.h>
28+
#include "py/mphal.h"
29+
#include "mpu.h"
30+
31+
#if (defined(STM32F4) && defined(MICROPY_HW_ETH_MDC)) || defined(STM32F7) || defined(STM32H7) || defined(STM32WB)
32+
33+
void mpu_config_region(uint32_t region, uint32_t base_addr, uint32_t attr_size) {
34+
MPU->RNR = region;
35+
MPU->RBAR = base_addr;
36+
MPU->RASR = attr_size;
37+
}
38+
39+
#elif defined(STM32H5)
40+
41+
void mpu_config_region(uint32_t region, uint32_t base_addr, uint32_t size) {
42+
if (region == MPU_REGION_ETH) {
43+
// Configure region 1 to make DMA memory non-cacheable.
44+
45+
__DMB();
46+
// Configure attribute 1, inner-outer non-cacheable (=0x44).
47+
MPU->MAIR0 = (MPU->MAIR0 & ~MPU_MAIR0_Attr1_Msk)
48+
| 0x44 << MPU_MAIR0_Attr1_Pos;
49+
__DMB();
50+
51+
// RBAR
52+
// BASE Bits [31:5] of base address
53+
// SH[4:3] 00 = Non-shareable
54+
// AP[2:1] 01 = Read/write by any privilege level
55+
// XN[0]: 1 = No execution
56+
57+
// RLAR
58+
// LIMIT Limit address. Contains bits[31:5] of the upper inclusive limit of the selected MPU memory region
59+
// AT[3:1] 001 = Attribute 1
60+
// EN[0] 1 = Enabled
61+
MPU->RNR = region;
62+
MPU->RBAR = (base_addr & MPU_RBAR_BASE_Msk)
63+
| MPU_ACCESS_NOT_SHAREABLE << MPU_RBAR_SH_Pos
64+
| MPU_REGION_ALL_RW << MPU_RBAR_AP_Pos
65+
| MPU_INSTRUCTION_ACCESS_DISABLE << MPU_RBAR_XN_Pos;
66+
MPU->RLAR = ((base_addr + size - 1) & MPU_RLAR_LIMIT_Msk)
67+
| MPU_ATTRIBUTES_NUMBER1 << MPU_RLAR_AttrIndx_Pos
68+
| MPU_REGION_ENABLE << MPU_RLAR_EN_Pos;
69+
}
70+
__DMB();
71+
}
72+
73+
#endif

ports/stm32/mpu.h

Lines changed: 7 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,7 @@ static inline uint32_t mpu_config_start(void) {
118118
return disable_irq();
119119
}
120120

121-
static inline void mpu_config_region(uint32_t region, uint32_t base_addr, uint32_t attr_size) {
122-
MPU->RNR = region;
123-
MPU->RBAR = base_addr;
124-
MPU->RASR = attr_size;
125-
}
121+
MP_NOINLINE void mpu_config_region(uint32_t region, uint32_t base_addr, uint32_t attr_size);
126122

127123
static inline void mpu_config_end(uint32_t irq_state) {
128124
__ISB();
@@ -172,44 +168,15 @@ static inline uint32_t mpu_config_start(void) {
172168
return disable_irq();
173169
}
174170

175-
static inline void mpu_config_region(uint32_t region, uint32_t base_addr, uint32_t size) {
176-
if (region == MPU_REGION_ETH) {
177-
// Configure region 1 to make DMA memory non-cacheable.
171+
MP_NOINLINE void mpu_config_region(uint32_t region, uint32_t base_addr, uint32_t attr_size);
172+
if (region == MPU_REGION_ETH) {
178173

174+
static inline void mpu_config_end(uint32_t irq_state) {
175+
__ISB();
176+
__DSB();
179177
__DMB();
180-
// Configure attribute 1, inner-outer non-cacheable (=0x44).
181-
MPU->MAIR0 = (MPU->MAIR0 & ~MPU_MAIR0_Attr1_Msk)
182-
| 0x44 << MPU_MAIR0_Attr1_Pos;
183-
__DMB();
184-
185-
// RBAR
186-
// BASE Bits [31:5] of base address
187-
// SH[4:3] 00 = Non-shareable
188-
// AP[2:1] 01 = Read/write by any privilege level
189-
// XN[0]: 1 = No execution
190-
191-
// RLAR
192-
// LIMIT Limit address. Contains bits[31:5] of the upper inclusive limit of the selected MPU memory region
193-
// AT[3:1] 001 = Attribute 1
194-
// EN[0] 1 = Enabled
195-
MPU->RNR = region;
196-
MPU->RBAR = (base_addr & MPU_RBAR_BASE_Msk)
197-
| MPU_ACCESS_NOT_SHAREABLE << MPU_RBAR_SH_Pos
198-
| MPU_REGION_ALL_RW << MPU_RBAR_AP_Pos
199-
| MPU_INSTRUCTION_ACCESS_DISABLE << MPU_RBAR_XN_Pos;
200-
MPU->RLAR = ((base_addr + size - 1) & MPU_RLAR_LIMIT_Msk)
201-
| MPU_ATTRIBUTES_NUMBER1 << MPU_RLAR_AttrIndx_Pos
202-
| MPU_REGION_ENABLE << MPU_RLAR_EN_Pos;
178+
enable_irq(irq_state);
203179
}
204-
__DMB();
205-
}
206-
207-
static inline void mpu_config_end(uint32_t irq_state) {
208-
__ISB();
209-
__DSB();
210-
__DMB();
211-
enable_irq(irq_state);
212-
}
213180

214181
#else
215182

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy