-
Notifications
You must be signed in to change notification settings - Fork 194
Description
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
- New: internal/MozziGuts_impl_CH32.hpp (Core logic).
- New: internal/config_checks_CH32.h (Hardware-specific defaults).
- Modified: hardware_defines.h (Architecture detection).
- Modified: internal/MozziGuts.hpp & internal/config_checks_generic.h (Routing).
- 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
- Install the openwch/arduino_core_ch32 core.
- Install the FixMath library.
- Install the Mozzi library.
- Apply the attached files from the ZIP.
- 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.