Skip to content

Commit 46cc59e

Browse files
committed
Add logging function for STM32F303
1 parent c8e2f91 commit 46cc59e

File tree

3 files changed

+100
-17
lines changed

3 files changed

+100
-17
lines changed

examples/stmicro/stm32/src/blinky.zig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ pub fn main() !void {
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")) {
2424
const pins = (stm32.pins.GlobalConfiguration{
25+
.GPIOC = .{
26+
.PIN4 = .{ .mode = .{ .alternate_function = .{ .afr = .AF7 } } },
27+
.PIN5 = .{ .mode = .{ .alternate_function = .{ .afr = .AF7 } } },
28+
},
2529
.GPIOE = .{
2630
.PIN8 = .{ .mode = .{ .output = .{ .resistor = .Floating, .o_type = .PushPull } } },
2731
.PIN9 = .{ .mode = .{ .output = .{ .resistor = .Floating, .o_type = .PushPull } } },

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

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
pub const microzig = @import("microzig");
2+
<<<<<<< HEAD
3+
=======
4+
pub const hal = microzig.hal;
5+
pub const rcc = hal.rcc;
6+
pub const pins = hal.pins;
7+
const UART_LOG = microzig.hal.uart.Uart(.UART1);
8+
>>>>>>> 6acb63ca (fixup! Add logging function for STM32F303)
29

310
pub const pin_map = .{
411
// circle of LEDs, connected to GPIOE bits 8..15
@@ -21,17 +28,19 @@ pub const pin_map = .{
2128
.LD6 = "PE15",
2229
};
2330

24-
const uart_pin: microzig.hal.uart.Pins = .{ .tx = null, .rx = null };
2531

26-
pub fn debug_write(string: []const u8) void {
27-
const uart1 = microzig.hal.uart.Uart(.UART1, uart_pin).get_or_init(.{
28-
.baud_rate = 9600,
29-
.word_bits = .eight,
30-
.parity = .none,
31-
.stop_bits = .Stop1,
32-
}) catch unreachable;
32+
var uart_log: ?UART_LOG = null;
3333

34-
for (string) |c| {
35-
uart1.tx(c);
34+
// Init should come first or the baud_rate would be too fast for the default HSI.
35+
pub fn init_log() void {
36+
_ = (pins.GlobalConfiguration{
37+
.GPIOC = .{
38+
.PIN4 = .{ .mode = .{ .alternate_function = .{ .afr = .AF7 } } },
39+
.PIN5 = .{ .mode = .{ .alternate_function = .{ .afr = .AF7 } } },
40+
},
41+
}).apply();
42+
uart_log = try microzig.hal.uart.Uart(.UART1).init(.{ .baud_rate = 115200 });
43+
if (uart_log) |*logger| {
44+
microzig.hal.uart.init_logger(logger);
3645
}
3746
}

port/stmicro/stm32/src/hals/STM32F303/uart.zig

Lines changed: 77 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,7 @@ pub const Config = struct {
5454
pub const StopBits = STOP;
5555
pub const DataBits = WordBits;
5656

57-
pub const Pins = struct {
58-
tx: ?type = null,
59-
rx: ?type = null,
60-
};
61-
62-
pub fn Uart(comptime index: UartNum, comptime uart_pins: Pins) type {
63-
_ = uart_pins;
57+
pub fn Uart(comptime index: UartNum) type {
6458
const regs = switch (index) {
6559
.UART1 => USART1,
6660
.UART2 => USART2,
@@ -163,5 +157,81 @@ pub fn Uart(comptime index: UartNum, comptime uart_pins: Pins) type {
163157
const data_with_parity_bit: u9 = regs.RDR.read().RDR;
164158
return @as(u8, @intCast(data_with_parity_bit & self.read_mask()));
165159
}
160+
161+
fn writer_fn(self: *Self, buffer: []const u8) error{}!usize {
162+
for (buffer) |byte| {
163+
self.tx(byte);
164+
}
165+
return buffer.len;
166+
}
167+
};
168+
}
169+
170+
pub fn UartWriter(comptime index: UartNum) type {
171+
return struct {
172+
uart: *Uart(index),
173+
interface: std.Io.Writer,
174+
175+
pub fn init(uart: *Uart(index), buffer: []u8) UartWriter(index) {
176+
return .{
177+
.uart = uart,
178+
.interface = .{
179+
.vtable = &.{
180+
.drain = drain,
181+
},
182+
.buffer = buffer,
183+
},
184+
};
185+
}
186+
187+
pub fn drain(io_w: *std.Io.Writer, data: []const []const u8, splat: usize) std.Io.Writer.Error!usize {
188+
const w: *UartWriter(index) = @alignCast(@fieldParentPtr("interface", io_w));
189+
var n: u32 = 0;
190+
if (io_w.buffer.len > 0) {
191+
_ = try w.uart.writer_fn(io_w.buffered());
192+
n += io_w.consumeAll();
193+
}
194+
for (data[0 .. data.len - 1]) |buf| {
195+
n += try w.uart.writer_fn(buf);
196+
}
197+
const pattern = data[data.len - 1];
198+
for (0..splat) |_| {
199+
n += try w.uart.writer_fn(pattern);
200+
}
201+
return n;
202+
}
203+
};
204+
}
205+
206+
var uart_logger: ?UartWriter(.UART1) = null;
207+
208+
///Set a specific uart instance to be used for logging.
209+
///
210+
///Allows system logging over uart via:
211+
///pub const microzig_options = .{
212+
/// .logFn = hal.uart.log,
213+
///};
214+
pub fn init_logger(uart: *Uart(.UART1)) void {
215+
uart_logger = .init(uart, &.{});
216+
if (uart_logger) |*logger| {
217+
var w = &logger.interface;
218+
w.writeAll("\r\n================ STARTING NEW LOGGER ================\r\n") catch {};
219+
}
220+
}
221+
222+
///Disables logging via the uart instance.
223+
pub fn deinit_logger() void {
224+
uart_logger = null;
225+
}
226+
227+
pub fn log(comptime level: std.log.Level, comptime scope: @TypeOf(.EnumLiteral), comptime format: []const u8, args: anytype) void {
228+
const prefix = comptime level.asText() ++ switch (scope) {
229+
.default => ": ",
230+
else => " (" ++ @tagName(scope) ++ "): ",
166231
};
232+
233+
if (uart_logger) |*logger| {
234+
var w = &logger.interface;
235+
w.print(prefix ++ format ++ "\r\n", args) catch {};
236+
}
167237
}

0 commit comments

Comments
 (0)