Import projects from STM32CubeMX to Embedded Studio
This article describes how to import STM32CubeMX projects into Embedded Studio. With Embedded Studio you get great benefits such as a runtime library and toolchain specifically tailored for embedded target devices, native J-Link support and many more.
Using the MakeFile configuration
Setup
The following software and hardware has been used to create the following example project and tutorial:
- STM32CubeMX 6.14.0.
- NUCLEO-G474RE MB1367C Evalboard
- SEGGER Embedded Studio V8.22a
Reference Project: NUCLEO-G474RE_CubeMXImport.zip
Project Import using Makefile configuration
This tutorial will show the steps necessary to get the resulting example project from above. The same steps apply for any of the ST boards and MCUs in STM32CubeMX. When using a project generated for Make, we start with an Embedded Studio project and then import all necessary files and information.
- Download your device family's CPU Support Package in Embedded Studio via Tools->Package Manager, select your device family and install the package
- For the STM32G474RE install the STM32G4xx Package
- Create a new project using the C/C++ executable template from the just installed package
- In this case we select "A C/C++ executable for STMicroelectronics STM32G4xx"
- In the project configuration, we select the correct target device. In this case STM32G474RETx.
- Everything else can be left at default
- As we plan to use most of the files provided by STM32CubeMX, we need to remove any files not related to the ES toolchain. Keep the following files and remove all other from the project.
- *_Vectors.s (in this case stm32g474xx_Vectors.s)
- SEGGER_THUMB_Startup.s
- *_Startup.s (in this case STM32G4xx_Startup.s)
- *_Target.js (in this case STM32G4xx_Target.js)
The project has now been prepared, so we can now go ahead and generate a project using CubeMX
- Start a new project in STM32CubeMX
- In this example we create a project for the NUCLEO-G474RE board
- Configure your STM32CubeMX project as usual
- Go to the Project Manager tab in your STM32CubeMX project
- Set the project name
- Set the project location to a folder within the Embedded Studio project (in this case $(ProjectDir)/CubeMX)
- Set the Toolchain/IDE to Makefile
- Press the Generate Code button
The source files are now prepared and ready to be integrated into the Embedded Studio project.
- Open the Embedded Studio project
- Open the generated Makefile in Embedded Studio or a text editor of your choice
- Find the section C_SOURCES. Here the core and driver source files are listed
- In ES, create a new folder (Right-click on project -> New folder...) Core. Add the files listed in the Makefile to the folder.
- For the drivers we create a dynamic folder. In the Create new project folder dialog open the Dynamic Folder Options below the name field when creating a folder and set the path to be used:
We add this directory dynamically, so all the drivers are still included should the CubeMX project be updated and exported again at a later point.
- Add all defines found in the C_DEFS section to the Code > Preprocessor > Preprocessor Definitions project option in the common configuration
-D should be omitted here.
- Next replace all include paths in Code > Preprocessor > User Include Directories with the ones found in the C_INCLUDES section
- Prepend the relative path using the $(ProjectDir) macro
-I should be omitted here as well.
We can now try to build the project:
In our case a template file is included, which does not compile. Either update it as described in the file itself, or remove it from the dynamic folder with a filter (Right-click on folder -> Setup..., as we will do here:
Any other template files may be handled the same way. To filter all template files, simply add *template*.
Now our project builds as expected and is ready for any further application development
Using the STM32CubeIDE configuration
Setup
The following software and hardware has been used to create the following example project and tutorial:
- STM32CubeMX 6.3.0.
- NUCLEO-G474RE MB1367C Evalboard
- SEGGER Embedded Studio V5.62
Reference Project: NUCLEO-G474RE_Blinky_Test.zip
The reference project will blink an LED on the eval board mentioned above.
Project Import using STM32CubeIDE configuration
This tutorial will show the steps necessary to get the resulting example project from above. The same steps apply for any of the ST boards and MCUs in STM32CubeMX.
- Start a new project in STM32CubeMX. (In this example we create a project for the NUCLEO-G474RE Board)
- Configure your STM32CubeMX project as usual.
- Go to the Project Manager tab in your STM32CubeMX project.
- Set Project Name and Project Location.
- Set the Toolchain/IDE to STM32CubeIDE.
- Press the Generate Code button.
- You will be prompted whether you want to open the projects folder. Select Open Folder and keep that folder open.
- Open Embedded Studio.
- Select File -> Import Project... -> Import Eclipse Project
- Navigate to the Project Location, select the .project file and press Open.
- Select Internal Toolchain and press OK.
- Embedded Studio imports the project, generates a project file project.emProject, and shows a success message.
- In Embedded Studio right click the Core folder in the Project Explorer window and select Setup...
- Edit the Exclude Specifications line to Core/;syscalls.c;sysmem.c;startup_*.s and press OK.
The basic project import and setup is done. The project can be further tweaked for the selected target device, by using target-specific files from the CPU Support Package:
- Download your device family's CPU Support Package in Embedded Studio via Tools->Package Manager, select your device family and install the package. (For the STM32G474RE install the STM32G4xx Package)
- Next go to the package folder via File->Open Studio Folder...->Packages Folder and open the just installed package folder. (In this example /STM32G4xx/)
- In the Project Location create a new folder /ES/ and enter it.
- Copy the following device-specific files from the package folder to this folder. (In this example from $(PackagesDir)/STM32G4xx/ to $(ProjectDir)/ES/)
- The startup code, /Source/<DeviceName>_Startup.s. (Source/STM32G4xx_Startup.s)
- The vector table, /Source/<DeviceName>_Vectors.s. (Source/STM32G474_Vectors.s)
- The linker script, /Scripts/<DeviceFamily>_Flash.icf. (Source/STM32G4xx_Flash.icf)
- The memory map, /XML/<DeviceName>_MemoryMap.xml. (/XML/STM32G474RETx_MemoryMap.xml)
- The registers file, /XML/<DeviceName>_Registers.xml. (/XML/STM32G474xx_Registers.xml)
- Add that /ES/ folder via drag and drop to your Embedded Studio project in the Project Explorer window.
- Open project options and change the build config to Common.
- Set Code -> Linker -> Linker Script File to the .icf file.
- Set Code -> Linker -> Memory Map File to the *_MemoryMap-.xml file.
- Set Debug -> Debugger -> Register Definition File to the *_Registers.xml (if available).
- In the projects Explorer remove the generic startup code in Internal Files/Cortex_M_Startup.s.
Done. The project is imported, setup, and tweaked for your target system. You can now add your code and debug your application in Embedded Studio.
Troubleshooting
- The project builds, but debugging is not working properly and unexpected code is being jumped to.
- Check your debug config settings. Unfortunately most CubeMX projects will have code optimization set to -Os (optimize for size) even for debug builds. Change the code optimization to 'None' instead.
- I can not find a CPU support package for my device.
- Generally the import can also be done without the device specific files from the CPU support packages as the generic files should also work for most cases. In such case simply make sure that the memory segments set in project options also fit your target device properly.
- Alternatively you can always contact our support team to see if a CPU support package could be added. You can reach the team under segger.com/ticket
- I can't find the .project file in the folder where I exported the CubeMX project to.
- On some OS's (e.g. Linux, macOS) files with an empty name before the file type are hidden in the file explorer. To fix this go to your host OS's explorer settings and disable that option which hides such files. Then you should be able to see the .project file again which then you can import as explained in the guide above.
Setup
The following software and hardware has been used to create the following example project and tutorial:
- STM32CubeMX 6.3.0.
- NUCLEO-G474RE MB1367C Evalboard
- SEGGER Embedded Studio V5.62
Reference Project: NUCLEO-G474RE_Blinky_Test.zip
The reference project will blink an LED on the eval board mentioned above.
Project Import
This tutorial will show the steps necessary to get the resulting example project from above. The same steps apply for any of the ST boards and MCUs in STM32CubeMX.
- Start a new project in STM32CubeMX. (In this example we create a project for the NUCLEO-G474RE Board)
- Configure your STM32CubeMX project as usual.
- Go to the Project Manager tab in your STM32CubeMX project.
- Set Project Name and Project Location.
- Set the Toolchain/IDE to STM32CubeIDE.
- Press the Generate Code button.
- You will be prompted whether you want to open the projects folder. Select Open Folder and keep that folder open.
- Open Embedded Studio.
- Select File -> Import Project... -> Import Eclipse Project
- Navigate to the Project Location, select the .project file and press Open.
- Select Internal Toolchain and press OK.
- Embedded Studio imports the project, generates a project file project.emProject, and shows a success message.
- In Embedded Studio right click the Core folder in the Project Explorer window and select Setup...
- Edit the Exclude Specifications line to Core/;syscalls.c;sysmem.c;startup_*.s and press OK.
The basic project import and setup is done. The project can be further tweaked for the selected target device, by using target-specific files from the CPU Support Package:
- Download your device family's CPU Support Package in Embedded Studio via Tools->Package Manager, select your device family and install the package. (For the STM32F474RE install the STM32G4xx Package)
- Next go to the package folder via File->Open Studio Folder...->Packages Folder and open the just installed package folder. (In this example /STM32G4xx/)
- In the Project Location create a new folder /ES/ and enter it.
- Copy the following device-specific files from the package folder to this folder. (In this example from $(PackagesDir)/STM32G4xx/ to $(ProjectDir)/ES/)
- The startup code, /Source/<DeviceName>_Startup.s. (Source/STM32G4xx_Startup.s)
- The vector table, /Source/<DeviceName>_Vectors.s. (Source/STM32G474_Vectors.s)
- The linker script, /Scripts/<DeviceFamily>_Flash.icf. (Source/STM32G4xx_Flash.icf)
- The memory map, /XML/<DeviceName>_MemoryMap.xml. (/XML/STM32G474RETx_MemoryMap.xml)
- The registers file, /XML/<DeviceName>_Registers.xml. (/XML/STM32G474xx_Registers.xml)
- Add that /ES/ folder via drag and drop to your Embedded Studio project in the Project Explorer window.
- Open project options and change the build config to Common.
- Set Code -> Linker -> Linker Script File to the .icf file.
- Set Code -> Linker -> Memory Map File to the *_MemoryMap-.xml file.
- Set Debug -> Debugger -> Register Definition File to the *_Registers.xml (if available).
- In the projects Explorer remove the generic startup code in Internal Files/Cortex_M_Startup.s.
Done. The project is imported, setup, and tweaked for your target system. You can now add your code and debug your application in Embedded Studio.
Troubleshooting
- The project builds, but debugging is not working properly and unexpected code is being jumped to.
- Check your debug config settings. Unfortunately most CubeMX projects will have code optimization set to -Os (optimize for size) even for debug builds. Change the code optimization to 'None' instead.
- I can not find a CPU support package for my device.
- Generally the import can also be done without the device specific files from the CPU support packages as the generic files should also work for most cases. In such case simply make sure that the memory segments set in project options also fit your target device properly.
- Alternatively you can always contact our support team to see if a CPU support package could be added. You can reach the team under segger.com/ticket
- I can't find the .project file in the folder where I exported the CubeMX project to.
- On some OS's (e.g. Linux, macOS) files with an empty name before the file type are hidden in the file explorer. To fix this go to your host OS's explorer settings and disable that option which hides such files. Then you should be able to see the .project file again which then you can import as explained in the guide above.