Sunday , 26 March 2017
iten
Home » Electronics » STM32 » How to quickly import a STM32CubeMX project inside an Eclipse project

How to quickly import a STM32CubeMX project inside an Eclipse project

I've implemented a faster way to automatically import CubeMX project into an Eclipse tool-chain based on the GNU ARM Plugin, as described either on this blog and in my book.

I've implemented a bare-bone python script that simply "translates" a CubeMX project for the SW4STM32 (aka AC6) tool-chain in a project generated with the GNU ARM plugin. The script can be downloaded from my github account. Let's see how this works.

First of all, we have to generate a new Eclipse project using the GNU ARM Plugin. Go to File->New->C Project and select "Hello World ARM Cortex-M C/C++ project. You can choose the project name you want. Click on “Next“. Here we assume "test1" as Eclipse project name.

Schermata 2015-06-04 alle 08.11.57In the next step you have to configure your microcontroller. For example, for a STM32-F4 you have to choose Cortex-M4 core, while for a STM32-F0 you have to choose Cortex-M0. The Clock, Flash size and RAM parameters depend on your MCU. For a STM32F401RE you can use the same values shown in the following picture. Set the other options as shown below.Schermata 2015-06-04 alle 08.14.12

If you are using a Nucleo, this table extracted from my book shows the right values for all Nucleo boards.

ch4-table-nucleo-specs

In the next step leave all parameters unchanged except for the last one: Vendor CMSIS name. Change it from DEVICE to stm32f4xx if you have a STM32F4 based board, or stm32f1xx for F1 boards, and so on. Please, be sure to use this pattern, otherwise the script simply doesn't work.Schermata 2015-06-04 alle 08.24.02Click on “Next“. You can leave the default parameters in the next steps. After a while, Eclipse will generate a new project for you. Now, click on the project root in the Project Explorer  view and click on "Close Project" entry. 

Now, use the CubeMX tool to configure your MCU according your needs. When finished, click on the Project->Generate code menu. In the Project Settings dialog give the name you want to the project and select an output directory where store the project. Here we assume that the CubeMX project name is "mymcu".  Choose SW4STM32 as Toolchain/IDE (this is really important, to not skip this step) and generate the code.

Finally, to use the tool I've made, you can easily type the following command at terminal prompt:

When finished, open the project "test1" in Eclipse, click with the right mouse button on the project root and choose "Refresh" (this will force the scan of the source tree, since it has been changed while the Eclipse project was closed). Finished. 🙂

Don't forget to update the file mem.ld, changing the FLASH origin address from 0x00000000 to 0x08000000.

The script now supports also the import of Middleware libraries (FatFS, FreeRTOS, LwIP).

The script is designed to work both in Python 2.7 and 3.x. It requires the lxml library.

Windows users can download a pre-compiled lxml package directly from here:

https://pypi.python.org/packages/2.7/l/lxml/lxml-3.5.0.win32-py2.7.exe#md5=3fb7a9fb71b7d0f53881291614bd323c

Linux and MacOS X users can install lxml using pip:

Please, let me know if it worked for you 😉


Check Also

How to restore ST-LINK interface after a bad update (2.26.15 firmware)

Several people are reporting me issues with the latest 2.26.16 firmware update for the ST-LINK …

