Sunday, April 3, 2016

Copying STMCubeMX libraries into Eclipse


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.
         
Select: Project / Generate Code

The resulting source directory look like this:

Setting up the project in Eclipse

    Create new project

      1. File/New/C++ Project
        1. Set Project Name.  
        2. Select "Hello World ARM Cortex-M C/C++ Project
        3. Target Processor 
          1. Processor Core:  Cortex-M3
          2. Clock (Hz): 16000000
          3. Flash Size (kB): 128
          4. Ram size (kB): 20
          5. Use System Calls: "Freestanding (no POSIX system calls)
          6. Trace output: ARM ITM (via SWO)
          7. Checkboxes
            1. Select only:
              1. Check some warnings
              2. Use -Og on debug
        4. Folders
          1. Include folder: include
          2. Source folder: src
          3. System folder: system
          4. CMSIS library folder: cmsis
          5. C library folder: newlib
          6. Linker scropts folder: ldscripts
          7. Vendor CMSIS name: stm32l1xx
        5. Select Configurations
          1. Select bot Debug and Release.
        6. Tool Chains
          1. Take defaults
        7. 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:
      1. <Eclipse_Project>/system/include/cmsis/*  (leave the empty folder)
      2. <Eclipse_Project>/system/include/cortexm  (whole directory)
      3. <Eclipse_Project>/system/src/cmsis/*
      4. <Eclipse_Project>/system/src/cortexm  (whole directory)
      5. <Eclipse_Project>/system/src/newlib (whole directory)
      6. <Eclipse_Project>/src/*.*
      7. <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
        1. Copy <cube_output>/Drivers/CMSIS/Include/*  to <Eclipse_Project>/system/include/cmsis
        2. Copy the following three files from <cube_output>/Drivers/CMSIS/Device/ST/STM32L1xx/Include/to <Eclipse_Project>/system/include/cmsis
          1. stm32l151xb.h 
          2. stm32l1xx.h 
          3. system_stm32l1xx.
        3. Copy <cube_output>/Drivers/STM32L1xx_HAL_Driver/inc/* to <Eclipse_Project>/system/include/stm32l1xx/
      Source files
        1. Copy the following two files from <cube_output>/Drivers/CMSIS/Device/ST/STM32L1xx/Source/Templates/ to <Eclipse_Project>/system/src/cmsis
          1. system_stm32l1xx.c
          2. gcc/startup_stm32l151xb.s
        2. Change the assembly file suffix to ".S"
          1. Rename <Eclipse_Project>/system/src/cmsis/startup_stm32l151xb.s to startup_stm32l151xb.S
        3. Copy <cube_output>/Drivers/STM32L1xx_HAL_Driver/src/* to <Eclipse_Project>/system/src/stm32l1xx/
        4. Copy <cube_output>/src/* to <Eclipse_Project>/src
        5. Rename <Eclipse_Project>/src/main.c -> main.cpp
        6. Copy <cube_output>/inc/* to <Eclipse_Project>/include
      Block unused files
      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
      1. Fix ldscripts/mem.ld
      2. 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
      1. Change line 10 include from "cmsis_device.h" to "stm32l1xx.h"

      Build the code

      No comments:

      Post a Comment