This is just a training project that reads, parses and plays WAV files using PWM from a USB drive (in FAT32 filesystem).
Uses a STM32F411CE "Black Pill" board running at 96MHz.
Supported WAV formats:
- 8, 16 bit depths (16bit is converted to 8bit).
- Stereo / mono (In mono mode, right channel is copied to the left channel).
- 8, 16, 22.05, 32, 44.1, 48, 96KHz sample rates.
22.05 and 44.1KHz have 0.04% error (22059Hz, 44117Hz), it plays only 1.5 seconds faster every hour.
Other WAV formats will be skipped.
In 8/16/32/48/96KHz PWM frequency is 384KHz, max PWM value 249, audio values over that will cause minor clipping.
I used Audacity and normalized the level to -0.1dB, that limited the output within the limits.
In 22.05/44.1KHz PWM frequency is 352.941KHz, max PWM value 271, so there's no clipping problem.
The PWM uses DMA burst mode, a very efficient and clean method to update multiple PWM channels at once.
PWM outputs are as follows:
- Right channel: PA8
- Left channel: PA9
For filtering the PWM, I used this calculator: http://sim.okawa-denshi.jp/en/Sallen3tool.php
The parameters were Chebyshev filtering, fc=22KHz.
The result was really good for coming from 8-bit PWM.
The SWO output is enabled on PB3, you can see the debug messages using SWO console.
You can see them without debugging, just connect the ST-Link utility, open "Prinf via SWO viewer", setting clock to 96000000Hz.
Example log:
Device connected
Device ready
Drive mounted
Opening file: 1-STER~1.WAV
WAV Info: Stereo, 48KHz, 8bit
Playback started
Playback stopped
Opening file: 7.WAV
WAV Info: Stereo, 44.1KHz, 16bit
Playback started
Playback stopped
Opening file: 1.WAV
WAV Info: Stereo, 48KHz, 16bit
Playback started
Playback stopped
Opening file: 6.WAV
WAV Info: Stereo, 96KHz, 16bit
Playback started
Playback stopped
For more details, check:
/Inc/files.h
/Inc/pwmAudio.h
/Src/files.c
/Src/pwmAudio.c
Connect a USB drive with wav files in the root folder (/), it will automatically start playing all files found in a loop .
You can download already compiled binaries in the Release folder (411.bin file)
If you want to compile your own:
- Download STM32 Cube IDE
- Clone or download the code
- Open STM32 Cube IDE, import existing project and select the folder where the code is.
- It should recognize it and be ready for compiling or modifying for your own needs.
The code is stripped from all the CubeMX libraries, it needs to regenerate them.
Open the CubeMX (.IOC) file and make any small change (Ex. set an unused GPIO as input), then and revert it and close saving changes.
CubeMx will make new code, and now it will compile correctly.