Skip to content
Snippets Groups Projects
README.md 2.41 KiB

SPI DAC Demo

This example shows how to set up the SPI port with a timer, circular DMA and IRQs to do continuous audio output through an inexpensive I2S DAC.

Scope image of sine and sawtooth waves

Theory

The CH32V003 does not have an I2S port which is usually required for driving audio DACs, but the inexpensive PT8211 stereo audio DAC is very forgiving of the signal format used to drive it so we can approximate an I2S signal using one of the CH32V003 on-chip timers to generate the frame sync signal. The SPI port then provides the bit clock and serial data.

Timer setup

TIM1 on the CH32V003 is set up to generate a 48kHz square wave which is output on GPIO pin PC4 and serves as the frame sync or WS. The timer is configured to run in center-aligned mode 3 which generates DMA requests on both rising and falling edges. Channel 4 is configured as the output and the threshold is set to 50% and the overall period is set to 48kHz.

SPI setup

SPI1 is configured for 16-bit TX with a bit clock of 48MHz/8 (3MHz) which is fast enough to clock out 16 bits of data between the edges of the frame sync signal. Transmit data arrives via DMA, but the SPI port does not control DMA - that is triggered from the timer above.

DMA setup