- Introduction
- Pinout & Configuration
- Design and Generate Filter Coefficients
- Code Configuration
- Demo
- Reference
Declaration | Value |
---|---|
Cut-off Frequency | 2000 Hz |
Fs | 48000Hz |
Fpass | 0 Hz - 2000 Hz |
Fstop | 5000 Hz - 24000 Hz |
TEST_LENGTH_SAMPLES | 19084 (number of input data) |
BLOCK_SIZE | 1 |
NUM_TAPS | 17 |
In this project, we store the input and output of the FIR filter in an SD card. Therefore, we need to set the interface between the SD card module and STM32.
SD card Pin | Board Pinout |
---|---|
VCC | 5V |
GND | GND |
SCK | PA5 |
MISO | PA6 |
MOSI | PA7 |
CS | PB6 |
-
First, create a new project using STM32 cube IDE. In the pinout & configuration, we will connect our SD card using the SPI1 pin. After enabling the SPI1 connection, our SCK pin will be auto-assigned to PA5, MISO pin to PA6 and MOSI pin to PA7. In the SPI1 setting, we need to set it to Full Duplex Master so that both master and slave can send data at the same time via the MOSI and MISO lines respectively. Then, change the clock prescaler to 128 to reduce the baud rate of the SD card.
-
Connect the CS pin to PB6 using a GPIO connection and label it as SD_CS.
-
Next, communicate the SD card module with the FAT file system provided by Middleware. We changed the maximum sector size from 512 to 4096 so that it can support any type of memory card.
-
In this project, we used the serial monitor to debug our connection and check our data transfer status. We connect the board to the serial monitor using default UART, USART2. The UART mode is set to asynchronous because UART interface does not use a clock signal to synchronize the transmitter and receiver devices and the baud rate was changed to 9600 Bits/s.
-
In the system mode setting, the debug state changed to serial wire mode. For the RCC setting, we used a crystal/ceramic resonator to generate our clock. While for the clock configuration, we set it to maximum frequency,180 MHz.
Design the filter using this website. In this project, we set the spec to:
from | to | gain | ripple/att. |
---|---|---|---|
0 Hz | 2000 Hz | 1 | 5db |
5000 Hz | 24000 Hz | 0 | -30db |
First, we need to import the FatFS SD card function (fatfs_sd.c and fatfs_sd.h) to our project and include it in the header of user_diskio.c and main.c. In the user_diskio.c Private Functions, we return all the data to the controller through SPI1 using the function we just added.
In the main.c file, we can check our SD card connection status using the f_mount function and print the status to the serial monitor using the send_uart function. After that, we enter our main loop and start to read-write data to our SD card and monitor through the serial monitor.
After include the CMSIS DSP library, we will use these two function to implement FIR in our project which are arm_fir_f32 and arm_fir_init_f32. The arm_fir_init_f32 is an associated initialization function for each data type. The initialization function has two main function. It is used to set the values of the internal structure fields and initialize the values in the state buffer. If the initialization function is used, then the instance structure cannot be placed into a constant data section. There is a total of five parameters are needed for this function. First, S is points to an instance of the floating-point FIR filter structure. numTaps is number of filter coefficients in the filter. firCoeffs32 points to the filter coefficients buffer which are the values of the impulse response generated from the online website. firStateF32 points to the state buffer, blockSize is the number of samples processed per call.
This figure below shows the use of FIR function in our project main.c file. All the variable and parameter are first declared. Then, the fir function is called in the main function and pass all the parameter and variables accordingly for the FIR filter algorithm computation.