From 891f1c8de710323fa975534fc6bc35b7c8b82450 Mon Sep 17 00:00:00 2001 From: Graziano Misuraca Date: Sun, 30 Aug 2026 11:13:21 -0400 Subject: [PATCH 1/2] ch32v: Revamp the board-level usart setup design Following up on discussion on discord, addressing some concerns. 1. 'Config' vs. 'Setup' 2. Stuff like baud should not be in the board file. It is an 'application level' configuration. 3. The setup function is clunky. Put it into a method. 4. The `@hasDecl` is clunky. Move it into the method. --- examples/wch/ch32v/src/uart_log.zig | 6 ++-- port/wch/ch32v/src/boards/LANA_TNY.zig | 2 +- port/wch/ch32v/src/boards/nanoCH32V203.zig | 5 +++- port/wch/ch32v/src/hals/usart.zig | 35 ++++++++++++---------- 4 files changed, 27 insertions(+), 21 deletions(-) diff --git a/examples/wch/ch32v/src/uart_log.zig b/examples/wch/ch32v/src/uart_log.zig index 3d5328ac9..f4dd85c9a 100644 --- a/examples/wch/ch32v/src/uart_log.zig +++ b/examples/wch/ch32v/src/uart_log.zig @@ -4,7 +4,7 @@ const hal = microzig.hal; const board = microzig.board; const time = hal.time; -const uart_cfg: hal.usart.UartConfig = if (@hasDecl(board, "uart_config")) board.uart_config else .{}; +const uart = hal.usart.UartSetup.default; pub const panic = microzig.panic; @@ -20,8 +20,8 @@ comptime { pub fn main() !void { board.init(); - hal.usart.setup_uart(uart_cfg); - hal.usart.init_logger(uart_cfg.instance); + uart.setup(.{ .baud_rate = 115200 }); + hal.usart.init_logger(uart.instance); var i: u32 = 0; while (true) : (i += 1) { diff --git a/port/wch/ch32v/src/boards/LANA_TNY.zig b/port/wch/ch32v/src/boards/LANA_TNY.zig index 832767d1e..dc6f8d3f5 100644 --- a/port/wch/ch32v/src/boards/LANA_TNY.zig +++ b/port/wch/ch32v/src/boards/LANA_TNY.zig @@ -21,7 +21,7 @@ pub fn init() void { } /// Default UART: USART2 on PA2 (exposed on the board header) -pub const uart_config: ch32v.usart.UartConfig = .{ +pub const uart_setup: ch32v.usart.UartSetup = .{ .instance = .USART2, .tx_pin = ch32v.gpio.Pin.init(0, 2), // PA2 }; diff --git a/port/wch/ch32v/src/boards/nanoCH32V203.zig b/port/wch/ch32v/src/boards/nanoCH32V203.zig index c69aa5481..2a55160c4 100644 --- a/port/wch/ch32v/src/boards/nanoCH32V203.zig +++ b/port/wch/ch32v/src/boards/nanoCH32V203.zig @@ -25,7 +25,10 @@ pub fn init() void { } /// Default UART: USART1 on PA9 -pub const uart_config: ch32v.usart.UartConfig = .{}; +pub const uart_setup: ch32v.usart.UartSetup = .{ + .instance = .USART1, + .tx_pin = ch32v.gpio.Pin.init(0, 9), // PA9 +}; pub const pin_config = ch32v.pins.GlobalConfiguration{ .GPIOA = .{ diff --git a/port/wch/ch32v/src/hals/usart.zig b/port/wch/ch32v/src/hals/usart.zig index d10cb4282..25ebc8390 100644 --- a/port/wch/ch32v/src/hals/usart.zig +++ b/port/wch/ch32v/src/hals/usart.zig @@ -92,24 +92,27 @@ pub const ReceiveError = error{ const gpio = hal.gpio; -/// Configuration for board-level UART defaults. -/// Boards export a `uart_config` const of this type with their preferred -/// USART instance, TX pin, and serial settings. Examples can use these -/// defaults directly or construct their own `UartConfig`. -pub const UartConfig = struct { - instance: USART = .USART1, - tx_pin: gpio.Pin = gpio.Pin.init(0, 9), // PA9 (USART1 default TX) - config: Config = .{ .baud_rate = 115200 }, +/// Physical-layer UART setup: which USART instance and TX pin to use. +/// Boards export a `uart_setup` const of this type. Application-level +/// settings (baud rate, parity, etc.) are passed separately via `Config`. +pub const UartSetup = struct { + instance: USART, + tx_pin: gpio.Pin, + + /// Throw a compile error if an application tries to use the default, but + /// the board it is being compiled for does not provide one. + pub const default: UartSetup = if (microzig.config.has_board and @hasDecl(microzig.board, "uart_setup")) + microzig.board.uart_setup + else + @compileError("board does not provide a default uart_setup"); + + /// Apply 'setup' and 'config' + pub fn setup(comptime self: UartSetup, comptime config: Config) void { + self.tx_pin.configure_alternate_function(.push_pull, .max_50MHz); + self.instance.apply(config); + } }; -/// Configure a UART from a `UartConfig`: sets up the TX pin as alternate -/// function push-pull, then applies the USART peripheral configuration -/// (clock enable, AFIO remap, baud rate, etc.). -pub fn setup_uart(comptime cfg: UartConfig) void { - cfg.tx_pin.configure_alternate_function(.push_pull, .max_50MHz); - cfg.instance.apply(cfg.config); -} - pub const instance = struct { pub const USART1: USART = .USART1; pub const USART2: USART = .USART2; From e7dbe15bc84ee7b6124dccbb3eaf79dbe300c8f8 Mon Sep 17 00:00:00 2001 From: Graziano Misuraca Date: Sun, 30 Aug 2026 14:01:45 -0400 Subject: [PATCH 2/2] s/setup/apply --- examples/wch/ch32v/src/uart_log.zig | 2 +- port/wch/ch32v/src/hals/usart.zig | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/wch/ch32v/src/uart_log.zig b/examples/wch/ch32v/src/uart_log.zig index f4dd85c9a..972e3083a 100644 --- a/examples/wch/ch32v/src/uart_log.zig +++ b/examples/wch/ch32v/src/uart_log.zig @@ -20,7 +20,7 @@ comptime { pub fn main() !void { board.init(); - uart.setup(.{ .baud_rate = 115200 }); + uart.apply(.{ .baud_rate = 115200 }); hal.usart.init_logger(uart.instance); var i: u32 = 0; diff --git a/port/wch/ch32v/src/hals/usart.zig b/port/wch/ch32v/src/hals/usart.zig index 25ebc8390..0db191031 100644 --- a/port/wch/ch32v/src/hals/usart.zig +++ b/port/wch/ch32v/src/hals/usart.zig @@ -106,8 +106,8 @@ pub const UartSetup = struct { else @compileError("board does not provide a default uart_setup"); - /// Apply 'setup' and 'config' - pub fn setup(comptime self: UartSetup, comptime config: Config) void { + /// Apply settings + pub fn apply(comptime self: UartSetup, comptime config: Config) void { self.tx_pin.configure_alternate_function(.push_pull, .max_50MHz); self.instance.apply(config); }