cancel
Showing results for 
Search instead for 
Did you mean: 

How to play audio files using STM32 part 1

B.Montanari
ST Employee

How to play audio files using STM32?

There are a few ways to play audio files with the STM32, in this article we’ll cover 2 very simple methods: using a low pass filter with the PWM output and using a DAC output. On both cases an audio amplifier and a microphone / speaker will be needed to properly output the audio.

It is also worth mentioning before diving that not all STM32 have a DAC, so make sure you properly adjust the code to your own STM32 peripheral availability
The code implemented was based on the AN4453 with the STSW-STM32022 package and update to use the HAL and LL driver, so this firmware can be easily tailored to any other STM32.
The focus of this article is to guide you over the firmware implementation for the low-level portion of an audio player, including how to configure the peripherals to play audio files and how to convert these files to be read by the STM32 without relying on other middleware’s, such as USB (mass storage) and Filesystem to store several audio files in external memories. This means that the audio file will be placed in the embedded FLASH of the MCU, with considerable limits the size of it.
These are the requirements for this article’s implementation:
STM32CubeIDE (version 1.7.0 or higher)
STM3G0 HAL firmware pack – you can install this from within STM32CubeIDE
STSW-STM32022 software pack
SOX – WAV to IMA converter, can be downloaded here
HxD – IMA to C vector, ca be downloaded here
NUCLEO-G071RB – our STM32 board
STEVAL-CCA037VI – our audio amplifier board
A small way to prototype the low pass filter
Your WAV file
Note: Have your WAV file ready – please be aware that this implementation will use the MCU’s embedded FLASH to store your audio file, so it is recommended to have an audio that has only a few seconds, otherwise it might not fit in the embedded FLASH. An improvement of this implementation would be to add an external memory to allow larger audio files to be played.

Before diving into the firmware, a few notes about the hardware construction. We’ll aim to have a 16bit resolution in total when using the PWM, this will be done by separated it in 8bit for the MSB (TIM3 channel 1) and 8bit for the LSB (TIM3 channel 2) – to achieve this, the PWM output pins must pass thru a combined low pass filters as shown in the image below – note that there is a relationship between the resistor values, where R2 is obtained by dividing R1 by 256:
500.png
Alternatively, for a simple experiment, you can use just the MSB portion (TIM3 channel 1) and have a simpler low pass filter, like this:
 502.png

The actual values used for this demo were: R1 = 8.2KOhm, R2 = 33Ohm C = 10nF, but the demo was also tested using only the PWM1 (no R2 connection), which provided a good output audio overall as well.
The hardware connection should be like this:
503.png
Since the goal is to have 16bits resolution divided by 2 PWMs, each portion segmented by 8bit resolution, we must configure the timer resolution to be equivalent to an 8bit variable, this means that the counter value should range from 0 up to 255 – this will be used later the STM32CubeIDE/configuration file.
The STM32G0 can go up to 64MHz and this application will use the PWM frequency at 125KHz, so we can have each audio sample outputted 8 times to ensure a stable quality audio level, thus rendering the actual audio output frequency as a 15.625KHz – a common sampled audio frequency.
The reason behind these numbers is to ensure the 16bit resolution in an exact amount. Here are the known parameters and how they reflect on the numbers we need:
504.png
506.png
507.png 
With these simple equations, you can adjust the time and numbers for your own MCU, as much as needed. Alright, now that we know the settings we’ll need for the timer, let’s start the actual project implementation. Please make sure to download the STSW-STM32022 software pack, as this provides the adaptive differential pulse coded modulation (ADPCM) algorithm used to encode and decode our audio files
Let’s start by creating a new project for the NUCLEO-G071RB – here it is assumed that you already know how to create a project using a given board as the default, in case you don’t, or you are not sure, here is a quick recap on the steps needed:
Open the STM32CubeIDE
Go to File->New->STM32 Project
Go to the second tab “Board Selector” and type “NUCLEO-G071RB”
Click on the board and then “Next”
508.png
Name the project and click Finish
When the pop-up message appears, make sure to accept the peripherals with their defaults

Great, you have a project tailored for the NUCLEO-G071RB and the first thing to do is configure the TIM3 to behave as explained previously
Start by making sure your clock is set as 64MHz, this can be done by going to the “Clock Configuration” tab and asserting the value if needed:
509.png
Back to the Pinout & Configuration Tab, add the TIM3 and configure it like this:
510.png
Note that for this demo, the PB4 and PB5 are the pins used for the TIM3_CH1 and TIM3_CH2 respectively:
511.png
Enable the TIM3 interrupt:
512.png
For those how wish to use the DAC instead of the PWM, you can configure it like this:
513.png
Before generating the code, let’s change the TIM3 and some other peripherals to use the LL instead of the HAL, this is mainly to reduce the application memory footprint and save room for the audio files. Don’t worry, this won’t increase the difficult to implement your code.
Go to the Project Manager Tab, Advanced Settings and select LL for all peripherals, except the DAC and USART (the USART is not used in the application, but you might want to add some debug messages for your own checking):
514.png
This implementation also used the Code Generator, under the same Project Manager tab to split the source and header file:
515.png
Alright, you are all set to generate your project so either use the short cut “Alt+K” or go to Project->Generate Code
Please continue with part 2 of the tutorial for the explanation on how to add the ADPCM module and your *.wav file in the project
See you soon!

 
Comments
Riscy
Senior

Hi, I was wondering when Part 2 be published?, this is good article and would like to use it for toy sound

LauraCx
ST Employee

Hello here the links to the 3 parts:

Part 1

Part 2

Part 3

Version history
Last update:
‎2022-06-22 06:37 AM
Updated by: