Skip to content

Support for WCH CH32X035 (RISC-V) #318

@NoNamedCat

Description

@NoNamedCat

This issue presents a full port of the Mozzi library for the WCH CH32X035 microcontroller (RISC-V architecture). The
port includes hardware-level implementation for audio interrupts, high-speed PWM generation, and support for the Hi-Fi
(2-pin PWM) output mode (Not tested).

Key Implementation Details

  • Low-Level Hardware Access: Since the CH32 Arduino core lacks a standard HardwareTimer class, this port uses the WCH
    Standard Peripheral Library (SPL) to directly configure registers for maximum performance.
  • Timers:
    • TIM1: Configured as the master audio interrupt timer (defaulting to 16,384Hz).
    • TIM3: Configured for high-speed PWM generation (~187.5 kHz carrier frequency).
  • Output Modes:
    • Standard PWM: Output on pin PA6 (TIM3_CH1).
    • Hi-Fi Mode: Dual-pin output on PA6 (High Byte) and PA7 (Low Byte) (Not tested).
  • Compiler Compatibility: RISC-V GCC is stricter than AVR/ARM regarding narrowing conversion. I added a #pragma GCC
    diagnostic ignored "-Wnarrowing" specifically inside the CH32 implementation file to allow legacy Mozzi tables
    (declared as int8_t but containing values > 127) to compile without modifying the original table files.

FixMath Dependency Fixes
During the porting process, I identified and fixed broken dependencies related to the new FixMath library requirement
in Mozzi 2.0+. Several headers (Oscil.h, AudioOutput.h, etc.) were attempting to include <FixMath.h> which was missing
or failing. I updated mozzi_fixmath.h to act as a proper wrapper for the external FixMath. **

(Don't know if I'm doing good with this)**.

Files Included in the Patch

  1. New: internal/MozziGuts_impl_CH32.hpp (Core logic).
  2. New: internal/config_checks_CH32.h (Hardware-specific defaults).
  3. Modified: hardware_defines.h (Architecture detection).
  4. Modified: internal/MozziGuts.hpp & internal/config_checks_generic.h (Routing).
  5. Modified: AudioOutput.h, Line.h, MetaOscil.h, mozzi_midi.h, Oscil.h, mozzi_fixmath.h (FixMath dependency
    corrections)(Don't know if I'm doing good with this).

How to Test

  1. Install the openwch/arduino_core_ch32 core.
  2. Install the FixMath library.
  3. Install the Mozzi library.
  4. Apply the attached files from the ZIP.
  5. Define #define MOZZI_AUDIO_MODE MOZZI_OUTPUT_2PIN_PWM for Hi-Fi mode (Not tested) or use default for standard output on PA6.

I am attaching a ZIP file with all the modified/new files. I hope this contribution is useful for the community and
can be integrated into the main branch.

Mozzi_CH32_Port.zip

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions