Having trouble compiling STM32F4 Discovery example code


I just got an STM32F4 discovery board and I’m trying to compile some audio example code I found.

I’m able to compile and run basic LED blinking code using the mbed framework but I’m having trouble with the non-mbed stuff (I’m guessing cmsis?).

Here’s the project I’m trying to compile: STM32F4 audio example code on github

What I’ve tried:

  1. Create new project: platformio init --board disco_f407vg
  2. Edit platformio.ini and set the framework to cmsis
  3. Copy the source from the github project into the src directory of my platformio project
  4. Compile with platformio run --target upload

At first I got an error that stm32f4_discovery.h was missing. I’m new to the STM/ARM world (I’ve only done basic arduino stuff), so I’m not sure how much of these libraries are included with platformio. Is there a way to get these standard STM32F4 discovery library files to be automatically included?

Anyway, through some digging I found that (and other) source files in that same github repo.

So I copied those source files over into my project, then tried compiling again. It gave me more errors of missing files, so I copied the other libraries. Eventually all the files were there, but now I’m getting lots of other errors.

Here’s a small snippet of the errors:

src/main.c:36:1: error: unknown type name 'RCC_ClocksTypeDef'
RCC_ClocksTypeDef RCC_Clocks;
src/main.c: In function 'main':
src/main.c:62:3: warning: implicit declaration of function 'RCC_GetClocksFreq' [-Wimplicit-function-declaration]
src/main.c:63:28: error: request for member 'HCLK_Frequency' in something not a structure or union
SysTick_Config(RCC_Clocks.HCLK_Frequency / 100);
src/main.c: In function 'TIM_LED_Config':
src/main.c:102:3: error: unknown type name 'TIM_OCInitTypeDef'
TIM_OCInitTypeDef  TIM_OCInitStructure;
src/main.c:103:3: error: unknown type name 'TIM_TimeBaseInitTypeDef'
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
src/main.c:104:3: error: unknown type name 'NVIC_InitTypeDef'

Anyway, long story short, my question is: am I missing something obvious and simple here? Is there some basic STM32F4 discovery set of libraries that come with platformio that I’m not seeing somehow?

Sorry for the newbish question but I’m scratching my head here trying to build simple standard example code and I feel like I’m having to manually import a lot of what seems like basic core library files (e.g. stm32f4xx_gpio.h) which I assumed would be part of platformio when initializing using the stm32f4 discovery board. So before I go insane trying to manually get things to compile I wanted to check first whether I’m doing something stupid :slight_smile:

Thanks for your help!

Hey nebs, very sad that nobody answered your question before, I’ve been there, and probably after all these years you found an answer and became better firmware developer. But if for any reason you did not find the answer, or just in case it helps others, I will add my suggestion.
Turns out that the code you were trying to use is STM32CubeMX generated code, I understand your frustration of mixing layers of who made which code, been there.
Anyways, CMSIS is a more general framework from ARM to describe all Cortex M (at least, not sure if all the Cortex)
So, what can help to diagnose this kind of things, look into the project source files, check the include files and how the things are declared. A very good book that’s helping me to identify these differences is “The definitive guide to Arm Cortex M3/M4…”
Anyways, also you could say that it’s from STMicro for the prefix, STM32, so it could be CubeMX generated, since it’s their tool to generate code, generally it looks way more like a high level framework than CMSIS.
Then, as platformio supports both frameworks, for example I just ran a search of the file in the platformio configurations folder and found


So, even not recognizing the function calls in the files, this lets us know that it’s cubemx generated code.
First things first, look for the specific STM32F4XX on platformio, at this point, if I run it, it shows me only this one STM32F407VGT6, you can see there that it supports STM32Cube, so all good.
Create a new project select your board and the STM32Cube framework.

If for some reason the board appears on the “boards” section on “PIO Home” but not in the search for new project boards, you could chose a board of the same line F4, like the discovery I mentioned above, then change it in platformio.ini afterward. Copy the specific name appearing in the ‘boards’ section into the ‘board’ property on the platformio.ini

Now if the board, does not appear on the section boards of PIO Home, then you need to add your custom board, way more complex, but still not that complex than configure it from scratch with command-line linker scripts and makefiles.
You can find more info here

I hope that I can add a bit of light to you or someone that, like us, got lost into the myriad of layers that this thing shows, I certainly know that this have been a very difficult path for me just because of these details.
Hope you’re doing great.

BTW, I just want to add that it seems that the path solver is having some issues, so sometimes it shows not found dependencies even when in the c_cpp_properties.json the correct paths are listed.
I’m still not sure how to fix this, but for me, enabling the “Squiggles errors” fixed (for now at least)
I pressed Ctrl + shift + p to open the command search, typed

enable error squiggles
The only option filtered popped up, then I selected it, and no more errors.
This has been commented in other posts, but just in case it helps here.