AN4841 Application Note - STMicroelectronics

3y ago
14 Views
2 Downloads
1.38 MB
25 Pages
Last View : 21d ago
Last Download : 3m ago
Upload by : Annika Witter
Transcription

AN4841Application noteDigital signal processing for STM32 microcontrollers using CMSISIntroductionThis application note describes the development of digital filters for analog signals, and thetransformations between time and frequency domains. The examples discussed in thisdocument include a low-pass and a high-pass FIR filter, as well as Fourier fast transformswith floating and fixed point at different frequencies.The associated firmware (X-CUBE-DSPDEMO), applicable to STM32F429xx andSTM32F746xx MCUs, can be adapted to any STM32 microcontroller.Digital Signal Processing (DSP) is the mathematical manipulation and processing ofsignals. Signals to be processed come in various physical formats that include audio, videoor any analog signal that carries information, such as the output signal of a microphone.Both Cortex -M4-based STM32F4 Series and Cortex -M7-based STM32F7 Series provideinstructions for signal processing, and support advanced SIMD (Single Instruction MultiData) and Single cycle MAC (Multiply and Accumulate) instructions.The use of STM32 MCUs in a real-time DSP application not only reduces cost, but alsoreduces the overall power consumption.The following documents are considered as references: PM0214, “STM32F3 and STM32F4 Series Cortex -M4 programming manual”, availableon www.st.com PM0253, “STM32F7 Series Cortex -M7 programming manual”, available on www.st.com CMSIS - Cortex Microcontroller Software Interface Standard, available onwww.arm.com Arm compiler toolchain Compiler reference, available on http://infocenter.arm.com “Developing Optimized Signal Processing Software on the Cortex -M4 Processor”,technical paper by Shyam Sadasivan, available on www.techonline.com.February 2018AN4841 Rev 21/25www.st.com1

ContentsAN4841Contents1Basic DSP notions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51.12342/251.1.1Floating point . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51.1.2Fixed point . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61.1.3Fixed-point vs. floating-point . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7Cortex DSP instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82.1Saturation instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82.2MAC instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82.3SIMD instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103.1Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103.2Transforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10DSP application development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114.1CMSIS library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .114.2DSP demonstration overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .114.35Data types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54.2.1FFT demonstration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124.2.2FFT performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134.2.3FIR filter demonstration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154.2.4FIR filter design specification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174.2.5FIR performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204.2.6FIR example software overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20Overview of STM32 product lines performance . . . . . . . . . . . . . . . . . . . . 22Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24AN4841 Rev 2

AN4841List of tablesList of tablesTable 1.Table 2.Table 3.Table 4.Table 5.Table 6.Pros and cons of number formats in DSP applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7Saturating instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8SIMD instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9FIR filter specifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17FFT performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24AN4841 Rev 23/253

List of figuresAN4841List of figuresFigure 1.Figure 2.Figure 3.Figure 4.Figure 5.Figure 6.Figure 7.Figure 8.Figure 9.Figure 10.Figure 11.Figure 12.Figure 13.Figure 14.Figure 15.4/25Single precision number format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5Double precision number format. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 532 bits fixed point number format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6FFT size calculation performance on STM32F429 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13FFT size calculation performance on STM32F746 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13Running FFT 1024 points with input data in Float-32 on STM32F429I-DISCO . . . . . . . . . 14Running FFT 1024 points with input data in Float-32 on STM32F746-DISCO. . . . . . . . . . 15Block diagram of the FIR example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15Generated input (sum of two sine waves) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16Magnitude spectrum of the input signal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17FIR filter verification using MATLAB FVT tool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19FIR filter computation performance for STM32F429. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20FIR filter computation performance for STM32F746. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20FIR demonstration results on STM32F429I-DISCO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21FIR demonstration results on STM32F746-DISCO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21AN4841 Rev 2

