Skip to content

ch32 USBHD/USBHS driver implementation#893

Draft
Copper280z wants to merge 94 commits into
ZigEmbeddedGroup:mainfrom
Copper280z:ch32_usb
Draft

ch32 USBHD/USBHS driver implementation#893
Copper280z wants to merge 94 commits into
ZigEmbeddedGroup:mainfrom
Copper280z:ch32_usb

Conversation

@Copper280z

@Copper280z Copper280z commented Jan 31, 2026

Copy link
Copy Markdown
Contributor

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

github-actions Bot commented Jan 31, 2026

Copy link
Copy Markdown

Lint Results

Found 11 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: 4 issues

Comment thread port/wch/ch32v/src/hals/usbhs.zig Outdated
_reserved1: u2 = 0,
});

fn baseAddr() usize {

Copy link
Copy Markdown
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

@Grazfather Grazfather left a comment

Copy link
Copy Markdown
Collaborator

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.

Comment thread core/src/core/usb.zig
Comment thread examples/wch/ch32v/build.zig
Comment thread examples/wch/ch32v/build.zig Outdated
Comment thread port/wch/ch32v/src/hals/ch32v20x.zig
Comment thread port/wch/ch32v/src/hals/ch32v30x.zig Outdated
Comment thread port/wch/ch32v/src/hals/clocks.zig
Comment thread examples/wch/ch32v/src/usb_cdc.zig Outdated
Comment thread examples/wch/ch32v/src/usb_cdc.zig
Comment thread examples/wch/ch32v/src/usb_cdc.zig Outdated
Comment thread examples/wch/ch32v/src/usb_cdc.zig Outdated
Comment on lines +114 to +116
// pins.tog.put(0);
usb_dev.poll(false, &usb_controller);
// pins.tog.put(1);

Copy link
Copy Markdown
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

Grazfather and others added 6 commits May 30, 2026 10:39
The previous `init() Self` returned a copy of the struct, but set up DMA
in the same function, causing DMA registers to be programmed with
addresses pointing into a stack temporary.
Rewrite hw_init to follow WCH SDK init sequence with raw register
writes, fixing endpoint mode configuration (disable EP4 which shares
DMA with EP0, only enable EP1-3 needed for CDC-ACM).

Fix poll loop: mask out SOF in the ignore filter to prevent spinning,
use single-pass with early return, and add EP0 OUT re-arm after IN
completion to correctly handle SETUP toggle sequencing.
_ = microzig.export_startup();
}

const USBController = usb.DeviceController(.{

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggestion: Rename USBController to USB_Controller, it should be more in line with our style guidelines. This automation is not perfect so take it with a grain of salt.

///
/// Note: The SVD names bit31 as `USBFSSRC`, but the reference manual
/// describes it as `USBHSSRC` ("USBHS 48MHz clock source selection").
pub const UsbHsClockConfig = struct {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggestion: Rename UsbHsClockConfig to USB_HsClockConfig, it should be more in line with our style guidelines. This automation is not perfect so take it with a grain of salt.


const Regs = peripherals.USB_OTG_FS;

const EpState = struct {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggestion: Rename EpState to EP_State, it should be more in line with our style guidelines. This automation is not perfect so take it with a grain of salt.

}

// We use offset-based access for per-EP regs to keep helpers compact.
const RegU32 = microzig.mmio.Mmio(packed struct(u32) { v: u32 = 0 });

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggestion: Rename RegU32 to Reg_U32, it should be more in line with our style guidelines. This automation is not perfect so take it with a grain of salt.


// We use offset-based access for per-EP regs to keep helpers compact.
const RegU32 = microzig.mmio.Mmio(packed struct(u32) { v: u32 = 0 });
const RegU16 = microzig.mmio.Mmio(packed struct(u16) { v: u16 = 0 });

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggestion: Rename RegU16 to Reg_U16, it should be more in line with our style guidelines. This automation is not perfect so take it with a grain of salt.

padding: u4 = 0,
});

fn baseAddr() usize {

Copy link
Copy Markdown

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.


const Regs = peripherals.USBHS;

const EpState = struct {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggestion: Rename EpState to EP_State, it should be more in line with our style guidelines. This automation is not perfect so take it with a grain of salt.

}

// We use offset-based access for per-EP regs to keep helpers compact.
const RegU32 = microzig.mmio.Mmio(packed struct(u32) { v: u32 = 0 });

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggestion: Rename RegU32 to Reg_U32, it should be more in line with our style guidelines. This automation is not perfect so take it with a grain of salt.


// We use offset-based access for per-EP regs to keep helpers compact.
const RegU32 = microzig.mmio.Mmio(packed struct(u32) { v: u32 = 0 });
const RegU16 = microzig.mmio.Mmio(packed struct(u16) { v: u16 = 0 });

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggestion: Rename RegU16 to Reg_U16, it should be more in line with our style guidelines. This automation is not perfect so take it with a grain of salt.

// We use offset-based access for per-EP regs to keep helpers compact.
const RegU32 = microzig.mmio.Mmio(packed struct(u32) { v: u32 = 0 });
const RegU16 = microzig.mmio.Mmio(packed struct(u16) { v: u16 = 0 });
const RegU8 = microzig.mmio.Mmio(packed struct(u8) { v: u8 = 0 });

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggestion: Rename RegU8 to Reg_U8, it should be more in line with our style guidelines. This automation is not perfect so take it with a grain of salt.

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