Skip to content
Merged
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
2 changes: 1 addition & 1 deletion examples/raspberrypi/rp2xxx/src/uart_echo.zig
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub fn main() !void {
continue;
};

//tries to write one byte with 100ms timeout
// Try to write one byte with 100ms timeout
_ = uart.write_blocking(&data, time.deadline_in_ms(100)) catch {
uart.clear_errors();
};
Expand Down
3 changes: 3 additions & 0 deletions examples/wch/ch32v/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,14 @@ pub fn build(b: *std.Build) void {
.{ .target = mb.ports.ch32v.chips.ch32v203x6, .name = "blinky_systick_ch32v203", .file = "src/blinky_systick.zig" },
.{ .target = mb.ports.ch32v.boards.ch32v203.nano_ch32v203, .name = "nano_ch32v203_blinky", .file = "src/board_blinky.zig" },
.{ .target = mb.ports.ch32v.boards.ch32v203.nano_ch32v203, .name = "nano_ch32v203_blinky_wfe", .file = "src/blinky_wfe.zig" },
.{ .target = mb.ports.ch32v.boards.ch32v203.nano_ch32v203, .name = "nano_ch32v203_uart_echo", .file = "src/uart_echo.zig" },
.{ .target = mb.ports.ch32v.boards.ch32v203.nano_ch32v203, .name = "nano_ch32v203_uart_log", .file = "src/uart_log.zig" },
.{ .target = mb.ports.ch32v.boards.ch32v203.nano_ch32v203, .name = "nano_ch32v203_usb_cdc", .file = "src/usb_cdc.zig" },
.{ .target = mb.ports.ch32v.boards.ch32v203.suzuduino_uno_v1b, .name = "suzuduino_blinky", .file = "src/board_blinky.zig" },
.{ .target = mb.ports.ch32v.boards.ch32v203.suzuduino_uno_v1b, .name = "suzuduino_usb_cdc", .file = "src/usb_cdc.zig" },
.{ .target = mb.ports.ch32v.boards.ch32v203.lana_tny, .name = "lana_tny_dma", .file = "src/dma.zig" },
.{ .target = mb.ports.ch32v.boards.ch32v203.lana_tny, .name = "lana_tny_ws2812", .file = "src/ws2812.zig" },
.{ .target = mb.ports.ch32v.boards.ch32v203.lana_tny, .name = "lana_tny_uart_echo", .file = "src/uart_echo.zig" },
.{ .target = mb.ports.ch32v.boards.ch32v203.lana_tny, .name = "lana_tny_uart_log", .file = "src/uart_log.zig" },
.{ .target = mb.ports.ch32v.boards.ch32v203.lana_tny, .name = "lana_tny_i2c_bus_scan", .file = "src/i2c_bus_scan.zig" },
.{ .target = mb.ports.ch32v.boards.ch32v203.lana_tny, .name = "lana_tny_i2c_eeprom", .file = "src/i2c_eeprom.zig" },
Expand Down
31 changes: 31 additions & 0 deletions examples/wch/ch32v/src/uart_echo.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const microzig = @import("microzig");

const hal = microzig.hal;
const board = microzig.board;
const time = hal.time;

const uart = board.uart_setup;

pub const panic = microzig.panic;

pub const std_options = microzig.std_options(.{});

comptime {
_ = microzig.export_startup();
}

pub fn main() !void {
board.init();

uart.apply(.{ .baud_rate = 115200 });

const usart = uart.instance;

var data: [1]u8 = .{0};
while (true) {
usart.read_blocking(&data, .no_deadline) catch {
continue;
};
_ = usart.write_blocking(&data, time.deadline_in_ms(100)) catch {};
}
}
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_setup = board.uart_setup;

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_setup.apply(.{ .baud_rate = 115200 });
hal.usart.init_logger(uart_setup.instance);

var i: u32 = 0;
while (true) : (i += 1) {
Expand Down
3 changes: 2 additions & 1 deletion port/wch/ch32v/src/boards/LANA_TNY.zig
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ 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.Setup = .{
.instance = .USART2,
.tx_pin = ch32v.gpio.Pin.init(0, 2), // PA2
.rx_pin = ch32v.gpio.Pin.init(0, 3), // PA3
};

pub const pin_config = ch32v.pins.GlobalConfiguration{
Expand Down
6 changes: 5 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,11 @@ pub fn init() void {
}

/// Default UART: USART1 on PA9
pub const uart_config: ch32v.usart.UartConfig = .{};
pub const uart_setup: ch32v.usart.Setup = .{
.instance = .USART1,
.tx_pin = ch32v.gpio.Pin.init(0, 9), // PA9
Comment thread
Grazfather marked this conversation as resolved.
.rx_pin = ch32v.gpio.Pin.init(0, 10), // PA10
};

pub const pin_config = ch32v.pins.GlobalConfiguration{
.GPIOA = .{
Expand Down
8 changes: 8 additions & 0 deletions port/wch/ch32v/src/hals/time.zig
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,11 @@ pub fn delay_us(us: u32) void {
asm volatile ("" ::: .{ .memory = true });
}
}

pub fn deadline_in_ms(time_ms: u32) microzig.drivers.time.Deadline {
return .init_relative(get_time_since_boot(), .from_ms(time_ms));
}

pub fn deadline_in_us(time_us: u64) microzig.drivers.time.Deadline {
return .init_relative(get_time_since_boot(), .from_us(time_us));
}
34 changes: 18 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,26 @@ 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 pins 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 Setup = struct {
instance: USART,
tx_pin: ?gpio.Pin = null,
rx_pin: ?gpio.Pin = null,

/// Apply settings: configure whichever pins are present, then apply
/// the USART peripheral config (clock, baud rate, etc.).
pub fn apply(comptime self: Setup, comptime config: Config) void {
if (self.tx_pin) |tx| tx.configure_alternate_function(.push_pull, .max_50MHz);
if (self.rx_pin) |rx| {
rx.enable_clock();
rx.set_input_mode(.floating);
}
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