Analog-To-Digital Audio Conversion Example Using STM32L4 Series Microcontroller Peripherals - en - DM00367673
Analog-To-Digital Audio Conversion Example Using STM32L4 Series Microcontroller Peripherals - en - DM00367673
Analog-To-Digital Audio Conversion Example Using STM32L4 Series Microcontroller Peripherals - en - DM00367673
Application note
Analog-to-digital audio conversion example using
STM32L4 Series microcontroller peripherals
Introduction
The purpose of this application note is to provide an analog-to-digital audio conversion
example using the STM32L4 Series microcontroller peripherals (OPAMP, ADC and the
Full-Speed USB).
This example uses the ADC dedicated features to improve the conversion resolution and to
automatically detect a programmable threshold level.
Note that the values provided in this application note are for reference only, measured in a
laboratory under typical conditions (unless otherwise specified) and not tested in production.
Contents
4 Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
4.1 Simple voice detection using ADC watchdog feature . . . . . . . . . . . . . . . . 21
7 Measurement conclusions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Appendix A Data format from ADC to USB device (uint32_t to int16_t type) . . 32
Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
List of tables
List of figures
6RXQGSUHVVXUHG%63/
P9UPV &RPPHUFLDOPXVLF
0LFURSKRQH
QRPLQDOOHYHO
VHQVLWLYLW\
615 G%
G%9
0LFURSKRQH
X9UPV
PLQLPXPOHYHO
VHQVLWLYLW\
FRQYHUVDWLRQDOVSHHFK
G%9 G%63/
X9UPV
(OHFWULFDOVFDOH $FRXVWLFVFDOH
$XGLELOLW\FXUYH
2.1 Principle
The STM32L4 ADC peripheral is used to convert an external analog signal (voltage) into
digital values for further processing in the digital domain. The ADC peripheral is a 12 bits
successive approximation converter (SAR ADC) with additional oversampling hardware.
Figure 2 represents the well-known ADC behavior:
9ROWDJH
WLPH
$QDORJLQSXWVLJQDO
9ROWDJH
WLPH
6DPSOHDQGKROGLQSXWVLJQDO
&RGH
WLPH
'LJLWDOL]HGVLJQDO
069
Figure 3. DC biasing
9ROWDJH 9ROWDJH
9 9''$
WLPH WLPH
$QDORJVLJQDODURXQGJURXQGYROWDJH $QDORJVLJQDODURXQG9''$
069
This signal adaptation is performed using the following schematic (high-pass filter):
The dynamic range is the ratio between the maximum and the minimum signal level (0 lsb is
the minimum level while the maximum level depends on the gain in the acquisition path).
'&ELDVLQJVWDJH $QWLDOLDVVWDJH
069
Note: The signal quality provided by the microphone is very sensitive to noise on its biasing
voltage: a clean signal should be used to power the bias voltage.
'0$
%LDVLQJ
$QWLDOLDV $'&6$5 86%DXGLR 86%
DQG'& 23$03
ILOWHU ELW GHYLFH FRQQHFWRU 86%
FRXSOLQJ
FDEOH
ZDWFKGRJ ,17 DXGLR PHDVXUHPHQW
DXGLRPHDVXUHPHQW
$QDORJ 670/5*7 RQFRPSXWHU
PLFURSKRQH
/(' &&$0
18&/(2/5*
18&/(2/5* &&$0
670/5* 86%FRQQHFWRU
069
The USB audio device is available under Windows 7 operating system as a stereo recording
device, as shown on Figure 8.
6WDUW
6\VWHPFORFNFRQILJXUDWLRQ
86%SHULSKHUDO
LQLWLDOL]DWLRQDQGVWDUW
23$03SHULSKHUDOFRQILJXUDWLRQ
DQGSRZHUXS
$'&FRQILJXUDWLRQ
$'&DFTXLVLWLRQZLWK'0$VWDUW
$'& \HV
ZDWFKGRJ 7XUQ/('2))
LQWHUUXSW"
\HV
7XUQ/('21
ZDLWPV
069
There is a second firmware mechanism to exchange data between the ADC and the USB
device using the DMA, as shown in Figure 10.
$'&'0$
FLUFXODUPHPRU\ 86%)6
$'&VDPSOH VWKDOIEORFN SHULSKHUDOPHPRU\
VDPSOHV D
V / 6WHUHREXIIHU
QGKDOIEORFN E 86%LQWHUIDFH
5 [VDPSOHV
VDPSOHV
$'&'0$FDOOEDFNHYHQW
$'&ZDWFKGRJVWDWXV VDPSOHVUHSHDWHG
069
AN5012
AN5012 Implementation with the STM32L4 Series microcontroller
Table 2. ADC clock frequency range versus OSR and clock cycle (48 kHz)
ADC clock frequency (kHz)
The following setup has been selected to continue the implementation of this application
note:
• ADC_SAMPLETIME =12.5 CYCLES
• ADC_OVERSAMPLING_RATIO (OSR) = 64
• FADC= SYSCLK = 76.8 MHz
N+]
$'&VDPSOLQJUDWH N+]
VDPSOHFKDQQHO
$'&WR'0$PHPRU\
$'& $'& $'&
WUDQVIHUUDWH+]
$&4 $&4 $&4
VDPSOHVFKDQQHO
PV
$'&WR'0$PHPRU\
KDOIIXOOWUDQVIHUUDWHN+]
VDPSOHVFKDQQHO
PV
OHIW $'&PHPRU\
, , ,
86%DXGLRGHYLFHSHULSKHUDO
WUDQVIHUWWRLQWHUIDFH
VDPSOHVFKDQQHOV
2 2 2
ULJKW $'&ZDWFKGRJOHYHO
7LPH
069
%LWV
PVE OVE
$'&RXWSXWGDWDZLGWK265
069
The USB peripheral data format is signed 16 bits (int16_t) which is corresponding to a
decimal range between -32768 and +32767. A format transformation is required to
guarantee a lossless transfer between the peripherals (ADC USB audio device). This
transformation requires several steps:
• Usage of the ADC embedded feature to automatically shift and obtain output data in
integer 16-bits wide.
PVE OVE
ŽƵƚƉƵƚĚĂƚĂǁŝĚƚŚ;K^ZсϲϰнZ^,/&dсϮͿ
069
• Final step to convert the unsigned word into signed word using the following formula:
– USB_Peripheralword = ADC_Peripheralword – 2(word_size-1) where word size= 16
"ITS
SIGN MSB LSB
h^ŽƵƚƉƵƚĚĂƚĂǁŽƌĚ
069
Using a 1kHz sinus wave with an amplitude higher than the input range of the ADC, the
input level can be saturated in order to measure the maximum and minimum level in the
numeric domain and also verify any wrapping effect due to binary signed words.
Figure 16 shows an audio signal example with a saturated sinus at the ADC input,
OSR = 64, RS = 2, ADC continous acquisition.
Note: The figure shows a saturated signal to highlight the full dynamic of the digital path from
-32768 lsb to +32767 lsb.
4 Application
A basic application covered with this application note is the voice activity detection with a
programmable threshold level. Once the detection occurs, the device can start the audio
recording which will be stored for an embedded signal processing or can immediately
transfer the audio data to another peripheral or system.
Figure 17. Audio and ADC analog watchdog (threshold +/- 128)
The programmable threshold must be chosen regarding the product application and its
environment. If it is very noisy, a large watchdog window is preferable while a quiet ambient
noise allows to set a reduced window size.
For this application note, a window width of +/-128 lsb has been selected for a full spread of
4096 lsb (see Figure 18). Note that the watchdog detection is an internal feature of the ADC
and the windows size is based on the acquisition resolution of the ADC itself (12 bits in our
case).
$'&FRGH
7KUHVROGOHYHO
7LPH
069
With the material setup using the external per-amplifier gain (42 dB) and the analog
microphone (ST-MP33AB01), the peak value corresponding to a 60 dBSPL sound pressure
level is:
• ADC analog input = 89.4 mVpp
• 6 dB margin for the threshold detection
• With VDDA=3.3V, the peak code calculated is:
AN5012
Figure 19. Spectrum plot: output signal level - 6 dBFS / Frequency 1 kHz
DocID030334 Rev 1
AN5012
AN5012 Signal acquisition measurements
WRWDOSRZHUFRQVXPSWLRQ P$
069
The signal acquisition duration can be limited to 50 ms and the MCU can be set in sleep
mode until the next wakeup interrupt. Thanks to this mechanism, the power consumption is
reduced to 0.43 mA (average value), as shown on Figure 26.
3RZHU :DNHXSSHULRG V
FRQVXPSWLRQ
P$
$FWLYLW\PRQLWRULQJ 7LPH
GXULQJPV $YHUDJHSRZHUFRQVXPSWLRQ P$
069
7 Measurement conclusions
A signal acquisition application from an analog microphone can be done in a very simple
way using the STM32L476 Series microcontroller with its embedded OPAMP and ADC
peripherals and the dedicated ADC features (oversampling ratio, watchdog).
The global performance is interesting for a simple consumer application to detect, store and
post-process an audio signal from an analog microphone.
The OPAMP is increasing the noise level at the input of the ADC but is also introducing
some non-linearity.
/**
* @brief Convert unsigned int32 to signed 16
* @param ADC value to convert
* @retval The converted value
*/
int16_t ADC_to_USB_Dataformat(uint32_t ADC_sample)
{
int32_t temp_val=0x0;
temp_val=(int32_t)(ADC_sample-32768); //changing format from unsigned 32
bits (16 used) to signed 16bits.
return (int16_t) temp_val;
}
%Starting code
remove_dc=false;
clc; pkg load ltfat;
disp('--------------- SCRIPT START ------------------');
audiodevice_count=audiodevinfo(1);
for i =1:audiodevice_count
audiodevname=audiodevinfo(1,i)
if(strfind(audiodevname,"STM32")>0)
disp("STM32 audio device found")
stm32_audio_device_ID=i;
break;
elseif
stm32_audio_device_ID=0;
endif
endfor
clf;
figure(1)
usbaudiodevice=stm32_audio_device_ID; audiodevinfo (1,usbaudiodevice);
disp(strcat("Average\t ",num2str(mean(ADCACQ)),"\tlsb"));
rmslevel=rms(ADCACQ, 'ac');
rmsleveldb=20*log10(rmslevel/2)+9;
disp(strcat("RMS level\t",num2str(rmslevel*ADCMAXCODE),"\tlsb"));
disp(strcat("RMS level\t",num2str(rmsleveldb),"\tdBFS"));
ADCACQ_FILT=ADCACQ.*blackman(LEN_ACQ)';%Windowing
ax=subplot(2,2,3);grid on; hold on
Windowing_scale_factor=13.65;FFTY=Windowing_scale_factor+20.*log10(abs(fft
(ADCACQ_FILT))./LEN_ACQ);
[FundBinLevel, FundBinIndex] = max (FFTY);
disp(strcat("Fund bin index\t",num2str(FundBinIndex)));
disp(strcat("Fund level\t",num2str(FundBinLevel),"\tdBFS"));
FBIN=sampleRate/length(FFTY);FI=(FundBinIndex-1)*FBIN;
disp(strcat("Fund Frequency\t",num2str(FI),"\tHz"));
freqlist=1:sampleRate/LEN_ACQ:sampleRate;
semilogx
(freqlist(1:LEN_ACQ/2),FFTY(1:LEN_ACQ/2),current_color);xlabel("Frequency(
kHz)");ylabel("dBFS");
axis ([100 sampleRate/2 -120 0])
title ("STM32 ADC Spectrum");
hold off;
analyzeSNR(FI,sampleRate,ADCACQ,current_color);
analyzeTHD(FI,sampleRate,FFTY,current_color);
subplot(2,2,3);grid on;
disp('---------------- SCRIPT END -------------------');
Revision history
STMicroelectronics NV and its subsidiaries (“ST”) reserve the right to make changes, corrections, enhancements, modifications, and
improvements to ST products and/or to this document at any time without notice. Purchasers should obtain the latest relevant information on
ST products before placing orders. ST products are sold pursuant to ST’s terms and conditions of sale in place at the time of order
acknowledgement.
Purchasers are solely responsible for the choice, selection, and use of ST products and ST assumes no liability for application assistance or
the design of Purchasers’ products.
Resale of ST products with provisions different from the information set forth herein shall void any warranty granted by ST for such product.
ST and the ST logo are trademarks of ST. All other product or service names are the property of their respective owners.
Information in this document supersedes and replaces information previously supplied in any prior versions of this document.