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.


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


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


    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?


    • I believe you failed in step 9.

      • Peter Mather

        No definitely done

        /* #define HAL_SPI_MODULE_ENABLED */
        /* #define HAL_TIM_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_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.

  • MVA

    Hi Tilen,
    Thank you for your Tutorial. It helped. I am using your MPU6050 Library. And using SWSTM32 IDE. But the Software shows errors:

    1.GPIO_AF4_I2C1′ undeclared (first use in this function)
    2.make: *** [Src/MPU6050_Lib/tm_stm32_i2c.o] Error 1
    3.recipe for target ‘Src/MPU6050_Lib/tm_stm32_i2c.o’ failed subdir.mk /BIF/Debug/Src/MPU6050_Lib

    I am new to SWSTM32 so I don’t know what these errors are. will you be able to help?

    • These are helper macros. You can simply replace GPIO_AF4_I2C1 with 0x04 and it will work the same.

  • Pawel

    I tried to use yours libraries but whatever i compile does work even simple led toggle .I pass all steps of this tutorial without any error

    • Directly saying anything to this message is hard as I don’t know what hardware you use (if startup file matches, if clock works, if oscillator is there, etc) neither which SW version you have.

      • Pawel

        clock set by CUBE to 100MHz on STM32F411V@DISCOVERY then import project to eclipse with yours quide

        • Did you clearly read section 3 of Step 2 – add TM libraries to project. TM DISCO lib is for leds. Anyway, I assume projects runs if it was generated with cube.

          • Pawel

            Yeah i read it few times. Finally i have two symbols : STM32F4xx and STM32F411_DISCOVERY. Files are where supposed to be i only use eclipse my project . Cube use to configure peripherals-> bulid ->import to eclipse.

          • Pawel Wiśniewski
          • Next step is to use debugger to see what code is doing. Also, I don’t see HAL_Init function call, nor other. By debugging, you can inspect your code in details.

          • Pawel Wiśniewski

            HAL_init() missing on screen . I remove it in combat action with code .The solution was simple .Remove all project form eclipse and add/import it again. I dont know why but it works now .

            Thanks for help

  • Igal

    Hi Tilen,
    Big thanks for this web site. It helped me a lot in learning.

    Can you please explain how to use your code with IAR or Keil workbench.
    Can you just put out there one file of the project.
    after importing you libraries unfortunately I was unable to change them to compile in IAR.


  • Baam ✓ᵛᵉʳᶦᶠᶦᵉᵈ

    Hi Tilen .
    I’m a fresh graduate Engineer from Egypt . I’m currently working on a project and it was decided that we will utilize the STM32F407G discovery board . The problem is that it was also decided that the Atollic truestudio lite is the IDE that we will be using as well . I have been searching far and wide and I found near to nigh of tutorials . I have been Using Microchip’s PIC up until now and I’m new to the ARM as well . Do you have any suggestions as to how I should proceed?

    • Atollic is for sure better, specially because ST acquired them and are now part of ST’s ecosystem. In the future you can expect more from TrueSTUDIO Lite version. There are tutorials available, for sure would suggest reading arm core documentation and use of STM32Cube software. For start I suggest HAL drivers and later moving to LL drivers to improve knowledge of low-level on MCU.

      • Baam ✓ᵛᵉʳᶦᶠᶦᵉᵈ

        and where can I find those tutorials ? all I’m getting is how to use the tools of truestudio and nothing of the design pattern that is favored or how to actually do serial , I2C or any other thing about that . It is probably because I’m looking in the wrong direction . I’m sorry to be asking so much, but a man in need can’t help but depend on those with knowledge .

        • STM32Cube software is available on ST.com. Since you are using STM32F4 family, searching for STM32Cube for STM32F4 will give you software package on link below. Download it and explore examples, drivers, etc. This is for sure first step.


          • Baam ✓ᵛᵉʳᶦᶠᶦᵉᵈ

            Thank you very much , I’ll get right into reading the documentation and see where this takes me .

  • Alejandro

    Hi, i follow step by step your tutorial and i have this error “‘TM_USART_PinsPack_3’ undeclared (first use in this function)”, can you help me please.
    Sorry for my english, i from Argentina

    • Alejandro

      When I include “tm_stm32_usart.h” in main.c this error appears “undefined reference to HAL_UART_Init”

  • Drukas

    Dear Tilen,

    in relation to a university project I am trying to install the hal libraries.
    Unfortunatelly, after working thru all the steps I do always get the following Problem:

    In all tm_stm32_… fils in the subfolder several type names (e.g. uint32_t, uint16_t, GPIO_TypeDef) are unknown.

    Can you maybe help me about where I messed up?
    I am using Atollic True Studio an STM32F4 Discovery Board.


    • To me this sounds like wrong include path setup. AC6 and TS prigrams have little different conf setup.

  • Alejo Aldana

    Hi Tilen.
    I been working thru all steps, and I do always what you said, but I get the same problem over and over: Cannot find binaries. I’m working with SW in Macosx thanks!

  • Elman

    Dear Tilen,

    Does this process work for 401?

    Kind Regards

  • Ratko

    Hi, Tilen,

    I’m trying to use your HAL libs in combination with CubeMX generated code for STM32F030 on SW4STM32.
    I have problems in compiling (‘USART_MODE_TX_RX’ undeclared (first use in this function), etc)….

    Problem was in that HAL definitions for USART differs from your usart library, which cause errors when using STM32F0 – HAL defines all as UART_….. and your libs defines USART_. I redefine all needed in defines.h, for example:
    #ifndef TM_USART1_MODE
    ….etc, for all of the USART’s,

    and after that there was one problem left: ‘USART_FLAG_TXE’ undeclared (first use in this function) on line:

    I tried to put this code into defines.h with changing USART_FLAG_TXE to UART_FLAG_TXE.

    That didn’t solve the problem, until I comment out this line in tm_stm32_usart. After that code compiles correctly.

    Could all of this be avoided in some next lib version if there is conditional compiling part for STM32F0? It’s probably not a big issue for experienced programmer, but for the beginners (like me 😉 can be.

    Thank you for your effort on all these libs and sharing with others.