|
| 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 |
0 commit comments