Stm32cube framework - How to build with proper CMSIS DSP library

Question

In PlatformIO STM32F103 uses the framework stm32cubef1, which has many driver and not all of them are consider during compile by the PlatformIO.

For Example I want to enable driver required for CMSIS DSP,

  • then libarm_cortexM3l_math.a is required, which is already present in ...\framework-stm32cubef1\Drivers\CMSIS\Lib\GCC
  • How enable or link this driver while compile? and what is correct way?

Current Work around[Working Good]

Step 1:

Copied the file libarm_cortexM3l_math.a from location ...\framework-stm32cubef1\Drivers\CMSIS\Lib\GCC into ..\lib\cmsis

Step 2:

In platformio.ini file

[env:bluepill_f103c8_128k]
platform = ststm32
board = bluepill_f103c8_128k
framework = stm32cube
upload_protocol = cmsis-dap
debug_tool = cmsis-dap
build_flags = 
    -D ARM_MATH_CM3 ; Required for CMSIS-DSP library(arm_math.h)
    -L lib/cmsis   ; add the directory where libarm_cortexM3l_math.a is located
    -l libarm_cortexM3l_math      ; instruct the linker to link this library

Step 3:

In ‘main.c’ file,

......
......
#include "arm_math.h"
int16_t data_set[] = {1234, 5678, 9876, 4321, 3456, 7890, 6543, 2109, 8765, 1234, 5678, 9012, 2345, 6789, 4321, 8765, 1098, 3456, 7890, 6543};
int16_t array_size = 19;
......
......
int main(void)
{
......
......
    int16_t _max_cmsis = 0;
    uint32_t _max_cmsis_index = -1;
    int16_t _min_cmsis = 0;
    uint32_t _min_cmsis_index = -1;
    arm_max_q15(data_set, array_size, &_max_cmsis, &_max_cmsis_index);
    arm_min_q15(data_set, array_size, &_min_cmsis, &_min_cmsis_index);
......
......
}
......
......

Other Case

Mentioned to download from CMSIS Version 5 library, which is no longer maintained. Arm moved to different repo named as CMSIS-DSP

You don’t need to do that at all. As documented, unless you set board_build.stm32cube.custom_dsp_library = yes in the platformio.ini, then the STM32Cube builder script will automatically find the path to CMSIS-DSP as it’s inside framework-stm32cubef1 and add it to the 1. includepath and 2. library include path.

However, it doesn’t yet link the library or define the right ARM_MATH_CMxxx macro.

Still, without copying any file, we can write

[env:bluepill_f103c8_128k]
platform = ststm32
board = bluepill_f103c8_128k
framework = stm32cube
build_flags = 
  -DARM_MATH_CM3
  -larm_cortexM3l_math

And that will use the libarm_cortexM3l_math.a from framework-stm32cubef1. (Note: When specfying to link a library with -l <library name>, that library name should be without the lib prefix. PlatformIO fixes up this common mistake silently though.)

So, this will compile without any problems

#include "stm32f1xx.h"
#include "arm_math.h"
int16_t data_set[] = {1234, 5678, 9876, 4321, 3456, 7890, 6543, 2109, 8765, 1234, 5678, 9012, 2345, 6789, 4321, 8765, 1098, 3456, 7890, 6543};
int16_t array_size = 19;

int main() {
    int16_t _max_cmsis = 0;
    uint32_t _max_cmsis_index = -1;
    int16_t _min_cmsis = 0;
    uint32_t _min_cmsis_index = -1;
    arm_max_q15(data_set, array_size, &_max_cmsis, &_max_cmsis_index);
    arm_min_q15(data_set, array_size, &_min_cmsis, &_min_cmsis_index);
    return 0;
}

Conversely, if you do have a custom-compiled CMSIS-DSP library, that is both header and library file, then you can add it like

[env:bluepill_f103c8_128k]
platform = ststm32
board = bluepill_f103c8_128k
framework = stm32cube
; DISABLE INTERNAL CMSIS-DSP LIBRARY
board_build.stm32cube.custom_dsp_library = yes
build_flags = 
    -D ARM_MATH_CM3 ; Required for CMSIS-DSP library(arm_math.h)
    ; assumes -I lib/cmsis is automatically present thanks to library structure
    -L lib/cmsis   ; add the directory where libarm_cortexM3l_math.a is located
    -l arm_cortexM3l_math      ; instruct the linker to link this library

Understand
Thanks for the detailed explanation @maxgerhardt