Skip to content

Conversation

@Copper280z
Copy link
Contributor

@Copper280z Copper280z commented Jan 31, 2026

This adds a new driver for the ch32 USBHD/USBHS peripheral, USB_OTG_FS peripheral, and examples for the ch32v307, and ch32v203 boards.

@github-actions
Copy link

github-actions bot commented Jan 31, 2026

Lint Results

Found 12 issues on changed lines in 4 files:

  • examples/wch/ch32v/src/usb_cdc.zig: 1 issue
  • port/wch/ch32v/src/hals/clocks.zig: 1 issue
  • port/wch/ch32v/src/hals/usbfs.zig: 5 issues
  • port/wch/ch32v/src/hals/usbhs.zig: 5 issues

_reserved1: u2 = 0,
});

fn baseAddr() usize {
Copy link

Choose a reason for hiding this comment

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

Please change to base_addr, in MicroZig we use snake case for function names.

Copy link
Collaborator

Choose a reason for hiding this comment

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

this one actually makes sense

Copy link
Collaborator

@Grazfather Grazfather left a comment

Choose a reason for hiding this comment

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

Reviewed everything except for the HALs themselves (since this is a draft). Will get to those when you think they're ready.

.bcd_usb = config.bcd_usb,
.device_triple = config.device_triple,
.max_packet_size0 = @max(config.max_supported_packet_size, 64),
.max_packet_size0 = @min(config.max_supported_packet_size, 64),
Copy link
Collaborator

Choose a reason for hiding this comment

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

woof

.{ .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_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" },
Copy link
Collaborator

Choose a reason for hiding this comment

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

is this one tested?

.{ .target = mb.ports.ch32v.boards.ch32v203.lana_tny, .name = "lana_tny_spi_loopback", .file = "src/spi_loopback.zig" },
.{ .target = mb.ports.ch32v.boards.ch32v203.lana_tny, .name = "lana_tny_spi_flash_w25q", .file = "src/spi_flash_w25q.zig" },
.{ .target = mb.ports.ch32v.boards.ch32v203.lana_tny, .name = "lana_tny_sharp_niceview", .file = "src/sharp_niceview.zig" },
.{ .target = mb.ports.ch32v.boards.ch32v203.lana_tny, .name = "lana_tny_usb_cdc", .file = "src/usb_cdc.zig" },
Copy link
Collaborator

Choose a reason for hiding this comment

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

and this one? I have this so I can test it for you.

pub const usart = @import("usart.zig");
pub const spi = @import("spi.zig");
pub const dma = @import("dma.zig");
pub const usb = @import("usbfs.zig");
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you add this in the test block as well

/// HSI (High Speed Internal) oscillator frequency
/// This is the fixed internal RC oscillator frequency for CH32V30x
pub const hsi_frequency: u32 = 8_000_000; // 8 MHz
pub const hse_frequency: u32 = 8_000_000; // 8 MHz
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should be board level, since it's external to the chip and may not be present.

/// selects whether the USBHS 48MHz clock comes from the system PLL clock or the USB PHY,
/// and enables the AHB clock gate for USBHS.
///
/// Note: The SVD names bit31 as `USBFSSRC`, but the reference manual
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we patch this? Which SVD? All of them?

Comment on lines +19 to +20
const tog_pin = gpio.Pin.init(0, 14);
const mco_pin = gpio.Pin.init(0, 8);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Might as well keep with the comments e.g. PA14 and PA8

.configurations = &.{.{
.attributes = .{ .self_powered = false },
.max_current_ma = 50,
.Drivers = struct { serial: USB_Serial },
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you maybe copy some of the extra comments I added to the cdc example for rpi?

Comment on lines +49 to +51
pub var usb_dev: hal.usb.Polled(
.{},
) = undefined;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
pub var usb_dev: hal.usb.Polled(
.{},
) = undefined;
pub var usb_dev: hal.usb.Polled(.{}) = undefined;

Comment on lines +114 to +116
// pins.tog.put(0);
usb_dev.poll(false, &usb_controller);
// pins.tog.put(1);
Copy link
Collaborator

Choose a reason for hiding this comment

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

gets rid of toggle pins completely or uncomment these please

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants