Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions examples/wch/ch32v/src/uart_log.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion port/wch/ch32v/src/boards/LANA_TNY.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
Expand Down
5 changes: 4 additions & 1 deletion port/wch/ch32v/src/boards/nanoCH32V203.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will make this optional, support rx_pin, but I don't have access to hardware to test the rx functionality.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay let me know once that's tested and we'll merge.

};

pub const pin_config = ch32v.pins.GlobalConfiguration{
.GPIOA = .{
Expand Down
35 changes: 19 additions & 16 deletions port/wch/ch32v/src/hals/usart.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading