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. Also electronic enthusiasts, web developer, 3D printer fan, handball player and more. Big fan of STM32F4 devices. In anticipation of the new Discovery board for STM32F7 lines.

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!
  • Pingback: All STM32F4 tutorials - STM32F4 Discovery()

  • Luiz kun

    Hi can you make Tourial like this for KeilC v5 ,please?

    • I think I can try hard to do it but I won’t guarantee anything.

      • Luiz kun

        Hi can i have you Fb or Email to contact to you , i don’t know how to use TM library with STM32 CubeMX and KeilC . Please help me

  • Peter Mather

    Hi

    Many thanks for this tutorial

    I’m having a problem where the STM HAL header files aren’t being picked up. So I’m getting lots of compile errors e.g.

    Description Resource Path Location Type
    Symbol ‘UART_ADVFEATURE_NO_INIT’ could not be resolved tm_stm32_usart.c /sw4stm32_tutorial/Src/TM line 1374 Semantic Error

    This symbol appears in file stm32f7xx_hal_uart.h which is in the project and I can browse to it in project explorer

    Can you help diagnose which bit of the jigsaw I am missing?

    Thanks

    • I believe you failed in step 9.

      • Peter Mather

        No definitely done

        /* #define HAL_SPI_MODULE_ENABLED */
        /* #define HAL_TIM_MODULE_ENABLED */
        #define HAL_UART_MODULE_ENABLED
        #define HAL_USART_MODULE_ENABLED
        /* #define HAL_IRDA_MODULE_ENABLED */
        /* #define HAL_SMARTCARD_MODULE_ENABLED */
        /* #define HAL_WWDG_MODULE_ENABLED */
        /* #define HAL_PCD_MODULE_ENABLED */
        /* #define HAL_HCD_MODULE_ENABLED */
        /* #define HAL_DFSDM_MODULE_ENABLED */
        /* #define HAL_DSI_MODULE_ENABLED */
        /* #define HAL_JPEG_MODULE_ENABLED */
        /* #define HAL_MDIOS_MODULE_ENABLED */
        /* #define HAL_SMBUS_MODULE_ENABLED */
        /* #define HAL_MMC_MODULE_ENABLED */
        #define HAL_GPIO_MODULE_ENABLED
        #define HAL_DMA_MODULE_ENABLED

        • Try to search for macro which is not found in project.

          • Peter Mather

            Seems like it was some caching issue or conditional compilation bug in Eclipse. I closed workbench and then re-opened and the problem went away.

            I still have one issue I would like help on.

            To get the example to compile I had to #include “tm_stm32_usart.h”

            in main.c otherwise I got error: ‘TM_USART_PinsPack_1’ undeclared

            Is this correct or should all the tm headers be picked up automatically?

            Finally, you should probably include in the tutorial that you need to do steps 3 and 8 for each configuration (release and debug)

            I used USART6, pinspack1 on 746 discovery which configures RX/TX on the normal pins on the Arduino header. Makes it easy to test.

          • Yes, you have to include tm_stm32_usart.h in main if you want to use it.
            USART I used is directly connected to USB port of ST-Link so everything you write to that USART will be automatically send to PC and vice versa.