Tutorial – How to use TM libraries with System Workbench for STM32

This tutorial will go step-by-step how to use my (TM) libraries with free SW4STM32 (System Workbech for STM32). This IDE is based on eclipse and it uses GCC compiler.

Tutorial coverage

  • Project template creation with STM32CubeMX software with correct clock setup,
  • How to manually enable HAL modules from STM32Cube package
  • How to add TM libraries to project and successfully compile it.

First things first

To avoid any misunderstanding and later troubles, please read this post how my HAL libraries are structured and which files are necessary with library to work.

Please note, all comments regarding topic explained in this post will be ignored.

This concept of tutorial will also work in Atollic TrueSTUDIO.

I will use STM32F7-Discovery board for tutorial presentation.

Step 1 – project generation with STM32CubeMX

If you already have project in SW4STM32 setup, you can skip this part, otherwise go step-by-step and follow instructions.

  1. Download STM32CubeMX if you don’t have it
  2. Download STM32Cube package for family. In my example, I have STM32CubeF7 package
  3. Open STM32CubeMX and create new project
  4. Select family and MCU and create project

    Select STM32 MCU

    Select STM32 MCU

  5. Enable HSE for RCC peripheral and select Crytal/Ceramic oscillator. There is a mistake on image!

    Select RCC and enable Crystal option for HSE

    Select RCC and enable Crystal option for HSE

  6.  On clock tab, set clock for your device. For F7-Discovery I used 25MHz HSE clock and 216MHz system clock.

    Set clock by writing clock speed in selected rectangle and press enter

    Set clock by writing clock speed in selected rectangle and press enter

  7. Export project and select SW4STM32 for Toolchain/IDE. Check Code Generation tab and choose Link external libraries
    Make sure your project export options are the same

    Make sure your project export options are the same

    Select link external libraries instead of copy

    Select link external libraries instead of copy

  8. Click OK and generate project.
  9. Open System Workbench and go to File -> Import. A new window will open where under General select Existing Projects into Workspace. Go next.
  10. Choose Select root directory and find folder of your project.

    Import existing project to workspace

    Import existing project to workspace

  11. Click finish and project will be imported to workspace.
  12. Project is imported, you should see something similar to image below if you open all directories.

    Imported project into workspace

    Imported project into workspace

Step 2 – add TM libraries to project

Read carefully post (link above) regarding my libraries as 2 files are important (defines.h and stm32fxxx_hal.h) for project to compile libraries.

This step will assume you have downloaded all my libraries (link above) and you have them in single directory somewhere on your disk. This is important as we will add libraries to project as external link instead of copy/paste inside project folder. This allows us to have multiple projects and single library file. If bug is found in library, fix has to be done only in single lib instead of everywhere.

  1. If you don’t already have it, create defines.h file inside Inc directory in project and set this structure:
  2. Make sure stm32fxxx_hal.h file is in directory where all TM HAL libraries are present.
  3. Open project properties. Go to Project -> Properties. On the left choose C/C++ General -> Paths and Symbols. Select tab Symbols in view and add new symbol which indicates family you use. In my case I used STM32F7, so I added new symbol STM32F7xx. With other words, these symbols are global defines for project and will be visible across entire project.
    Add necessary symbols

    Add necessary symbols

    When you use TM DISCO library, you will have to additionally add symbol to specify discovery/nucleo/eval board if you have it. With this symbol you tell TM DISCO library how to setup leds, buttons and UART settings if used in project. For STM32F7-Discovery, add additional symbol STM32F7_DISCOVERY, for other boards check TM DISCO library documentation.

  4. Click OK to close properties window.
  5. Next step is to add virtual directory for TM libraries. Create new directory inside project (Right click on folder -> New -> Folder). I created new folder inside Src folder with name TM.

    New TM folder inside Src folder

    New TM folder inside Src folder

  6. It is time to add external TM libraries to project. In explorer go to folder where you have TM libraries and select all .c files you need in project. Since this tutorial will cover UART example, we need TM USART, TM GPIO and TM BUFFER libraries. Select tm_stm32_usart.c, tm_stm32_buffer.c and tm_stm32_gpio.c files and use drag&drop technique to add them to eclipse project. Eclipse will ask you to Copy or Link files. Choose Link to files and click OK.

    Drag&drop TM libraries to TM folder in eclipse

    Drag&drop TM libraries to TM folder in eclipse

  7. Now we have added TM libraries for USART functionality. Since we added project with default settings, we are missing STM32Cube HAL libraries for USART peripheral. With the same technique, locate STM32Cube package you downloaded and under Drivers/STM32…_HAL_Drivers/Inc folder are files for usart and uart. Add both files to project.

    HAL Drivers for UART

    HAL Drivers for UART

  8. We have .c files included in project and now we have to specify where our .h files are for those .c files. Go to Properties -> C/C++ General -> Paths and Symbols -> Includes -> GNU C and add path where .h files are (include files). In my case, I have .c and .h files in the same folder, so I added the same folder as from where I linked my libraries.

    Add include path for TM libraries

    Add include path for TM libraries

  9. In project, open file Inc/stm32…_hal_conf.h and uncomment modules you require in project. In our case, I uncommented macros for HAL_USART_MODULE_ENABLED and HAL_UART_MODULE_ENABLED. This will enable to use HAL UART/USART functions in project.
  10. Compile project with CTRL + B shortcut.
  11. In main.c file add 2 lines of code to send data via UART. On STM32F7-Discovery board you can use this code:

    Final code and result

    Final code and result

If you don’t know how to run code after compile, go to Run -> Run and select AC6 STM32 C/C++ Application.

Finish

That is all in this tutorial. In any problems, make a comment and will try to figure it out.

tilz0R

Owner of this site. Application engineer, currently employed by STMicroelectronics. Exploring latest technologies and owner of different libraries posted on Github.

You may also like...

Read before commenting!

Before you make a new comment, make sure you agree with things listed below:

  • - Read post to make sure if it is already posted what you are asking for,
  • - Make sure you have the latest version of libraries used in your project,
  • - Make a clean and grammatically correct written message,
  • - Report as many details as possible, including what have you done so far,
  • - Do NOT post any code here. Use Pastebin,
  • - Do NOT post any error codes here. Use Pastebin,
  • - Specify STM32Fxxx family and used Discovery/EVAL/Nucleo or custom made board,
  • - Make sure your clock is set correct for PLL,
  • - If you are using my HAL drivers, please check this post how to start.
Comment will be deleted on breaking these rules without notification!