AN4841Basic DSP notions1Basic DSP notions1.1Data typesDSP operations can use either floating-point or fixed-point formats.1.1.1Floating pointFloating point is a method to represent real numbers.The floating point unit in the Cortex -M4 is only single precision, as it includes an 8-bitexponent field and a 23-bit fraction, for a total of 32 bits (see Figure 1). The floating pointunit in the Cortex -M7 supports both single and double precision, as indicated in Figure 2.The representation of single/double precision floating-point number is, respectivelyValue (-1)s x M x 2(E-127), or Value (-1)s x M x 2(E-1023)where S is the value of the sign bit, M is the value of the mantissa, and E is the value of theexponent.Figure 1. Single precision number format ELWV6LJQ ELW([SRQHQW ELWV0DQWLVVD ELWV06 9 Figure 2. Double precision number format ELWV 6LJQ ELW([SRQHQW ELWV0DQWLVVD ELWV06 9 AN4841 Rev 25/2524

Basic DSP notions1.1.2AN4841Fixed pointFixed point representation expresses numbers with an integer part and a fractional part, in a2-complement format. As an example, a 32-bit fixed point representation, shown in Figure 3,allocates 24 bits for the integer part and 8 bits for the fractional part.Figure 3. 32 bits fixed point number format ELWV,QWHJHU SDUW ELWV)UDFWLRQ ELWV06 9 Available fixed-point data sizes in Cortex -Mx cores are 8-, 16- and 32-bit.The most common format used for DSP operations are Q7, Q15 and Q31, with onlyfractional bits to represent numbers between -1.0 and 1.0.The representation of a Q15 number is:Value ( – 1 )bs ( b 14 2–1 b 13 2–2 b 1 2– 14 b0 2– 15)where bs is the sign bit (the 15th bit), and bn is the digit for bit n.The range of numbers supported in a Q15 number is comprised between -1.0 and 1.0,corresponding to the smallest and largest integers that can be represented, respectively-32768 and 32767.For example, the number 0.25 will be encoded in Q15 as 0x2000(8192).When performing operations on fixed-point the equation is as follows:c a operand bwhere a, b and c are all fixed-point numbers, and operand refers to addition, subtraction,multiplication, or division. This equation remains true for floating-point numbers as well.Note:6/25Care must be taken when doing operations on fixed-point numbers.For example, if c a x b with a and b in Q31 format, this will lead to a wrong result since thecompiler will treat it as an integer operation, consequently it will generate “muls a, b” and willkeep only the least significant 32 bits of the result.AN4841 Rev 2

AN48411.1.3Basic DSP notionsFixed-point vs. floating-pointTable 1 highlights the main advantages and disadvantages of fixed-point vs. floating-point inDSP applications.Table 1. Pros and cons of number formats in DSP applicationsNumber formatFixed pointFloating pointAdvantagesFast implementationSupports a much wider range of valuesDisadvantagesLimited number rangeCan easily go in overflowNeeds more memory spaceAN4841 Rev 27/2524

Cortex DSP instructions2AN4841Cortex DSP instructionsThe Cortex -Mx cores feature several instructions that result in efficient implementation ofDSP algorithms.2.1Saturation instructionsSaturating, addition and subtraction instructions are available for 8-, 16- and 32-bit values,some of these instructions are listed in Table 2.Table 2. Saturating instructionsCodeFunctionQADD8Saturating four 8-bit integer additionsQSUB8Saturating four 8-bit integer subtractionQADD16Saturating two 16-bit integer additionsQSUB16Saturating two 16-bit integer subtractionQADDSaturating 32-bit addQSUBSaturating 32-bit subtractionThe SSAT (Signed SATurate) instruction is used to scale and saturate a signed value to anybit position, with optional shift before saturating.2.2MAC instructionsMultiply ACcumulate (MAC) instructions are widely used in DSP algorithms, as in the caseof the Finite Impulse Response (FIR) and Infinite Impulse Response (IIR).Executing multiplication and accumulation in single cycle instruction is a key requirement forachieving high performance.The following example explains how the SMMLA (Signed Most significant word MuLtiplyAccumulate) instruction works.2.3SIMD instructionsIn addition to MAC instructions that execute a multiplication and an accumulation in a singlecycle, there are the SIMD (Single Instruction Multiple Data) instructions, performing multipleidentical operations in a single cycle instruction.8/25AN4841 Rev 2

AN4841Cortex DSP instructionsTable 3 lists some SIMD instructions.Table 3. SIMD instructionsCodeFunctionqadd16Performs two 16-bit integer arithmetic additions in parallel, saturating the results to the16-bit signed integer range -215 x 215 - 1.uhadd16Performs two unsigned 16-bit integer additions, halving the results.shadd18Performs four signed 8-bit integer additions, halving the results.smlsdPerforms two 16-bit signed multiplications, takes the difference of the products,subtracting the high half-word product from the low half-word product, and adds thedifference to a 32-bit accumulate operand.The following example explains how the shadd8 instruction works.The shadd8 intrinsic returns: The halved addition of the first bytes from each operand, in the first byte of the returnvalue The halved addition of the second bytes from each operand, in the second byte of thereturn value The halved addition of the third bytes from each operand, in the third byte of the returnvalue The halved addition of the fourth bytes from each operand, in the fourth byte of thereturn valueAN4841 Rev 29/2524

AlgorithmsAN48413Algorithms3.1FiltersThe most common digital filters are: FIR (Finite Impulse Response): used, among others, in motor control and audioequalization IIR (Infinite Impulse Response): used in smoothing dataThe IIR filter can be used to implement filters such as Butterworth, Chebyshev, and Bessel.3.2TransformsA transform is a function that converts data from a domain into another.The FFT (Fast Fourier Transform) is a typical example: it is an efficient algorithm used toconvert a discrete time-domain signal into an equivalent frequency-domain signal based onthe Discrete Fourier Transform (DFT).10/25AN4841 Rev 2

AN4841DSP application development4DSP application development4.1CMSIS libraryThe Arm Cortex Microcontroller Software Interface Standard (CMSIS) is avendor-independent hardware abstraction layer for all Cortex processor based devices.CMSIS has been developed by Arm in conjunction with silicon, tools and middlewarepartners.The idea behind CMSIS is to provide a consistent and simple software interface to theprocessor for interface peripherals, real-time operating systems, and middleware,simplifying software re -use, reducing the learning curve for new microcontrollerdevelopments and reducing the time to market for new devices.CMSIS library comes with ST firmware under \Drivers\CMSIS\.The CMSIS-DSP library includes: Basic mathematical functions with vector operations Fast mathematical functions, like sine and cosine Complex mathematical functions like calculating magnitude Filtering functions like FIR or IIR Matrix computing functions Transform functions like FFT Controller functions like PID controller Statistical functions like calculating minimum or maximum Support functions like converting from one format to another Interpolation functionsMost algorithms uses floating-point and fixed-point in various formats. For example, in FIRcase, the available Arm functions are: 4.2arm fir init f32arm fir f32arm fir init q31arm fir q31arm fir fast q31arm fir init q15arm fir q15arm fir fast q15arm fir init q7arm fir q7DSP demonstration overviewThe goal of this demonstration is to show a full integration with STM32F429 using ADC,DAC, DMA and timers, and also calling CMSIS routines, all with the use of graphics, takingadvantage of the 2.4" QVGA TFT LCD included in the discovery board.AN4841 Rev 211/2524

DSP application developmentAN4841This demonstration also shows how easy it is to migrate an application from an STM32F4microcontroller to one of the STM32F7 Series.A graphical user interface is designed using STemWin, to simplify access to differentfeatures of the demonstration.4.2.1FFT demonstrationThe main features of this FFT example are For the STM32F429–Generate data signal and transfer it through DMA1 Stream6 Channel7 to DACoutput Channel2–Acquire data signal with ADC Channel0 and transfer it for elaboration throughDMA2 Stream0 Channel0–Vary the frequency of the input signal using Timer 2–Initialize FFT processing with various data: Float-32, Q15 and Q31–Perform FFT processing and calculate the magnitude values–Draw input and output data on LCD screenFor the STM32F746–Generate data signal and transfer it through DMA1 Stream5 Channel7 to DACoutput Channel1–Acquire data signal with ADC Channel4 and transfer it for elaboration throughDMA2 Stream0 Channel0–Vary the frequency of the input signal using Timer 2–Initialize FFT processing with various data: Float-32, Q15 and Q31–Perform FFT processing and calculate the magnitude values–Draw input and output data on LCD screenThe code below shows how to initialize the CFFT function to compute a 1024, 256 or 64points FFT and transform the input signals (aFFT Input f32) from the time domain to thefrequency domain, then calculate the magnitude at each bin, and finally calculate and returnthe maximum magnitude value.FFT Length depends on the user choice, it can be 1024, 256 or 64. The user can find FFTinitialization and processing for other formats in the fft processing.c source file.12/25AN4841 Rev 2

AN48414.2.2DSP application developmentFFT performanceFigure 4 shows the absolute execution time and the number of cycles taken to perform anFFT on STM32F429 device running at 180 MHz, while Figure 5 refers to the sameparameters measured on an STM32F746 device running at 216 MHz, in both cases usingMDK-Arm (5.14.0.0) toolchain supporting C Compiler V5.05 with Level 3 (-O3) for timeoptimization.Figure 4. FFT size calculation performance on STM32F429Figure 5. FFT size calculation performance on STM32F746AN4841 Rev 213/2524

DSP application developmentAN4841Results on STM32F429I-DISCOTo run one of the FFT examples select FFT, then connect PA5 to PA0.Signal shape and spectrum are displayed on the LCD.By varying the slider position the user can see the new input signal shape and the FFTspectrum of the input signal updated in real time, as illustrated in Figure 6.Figure 6. Running FFT 1024 points with input data in Float-32 on STM32F429I-DISCO14/25AN4841 Rev 2

AN4841DSP application developmentResults on STM32F746-DISCOIn this case it is possible to take advantage of the existing connection between PA4 andDCMI HSYNC. No other connections are needed since PA4 is configured as an output forDAC1 and an input for ADC1.Signal shape and spectrum are displayed on the LCD.By varying the slider position the user can see the new input signal shape and the FFTspectrum of the input signal updated in real time, as illustrated in Figure 7.Figure 7. Running FFT 1024 points with input data in Float-32 on STM32F746-DISCO4.2.3FIR filter demonstrationThe goal of this demonstration is to remove the spurious signal (a sine wave at 15 kHz) fromthe desired signal (a sine wave at 1 kHz), applying a low-pass FIR filter in different format.When choosing the Q15 format, it is possible to isolate the spurious signal applying ahigh-pass FIR filter.The block diagram of the FIR example is shown in Figure 8.Figure 8. Block diagram of the FIR exampleAN4841 Rev 215/2524

DSP application developmentAN4841The code below shows the initialization and the processing function for the floating-pointFIR filter.The user can find FIR initialization and processing for other formats in the fir processing.csource file.Input data to the FIR filter is the sum of the 1 kHz and 15 kHz sine waves (see Figure 9),generated by MATLAB in floating point format using the following script:Figure 9. Generated input (sum of two sine waves)16/25AN4841 Rev 2

AN4841DSP application developmentThe magnitude spectrum of the input signal (Figure 10) shows that there are twofrequencies, 1 kHz and 15 kHz.Figure 10. Magnitude spectrum of the input signalAs the noise is positioned around 15 kHz, the cutoff point must be set at a lower frequency,namely at 6 kHz.4.2.4FIR filter design specificationThe main features are listed in Table 4.Table 4. FIR filter specificationsFeature / ParameterValueTypeLow-passOrder28Sampling frequency48 kHzCut-off frequency6 kHzAN4841 Rev 217/2524

DSP application developmentAN4841The low-pass filter is designed with MATLAB , using the commands shown belowNote:FIR filter order is equal to the number of coefficients -1.In order to verify the designed filter, it’s possible to use the Filter Visualization Tool inMATLAB using the following command:The Filter Visualization Tool (FVT) is a practical tool allowing the user

Digital Signal Processing (DSP) is the mathematical manipulation and processing of signals. Signals to be processed come in various physical formats that include audio, video or any analog signal that carries information, such as the output signal of a microphone. Both Cortex -M4-based STM32F4 Series and Cortex -M7-based STM32F7 Series provide

Related Documents:

STM32CubeIDE quick start guide - User manual Author: STMICROELECTRONICS Subject: STM32CubeIDE is STMicroelectronics integrated development environment for STM32 microcontrollers and microprocessors. Keywords: STM32, IDE, Eclipse , QSG Created Date: 11/2/2020 4:57:17 PM

[2]. Ben Jordan, Amit Bahl Rigid - Flex PCB Design a guide book for design, Sierra Proto express, 2013 [3]. STMicroelectronics. STM32 embedded graphic objects/ touchscreen library, 2011. [4]. STMicroelectronics. STM32F10xxx ı2c optimized examples, 2010. [5]. STMicroelectronics. Migratin

How a designer can make the most of STMicroelectronics serial EEPROMs Introduction Electrically Erasable and PROgrammable Memory (EEPROM) devices are standard products used for the non-volatile storage of data parameters, with a fine-granularity. This application note describes most of the internal architecture and related functionality of

– Electrostatic discharge protection Application Reverse battery protection of an electronic control unit Description The VN5R003H-E is a device made using STMicroelectronics VIPower technology. It is intended for providing reverse battery protection to an electronic module. This devic

S12ZVL LIN Enabled Ultrasonic Distance Measurement, Rev. 1.0 Freescale Semiconductor 11 3.4 Echo signal sampling and processing The object echo signal is sampled using the ADC module. The ADC module samples the data with an 8-bit resolution and a sampling speed of 2.2 us per one sample. When using the 40 kHz ultrasonic transducer,

Application note SPC560Pxx/SPC56APxx HW design guideline Introduction This application note is intended for hardware designers. It gives hardware design references on SPC560Pxx/SPC56APxx microcontroller. Four topics are covered: Voltage Regulator (VREG) Main oscillator Supply pins Reference Reset circuit www.st.com

March 2017 DocID028650 Rev 3 1/7 For further information contact your local STMicroelectronics sales office www.

From Ramsey/Sleeper Architectural Graphic Standards, 9th ed., The American Institute of Architects, 1994. 37 Overhangs Overhang variances are needed where curbs, sidewalks, and signage exist. Overhangs also come into effect where retaining walls are present. From Ramsey/Sleeper Architectural Graphic Standards, 9th ed., The American Institute of Architects, 1994. 38 Vertical Clearance Diagram .