40 comments

  1. Hello!

    I run your script on windows 7. When I type such a line in cmd I get this

    http://itmag.es/1HOyO

    • Hi,
      The script is coded in Python 2.7, not Python 3.x. Use Python 2.7 to run it 😉

      • Hi Carmine!

        First a great big THANK YOU for all your hard work putting together guides, scripts and other material regarding STM32 development!

        I am trying to run your script on Win 8 using Python 2.7, but installing lxml (which I understand is needed) seems a bit out of my depth.

        Any advice?
        //Henrik

        • Hi Henrik,
          Sorry for late replay, but I'm experiencing an issue with this blog and emails (I don't receive email about comments any more and I don't know why.....).

          You can find an lxml binary distribution for Windows here:

          ftp://ftp.zlatkovic.com/libxml/

          Please, let me know if it works for you.

          Carmine

  2. Carmine,

    This is awesome. Thank you for providing this! The only issue I ran into was that the python script requires lxml (I had neither installed), but I found a Windows installation at https://pypi.python.org/simple/lxml/. I downloaded xml-3.5.0.win32-py2.7.exe and your script ran with no errors, and I was able to build a project according to your instructions.

    This will be extremely helpful to many of us that were using GCC and Eclipse with the old standard peripheral libraries. I greatly appreciate your efforts.

  3. Carmine,

    FYI, I tried enabling the FreeRTOS middleware, but the libraries did not get imported. Guess it's time to learn Python. The script you've already provided should give me a big head start.

    • Yes, the script currently doesn't import middleware libraries. I'll update it soon.

    • Hi Kevin,
      The script now supports the import of middleware libraries.

      • I was about to dig into Python yesterday when I saw your update. I only had one issue on my Windows machine - the include paths for the Middlewares were incorrect. I had to replace the forward slashes in line 320 with double backslashes to generate the correct include paths. Thanks again for your work. I had just tried the latest CubeMX with the new version of Atollic TrueStudio (v5.4 Lite is no longer code size limited), and I could not get it to import correctly, so I guess I'll be sticking with the GNU tools for now (thanks to you!)

        • Thanks for advising me of this incompatibility on Windows.

        • FYI, an update with respect to TrueStudio Lite and CubeMX - I was able to get it to work properly only if I did NOT copy the project into the workspace. The files MUST be referenced from the CubeMX TrueStudio output folder. When I left the Copy Project into Workspace checkbox unchecked in the Atollic Import Existing Projects into Workspace dialog, it worked seamlessly. This is now a really great option for building large projects using STM32 devices with CubeMX if you don't want to mess with setting up all of the GNU tools and GDB. I was able to import the CubeMX (with FreeRTOS) onto a 429i-Disco board, compile and debug, without having to modify anything. Kudos to Atollic for giving the Lite version unlimited code size capability. After I do some performance testing, I will likely upgrade to the Pro version to get the RTOS-aware debugging capabilities.

  4. Thank you for writing this script. It is quite helpful!

    I found that it worked best if I replaced mem.ld with a mem.ld file from a project created by (for example) the "STM32F4xx C/C++ Project" template. This gave me not only the correct flash and RAM origin addresses, but also correct definitions of other memory regions. However, I was not able to use your script to import STM32CubeMX code directly into such a project, due to mis-naming of the vendor CMSIS directory. So, I imported code with your script into a project created with the "Hello World..." template as directed, then replaced its mem.ld with one created in a different project that came from the "STM32F4xx..." template.

    Do you think you might consider adding support for importing STM32CubeMX code into Eclipse projects created with the various "STM32F..." templates? I think that this would make it a little bit easier to launch new projects with correct linker scripts for the more complex STM32F variants.

    • Hi,
      I've plans to improve the script. Maybe a good solution would be to have the ability to specify the CMSIS directory at command line, or a way to automatically discover its name.

  5. Hello,

    Thank you for your tutorial and the time spent on it.

    When I try your python script, i get the following error, any clue of where it comes ?

    Traceback (most recent call last):
    File "CubeMXImporter-master\cubemximporter.py", line 420, in
    cubeImporter.cubeMXProjectPath = args.cubemx_path
    File "CubeMXImporter-master\cubemximporter.py", line 49, in setCubeMXProjectPath
    self.detectHALInfo()
    File "CubeMXImporter-master\cubemximporter.py", line 188, in detectHALInfo
    options = root.xpath("//option[@superClass='gnu.c.compiler.option.preprocess
    or.def.symbols']")[0]
    AttributeError: 'str' object has no attribute 'xpath'

    • Carmine Noviello

      Hi,
      It seems that there is no .cproject file inside the "SW4STM32/project-name Configuration" folder. I have to add a better check inside the detectHALInfo() method.
      Try to regenerate the CubeMX C project ensuring that the selected toolchain is SW4STM32.

  6. you should really test the SW4STM32 (System Workbench for STM32, aka AC6) eclipse plugin.
    with the last version i'm able to directly open a CubeMX generated project and it builds and debugs without any further steps necessary.

  7. It worked for me, thanks!

  8. Hi Carmine,
    Thanks for the script and your guides, worked great and very helpful!

  9. Hai

    I am currently using your cubemximporter ,I tried to port freertos program to eclipse but the middlewares files are not importing properly. when i give the cubemximporter command in terminal it shows like :

    "The original CubeMX project contains the FreeRTOS middleware library.
    This library was imported in the Eclipse project correctly, but you still need to
    configure your tool-chain 'Float ABI' and 'FPU Type' if your STM32 support hard float
    (e.g. for a STM32F4 MCU set 'Float ABI'='FP Instructions(hard)'' and 'FPU Type'='fpv4-sp-d16'.
    Moreover, exclude from build those MemManage files (heap_1.c, etc) not needed for your project."

    in eclipse the error comes like

    " arm-none-eabi-gcc -mcpu=cortex-m3 -mthumb -Os -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections -ffreestanding -Wall -Wextra -g -DOS_USE_TRACE_SEMIHOSTING_DEBUG -DSTM32F103xB -I"../include" -I"../system/include" -I"../system/include/cmsis" -I"../system/include/stm32f103" -I"../system/include/stm32f1xx" -I"../system/include/cmsis/device" -I"Middlewares/Third_Party/FreeRTOS/Source/portable/GCC/ARM_CM3" -I"Middlewares/Third_Party/FreeRTOS/Source/include" -I"Middlewares/Third_Party/FreeRTOS/Source/CMSIS_RTOS" -std=gnu11 -MMD -MP -MF"src/freertos.d" -MT"src/freertos.o" -c -o "src/freertos.o" "../src/freertos.c"
    ../src/freertos.c:35:22: fatal error: FreeRTOS.h: No such file or directory
    #include "FreeRTOS.h"
    ^
    compilation terminated.
    make: *** [src/freertos.o] Error 1

    How to solve this?I have successfully ported the Hal files but problem with the freertos middlewares. Can you please help me?

    • I've checked with the latest CubeMX and CubeMXImporter tool and it works correctly. Your project seems to use FreeRTOS, but something wrong happened with your project settings. Review all the steps carefully.

  10. can u help me to add freertos n an existng file to a coide

  11. can u help me to add freertos n an existng project file to a coide from coocox

  12. Krzysztof Pudłowski

    Afret upgrading CubeMx to 4.19

    I have problem ....

    Microsoft Windows [Wersja 6.1.7601]
    Copyright (c) 2009 Microsoft Corporation. Wszelkie prawa zastrzeżone.

    C:\developing\stm32\python\CubeMXImporter-master>"convert - l1 blinkings.bat"

    C:\developing\stm32\python\CubeMXImporter-master>c:\python27\python.exe cubemxim
    porter.py C:\developing\stm32\workspace\STM32Cubel1Blinking D:\installs\stm\cub
    emxl1\stm32l1mxblinking\STM32l1mxblinking
    Traceback (most recent call last):
    File "cubemximporter.py", line 536, in
    cubeImporter.importCMSIS()
    File "cubemximporter.py", line 310, in importCMSIS
    shutil.copy(loc[0], loc[1])
    File "c:\python27\lib\shutil.py", line 119, in copy
    copyfile(src, dst)
    File "c:\python27\lib\shutil.py", line 82, in copyfile
    with open(src, 'rb') as fsrc:
    IOError: [Errno 2] No such file or directory: 'D:\\installs\\stm\\cubemxl1\\stm3
    2l1mxblinking\\STM32l1mxblinking\\Drivers/CMSIS/Device/ST/STM32L1xx/Source/Templ
    ates/gcc/startup_stm32l100xc.s'

    C:\developing\stm32\python\CubeMXImporter-master>

  13. Krzysztof Pudłowski

    And next while building Eclipse project

    Building target: STM32CubeL1Blinking.elf
    Invoking: Cross ARM C++ Linker
    arm-none-eabi-g++ -mcpu=cortex-m3 -mthumb -Og -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections -ffreestanding -fno-move-loop-invariants -Wall -Wextra -g3 -T mem.ld -T libs.ld -T sections.ld -nostartfiles -Xlinker --gc-sections -L"../ldscripts" -Wl,-Map,"STM32CubeL1Blinking.map" -o "STM32CubeL1Blinking.elf" ./system/src/stm32l1xx/stm32l1xx_hal.o ./system/src/stm32l1xx/stm32l1xx_hal_adc.o ./system/src/stm32l1xx/stm32l1xx_hal_adc_ex.o ./system/src/stm32l1xx/stm32l1xx_hal_cortex.o ./system/src/stm32l1xx/stm32l1xx_hal_dac.o ./system/src/stm32l1xx/stm32l1xx_hal_dac_ex.o ./system/src/stm32l1xx/stm32l1xx_hal_dma.o ./system/src/stm32l1xx/stm32l1xx_hal_flash.o ./system/src/stm32l1xx/stm32l1xx_hal_flash_ex.o ./system/src/stm32l1xx/stm32l1xx_hal_flash_ramfunc.o ./system/src/stm32l1xx/stm32l1xx_hal_gpio.o ./system/src/stm32l1xx/stm32l1xx_hal_pwr.o ./system/src/stm32l1xx/stm32l1xx_hal_pwr_ex.o ./system/src/stm32l1xx/stm32l1xx_hal_rcc.o ./system/src/stm32l1xx/stm32l1xx_hal_rcc_ex.o ./system/src/stm32l1xx/stm32l1xx_hal_tim.o ./system/src/stm32l1xx/stm32l1xx_hal_tim_ex.o ./system/src/newlib/_cxx.o ./system/src/newlib/_exit.o ./system/src/newlib/_sbrk.o ./system/src/newlib/_startup.o ./system/src/newlib/_syscalls.o ./system/src/newlib/assert.o ./system/src/diag/Trace.o ./system/src/diag/trace_impl.o ./system/src/cortexm/_initialize_hardware.o ./system/src/cortexm/_reset_hardware.o ./system/src/cortexm/exception_handlers.o ./system/src/cmsis/system_stm32l1xx.o ./src/main.o ./src/stm32l1xx_hal_msp.o ./src/stm32l1xx_it.o ./src/system_stm32l1xx.o
    ./src/system_stm32l1xx.o: In function SystemInit':
    C:\developing\stm32\workspace\STM32CubeL1Blinking\Debug/../src/system_stm32l1xx.c:161: multiple definition of
    SystemInit'
    ./system/src/cmsis/system_stm32l1xx.o:C:\developing\stm32\workspace\STM32CubeL1Blinking\Debug/../system/src/cmsis/system_stm32l1xx.c:161: first defined here
    ./src/system_stm32l1xx.o: In function SystemCoreClockUpdate':
    C:\developing\stm32\workspace\STM32CubeL1Blinking\Debug/../src/system_stm32l1xx.c:232: multiple definition of
    SystemCoreClockUpdate'
    ./system/src/cmsis/system_stm32l1xx.o:C:\developing\stm32\workspace\STM32CubeL1Blinking\Debug/../system/src/cmsis/system_stm32l1xx.c:232: first defined here
    ./src/system_stm32l1xx.o:(.rodata.APBPrescTable+0x0): multiple definition of APBPrescTable'
    ./system/src/cmsis/system_stm32l1xx.o:(.rodata.APBPrescTable+0x0): first defined here
    ./src/system_stm32l1xx.o: In function
    .LANCHOR2':
    system_stm32l1xx.c:(.rodata.AHBPrescTable+0x0): multiple definition of AHBPrescTable'
    ./system/src/cmsis/system_stm32l1xx.o:system_stm32l1xx.c:(.rodata.AHBPrescTable+0x0): first defined here
    ./src/system_stm32l1xx.o: In function
    .LANCHOR1':
    system_stm32l1xx.c:(.rodata.PLLMulTable+0x0): multiple definition of PLLMulTable'
    ./system/src/cmsis/system_stm32l1xx.o:system_stm32l1xx.c:(.rodata.PLLMulTable+0x0): first defined here
    ./src/system_stm32l1xx.o: In function
    .LANCHOR0':
    system_stm32l1xx.c:(.data.SystemCoreClock+0x0): multiple definition of `SystemCoreClock'
    ./system/src/cmsis/system_stm32l1xx.o:system_stm32l1xx.c:(.data.SystemCoreClock+0x0): first defined here
    collect2.exe: error: ld returned 1 exit status
    cs-make: *** [STM32CubeL1Blinking.elf] Error 1

  14. Krzysztof Pudlowski

    I have problem with generating template from Cube to Eclipse for Discovery F0.
    Program seems to by dead, When running from ST-link, I have halted state, and when I press in Core Panel Run button I have "Unable to run application message"

  15. Krzysztof Pudłowski

    I have four discovery boards

    1. F0Discovery Device family :STM32F051x4/F051x6/F051x8/F030x8
    2. F1Discovery Device family :STM32F100xx Low/Medium density Value Line
    3. F4Discovery Device family :STM32F411xC/E
    4. L1Discovery Device family :STM32L100xC/L15xxC/L162xC

    I'm trying to begin my work using hal templates for all of tose boards.

    Projects in CubeMX are here
    https://drive.google.com/open?id=0BzX4UpG5CGNzdVF4eHZza0dRT0k

    and after conversion in python

    Projects in Eclipse are here

    https://drive.google.com/open?id=0BzX4UpG5CGNzSktoTkpnTnBWVVU

    I'm doing something stupid wrong...

    F0 - Unable to run application message
    F1 - main loop is dead,
    F4 - works
    L1 - main loop is dead

    I'm interested at the beginning in using ADC for three pots, DAC for DDS audio output

    Eclipse IDE for C/C++ Developers

    Version: Mars.2 Release (4.5.2)
    Build id: 20160218-0600

    CubeMx version 4.19.0

    • I just checked the L1 project. If I'm not wrong, that MCU has 16K of SRAM + 4K of EEPROM. So the SRAM definition in mem.ld file is wrong (put it at 16K).

      • Krzysztof Pudłowski

        Changing to

        FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 16K
        RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 4K

        I Have

        Building target: STM32CubeL1Blinking.elf
        Invoking: Cross ARM C++ Linker
        arm-none-eabi-g++ -mcpu=cortex-m3 -mthumb -Og -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections -ffreestanding -fno-move-loop-invariants -Wall -Wextra -g3 -T mem.ld -T libs.ld -T sections.ld -nostartfiles -Xlinker --gc-sections -L"../ldscripts" -Wl,-Map,"STM32CubeL1Blinking.map" -o "STM32CubeL1Blinking.elf" ./system/src/stm32l1xx/stm32l1xx_hal.o ./system/src/stm32l1xx/stm32l1xx_hal_adc.o ./system/src/stm32l1xx/stm32l1xx_hal_adc_ex.o ./system/src/stm32l1xx/stm32l1xx_hal_cortex.o ./system/src/stm32l1xx/stm32l1xx_hal_dac.o ./system/src/stm32l1xx/stm32l1xx_hal_dac_ex.o ./system/src/stm32l1xx/stm32l1xx_hal_dma.o ./system/src/stm32l1xx/stm32l1xx_hal_flash.o ./system/src/stm32l1xx/stm32l1xx_hal_flash_ex.o ./system/src/stm32l1xx/stm32l1xx_hal_flash_ramfunc.o ./system/src/stm32l1xx/stm32l1xx_hal_gpio.o ./system/src/stm32l1xx/stm32l1xx_hal_pwr.o ./system/src/stm32l1xx/stm32l1xx_hal_pwr_ex.o ./system/src/stm32l1xx/stm32l1xx_hal_rcc.o ./system/src/stm32l1xx/stm32l1xx_hal_rcc_ex.o ./system/src/stm32l1xx/stm32l1xx_hal_tim.o ./system/src/stm32l1xx/stm32l1xx_hal_tim_ex.o ./system/src/newlib/_cxx.o ./system/src/newlib/_exit.o ./system/src/newlib/_sbrk.o ./system/src/newlib/_startup.o ./system/src/newlib/_syscalls.o ./system/src/newlib/assert.o ./system/src/diag/Trace.o ./system/src/diag/trace_impl.o ./system/src/cortexm/_initialize_hardware.o ./system/src/cortexm/_reset_hardware.o ./system/src/cortexm/exception_handlers.o ./system/src/cmsis/startup_stm32l100xc.o ./system/src/cmsis/system_stm32l1xx.o ./src/main.o ./src/stm32l1xx_hal_msp.o ./src/stm32l1xx_it.o
        c:/developing/stm32/sourcery_codebench_lite_for_arm_eabi/bin/../lib/gcc/arm-none-eabi/4.8.1/../../../../arm-none-eabi/bin/ld.exe: STM32CubeL1Blinking.elf section .text' will not fit in region FLASH'
        c:/developing/stm32/sourcery_codebench_lite_for_arm_eabi/bin/../lib/gcc/arm-none-eabi/4.8.1/../../../../arm-none-eabi/bin/ld.exe: region `FLASH' overflowed by 19780 bytes

        Best regards

        • The right definitions for your MCU are:

          FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 128K
          RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 16K

          • Krzysztof Pudłowski

            My mistake....
            You saved my day.
            Now it works for
            F0,F1,L1 and F4
            At least blinking demos.

            Thanks a lot.

  16. Hi Carmine,

    Your tutorials about STM32 dev with Eclipse+GCC+OpenOCD are very helpful, thank you !
    Unfortunately, I get the following error when running your script (Ubuntu 16.04 32-bit, Eclipse Neon, latest lxml pkg.) :

    Traceback (most recent call last):
    File "/home/user/CubeMXImporter/cubemximporter.py", line 541, in
    cubeImporter.importHAL()
    File "/home/user/CubeMXImporter/cubemximporter.py", line 339, in importHAL
    self.addAssemblerMacros((self.HAL_MCU_TYPE,))
    File "/home/user/CubeMXImporter/cubemximporter.py", line 129, in addAssemblerMacros
    self.__addOptionValuesToProject(macros, "ilg.gnuarmeclipse.managedbuild.cross.option.assembler.defs", False)
    File "/home/user/CubeMXImporter/cubemximporter.py", line 101, in __addOptionValuesToProject
    options[0]] # List all available values to avoid reinsert again the same value
    IndexError: list index out of range

    I am a bit familiar to Python, but not enough to spot my problem..

    • Are you exporting the project in CubeMX selecting the SW4STM32 toolchain?

      • Yes I selected SW4STM32 as target IDE in CubeMX. I tried again this morning with some fresh Eclipse and CubeMX projects, but got the same error.

        I wanted to see what would be my python error if I change the CubeMX output project format : I got the same error as in my previous post for all of them, except for TrueSTUDIO format : "The generated CubeMX project is not for SW4STM32"

        Running the python script as root didn't help either.

        • Are you using latest CubeMX 4.19? What about GNU ARM Eclipse plug-ins? Can you post both Eclipse and CubeMX projects somewhere so that I can give them a look?

          • OK, I spotted what was my problem :

            When creating the Eclipse project, if you uncheck "Debug" to only leave "Release", you'll get the same error as me. I suppose your script expects both Debug and Release to be checked, as implied in the tutorial.

            This was my only issue !
            It builds, it flashes, my Nucleo-F401RE is blinking, I am happy !
            Thank you. I hope your e-book will be published as paper version soon !

Leave a Reply

Your email address will not be published. Required fields are marked *