Skip to content

Commit 9f64648

Browse files
authored
Better HAL for STM32F303 (#729)
* Rework STM32F303 HAL to better match other HAL and fix to support latest regz register. * Add alternate function to GPIO * Migrate UART for STM32F303 to its own file and add better support. * Add logging function for STM32F303 * Initial setup for RCC. * Add I2C and improve FPU for stm32f3 core
1 parent f154f03 commit 9f64648

File tree

11 files changed

+1023
-583
lines changed

11 files changed

+1023
-583
lines changed

core/src/cpus/cortex_m/m4.zig

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,23 @@ pub const SystemControlBlock = extern struct {
8484
BFAR: u32,
8585
/// Auxilary Feature Register.
8686
AFSR: u32,
87+
reserved0: [18]u32,
88+
CPACR: mmio.Mmio(packed struct(u32) {
89+
reserved0: u20,
90+
CP10: Privilege,
91+
CP11: Privilege,
92+
reserved1: u8,
93+
94+
pub const Privilege = enum(u2) {
95+
/// Access denied. Any attempted access generates a NOCP UsageFault.
96+
access_denied = 0b00,
97+
/// Privileged access only. An unprivileged access generates a NOCP UsageFault.
98+
priviledged_access_only = 0b01,
99+
reserved = 0b10,
100+
/// Full access.
101+
full_access = 0b11,
102+
};
103+
}),
87104
};
88105

89106
pub const NestedVectorInterruptController = extern struct {
@@ -187,3 +204,39 @@ pub const MemoryProtectionUnit = extern struct {
187204
reserved2: u3 = 0,
188205
});
189206
};
207+
208+
pub const FloatingPointUnit = extern struct {
209+
FPCCR: mmio.Mmio(packed struct(u32) {
210+
LSPACT: u1,
211+
USER: u1,
212+
reserved0: u1 = 0,
213+
THREAD: u1,
214+
HFRDY: u1,
215+
MMRDY: u1,
216+
BFRDY: u1,
217+
reserved1: u1 = 0,
218+
MONRDY: u1,
219+
reserved2: u21 = 0,
220+
/// Automatic state preservation enable. Enables lazy context save of
221+
/// floating-point state. The possible values of this bit are:
222+
/// 0 = Disable automatic lazy context save.
223+
/// 1 = Enable automatic lazy state preservation for floating-point
224+
/// context.
225+
///
226+
/// Writes to this bit from Non-secure state are ignored if LSPENS is
227+
/// set to one.
228+
LSPEN: u1,
229+
/// Automatic state preservation enable. Enables CONTROL.FPCA setting
230+
/// on execution of a floating-point instruction. This results in
231+
/// automatic hardware state preservation and restoration, for
232+
/// floating-point context, on exception entry and exit. The possible
233+
/// values of this bit are:
234+
/// 1 = Enable CONTROL.FPCA setting on execution of a floating-point
235+
/// instruction.
236+
/// 0 = Disable CONTROL.FPCA setting on execution of a
237+
/// floating-point instruction.
238+
ASPEN: u1,
239+
}),
240+
FPCAR: u32,
241+
FPDSCR: u32,
242+
};

