Skip to content

Commit 875ea06

Browse files
committed
new general MPU config facility; also fixes LTDC slew
1 parent 68b8aa9 commit 875ea06

File tree

2 files changed

+256
-0
lines changed

2 files changed

+256
-0
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
------------------------------------------------------------------------------
2+
-- --
3+
-- Copyright (C) 2026, AdaCore --
4+
-- --
5+
-- Redistribution and use in source and binary forms, with or without --
6+
-- modification, are permitted provided that the following conditions are --
7+
-- met: --
8+
-- 1. Redistributions of source code must retain the above copyright --
9+
-- notice, this list of conditions and the following disclaimer. --
10+
-- 2. Redistributions in binary form must reproduce the above copyright --
11+
-- notice, this list of conditions and the following disclaimer in --
12+
-- the documentation and/or other materials provided with the --
13+
-- distribution. --
14+
-- 3. Neither the name of the copyright holder nor the names of its --
15+
-- contributors may be used to endorse or promote products derived --
16+
-- from this software without specific prior written permission. --
17+
-- --
18+
-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS --
19+
-- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT --
20+
-- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR --
21+
-- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT --
22+
-- HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, --
23+
-- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT --
24+
-- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, --
25+
-- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY --
26+
-- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT --
27+
-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE --
28+
-- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. --
29+
-- --
30+
------------------------------------------------------------------------------
31+
32+
with System.Machine_Code; use System.Machine_Code;
33+
with Ada.Unchecked_Conversion;
34+
35+
with Cortex_M_SVD.MPU; use Cortex_M_SVD.MPU;
36+
37+
package body Cortex_M.MPU is
38+
39+
procedure DSB with Inline_Always;
40+
procedure ISB with Inline_Always;
41+
42+
procedure DSB is
43+
begin
44+
Asm ("dsb", Volatile => True);
45+
end DSB;
46+
47+
procedure ISB is
48+
begin
49+
Asm ("isb", Volatile => True);
50+
end ISB;
51+
52+
------------
53+
-- Enable --
54+
------------
55+
56+
procedure Enable (Control : MPU_Control) is
57+
begin
58+
MPU_Periph.CTRL := (ENABLE => True,
59+
HFNMIENA => Control.Hard_Fault_NMI_Enable,
60+
PRIVDEFENA => Control.Privileged_Default,
61+
others => <>);
62+
DSB;
63+
ISB;
64+
end Enable;
65+
66+
-------------
67+
-- Disable --
68+
-------------
69+
70+
procedure Disable is
71+
begin
72+
DSB;
73+
MPU_Periph.CTRL := (ENABLE => False,
74+
HFNMIENA => False,
75+
PRIVDEFENA => False,
76+
others => <>);
77+
ISB;
78+
end Disable;
79+
80+
----------------------
81+
-- Configure_Region --
82+
----------------------
83+
84+
procedure Configure_Region (Config : Region_Configuration) is
85+
86+
function To_UInt32 is new Ada.Unchecked_Conversion
87+
(System.Address, HAL.UInt32);
88+
89+
use HAL;
90+
91+
Addr : constant UInt32 := To_UInt32 (Config.Base_Address);
92+
93+
Size_Val : constant UInt5 := Region_Size'Enum_Rep (Config.Size);
94+
AP_Val : constant UInt3 := Access_Permission'Enum_Rep (Config.Access_Permission);
95+
begin
96+
-- Select the region
97+
MPU_Periph.RNR := (REGION => HAL.UInt8 (Config.Number), others => <>);
98+
99+
-- Set base address
100+
MPU_Periph.RBAR := (ADDR => UInt27 (Addr / 2**5), VALID => False, REGION => 0);
101+
102+
-- Set attributes and enable
103+
MPU_Periph.RASR := (ENABLE => True,
104+
SIZE => Size_Val,
105+
SRD => (As_Array => False, Val => Config.Subregion_Disable),
106+
B => Config.Bufferable,
107+
C => Config.Cacheable,
108+
S => Config.Shareable,
109+
TEX => Config.TEX,
110+
AP => Cortex_M_SVD.MPU.RASR_AP_Field'Val (AP_Val),
111+
XN => (if Config.Execute_Never
112+
then Cortex_M_SVD.MPU.I_Disabled
113+
else Cortex_M_SVD.MPU.I_Enabled),
114+
others => <>);
115+
end Configure_Region;
116+
117+
--------------------
118+
-- Disable_Region --
119+
--------------------
120+
121+
procedure Disable_Region (Number : Region_Number) is
122+
begin
123+
MPU_Periph.RNR := (REGION => HAL.UInt8 (Number), others => <>);
124+
MPU_Periph.RASR := (ENABLE => False, others => <>);
125+
end Disable_Region;
126+
127+
end Cortex_M.MPU;
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
------------------------------------------------------------------------------
2+
-- --
3+
-- Copyright (C) 2026, AdaCore --
4+
-- --
5+
-- Redistribution and use in source and binary forms, with or without --
6+
-- modification, are permitted provided that the following conditions are --
7+
-- met: --
8+
-- 1. Redistributions of source code must retain the above copyright --
9+
-- notice, this list of conditions and the following disclaimer. --
10+
-- 2. Redistributions in binary form must reproduce the above copyright --
11+
-- notice, this list of conditions and the following disclaimer in --
12+
-- the documentation and/or other materials provided with the --
13+
-- distribution. --
14+
-- 3. Neither the name of the copyright holder nor the names of its --
15+
-- contributors may be used to endorse or promote products derived --
16+
-- from this software without specific prior written permission. --
17+
-- --
18+
-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS --
19+
-- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT --
20+
-- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR --
21+
-- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT --
22+
-- HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, --
23+
-- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT --
24+
-- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, --
25+
-- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY --
26+
-- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT --
27+
-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE --
28+
-- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. --
29+
-- --
30+
------------------------------------------------------------------------------
31+
32+
-- This package provides subprograms to configure the Memory Protection
33+
-- Unit (MPU) on the Cortex-M7 family of CPU.
34+
--
35+
-- On Cortex-M7, the MPU is required when the D-cache is enabled in order
36+
-- to prevent speculative reads from causing bus contention on external
37+
-- memory interfaces (e.g., FMC/SDRAM). Without MPU configuration, the
38+
-- default memory map treats all external memory as Normal, allowing the
39+
-- CPU to issue speculative reads that can starve bus masters such as the
40+
-- LTDC display controller.
41+
42+
with HAL;
43+
with System;
44+
45+
package Cortex_M.MPU is
46+
47+
type Region_Number is range 0 .. 7;
48+
49+
type Region_Size is
50+
(Size_32B, Size_64B, Size_128B, Size_256B,
51+
Size_512B, Size_1KB, Size_2KB, Size_4KB,
52+
Size_8KB, Size_16KB, Size_32KB, Size_64KB,
53+
Size_128KB, Size_256KB, Size_512KB, Size_1MB,
54+
Size_2MB, Size_4MB, Size_8MB, Size_16MB,
55+
Size_32MB, Size_64MB, Size_128MB, Size_256MB,
56+
Size_512MB, Size_1GB, Size_2GB, Size_4GB);
57+
58+
for Region_Size use
59+
(Size_32B => 16#04#, Size_64B => 16#05#,
60+
Size_128B => 16#06#, Size_256B => 16#07#,
61+
Size_512B => 16#08#, Size_1KB => 16#09#,
62+
Size_2KB => 16#0A#, Size_4KB => 16#0B#,
63+
Size_8KB => 16#0C#, Size_16KB => 16#0D#,
64+
Size_32KB => 16#0E#, Size_64KB => 16#0F#,
65+
Size_128KB => 16#10#, Size_256KB => 16#11#,
66+
Size_512KB => 16#12#, Size_1MB => 16#13#,
67+
Size_2MB => 16#14#, Size_4MB => 16#15#,
68+
Size_8MB => 16#16#, Size_16MB => 16#17#,
69+
Size_32MB => 16#18#, Size_64MB => 16#19#,
70+
Size_128MB => 16#1A#, Size_256MB => 16#1B#,
71+
Size_512MB => 16#1C#, Size_1GB => 16#1D#,
72+
Size_2GB => 16#1E#, Size_4GB => 16#1F#);
73+
74+
type Access_Permission is
75+
(No_Access,
76+
Privileged_RW,
77+
Privileged_RW_Unprivileged_RO,
78+
Full_Access,
79+
Privileged_RO,
80+
Privileged_RO_Unprivileged_RO);
81+
82+
for Access_Permission use
83+
(No_Access => 0,
84+
Privileged_RW => 1,
85+
Privileged_RW_Unprivileged_RO => 2,
86+
Full_Access => 3,
87+
Privileged_RO => 5,
88+
Privileged_RO_Unprivileged_RO => 6);
89+
90+
subtype TEX_Level is HAL.UInt3;
91+
92+
subtype Subregion_Disable is HAL.UInt8;
93+
94+
type Region_Configuration is record
95+
Number : Region_Number;
96+
Base_Address : System.Address;
97+
Size : Region_Size;
98+
Subregion_Disable : MPU.Subregion_Disable := 16#00#;
99+
TEX : TEX_Level := 0;
100+
Access_Permission : MPU.Access_Permission := Full_Access;
101+
Execute_Never : Boolean := False;
102+
Shareable : Boolean := False;
103+
Cacheable : Boolean := False;
104+
Bufferable : Boolean := False;
105+
end record;
106+
107+
-- Enable/disable control bits for HAL_MPU_Enable
108+
type MPU_Control is record
109+
Hard_Fault_NMI_Enable : Boolean;
110+
-- When True, the MPU is enabled during hard fault, NMI, and FAULTMASK
111+
Privileged_Default : Boolean;
112+
-- When True, enables the default memory map as a background region
113+
-- for privileged access
114+
end record;
115+
116+
procedure Enable (Control : MPU_Control);
117+
-- Enable the MPU with the specified control options.
118+
-- DSB and ISB are issued after enabling.
119+
120+
procedure Disable;
121+
-- Disable the MPU. DSB and ISB are issued after disabling.
122+
123+
procedure Configure_Region (Config : Region_Configuration);
124+
-- Configure a single MPU region. The region is enabled upon return.
125+
126+
procedure Disable_Region (Number : Region_Number);
127+
-- Disable a single MPU region.
128+
129+
end Cortex_M.MPU;

0 commit comments

Comments
 (0)