1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Pure FRAM-MPU segmentation math for the `mpu` module.
//
// Like `fram_addr.rs` and `comp_ladder.rs`, this file is intentionally
// dependency-free (pure `core` integer arithmetic, no PAC/HAL types) so the
// exact same source can be `include!`d by the host-side test crate in
// `unit_tests/`. The `mpu::Mpu` driver is a thin wrapper over these
// conversions — a regression here (a shifted border, a transposed MPUSAM
// nibble) fails the host tests without a board on the desk. Do not add
// external `use`s. (Regular `//` comments, not `//!`, so the file can be
// `include!`d mid-crate.)
//
// # The hardware's segmentation model (SLAU367P chapter 10)
//
// The MPU carves the *main* FRAM (`0x4400..=0x13FFF` on the FR5969 — both
// banks, straddling the 16-bit boundary) into three contiguous segments with
// two movable borders, plus one fixed segment for the Information FRAM:
//
// ```text
// 0x4400 ──── MMSEG1 ──── B1 ──── MMSEG2 ──── B2 ──── MMSEG3 ──── 0x13FFF
// 0x1800 ─────────────── info segment ─────────────────────────── 0x19FF
// ```
//
// A border register holds the border address right-shifted by 4 (the
// registers are 16 bits wide but must name 20-bit addresses; 16-byte
// granularity is what falls out). Each segment gets a 4-bit nibble in
// `MPUSAM`: read / write / execute enables plus a "violation select" that
// chooses between latching a flag (optionally escalated to a system NMI) and
// resetting the chip with a PUC.
/// First byte of main FRAM — where segment 1 begins.
pub const MAIN_START: u32 = 0x4400;
/// One past the last byte of main FRAM (`0x13FFF`) — where segment 3 ends.
/// A border placed here makes segment 3 empty, just as a border at
/// [`MAIN_START`] makes segment 1 empty.
pub const MAIN_END: u32 = 0x1_4000;
/// Errors from MPU segmentation setup.
/// What a segment violation does, beyond being latched in `MPUCTL1`
/// (the `MPUSEGxVS` bit of the segment's `MPUSAM` nibble).
/// Access rights for one MPU segment — one `MPUSAM` nibble.
/// Convert a border address to its `MPUSEGBx` register value (`addr >> 4`).
///
/// The address must be 16-byte aligned (the register cannot represent finer)
/// and within main FRAM `0x4400..=0x14000` — both ends inclusive, because a
/// border *at* either end is the legitimate way to make the outer segment
/// empty.
pub
/// The border address a raw `MPUSEGBx` register value names (`value << 4`).
pub
/// Compose the full 16-bit `MPUSAM` value from the four segment nibbles.
/// Segment 1 sits in bits 3:0, segment 2 in 7:4, segment 3 in 11:8, and the
/// info segment in 15:12. Sanity anchor: four `Access::rwx()` gives `0x7777`,
/// the register's documented reset value.
pub
/// Which main-memory segment (1, 2, or 3) contains `addr`, given the two
/// border addresses. Borders belong to the higher segment: segment 1 is
/// `[MAIN_START, b1)`, segment 2 `[b1, b2)`, segment 3 `[b2, MAIN_END)` —
/// SLAU367P: "the border address is the first address of the next segment".
/// Useful for predicting which violation flag an access will raise; `addr`
/// is assumed to be within main FRAM.