examples/stmicro/stm32/src/blinky.zig

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,33 @@ pub fn main() !void {
2121
};
2222
break :res .{ pins, all_leds };
2323
} else if (comptime microzig.config.board_name != null and std.mem.eql(u8, microzig.config.board_name.?, "STM32F3DISCOVERY")) {
24-
const pins = (stm32.pins.GlobalConfiguration{ .GPIOE = .{
25-
.PE8 = .{ .mode = .{ .output = .push_pull } },
26-
.PE9 = .{ .mode = .{ .output = .push_pull } },
27-
.PE10 = .{ .mode = .{ .output = .push_pull } },
28-
.PE11 = .{ .mode = .{ .output = .push_pull } },
29-
.PE12 = .{ .mode = .{ .output = .push_pull } },
30-
.PE13 = .{ .mode = .{ .output = .push_pull } },
31-
.PE14 = .{ .mode = .{ .output = .push_pull } },
32-
.PE15 = .{ .mode = .{ .output = .push_pull } },
33-
} }).apply();
24+
const pins = (stm32.pins.GlobalConfiguration{
25+
.GPIOC = .{
26+
.PIN4 = .{ .mode = .{ .alternate_function = .{ .afr = .AF7 } } },
27+
.PIN5 = .{ .mode = .{ .alternate_function = .{ .afr = .AF7 } } },
28+
},
29+
.GPIOE = .{
30+
.PIN8 = .{ .mode = .{ .output = .{ .resistor = .Floating, .o_type = .PushPull } } },
31+
.PIN9 = .{ .mode = .{ .output = .{ .resistor = .Floating, .o_type = .PushPull } } },
32+
.PIN10 = .{ .mode = .{ .output = .{ .resistor = .Floating, .o_type = .PushPull } } },
33+
.PIN11 = .{ .mode = .{ .output = .{ .resistor = .Floating, .o_type = .PushPull } } },
34+
.PIN12 = .{ .mode = .{ .output = .{ .resistor = .Floating, .o_type = .PushPull } } },
35+
.PIN13 = .{ .mode = .{ .output = .{ .resistor = .Floating, .o_type = .PushPull } } },
36+
.PIN14 = .{ .mode = .{ .output = .{ .resistor = .Floating, .o_type = .PushPull } } },
37+
.PIN15 = .{ .mode = .{ .output = .{ .resistor = .Floating, .o_type = .PushPull } } },
38+
},
39+
}).apply();
3440
const all_leds = .{
35-
pins.PE8,
36-
pins.PE9,
37-
pins.PE10,
38-
pins.PE11,
39-
pins.PE12,
40-
pins.PE13,
41-
pins.PE14,
42-
pins.PE15,
41+
pins.PIN8,
42+
pins.PIN9,
43+
pins.PIN10,
44+
pins.PIN11,
45+
pins.PIN12,
46+
pins.PIN13,
47+
pins.PIN14,
48+
pins.PIN15,
4349
};
50+
4451
break :res .{ pins, all_leds };
4552
} else {
4653
@compileError("blinky is not (yet?) implemented for this target");

port/stmicro/stm32/src/boards/STM32F3DISCOVERY.zig

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
const std = @import("std");
2+
13
pub const microzig = @import("microzig");
24

3-
pub const cpu_frequency = 8_000_000;
5+
pub const hal = microzig.hal;
6+
pub const rcc = hal.rcc;
7+
pub const pins = hal.pins;
8+
const UART_LOG = microzig.hal.uart.Uart(.UART1);
49

510
pub const pin_map = .{
611
// circle of LEDs, connected to GPIOE bits 8..15
@@ -23,15 +28,29 @@ pub const pin_map = .{
2328
.LD6 = "PE15",
2429
};
2530

26-
pub fn debug_write(string: []const u8) void {
27-
const uart1 = microzig.core.experimental.Uart(1, .{}).get_or_init(.{
28-
.baud_rate = 9600,
29-
.data_bits = .eight,
30-
.parity = null,
31-
.stop_bits = .one,
32-
}) catch unreachable;
31+
pub fn init() void {
32+
hal.enable_fpu();
33+
rcc.enable_hse(8_000_000);
34+
rcc.enable_pll(.HSE, .Div1, .Mul5) catch {
35+
@panic("PLL faile to enable");
36+
};
37+
rcc.select_pll_for_sysclk() catch {
38+
@panic("Faile to select sysclk");
39+
};
40+
}
41+
42+
var uart_log: ?UART_LOG = null;
3343

34-
const writer = uart1.writer();
35-
_ = writer.write(string) catch unreachable;
36-
uart1.internal.txflush();
44+
// Init should come first or the baud_rate would be too fast for the default HSI.
45+
pub fn init_log() void {
46+
_ = (pins.GlobalConfiguration{
47+
.GPIOC = .{
48+
.PIN4 = .{ .mode = .{ .alternate_function = .{ .afr = .AF7 } } },
49+
.PIN5 = .{ .mode = .{ .alternate_function = .{ .afr = .AF7 } } },
50+
},
51+
}).apply();
52+
uart_log = try microzig.hal.uart.Uart(.UART1).init(.{ .baud_rate = 115200 });
53+
if (uart_log) |*logger| {
54+
microzig.hal.uart.init_logger(logger);
55+
}
3756
}

0 commit comments

Comments
 (0)