ST AN2394 Application note

AN2394
Application note
STR7/STR9 audio generation with PWM
Introduction
The purpose of this application note is to provide a hardware and firmware solution to STR7 and STR9 microcontroller users for audio playback of a .WAV file.
The approach is optimized in that it uses a minimal number of components external to the microcontroller, and offers a high degree of flexibility to the end-user for use with their own .WAV files. There are two .WAV file parameters that can be controlled by the user; the sample rate and the file size which depends on the application requirements. The actual content of the .WAV file is irrelevant and may consist of speech, music, etc., and the only limitation is the audio format. In fact, this application assumes that the .WAV file format must be: PCM (no compression), 8000/11025/22050/44100 Hz sample rate, 8-bit and mono.
This document is structured as follows: a brief description of the .WAV file format in
Section 1. Section 2 provides a detailed description of the basics of audio playback. Finally, Section 3 presents in detail an example of an application built around an STR711F
microcontroller and that can be easily tailored to any other STR7/STR9 microcontroller.
December 2007 Rev 1 1/13
www.st.com
Contents AN2394

Contents

1 .WAV file format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2 Audio playback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.1 STRx PWM description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.2 Audio playback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3 Implementation example with STR711F . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.1 Hardware description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.2 Firmware description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
4 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
5 Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2/13
AN2394 .WAV file format

1 .WAV file format

The .WAV file format is a subset of the Resource Interchange File Format (RIFF) specification used for the storage of multimedia files. A RIFF file starts with a file header followed by a sequence of data chunks. A .WAV file is often just a RIFF file with a single "WAVE" chunk consisting of two sub-chunks:
1. a fmt chunk specifying the data format
2. a data chunk containing the actual sample data.
The numerical format used is Little-Endian (least significant byte first) and Big-Endian (most significant byte first) order.
The WAVE file format starts with the RIFF header:
Endian Offset Length Contents big 0 4 bytes 'RIFF' // 0x52494646 little 4 4 bytes <file length - 8> big 8 4 bytes 'WAVE' // 0x57415645
Next, the fmt chunk describes the sample format:
big 12 4 bytes 'fmt ' // 0x666D7420 little 16 4 bytes 0x00000010 // Length of the fmt data (16 bytes) little 20 2 bytes 0x0001 // Format tag: 1 = PCM little 22 2 bytes <channels> // Channels: 1 = mono, 2 = stereo little 24 4 bytes <sample rate> // Samples per second: e.g., 22050 little 28 4 bytes <bytes/second> // sample rate * block align little 32 2 bytes <block align> // channels * bits/sample / 8 little 34 2 bytes <bits/sample> // 8 or 16
Finally, the data chunk contains the sample data:
big 36 4 bytes 'data' // 0x64617461 little 40 4 bytes <length of the data block> little 44 * <sample data>
This application assumes that the .WAV file to be played has the following format:
Audio Format: PCM (an uncompressed wave data format in which each value
represents the amplitude of the signal at the time of sampling.)
Sample rate: may be 8000, 11025, 22050 or 44100 Hz.
Bits Per Sample: 8-bit (Audio sample data values are in the range [0-255] ).
Number Of Channels: 1 (Mono)
3/13
Audio playback AN2394

2 Audio playback

The audio playback requires two dedicated timers:
Timer used as system timer, SysTimer: generates an interrupt at a programmable rate
(fixed by the .WAV file sample rate value).
Timer used in PWM mode. This timer should be able to produce high frequency PWM
signal.
The .WAV file data can be stored in a compatible SPI-based Flash (requiring an SPI interface), in internal Flash memory (if the chosen STR7/SRT9 MCU has sufficient capacity) or on any other external memory type supported by the microcontroller .
The audio playback flow is presented in the following figure:
Figure 1. Audio playback flow
STR7/STR9
Low-Pass
filter
Audio
Amplifier
Speaker
External Memory,
example: SPI Flash
.WAV
OR
Internal
PWM
.WAV
Flash
To playback the audio, the microcontroller reads each 8-bit sample and then outputs it to a digital-to-analog converter (DAC) at the desired sample rate. The analog wave produced by the DAC is amplified and fed to a speaker to produce the sound.
As the STR7/STR9 MCU doesn't have an on-chip DAC, an alternative method is used to implement 1 channel DAC. Such a method is the use of the built-in Pulse Width Modulation (PWM) module to generate a signal whose pulse width is proportional to the amplitude of the sample data. The PWM output signal is then integrated by a low-pass filter to remove high frequency components, leaving only the low-frequency content. The output of the low­pass filter provides a reasonable reproduction of the original analog signal.

2.1 STR7/STR9 PWM description

This section introduces the basic concepts of the PWM mode of the STR7/STR9 TIMER (TIM).
Pulse Width Modulation (PWM) consists of a signal with a fixed period whose duty cycle is variable. The duty cycle represent the ratio of ON period to the period of the signal.
For the TIM, the PWM period and duty cycle are determined by the value of the OCAR and OCBR registers.
The PWM output signal period is fixed by the OCBR which determines the maximum value that the counter can count before starting a new period:
T = OCBR * Prescaler * T
4/13
Timer
Loading...
+ 9 hidden pages