Skip to content

Conversation

@Grazfather
Copy link
Collaborator

@Grazfather Grazfather commented Jan 7, 2026

Summary

Adds a complete SPI HAL implementation for CH32V (specifically CH32V203) with optional DMA support, following the proven I2C HAL pattern.

Key Features:

  • Master-only mode (covers 95% of use cases)
  • Blocking I/O (always available)
  • Optional DMA support with threshold-based automatic selection
  • Vectored I/O for zero-copy operations (required for display drivers)
  • Compile-time DMA channel validation
  • All 4 SPI modes (CPOL/CPHA combinations)
  • Configurable baud rate, bit order, data size
  • SPI_Datagram_Device wrapper for integration with display drivers

DMA Integration:

  • Threshold-based selection: transfers ≥16 bytes use DMA, smaller use polling
  • Compile-time channel validation prevents invalid configurations
  • Full-duplex DMA (both TX and RX channels required)
  • Channel mappings:
    • SPI1_TX: DMA1 Ch3
    • SPI1_RX: DMA1 Ch2
    • SPI2_TX: DMA1 Ch5

Examples Included:

  1. spi_loopback.zig (16,000 bytes) - Tests all 4 SPI modes with DMA enabled, includes 32-byte pattern to trigger DMA path
  2. spi_flash_w25q.zig (16,592 bytes) - W25Q128 flash operations (JEDEC ID, erase, write, read, verify)
  3. spi_ssd1306.zig (4,736 bytes) - SSD1306 OLED display driver integration via SPI_Datagram_Device

Implementation Notes:

  • Follows I2C HAL patterns for consistency
  • Pin configuration is user responsibility (platform-specific)
  • Hybrid CS approach: optional HAL-managed or wrapper-managed
  • Uses inline for for compile-time mode iteration in examples
  • GPIO register fix: IDRINDR for CH32V

Tested On:

  • Hardware: LANA_TNY (CH32V203)
  • All examples verified with --release=small builds
  • Loopback test confirms both polling and DMA paths work correctly

🤖 This PR is 100% AI-generated using Claude Code (claude.ai/code). No human edits were made to any code in this PR. The entire implementation—from architecture design through DMA integration to working examples—was created through conversation with Claude Sonnet 4.5.

(Ok not 100% anymore, I made some changes)

Co-Authored-By: Claude Sonnet 4.5 noreply@anthropic.com

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

🔍 Lint Results

Found 7 issues on changed lines in 4 files:

  • examples/wch/ch32v/src/i2c_dma.zig: 1 issue
  • examples/wch/ch32v/src/spi_loopback.zig: 1 issue
  • examples/wch/ch32v/src/spi_ssd1306.zig: 1 issue
  • port/wch/ch32v/src/hals/spi.zig: 4 issues
ℹ️ Additional issues on unchanged lines
The following 23 issue(s) exist but are not on lines changed in this PR:

drivers/sensor/ICM-20948.zig:27: TODO style comments need to have a linked microzig issue on the same line.
drivers/sensor/ICM-20948.zig:200: TODO style comments need to have a linked microzig issue on the same line.
drivers/sensor/ICM-20948.zig:208: TODO style comments need to have a linked microzig issue on the same line.
drivers/sensor/ICM-20948.zig:230: TODO style comments need to have a linked microzig issue on the same line.
drivers/sensor/ICM-20948.zig:494: TODO style comments need to have a linked microzig issue on the same line.
drivers/sensor/ICM-20948.zig:535: Suggestion: Rename `Accel_data_unscaled` to `AccelDataUnscaled`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
drivers/sensor/ICM-20948.zig:556: Suggestion: Rename `Accel_data` to `AccelData`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
drivers/sensor/ICM-20948.zig:599: Suggestion: Rename `Gyro_data_unscaled` to `GyroDataUnscaled`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
drivers/sensor/ICM-20948.zig:620: Suggestion: Rename `Gyro_data` to `GyroData`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
drivers/sensor/ICM-20948.zig:827: Suggestion: Rename `Mag_data_unscaled` to `MagDataUnscaled`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
drivers/sensor/ICM-20948.zig:868: Suggestion: Rename `Mag_data` to `MagData`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
drivers/sensor/ICM-20948.zig:886: Suggestion: Rename `TestI2CDevice` to `Test_I2C_Device`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/wch/ch32v/src/hals/dma.zig:18: TODO style comments need to have a linked microzig issue on the same line.
port/wch/ch32v/src/hals/dma.zig:47: TODO style comments need to have a linked microzig issue on the same line.
port/wch/ch32v/src/hals/dma.zig:263: TODO style comments need to have a linked microzig issue on the same line.
port/wch/ch32v/src/hals/dma.zig:16: Suggestion: Rename `DmaRegs` to `DMA_Regs`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/wch/ch32v/src/hals/drivers.zig:16: Suggestion: Rename `Datagram_Device` to `DatagramDevice`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/wch/ch32v/src/hals/drivers.zig:17: Suggestion: Rename `Stream_Device` to `StreamDevice`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/wch/ch32v/src/hals/drivers.zig:19: Suggestion: Rename `Clock_Device` to `ClockDevice`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/wch/ch32v/src/hals/drivers.zig:20: Suggestion: Rename `I2CError` to `I2C_Error`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/wch/ch32v/src/hals/drivers.zig:21: Suggestion: Rename `I2CAddress` to `I2C_Address`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/wch/ch32v/src/hals/gpio.zig:68: TODO style comments need to have a linked microzig issue on the same line.
port/wch/ch32v/src/hals/gpio.zig:119: TODO style comments need to have a linked microzig issue on the same line.

@github-actions github-actions bot dismissed their stale review January 7, 2026 18:19

Updating with new lint results

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

🔍 Lint Results

Found 6 issues on changed lines in 3 files:

  • examples/wch/ch32v/src/spi_loopback.zig: 1 issue
  • examples/wch/ch32v/src/spi_ssd1306.zig: 1 issue
  • port/wch/ch32v/src/hals/spi.zig: 4 issues
ℹ️ Additional issues on unchanged lines
The following 23 issue(s) exist but are not on lines changed in this PR:

drivers/sensor/ICM-20948.zig:27: TODO style comments need to have a linked microzig issue on the same line.
drivers/sensor/ICM-20948.zig:200: TODO style comments need to have a linked microzig issue on the same line.
drivers/sensor/ICM-20948.zig:208: TODO style comments need to have a linked microzig issue on the same line.
drivers/sensor/ICM-20948.zig:230: TODO style comments need to have a linked microzig issue on the same line.
drivers/sensor/ICM-20948.zig:494: TODO style comments need to have a linked microzig issue on the same line.
drivers/sensor/ICM-20948.zig:535: Suggestion: Rename `Accel_data_unscaled` to `AccelDataUnscaled`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
drivers/sensor/ICM-20948.zig:556: Suggestion: Rename `Accel_data` to `AccelData`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
drivers/sensor/ICM-20948.zig:599: Suggestion: Rename `Gyro_data_unscaled` to `GyroDataUnscaled`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
drivers/sensor/ICM-20948.zig:620: Suggestion: Rename `Gyro_data` to `GyroData`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
drivers/sensor/ICM-20948.zig:827: Suggestion: Rename `Mag_data_unscaled` to `MagDataUnscaled`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
drivers/sensor/ICM-20948.zig:868: Suggestion: Rename `Mag_data` to `MagData`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
drivers/sensor/ICM-20948.zig:886: Suggestion: Rename `TestI2CDevice` to `Test_I2C_Device`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/wch/ch32v/src/hals/dma.zig:18: TODO style comments need to have a linked microzig issue on the same line.
port/wch/ch32v/src/hals/dma.zig:47: TODO style comments need to have a linked microzig issue on the same line.
port/wch/ch32v/src/hals/dma.zig:263: TODO style comments need to have a linked microzig issue on the same line.
port/wch/ch32v/src/hals/dma.zig:16: Suggestion: Rename `DmaRegs` to `DMA_Regs`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/wch/ch32v/src/hals/drivers.zig:16: Suggestion: Rename `Datagram_Device` to `DatagramDevice`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/wch/ch32v/src/hals/drivers.zig:17: Suggestion: Rename `Stream_Device` to `StreamDevice`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/wch/ch32v/src/hals/drivers.zig:19: Suggestion: Rename `Clock_Device` to `ClockDevice`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/wch/ch32v/src/hals/drivers.zig:20: Suggestion: Rename `I2CError` to `I2C_Error`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/wch/ch32v/src/hals/drivers.zig:21: Suggestion: Rename `I2CAddress` to `I2C_Address`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/wch/ch32v/src/hals/gpio.zig:68: TODO style comments need to have a linked microzig issue on the same line.
port/wch/ch32v/src/hals/gpio.zig:119: TODO style comments need to have a linked microzig issue on the same line.

