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.
- 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.
- Download STM32CubeMX if you don’t have it
- Download STM32Cube package for family. In my example, I have STM32CubeF7 package
- Open STM32CubeMX and create new project
- Select family and MCU and create project
- Enable HSE for RCC peripheral and select Crytal/Ceramic oscillator. There is a mistake on image!
- On clock tab, set clock for your device. For F7-Discovery I used 25MHz HSE clock and 216MHz system clock.
- Export project and select SW4STM32 for Toolchain/IDE. Check Code Generation tab and choose Link external libraries
- Click OK and generate project.
- Open System Workbench and go to File -> Import. A new window will open where under General select Existing Projects into Workspace. Go next.
- Choose Select root directory and find folder of your project.
- Click finish and project will be imported to workspace.
- Project is imported, you should see something similar to image below if you open all directories.
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.
- If you don’t already have it, create defines.h file inside Inc directory in project and set this structure:
defines.h empty fileC123456#ifndef __DEFINES_H#define __DEFINES_H/* Project definitions for TM libraries if necessary */#endif
- Make sure stm32fxxx_hal.h file is in directory where all TM HAL libraries are present.
- 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.
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.
- Click OK to close properties window.
- 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.
- 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.
- 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.
- 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.
- 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.
- Compile project with CTRL + B shortcut.
- In main.c file add 2 lines of code to send data via UART. On STM32F7-Discovery board you can use this code:
UART init and test data sendC12TM_USART_Init(USART1, TM_USART_PinsPack_3, 115200);TM_USART_Puts(USART1, "Hello world!\r\n");
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.