STMicroelectronics AN2394 The purpose of this application note Datasheet

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
3
2.1
STRx PWM description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.2
Audio playback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
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
1
.WAV file format
.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
big
little
big
Offset
0
4
8
Length
4 bytes
4 bytes
4 bytes
Contents
'RIFF'
<file length - 8>
'WAVE'
// 0x52494646
// 0x57415645
Next, the fmt chunk describes the sample format:
big
little
little
little
little
little
little
little
12
16
20
22
24
28
32
34
4
4
2
2
4
4
2
2
bytes
bytes
bytes
bytes
bytes
bytes
bytes
bytes
'fmt '
// 0x666D7420
0x00000010
// Length of the fmt data (16 bytes)
0x0001
// Format tag: 1 = PCM
<channels>
// Channels: 1 = mono, 2 = stereo
<sample rate> // Samples per second: e.g., 22050
<bytes/second> // sample rate * block align
<block align> // channels * bits/sample / 8
<bits/sample> // 8 or 16
Finally, the data chunk contains the sample data:
big
little
little
36
40
44
4 bytes
4 bytes
*
'data'
// 0x64617461
<length of the data block>
<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
2
AN2394
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
Speaker
PWM
External Memory,
example: SPI Flash
.WAV
Audio
Amplifier
OR
.WAV
Internal
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 lowpass 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 * TTimer
4/13
AN2394
Audio playback
Where:
OCBR: Output Compare B register value
Prescaler: Timer clock prescaler
TTimer : period of the Timer clock that drives the Prescaler.
The value set in the OCAR determines the duty-cycle:
Ton = OCAR * Prescaler * TTimer
dcycle = (Ton/ T)*100 = (OCAR / OCBR) *100 %
Where:
OCAR: Output Compare A register value
Figure 2.
Typical PWM signal waveform
PWM
Output
dcycle = 50%
Ton
dcycle = 75%
t
Ton
Ton
T
2.2
dcycle = 25%
T
T
Audio playback
Before the audio data can be played, the header must be parsed so that the sampling rate of
the data and its length can be determined.
The task of reproducing audio is achieved by using sampled data (data contained in the
.WAV file) to vary the duty cycle of the PWM signal who’s period is held at a constant value.
The duty cycle represents the ratio of the pulse width to the pulse period, i.e. it represents
the ratio of the OCAR value to the OCBR one.
As the duty cycle varies according to the values of the sampled data and this data is coded
in 8 bits (with values from 0 to 255), the OCBR value must be set to 255 and the OCAR
value fills with the sampled data values (the OCAR value varies from 0 to 255). In this
manner the duty cycle varies from 0% to 100%.
The OCAR value is updated at intervals specified by the SysTimer period which is equal to
the sample rate of the .WAV file.
The PWM output signal is then fed to a simple RC low-pass filter to generate an output
voltage directly proportional to the average time spent in the HIGH state (i.e., 50% duty
cycle is equal to 1.65V when VDD = 3.3V). When this is averaged over time, we get a
reasonable reproduction of the original analog signal.
There are several design considerations involved in the selection of the cut-off frequency of
the low-pass filter. Primarily, the filter cut-off frequency must be much lower than the PWM
frequency to reduce the noise generated by PWM switching. A PWM frequency of 10 times
the cut-off frequency is a generally sufficient. However, the cut-off frequency of the filter is
critical to the bandwidth of the output audio.
In addition, it is recommended, on the basis of the Nyquist Criterion, that the cut-off
frequency of any filter used be set at half the frequency of the sample playback rate. Since in
5/13
Audio playback
AN2394
this application different sample rates are supported (8000/11025/22050/44100 Hz), we
must take into account the bandwidth limitation imposed by the lower sample rate.
From a hardware standpoint, a higher PWM frequency is easier to filter, but it corresponds to
a smaller reload value and therefore results in lower resolution. By contrast, a smaller PWM
frequency is difficult to filter; besides, the lower limit is dictated by the data input rate.
Incorporating this guideline, the filter cut-off frequency is set to 4 kHz. This ensures that
almost the entire audio spectrum is passed through, but the PWM carrier is removed.
The values for R and C are computed with this following formula:
1 f = --------------2πRC
where f is the cut-off frequency of the filter.
6/13
AN2394
3
Implementation example with STR711F
Implementation example with STR711F
This section describes a hardware and firmware solution of .WAV file playback implemented
for the STR711F microcontroller and that can be easily tailored to any other STR7/STR9
microcontroller.
The following STR711F peripherals are used:
3.1
●
TIM0 in Output Compare timing mode to act as SysTimer. The TIM0 global interrupt
can be linked to IRQ (Interrupt Request) channel0 or to FIQ (Fast Interrupt Request)
channel0. For fast, low latency interrupt handling, FIQ channel0 is used to handle TIM0
interrupts.
●
TIM3 in PWM mode.
●
BSPI0 to communicate with the SPI Flash.
Hardware description
The electrical schematic of the circuit, which the proposed application refers to is shown in
Figure 3.
An M25P64 SPI-based Flash memory is used to store and send back the .WAV file data to
the ICU. The M25P64 is a 64-Mbit (8M x 8) Serial Flash memory from STMicroelectronics,
accessed by a high speed SPI-compatible bus.
The connection scheme between the microcontroller and the SPI Flash is as the following:
pin S0.SCLK(P0.2) is connected to the Serial Clock pin (C) for data synchronization, pin
S0.MOSI(P0.1) and pin S0.MISO(P0.0) are connected, respectively, to Serial Data Input pin
(D) and Serial Data Output pin (Q) for data transmission. To select and deselect the Flash,
its Chip Select pin (S) is dynamically driven by P1.1 pin.
The T3.OCMPA pin (P1.2) is used to output the PWM signal. This signal is passed through
a simple low-pass RC (R33 and C15) filter. The cut-off frequency of this filter is set at 4 kHz.
This ensures that almost the entire audio spectrum is passed through, but the PWM carrier
is cut off.
Since the cut-off frequency of the low-pass filter is set at 4kHz, the R and C component
values are calculated as: R = 1.2 kΩ, C = 33 nF.
The filter output is fed to the input of a common TS4871 audio amplifier through a
potentiometer to allow continuous control of the audio volume.
The TS4871 is a monolithic integrated audio power amplifier chip from STMicroelectronics
capable of delivering 0.5W of output power into an 8ohm load at 3V3. The amplifier output is
used to drive an 8ohm speaker.
7/13
D
C
B
Audio_Out
1
1.2K
R33
C15
33nF
M25P64-VME6G
RV1
50K
2
100nF U2
7
HOLD
C
8
VCC
D
1
SPI_CS
S
VSS
SPI_MISO 2
Q
W
1
3
150nF
22K
R34
SPI_SCLK
SPI_MOSI
+3V3
C16
6
5
4
3
2
C17
2.2uF
1
4
3
2
TS4871IST
Uout1
Vout2
VCC
GND
100K
Standby
VinVin+
Bypass
U1
R35
470pF
C9
5
8
6
7
SP1
52
53
54
55
33
34
35
36
37
40
41
45
60
62
64
2
C1
10uF
3
KDMG15008-03
PWM
SPI_MISO
SPI_MOSI
SPI_SCLK
C19
100uF
+3V3
C18
100nF
2
1
Audio_Out
R4
10K
+3V3
3
C3
1.0uF
P1.6
P1.7
P1.8
P1.9
P0.5
P0.7
P0.9
P0.11
P0.0/S0.MISO
P0.1/S0.MOSI
P0.2/S0.SCLK
P0.3/S0.SSN
P1.2/T3.OCMPA/AIN2
P1.3
P1.4
STR711F
P1.5
V33
V33
V33
RTCXTO
RTCXTI
CK
HCLK
nSTDBY
P0.12
nRSTIN
P0.15/WAKEUP
P0.6
P0.8
P0.10
P0.13
P1.14
27
58
28
57
4
59
50
44
8
39
18
25
30
26
29
38
C2
33nF
R27
10K
4
nRSTIN
JTDO
nJRTST
JTDI
JTMS
JTCK
2.2K
R28
CK
R29
10K
R30
10K
R5 R6 R7 R8
10K 10K 10K 10K
+3V3
51
17
9
22
21
19 CK
47
23
3
24 nRSTIN
20
61
63
1
5
48
+3V3
1
3
5
7
9
11
13
15
17
19
2
4
6
8
10
12
14
16
18
20
HEADER 10X2
J1
5
+3V3
5
S1
+3V3
C6
10nF
SW-PB (Reset)
R1
100K
+3V3
2
4
3
1
5
4
+3V3
C14
2
X2
CK
4 MHz OSC
1
V18
V18
VSS18
VSS18
VSS
VSS
VSS
VSS
VSS
VSS
VSS
VSSB KP
AVSS
V18BKP
AVDD
V33
SPI_CS
4
4
1
P0.4
P0.14
P1.10
P1.15
P1.1
P1.0
USBDN
USBDP
BOOTEN
JTDI
JTMS
JTCK
JTDO
nJTR ST
NU
TEST
56
6
46
49
32
31
43
42
BOOTEN 7
JTDI
10
11
JTMS
12
JTCK
13
JTDO
nJRTST 14
15
16
3
3
8/13
2
6
6
R4
10K
CK
D
C
B
A
Figure 3.
2
A
1
Implementation example with STR711F
AN2394
Audio playback with STR711F
AN2394
3.2
Implementation example with STR711F
Firmware description
The software associated with this application is developed in C using the STR71x Firmware
Library from STMicroelectronics. The STR71x Firmware Library provides a set of functions
covering the functionality of the STR71x peripheral. As a result, users can develop
applications for any STR71x device without an in-depth study of each peripheral
specification.
Note:
The firmware assumes that the .WAV file is stored at address 0x00 of the Flash.
The first part of the software performs the configuration of the different system clocks. The
main clock (MCLK), PCLK1 (including serial communication peripherals, such as BSPI0)
and PCLK2 (including system peripherals, such as TIM0 and TIM3) are set to 32MHz using
an external 4MHz oscillator.
Once the BSPI0 peripheral and its associated pin are configured, the baudrate is set to
5.3Mb/s.
The task of audio playback is achieved by calling the WAVE_PlayBack function. This
function starts by a call to the WAVE_Parsing function. This checks the format of the .WAV
file and gets information about the audio format. This is done by reading the value of a
number of parameters stored in the file header and comparing these to the values expected
for the format of a standard .WAV file. If a valid .WAV file format, it continues reading the
header to determine the audio format in terms of the sample rate and the sampled data size.
If the audio format is supported by this application, it retrieves the audio format in the
WAVE_FormatTypeDef structure and returns a zero value. Otherwise the function fails and
the return value is nonzero. In this case, the return value specifies the cause of the function
fails.
The WAVE_PlayBack function tests the value returned by WAVE_Parsing function, if it is a
nonzero value the audio playback fails. Otherwise, it configures the TIM0 and TIM3 timers.
TIM3 is programmed to generate 125.5kHz PWM signal and TIM0 is programmed to
generate an interrupt at frequency equal to the .WAV file sample rate value.
Note:
As TIM3 OCBR value is fixed to 255, the maximum PWM frequency that can be reached is
125.5kHz.
On each TIM0 interrupt, a byte is read from the SPI Flash to update the TIM3 Output
Compare A register (OCAR) value. A counter loop operates by counting down from the
sampled data size value and when zero is reached the playback is stopped.
Usually, to read a byte from the SPI Flash several steps are performed:
1.
Drive the S line low to select the device
2.
Send read data instruction
3.
Send a 3 byte address
4.
Send a dummy byte. Then the memory content, at that address, is shifted out on Serial
Data Output (Q)
5.
Drive the S line high to deselect the device
Repeating this procedure for each byte read from the SPI Flash increases the processing
time of the TIM0 interrupt service routine. This can affect the CPU load and limits other
tasks that the application may demand.
9/13
Implementation example with STR711F
AN2394
To avoid this and decrease the CPU load, a solution based on the capability to read the
whole SPI Flash memory with a single read data instruction is implemented:
–
the first time a data byte is needed, a call to Start_Read_Sequence function is
made. This initiates a read data byte sequence from the SPI Flash. This is done by
driving the S line low to select the device, then the read data instruction is
transmitted followed by 24-bit address. This function exits and keeps the S line
low, with the SPI Flash still being selected.
The next routine which needs to read a byte from the SPI Flash has only to generate a
dummy byte on the Serial Data Input line of the SPI Flash and get the data on the Serial
Data Output line. So only step 4 is performed each time a data is read from the SPI Flash
which significantly reduces the CPU overhead.
When the playback is finished, the read data byte sequence is terminated by driving Chip
Select high.
10/13
AN2394
4
Conclusion
Conclusion
This document proposes a straightforward and flexible solution to use a member of the STR
family, STR711F, for playback of a .WAV file stored in an SPI Flash memory. This can be
used in many applications where high-performance audio playback is not required such as:
talk back units, man-machine interfacing and low-cost music players.
11/13
Revision history
5
AN2394
Revision history
Table 1.
12/13
Document revision history
Date
Revision
06-Dec-2007
1
Changes
Initial release.
AN2394
Please Read Carefully:
Information in this document is provided solely in connection with ST products. STMicroelectronics NV and its subsidiaries (“ST”) reserve the
right to make changes, corrections, modifications or improvements, to this document, and the products and services described herein at any
time, without notice.
All ST products are sold pursuant to ST’s terms and conditions of sale.
Purchasers are solely responsible for the choice, selection and use of the ST products and services described herein, and ST assumes no
liability whatsoever relating to the choice, selection or use of the ST products and services described herein.
No license, express or implied, by estoppel or otherwise, to any intellectual property rights is granted under this document. If any part of this
document refers to any third party products or services it shall not be deemed a license grant by ST for the use of such third party products
or services, or any intellectual property contained therein or considered as a warranty covering the use in any manner whatsoever of such
third party products or services or any intellectual property contained therein.
UNLESS OTHERWISE SET FORTH IN ST’S TERMS AND CONDITIONS OF SALE ST DISCLAIMS ANY EXPRESS OR IMPLIED
WARRANTY WITH RESPECT TO THE USE AND/OR SALE OF ST PRODUCTS INCLUDING WITHOUT LIMITATION IMPLIED
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE (AND THEIR EQUIVALENTS UNDER THE LAWS
OF ANY JURISDICTION), OR INFRINGEMENT OF ANY PATENT, COPYRIGHT OR OTHER INTELLECTUAL PROPERTY RIGHT.
UNLESS EXPRESSLY APPROVED IN WRITING BY AN AUTHORIZED ST REPRESENTATIVE, ST PRODUCTS ARE NOT
RECOMMENDED, AUTHORIZED OR WARRANTED FOR USE IN MILITARY, AIR CRAFT, SPACE, LIFE SAVING, OR LIFE SUSTAINING
APPLICATIONS, NOR IN PRODUCTS OR SYSTEMS WHERE FAILURE OR MALFUNCTION MAY RESULT IN PERSONAL INJURY,
DEATH, OR SEVERE PROPERTY OR ENVIRONMENTAL DAMAGE. ST PRODUCTS WHICH ARE NOT SPECIFIED AS "AUTOMOTIVE
GRADE" MAY ONLY BE USED IN AUTOMOTIVE APPLICATIONS AT USER’S OWN RISK.
Resale of ST products with provisions different from the statements and/or technical features set forth in this document shall immediately void
any warranty granted by ST for the ST product or service described herein and shall not create or extend in any manner whatsoever, any
liability of ST.
ST and the ST logo are trademarks or registered trademarks of ST in various countries.
Information in this document supersedes and replaces all information previously supplied.
The ST logo is a registered trademark of STMicroelectronics. All other names are the property of their respective owners.
© 2007 STMicroelectronics - All rights reserved
STMicroelectronics group of companies
Australia - Belgium - Brazil - Canada - China - Czech Republic - Finland - France - Germany - Hong Kong - India - Israel - Italy - Japan Malaysia - Malta - Morocco - Singapore - Spain - Sweden - Switzerland - United Kingdom - United States of America
www.st.com
13/13
Similar pages