@github-actions github-actions bot dismissed their stale review January 7, 2026 18:25

Updating with new lint results

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

🔍 Lint Results

Found 6 issues on changed lines in 3 files:

  • examples/wch/ch32v/src/spi_loopback.zig: 1 issue
  • examples/wch/ch32v/src/spi_ssd1306.zig: 1 issue
  • port/wch/ch32v/src/hals/spi.zig: 4 issues
ℹ️ Additional issues on unchanged lines
The following 11 issue(s) exist but are not on lines changed in this PR:

port/wch/ch32v/src/hals/dma.zig:18: TODO style comments need to have a linked microzig issue on the same line.
port/wch/ch32v/src/hals/dma.zig:47: TODO style comments need to have a linked microzig issue on the same line.
port/wch/ch32v/src/hals/dma.zig:263: TODO style comments need to have a linked microzig issue on the same line.
port/wch/ch32v/src/hals/dma.zig:16: Suggestion: Rename `DmaRegs` to `DMA_Regs`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/wch/ch32v/src/hals/drivers.zig:16: Suggestion: Rename `Datagram_Device` to `DatagramDevice`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/wch/ch32v/src/hals/drivers.zig:17: Suggestion: Rename `Stream_Device` to `StreamDevice`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/wch/ch32v/src/hals/drivers.zig:19: Suggestion: Rename `Clock_Device` to `ClockDevice`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/wch/ch32v/src/hals/drivers.zig:20: Suggestion: Rename `I2CError` to `I2C_Error`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/wch/ch32v/src/hals/drivers.zig:21: Suggestion: Rename `I2CAddress` to `I2C_Address`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/wch/ch32v/src/hals/gpio.zig:68: TODO style comments need to have a linked microzig issue on the same line.
port/wch/ch32v/src/hals/gpio.zig:119: TODO style comments need to have a linked microzig issue on the same line.

var rx_buffer: [64]u8 = undefined; // Increased to handle 32-byte pattern

// SPI modes to test (all 4 combinations of CPOL and CPHA)
const SpiMode = struct {
Copy link

Choose a reason for hiding this comment

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

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

}

// Digital_IO wrapper for D/C pin
const DC_Pin_Wrapper = struct {
Copy link

Choose a reason for hiding this comment

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

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

@Grazfather Grazfather marked this pull request as draft January 8, 2026 03:24
@github-actions github-actions bot dismissed their stale review January 8, 2026 23:45

Updating with new lint results

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

🔍 Lint Results

Found 3 issues on changed lines in 2 files:

  • examples/wch/ch32v/src/spi_loopback.zig: 1 issue
  • port/wch/ch32v/src/hals/spi.zig: 2 issues
ℹ️ Additional issues on unchanged lines
The following 11 issue(s) exist but are not on lines changed in this PR:

port/wch/ch32v/src/hals/dma.zig:18: TODO style comments need to have a linked microzig issue on the same line.
port/wch/ch32v/src/hals/dma.zig:47: TODO style comments need to have a linked microzig issue on the same line.
port/wch/ch32v/src/hals/dma.zig:263: TODO style comments need to have a linked microzig issue on the same line.
port/wch/ch32v/src/hals/dma.zig:16: Suggestion: Rename `DmaRegs` to `DMA_Regs`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/wch/ch32v/src/hals/drivers.zig:16: Suggestion: Rename `Datagram_Device` to `DatagramDevice`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/wch/ch32v/src/hals/drivers.zig:17: Suggestion: Rename `Stream_Device` to `StreamDevice`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/wch/ch32v/src/hals/drivers.zig:19: Suggestion: Rename `Clock_Device` to `ClockDevice`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/wch/ch32v/src/hals/drivers.zig:20: Suggestion: Rename `I2CError` to `I2C_Error`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/wch/ch32v/src/hals/drivers.zig:21: Suggestion: Rename `I2CAddress` to `I2C_Address`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/wch/ch32v/src/hals/gpio.zig:68: TODO style comments need to have a linked microzig issue on the same line.
port/wch/ch32v/src/hals/gpio.zig:119: TODO style comments need to have a linked microzig issue on the same line.

var rx_buffer: [64]u8 = undefined; // Increased to handle 32-byte pattern

// SPI modes to test (all 4 combinations of CPOL and CPHA)
const SpiMode = struct {
Copy link

Choose a reason for hiding this comment

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

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


if (total_len >= dma_cfg.threshold) {
// For vectored DMA, we need to send chunks sequentially
// TODO: Optimize with scatter-gather DMA in the future
Copy link

Choose a reason for hiding this comment

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

TODO style comments need to have a linked microzig issue on the same line.


if (total_len >= dma_cfg.threshold) {
// For vectored DMA, we need to read chunks sequentially
// TODO: Optimize with scatter-gather DMA in the future
Copy link

Choose a reason for hiding this comment

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

TODO style comments need to have a linked microzig issue on the same line.

@github-actions github-actions bot dismissed their stale review January 8, 2026 23:47

Updating with new lint results

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

🔍 Lint Results

Found 2 issues on changed lines in 1 file:

  • port/wch/ch32v/src/hals/spi.zig: 2 issues
ℹ️ Additional issues on unchanged lines
The following 11 issue(s) exist but are not on lines changed in this PR:

port/wch/ch32v/src/hals/dma.zig:18: TODO style comments need to have a linked microzig issue on the same line.
port/wch/ch32v/src/hals/dma.zig:47: TODO style comments need to have a linked microzig issue on the same line.
port/wch/ch32v/src/hals/dma.zig:263: TODO style comments need to have a linked microzig issue on the same line.
port/wch/ch32v/src/hals/dma.zig:16: Suggestion: Rename `DmaRegs` to `DMA_Regs`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/wch/ch32v/src/hals/drivers.zig:16: Suggestion: Rename `Datagram_Device` to `DatagramDevice`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/wch/ch32v/src/hals/drivers.zig:17: Suggestion: Rename `Stream_Device` to `StreamDevice`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/wch/ch32v/src/hals/drivers.zig:19: Suggestion: Rename `Clock_Device` to `ClockDevice`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/wch/ch32v/src/hals/drivers.zig:20: Suggestion: Rename `I2CError` to `I2C_Error`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/wch/ch32v/src/hals/drivers.zig:21: Suggestion: Rename `I2CAddress` to `I2C_Address`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/wch/ch32v/src/hals/gpio.zig:68: TODO style comments need to have a linked microzig issue on the same line.
port/wch/ch32v/src/hals/gpio.zig:119: TODO style comments need to have a linked microzig issue on the same line.


if (total_len >= dma_cfg.threshold) {
// For vectored DMA, we need to send chunks sequentially
// TODO: Optimize with scatter-gather DMA in the future
Copy link

Choose a reason for hiding this comment

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

TODO style comments need to have a linked microzig issue on the same line.


if (total_len >= dma_cfg.threshold) {
// For vectored DMA, we need to read chunks sequentially
// TODO: Optimize with scatter-gather DMA in the future
Copy link

Choose a reason for hiding this comment

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

TODO style comments need to have a linked microzig issue on the same line.

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.

2 participants