Building STM32CubeMX code in Eclipse
The process of building code in Eclipse from STM32CubeMX can be complicated. Below are step by step instructions I've used to successfully build code for an STM32L151CB processor. They should work for any STM32Lxx processor, I suspect.
Export the libraries from Cube
From STM32CubeMX
Select Project / Settings
Notice, I have my IDE set to EWARM. There is no option for Eclipse, unfortunately.
The resulting source directory look like this:
Setting up the project in Eclipse
Create new project
- File/New/C++ Project
- Set Project Name.
- Select "Hello World ARM Cortex-M C/C++ Project
- Target Processor
- Processor Core: Cortex-M3
- Clock (Hz): 16000000
- Flash Size (kB): 128
- Ram size (kB): 20
- Use System Calls: "Freestanding (no POSIX system calls)
- Trace output: ARM ITM (via SWO)
- Checkboxes
- Select only:
- Check some warnings
- Use -Og on debug
- Folders
- Include folder: include
- Source folder: src
- System folder: system
- CMSIS library folder: cmsis
- C library folder: newlib
- Linker scropts folder: ldscripts
- Vendor CMSIS name: stm32l1xx
- Select Configurations
- Select bot Debug and Release.
- Tool Chains
- Take defaults
- Finish
Delete Prototype files
We'll be using the files from Cube. We get rid of the ones placed in here by Eclipse. We'll just use the hierarchy and some of the configuration it created.
From within the Eclipse IDE, delete the following files from the new project:
- <Eclipse_Project>/system/include/cmsis/* (leave the empty folder)
- <Eclipse_Project>/system/include/cortexm (whole directory)
- <Eclipse_Project>/system/src/cmsis/*
- <Eclipse_Project>/system/src/cortexm (whole directory)
- <Eclipse_Project>/system/src/newlib (whole directory)
- <Eclipse_Project>/src/*.*
- <Eclipse_Project>/include/*.*
Drag and drop files from Cube
Drag and drop files from Windows (or your platform) into the Eclipse IDE. That way, they will automatically be added to the makefiles. When prompted to "Copy" vs "Link the files", select "Copy".
Header files
- Copy <cube_output>/Drivers/CMSIS/Include/* to <Eclipse_Project>/system/include/cmsis
- Copy the following three files from <cube_output>/Drivers/CMSIS/Device/ST/STM32L1xx/Include/to <Eclipse_Project>/system/include/cmsis
- stm32l151xb.h
- stm32l1xx.h
- system_stm32l1xx.
- Copy <cube_output>/Drivers/STM32L1xx_HAL_Driver/inc/* to <Eclipse_Project>/system/include/stm32l1xx/
- Copy the following two files from <cube_output>/Drivers/CMSIS/Device/ST/STM32L1xx/Source/Templates/ to <Eclipse_Project>/system/src/cmsis
- system_stm32l1xx.c
- gcc/startup_stm32l151xb.s
- Change the assembly file suffix to ".S"
- Rename <Eclipse_Project>/system/src/cmsis/startup_stm32l151xb.s to startup_stm32l151xb.S
- Copy <cube_output>/Drivers/STM32L1xx_HAL_Driver/src/* to <Eclipse_Project>/system/src/stm32l1xx/
- Copy <cube_output>/src/* to <Eclipse_Project>/src
- Rename <Eclipse_Project>/src/main.c -> main.cpp
- Copy <cube_output>/inc/* to <Eclipse_Project>/include
Right-click on the following files in Eclipse, select "Resource Configurations / Exclude from Build". Hit "Select All" to choose both Debug and Release, and hit "OK".
- <Eclipse_Project>/system/src/stm32l1xx/stm32l1xx_hal_msp_template.c
Update properties / includes, and pre-processor macros
Preprocessor definitions
For simplicity, set the preprocessor defines the same for the Assembler, C, and C++. The following defines are set:
- STM32L151xB
- USE_HAL_DRIVER
- OS_USE_TRACE_ITM
- TRACE
- DEBUG
- ARM_MATH_CM3
- HSE_VALUE=16000000
- USE_FULL_ASSERT
Fix Linker parameters
Under Properties / C/C++ Build / Tool Settings / Cross ARM C++ Linker /
- General
- All checkboxes cleared except "Remove Unused Sections"
- Miscellaneous
- All checkboxes cleared.
- Other linker Flags: --specs=rdimon.specs -Wl,--start-group -lgcc -lc -lc -lm -lrdimon -Wl,--end-group
- Fix ldscripts/mem.ld
MEMORY { RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 16K CCMRAM (xrw) : ORIGIN = 0x00000000, LENGTH = 0K FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 128K RW_EEPROM (rw) : ORIGIN = 0x08080000, LENGTH = 4K FLASHB1 (rx) : ORIGIN = 0x00000000, LENGTH = 0 EXTMEMB0 (rx) : ORIGIN = 0x00000000, LENGTH = 0 EXTMEMB1 (rx) : ORIGIN = 0x00000000, LENGTH = 0 EXTMEMB2 (rx) : ORIGIN = 0x00000000, LENGTH = 0 EXTMEMB3 (rx) : ORIGIN = 0x00000000, LENGTH = 0 MEMORY_ARRAY (xrw) : ORIGIN = 0x20002000, LENGTH = 0 }
Source code modifications
The trace code from Eclipse has to be modified slightly to compile with the Cube libraries.
Modify system/src/diag/trace_impl.c
- Change line 10 include from "cmsis_device.h" to "stm32l1xx.h"
- Change line 10 include from "cmsis_device.h" to "stm32l1xx.h"
No comments:
Post a Comment