This example uses a 4th order Butterworth low pass filter that was designed in GNU Octave. The sampling rate was set to 200kHz and the cut-off frequency was set to 20kHz. The filter output at 20kHz is shown below and, as expected, shows an attenuation of 0.7 (approx the square root of 2).
Various attempts were made to optimize the performance of the filter. The execution time was measured by flipping an output bit either side of the filter code. An oscilloscope trace of this output is below.
As can be seen, the execution time is 1.78 microseconds. This is pretty quick given that floating point numbers are being used. I found that my attempts to manually improve the performance made no significant difference compared to what the compiler’s optimizer could do. I also found that gcc’s -O2 optimization setting produced a faster filter than -O3. The filter shuffles data in the input and output delay lines. This may be considered less than optimal but, given that the order of the filter is low, it probably would make little difference to use circular buffers (and manage buffer state etc).
Code can be downloaded here on Github and should be easily compiled on Linux/Windows/Mac
The STM32L476 Discovery board has an LSM303 Accelerometer/Compass IC and an L3GD20 gyroscope attached to the MCU using an SPI bus and some chip select lines. I wanted to experiment with them with a view to putting together a balancing robot. Supporting code for the following was needed for this:
- an SPI interface
- the LSM303
- the L3GD20
- serial communications
- periodic interrupts to pace data capture
Rather than build a complex Makefile I went with a simple shell script (or batch file if you prefer) with the following commands:
arm-none-eabi-gcc -static -mthumb -g -mcpu=cortex-m4 *.c -T linker_script.ld -o main.elf -nostartfiles
arm-none-eabi-objcopy -g -O binary main.elf main.bin
Note: your PATH environment variable must include the directory where arm-none-eabi-gcc is located.
The resulting main.bin file can then be copied to the virtual disk presented by the mbed interface on the STM32L476 discovery board. (The program waits for you to press the centre joystick button before starting).`
Serial communications is carried out over the built-in ST-Link USB-Serial emulator so no additional hardware is needed (9600,n,8,1).
Code is available over here
I have begun working the STM32L476 Discovery board taking a “Bare metal” approach. It is a great board with some nice peripherals. Code will be built up over time over here
To compile this code you need the a cross compiler for ARM that works on your system. You don’t need a fancy debugger or complicated software : the board has an mbed interface so you can just copy the program you develop to the board as if it was a removable disk.