Skip to content

Commit b1fd122

Browse files
devBoi76Jan Mrzygłód
andauthored
Add cortex m7 register definitions (#294)
* Add cortex-m7 register definitions * Add cortex m7, compile error message fix * fix dbg address * Add a file containing utilities for cortex-m7 registers * Change helper names to snake_case --------- Co-authored-by: Jan Mrzygłód <[email protected]>
1 parent 06de880 commit b1fd122

File tree

3 files changed

+393
-11
lines changed

3 files changed

+393
-11
lines changed

core/src/cpus/cortex_m.zig

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -155,27 +155,34 @@ const properties = microzig.chip.properties;
155155
// TODO: will have to standardize this with regz code generation
156156
const mpu_present = @hasDecl(properties, "__MPU_PRESENT") and std.mem.eql(u8, properties.__MPU_PRESENT, "1");
157157

158-
const core = blk: {
159-
const Core = enum {
160-
cortex_m0,
161-
cortex_m0p,
162-
cortex_m3,
163-
cortex_m33,
164-
cortex_m4,
165-
};
158+
const Core = enum {
159+
cortex_m0,
160+
cortex_m0p,
161+
cortex_m3,
162+
cortex_m33,
163+
cortex_m4,
164+
cortex_m7,
165+
};
166166

167-
const cortex_m = std.meta.stringToEnum(Core, microzig.config.cpu_name) orelse
168-
@panic(std.fmt.comptimePrint("Unrecognized Cortex-M core name: {s}", .{microzig.config.cpu_name}));
167+
const cortex_m = std.meta.stringToEnum(Core, microzig.config.cpu_name) orelse
168+
@compileError(std.fmt.comptimePrint("Unrecognized Cortex-M core name: {s}", .{microzig.config.cpu_name}));
169169

170+
const core = blk: {
170171
break :blk switch (cortex_m) {
171172
.cortex_m0 => @import("cortex_m/m0"),
172173
.cortex_m0p => @import("cortex_m/m0plus.zig"),
173174
.cortex_m3 => @import("cortex_m/m3.zig"),
174175
.cortex_m33 => @import("cortex_m/m33.zig"),
175-
.cortex_4 => @import("cortex_m/m4.zig"),
176+
.cortex_m4 => @import("cortex_m/m4.zig"),
177+
.cortex_m7 => @import("cortex_m/m7.zig"),
176178
};
177179
};
178180

181+
pub const utils = switch (cortex_m) {
182+
.cortex_m7 => @import("cortex_m/m7_utils.zig"),
183+
else => void{},
184+
};
185+
179186
pub const peripherals = struct {
180187
/// System Control Block (SCB).
181188
pub const scb: *volatile types.peripherals.SystemControlBlock = @ptrFromInt(scb_base);
@@ -191,6 +198,21 @@ pub const peripherals = struct {
191198
@ptrFromInt(mpu_base)
192199
else
193200
@compileError("This chip does not have a MPU.");
201+
202+
pub const dbg: (if (@hasDecl(core, "DebugRegisters"))
203+
*volatile core.DebugRegisters
204+
else
205+
*volatile anyopaque) = @ptrFromInt(coredebug_base);
206+
207+
pub const itm: (if (@hasDecl(core, "ITM"))
208+
*volatile core.ITM
209+
else
210+
*volatile anyopaque) = @ptrFromInt(itm_base);
211+
212+
pub const tpiu: (if (@hasDecl(core, "TPIU"))
213+
*volatile core.TPIU
214+
else
215+
*volatile anyopaque) = @ptrFromInt(tpi_base);
194216
};
195217

196218
pub const types = struct {

core/src/cpus/cortex_m/m7.zig

Lines changed: 301 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,301 @@
1+
const microzig = @import("microzig");
2+
const mmio = microzig.mmio;
3+
4+
pub const SystemControlBlock = extern struct {
5+
/// CPUID Base Register
6+
CPUID: u32,
7+
/// Interrupt Control and State Register
8+
ICSR: mmio.Mmio(packed struct(u32) {
9+
VECTACTIVE: u9,
10+
reserved0: u2 = 0,
11+
RETTOBASE: u1,
12+
VECTPENDING: u9,
13+
reserved1: u1 = 0,
14+
ISRPENDING: u1,
15+
ISRPREEMPT: u1,
16+
reserved2: u1 = 0,
17+
PENDSTCLR: u1,
18+
PENDSTSET: u1,
19+
PENDSVCLR: u1,
20+
PENDSVSET: u1,
21+
reserved3: u2 = 0,
22+
NMIPENDSET: u1,
23+
}),
24+
/// Vector Table Offset Register
25+
VTOR: u32,
26+
/// Application Interrupt and Reset Control Register
27+
AIRCR: u32,
28+
/// System Control Register
29+
SCR: u32,
30+
/// Configuration Control Register
31+
CCR: mmio.Mmio(packed struct(u32) {
32+
NONBASETHRDENA: u1,
33+
USERSETMPEND: u1,
34+
_reserved0: u1 = 0,
35+
UNALIGN_TRP: u1,
36+
DIV_0_TRP: u1,
37+
_reserved1: u3 = 0,
38+
BFHFNMIGN: u1,
39+
STKALIGN: u1,
40+
_padding: u22 = 0,
41+
}),
42+
/// System Handlers Priority Registers
43+
SHP: [3]u8,
44+
/// System Handler Contol and State Register
45+
SHCSR: u32,
46+
/// Configurable Fault Status Register
47+
CFSR: u32,
48+
/// MemManage Fault Status Register
49+
MMSR: u32,
50+
/// BusFault Status Register
51+
BFSR: u32,
52+
/// UsageFault Status Register
53+
UFSR: u32,
54+
/// HardFault Status Register
55+
HFSR: u32,
56+
/// MemManage Fault Address Register
57+
MMAR: u32,
58+
/// BusFault Address Register
59+
BFAR: u32,
60+
/// Auxiliary Fault Status Register not implemented
61+
AFSR: u32,
62+
63+
/// Processor Feature Register
64+
PFR: [2]u32,
65+
/// Debug Feature Register
66+
DFR: u32,
67+
/// Auxilary Feature Register
68+
ADR: u32,
69+
/// Memory Model Feature Register
70+
MMFR: [4]u32,
71+
/// Instruction Set Attributes Register
72+
ISAR: [5]u32,
73+
RESERVED0: [5]u32,
74+
/// Coprocessor Access Control Register
75+
CPACR: u32,
76+
};
77+
78+
pub const NestedVectorInterruptController = extern struct {
79+
/// Interrupt Set-enable Registers
80+
ISER: [7]u32,
81+
_reserved0: [25]u32,
82+
/// Interrupt Clear-enable Registers
83+
ICER: [7]u32,
84+
_reserved1: [25]u32,
85+
/// Interrupt Set-pending Registers
86+
ISPR: [7]u32,
87+
_reserved2: [25]u32,
88+
/// Interrupt Clear-pending Registers
89+
ICPR: [7]u32,
90+
_reserved3: [25]u32,
91+
/// Interrupt Active Bit Registers
92+
IABR: [7]u32,
93+
_reserved4: [57]u32,
94+
/// Interrupt Priority Registers
95+
IP: [239]u8,
96+
_reserved5: [2577]u8,
97+
/// Software Trigger Interrupt Register
98+
STIR: u32,
99+
};
100+
101+
pub const MemoryProtectionUnit = extern struct {
102+
/// MPU Type Register
103+
TYPE: mmio.Mmio(packed struct(u32) {
104+
SEPARATE: u1,
105+
_reserved0: u7,
106+
DREGION: u8,
107+
IREGION: u8,
108+
_reserved1: u8,
109+
}),
110+
/// MPU Control Register
111+
CTRL: mmio.Mmio(packed struct(u32) {
112+
ENABLE: u1,
113+
HFNMIENA: u1,
114+
PRIVDEFENA: u1,
115+
padding: u29,
116+
}),
117+
/// MPU RNRber Register
118+
RNR: mmio.Mmio(packed struct(u32) {
119+
REGION: u8,
120+
padding: u24,
121+
}),
122+
/// MPU Region Base Address Register
123+
RBAR: RBAR,
124+
/// MPU Region Attribute and Size Register
125+
RASR: RASR,
126+
/// MPU Alias 1 Region Base Address Register
127+
RBAR_A1: RBAR,
128+
/// MPU Alias 1 Region Attribute and Size Register
129+
RASR_A1: RASR,
130+
/// MPU Alias 2 Region Base Address Register
131+
RBAR_A2: RBAR,
132+
/// MPU Alias 2 Region Attribute and Size Register
133+
RASR_A2: RASR,
134+
/// MPU Alias 3 Region Base Address Register
135+
RBAR_A3: RBAR,
136+
/// MPU Alias 3 Region Attribute and Size Register
137+
RASR_A3: RASR,
138+
139+
pub const RBAR = mmio.Mmio(packed struct(u32) {
140+
REGION: u4,
141+
VALID: u1,
142+
ADDR: u27,
143+
});
144+
145+
pub const RASR = mmio.Mmio(packed struct(u32) {
146+
/// Region enable bit
147+
ENABLE: u1,
148+
/// Region Size
149+
SIZE: u5,
150+
_reserved0: u2,
151+
/// Sub-Region Disable
152+
SRD: u8,
153+
/// ATTRS.B
154+
B: u1,
155+
/// ATTRS.C
156+
C: u1,
157+
/// ATTRS.S
158+
S: u1,
159+
/// ATTRS.TEX
160+
TEX: u3,
161+
_reserved1: u2,
162+
/// ATTRS.AP
163+
AP: u3,
164+
/// ATTRS.XN
165+
XN: u1,
166+
padding: u4,
167+
});
168+
};
169+
170+
pub const DebugRegisters = extern struct {
171+
/// Debyg Halting Control and Status Register
172+
DHCSR: mmio.Mmio(packed struct {
173+
_reserved_0: u6,
174+
S_RESET_ST: u1,
175+
S_RETIRE_ST: u1,
176+
_reserved_1: u4,
177+
S_LOCKUP: u1,
178+
S_SLEEP: u1,
179+
S_HALT: u1,
180+
S_REGRDY: u1,
181+
_reserved_2: u10,
182+
C_SNAPSTALL: u1,
183+
_reserved_3: u1,
184+
C_MASKINTS: u1,
185+
C_STEP: u1,
186+
C_HALT: u1,
187+
C_DEBUGEN: u1,
188+
}),
189+
/// Debug Core Register Selector Register
190+
/// TODO: Reserved have values ? see armv7-m reference manual
191+
DCRSR: mmio.Mmio(packed struct {
192+
_reserved_0: u15,
193+
REGWnR: u1,
194+
_reserved_1: u9,
195+
REGSEL: u7,
196+
}),
197+
/// Debug Core Register Data Register
198+
DCRDR: mmio.Mmio(packed struct {
199+
DBGTMP: u32,
200+
}),
201+
/// Debug exception and Monitor Control Register
202+
DEMCR: mmio.Mmio(packed struct {
203+
_reserved_0: u7,
204+
TRCENA: u1,
205+
_reserved_1: u4,
206+
MON_REQ: u1,
207+
MON_STEP: u1,
208+
MON_PEND: u1,
209+
MON_EN: u1,
210+
_reserved_2: u5,
211+
VC_HARDERR: u1,
212+
VC_INTERR: u1,
213+
VC_BUSERR: u1,
214+
VC_STATERR: u1,
215+
VC_CHKERR: u1,
216+
VC_NOCPERR: u1,
217+
VC_MMERR: u1,
218+
_reserved_3: u3,
219+
VC_CORERESET: u1,
220+
}),
221+
};
222+
223+
pub const ITM = extern struct {
224+
/// Stimulus Port Registers (0-255)
225+
STIM: [256]mmio.Mmio(packed union {
226+
WRITE_U8: u8,
227+
WRITE_U16: u16,
228+
WRITE_U32: u32,
229+
READ: packed struct(u32) {
230+
FIFOREADY: u1,
231+
_reserved: u31,
232+
},
233+
}),
234+
235+
_reserved0: [640]u32, // Padding to 0xE00
236+
237+
/// Trace Enable Registers (0-7)
238+
TER: [8]mmio.Mmio(packed struct(u32) {
239+
STIMENA: u32, // Enable bits for stimulus ports
240+
}),
241+
242+
_reserved1: [10]u32, // Padding to 0xE40
243+
244+
/// Trace Privilege Register
245+
TPR: mmio.Mmio(packed struct(u32) {
246+
PRIVMASK: u32, // Privilege mask for stimulus ports
247+
}),
248+
249+
_reserved2: [15]u32, // Padding to 0xE80
250+
251+
/// Trace Control Register
252+
TCR: mmio.Mmio(packed struct(u32) {
253+
ITMENA: u1, // ITM enable
254+
TSENA: u1, // Local timestamp enable
255+
SYNCENA: u1, // Sync packet enable
256+
TXENA: u1, // DWT packet forwarding enable
257+
SWOENA: u1, // Async clock enable
258+
_reserved0: u3,
259+
TSPrescale: u2, // Local timestamp prescaler
260+
GTSFREQ: u2, // Global timestamp frequency
261+
_reserved1: u4,
262+
TraceBusID: u7, // Trace bus ID
263+
BUSY: u1, // ITM busy flag
264+
_reserved2: u8,
265+
}),
266+
};
267+
268+
pub const TPIU = extern struct {
269+
/// Supported Parallel Port Sizes Register
270+
SSPSR: mmio.Mmio(packed struct(u32) {
271+
SWIDTH: u32,
272+
}),
273+
/// Current Parallel Port Size Register
274+
CSPSR: mmio.Mmio(packed struct(u32) {
275+
CWIDTH: u32,
276+
}),
277+
_reserved0: [2]u32,
278+
/// Asynchronous Clock Prescaler Register
279+
ACPR: mmio.Mmio(packed struct(u32) {
280+
SWOSCALER: u16,
281+
_padding: u16,
282+
}),
283+
_reserved1: [55]u32,
284+
/// Selected Pin Protocol Register
285+
SPPR: mmio.Mmio(packed struct(u32) {
286+
TXMODE: u2,
287+
_padding: u30,
288+
}),
289+
_reserved2: [524]u32,
290+
/// TPIU Type Register
291+
TYPE: mmio.Mmio(packed struct(u32) {
292+
_reserved0: u6,
293+
FIFOSZ: u3,
294+
PTINVALID: u1,
295+
MANCVALID: u1,
296+
NRZVALID: u1,
297+
_implementation_defined0: u4,
298+
_padding: u16,
299+
}),
300+
_reserved3: [13]u32,
301+
};

0 commit comments

Comments
 (0)