Hey! So you most likely here because you would like to challenge yourself and try to program 32bit microcontrollers using minimal amount of libraries ( HAL/CMSIS ). On the internet you will find multiple discussions which approach to use – I will not be touching that – I guess everyone has his own way 😉
We will starting by shortly discussing environment used and then we will move on to configuring our project. I will be using for this STM324F407 discovery board – but with the right changes you will be able to adjust it to your own needs.
I will cover in this post:
- Creating new project in Eclipse
- Setting all libraries
- Setting project & compilator settings
Before you start you should have
- Running Eclipse environment
- OpenOCD ( or equivalent )
- toolchain
- Installing STM32CubeMX
This project will use several files i.e. startup file / linker script and several header files.
Setting up project
Go ahead and create new ARM project. Here we will not be using any ‘Hello world’ or prepared examples – we start blank!
Here just go through the wizard and finish. When done we will move on getting extra files we need for our project.
Create folders inside the project folder so the structure will look as follow
Download firmware using STM32Cube
Now we need to start our STM32Cube to get all files we will need to move on with our project. Go to help -> install new libraries and then select the firmware for your cortex. For me it will be for STM32F4
Get following files into the project directories:
├── cmsis │ ├── include │ │ ├── cmsis_gcc.h │ │ ├── core_cm0plus.h │ │ ├── core_cm4.h ** THIS IS BASED ON YOUR CORTEX!! If you have different take different lib file !! ** │ │ ├── core_cmFunc.h │ │ ├── core_cmInstr.h │ │ └── core_cmSimd.h │ └── src ├── include ├── src │ └── main.c ( this is file created by you ) └── system ├── include │ ├── stm32f407xx.h │ ├── stm32f4xx.h │ └── system_stm32f4xx.h └── src ├── startup_stm32f407xx.s └── system_stm32f4xx.c
Modify project settings
Now since we have all the files we need your project should look similar to the following
We will now go and modify several project settings. Open project preferences and make required modifications
- Change extension used by assembler source file to be *.s ( it’s project specific setting )
- Change to appropriate cortex family model
- Modify preprocessor definitions to match your processor ( this settings in image below are for STM32F407VGx )
- Modify includes in Assembler and Compiler so it contains “${ProjDirPath}/cmsis/include” and “${ProjDirPath}/system/include“
- Under Linker -> General have the following ${ProjDirPath}/STM32F407VGTx_FLASH.ld ( if you are using different MCU then you will need to change this )
- Modify paths which include source code
Blink the diode
So now you are in position to just blink the diode using the following code in main.c
/* * main.c * * Created on: 12 Nov 2016 * Author: rafpe */ #include "stm32f4xx.h" #include "stm32f407xx.h" int main(void) { volatile uint32_t delay; RCC->AHB1ENR |= RCC_AHB1ENR_GPIODEN; // enable the clock to GPIOD __DSB(); GPIOD->MODER = (1 << 26); // set pin 13 to be general purpose output while (1) { for (delay = 1000000; delay; delay--) { GPIOD->ODR ^= (1 << 13); } } }
Compile and you are ready to flash your device.
Caveouts
Given all setup here we are HSI is used as system clock source. We will definitely come back to this as we will need more than i.e. 16 Mhz! Happy coding!