-
Notifications
You must be signed in to change notification settings - Fork 0
How to make printf work
This tutorial has only been tested on linux systems |
At some point, it would have been good for all of us to be able to use the printf
function as if the code were running on our computer. The objective of this article is to explain how we can make this function work in an STM32CubeIDE console.
To make printf
work in our computer is more complex than it may seem because what we want is that printf
writes in a terminal that is in a different computer to the one that is executing the code. To achieve this we have to make that printf
can communicate with our computer, and to make it comfortable this must be through the own cable with which we programmed the microcontroller. This can be achieved using UART.
To make the use of printf
more comfortable and avoid having to configure it for each new project, a small extension has been added to the UART service to allow the use of printf with only two lines of code.
The following tutorial is divided in two parts.
- The first part explains the code needed for the
printf
function to send messages via UART. - The second part explains how to prepare an STM32CubeIDE console to receive and display the messages.
In this part we will see how to configure the UART and the code that we must add to make the messages be sent by UART.
Para hacer funcionar los printf
tan solo deberemos llamar a la funcion set_up_printf
que se encargara de hacer todas las configuraciones necesarias.
UART::set_up_printf(UART::uart3);
UART::start();
printf("123456789%s", endl);
fprintf(stderr, "Error xD!%s", endl);
Read the following warning |
The UART
used by default in nucleo porjects is uart3
, this is the one that is connected to the programmer cable, which makes it very easy to use. However this has a big problem and that is that the boards are not going to use this UART
, therefore you have to use uart1
or uart2
. If we tried to use the uart3
on a board we would cause a short circuit, that's why this UART
is not available by default in the runes, in case you want to use it for a nucleo it must be configured by hand. For this you just have to go to runes and copy and paste the code for the rest of the UARTs
and change the pins to those of the UART
in question.
You have to be very careful not to upload code that uses uart3
on a board, so it is recommended that you never use it. In other words, even when using a nucleo it is recommended to connect using a UART -> USB
cable.
To prepare a console capable of receiving data through UART is as simple as following the steps below:
-
The first thing we have to do is to connect the microcontroller to our computer.
-
Then we open the STM32CubeIDE.
-
We select the project in which we want to use
printf
. -
Now we must click on Open Console and select 3 Command Shell Console as you can see in the following figure.
- Next we must select the options shown in the following figure and click on New.
- Fill in all the data as shown in the following figure. The name is recommended to be as descriptive as possible as shown. The serial port that you must select will probably be called the same as the one in the figure, it is also possible that it is called
/dev/ttyUSBx
or/dev/ttyACMx
where the x's must be replaced by the corresponding number. To get the serial port you must have the microcontroller connected. One way to see which one is yours in your case is to use the following command in a linux terminalls /dev/tty*
with the microcontroller connected and not connected, in this way observing which one is the one that varies you will know which one is yours. The baud rate and the rest of the options must be exactly the same as the one used by the UART of the microcontroller.
- Once everything finished we give to finish and accept. Next the console will be created and we will be able to select it as it is seen in the following image, if we want to anchor this console so that always this visible we will have to click in the icon that there is to the left of the one to select the console.
- Finally if everything went well you should see the console with the message CONNECTED.
As you may have seen it is relatively simple to set everything up and the reward is great. The printf
opens up a world of testing possibilities unimaginable without it. It is a very reliable way to receive feedback on what is happening.
If you have followed all the steps you can now write your first program that makes use of this function. The simplest possible one is to send a Hola Mundo!
printf("Hola Mundo!")
When this program is executed you will be able to see in your console (make sure you have selected the one we have just created) the message.