diff --git a/examples/wch/ch32v/src/uart_log.zig b/examples/wch/ch32v/src/uart_log.zig index 3d5328ac9..972e3083a 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.apply(.{ .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..0db191031 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 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); + } }; -/// 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;