By default, most microcontrollers have no concept of a console, so you have to help them out a bit. When it comes to debugging, outputting information to some kind of console can be extremely useful. One option is to use semihosting with STM32CubeIDE.
However, semihosting can be extremely slow. Another good option is to output debug information over the serial port (UART). We can call the STM32 HAL functions (e.g. HAL_UART_Transmit), but sometimes it’s easier to use the standard C library functions printf, scanf, and so on. To do that, we need to re-write the underlying functions.
System Workbench for STM32 – Bare Metal Edition is an Eclipse integrated IDE. It provides a software development platform for your STM32 board. The IDE helps you to quickly create a C embedded project for your target. It also integrates a complete code editor, compilation (compiler, assembler, linker) tools and remote-debugging tools. GitHub is where people build software. More than 50 million people use GitHub to discover, fork, and contribute to over 100 million projects.
Note: the code for this section is taken from Carmine Noviello’s Mastering STM32 book. I am simply updating the process for how to get retargeting working in STM32CubeIDE. All credit goes to Carmine Noviello for his code. You are welcome to write your own retarget.h and retarget.c files, if you wish.
Start a new STM32 project with all the defaults. I am using a Nucleo-L476RG for this example. Enable UART, if needed. For almost all the Nucleo boards, UART2 is tied to the ST-LINK microcontroller, which gives us a virtual COM port over USB. Save and generate code.
The first thing we need to do is disable syscalls.c. This file defines many of the same functions that we are trying to make. If you do not disable it, you will likely get a “multiple definition” error when you try to compile and link. For example:
Right-click on the syscalls.c file and select Properties. Under C/C++ Build > Settings, check Exclude resource from build.
Click Apply and Close. The file name in the project browser should now be grayed out.
This will allow us to not include a file (or folder) when we compile and link in STM32CubeIDE. The process is similar for most other Eclipse-based IDEs.
Create a new file in the Inc directory called retarget.h. Copy the following code into this file:
Save this file.
Create a new file in the Src directory called retarget.c. Copy the following code into this file:
Save this file.
Your project directory structure should look like the following:
Notice that we’ve added retarget.h and retarget.c. We also removed syscalls.c from the build, but the file still exists in our project directory.
Include stdio.h and retarget.h in your main.c file. You can now use printf and scanf, as shown here:
Build this project and open a debugging session in STM32CubeIDE. Open your favorite serial terminal program and connect to the Nucleo’s COM port (baud rate of 115200, 8-N-1). You should be greeted by a query. Type out some answer and press
enter. Note that you will not be able to see what you typed. However, the program should append your entered text to a “Hello” message.
Try not to overflow your assigned character buffer. Bad things happen if you do. Or you could write some kind of bounds checking in your code. Anyway, this is just a starting point to hopefully help you with debugging on your STM32 project.
This is first of many upcoming tutorial articles on STM32 microcontrollers. I am learning and with them I will be sharing what I learn. In this article I will try to explain how to install System Workbench for STM32 or SW4STM32 short. Just a reminder, I was installing this over my previous installation, so there might be some differences from your installation.
First you need the software. You can find it on this link: http://www.openstm32.org/HomePage
Software is FREE but you need to be logged in to download it, so you need to register and login. There is link in the text.
Stm32 System Workbench
After login it will take you to download area. If not, click home on left side and then find link to download area in the text
Click on the version you need for your operating system and store it somewhere where you will find it later. I use windows 7 64bit so I need the install_sw4stm32_win_64bits-latest.exe as this is always the latest version available. You might need other if you have different operating system.
After downloading start setup. You are greeted with this window:
Click Next and proceed to next window.
Read license agreement and accept it. Then click on Next.
US, Canada, Mexico Time Zones. Atlantic Daylight Time (ADT). Eastern Daylight Time (EDT). Central Daylight Time (CDT). Mountain Daylight Time (MDT). Pacific Daylight Time (PDT). Alaska Daylight Time (AKDT). Hawaii Time. Arizona. Saskatoon. New York. Toronto. Mexico City. San Francisco. Chicago. Houston. Cdt time zone converter. Converting CDT to EST. This time zone converter lets you visually and very quickly convert CDT to EST and vice-versa. Simply mouse over the colored hour-tiles and glance at the hours selected by the column. CDT stands for Central Daylight Time. EST is known as Eastern Standard Time. EST is 1 hours ahead of CDT.
Here you select where the software will be installed. It is best to just leave it as it is and click Next.
A popup window tells you that new folder will be created. Just click Ok. If you have previous version installed, this window will let you know that you will be overwriting the previous version.
On this windows, setup will show you the summary of software to be installed. Just click Next.
On this window there are a few more things that can be set. You can have (or not) a shortcuts in the Start-Menu, you can select to have (or not) a link to System workbench on Desktop… It is best to just click Next.
Here on this window is last summary before changing your system. Click Next if everything looks OK.
If you see this window with the bars full you are on the right track and near the finish. Click Next.
Now you should see this window which will instal drivers for ST-LINK. Just click Next (it is starting to become boring 🙂 ).
There might be some steps before this window. I was reinstalling this for the tutorial, so I am not sure about steps in between when fresh installing, if needed contact me and let me know and I will delete and reinstall drivers to see the steps here. You must get to this window and there must be green check marks on left and Ready to use on the right. Click Finish to close window (I live in Slovenia so the button label is different from English).
System Workbench Ide
After clicking Finish on previous windows it will close and you will see this window. Just click Next.
Stm32 System Workbench C++
We are finally finished. Just click Done and you have a nice new FREE software for your favourite microcontroller. When you will first start SW4STM32 it will ask you for location of Workspae. I advise you to make a directory for STM32 programming, tell SW4STM32 location to this folder and check the box saying use this location as default workspoace. It will soon be time for Blinky.