STM32 Nucleo-64 boards from STMicroelectronics are very common evaluation boards which are often used by hobbists and professionists. Thanks to a practical board design and to the available tools, prototyping with a Nucleo-64 it’s a very stressless and straightforward experience.
In this article we are going to see how can we use the on-board serial port connection which is accessible through the mini USB connector. The USB is connected to an internal UART peripheral of the microcontroller. Learning to use the USB serial port, which is also called “Serial Debug Port”, can give us a powerful tool to improve our projects, giving the possibilty to print on output some debug information while our system is running or to even give some inputs to a running system, allowing for example to change some configurations.
In the following example, we will try to use the port in both directions, to see how can we effectively interact with our board from a PC.
Create and configure an STM32 CubeMX project
At first a CubeMX project can be created, which allows to define the proper hardware configuration. Let’s open the tool STM32 CubeMx and create a new project. Select the correct hardware (in my case it’s a NUCLEO-F446RE board) and click on “start project”. Once the project is created the following configurator view will be shown (Image 1).
A basic configuration which allows the microcontroller to work is already provided, so we can skip directly to the serial port configuration. Click on Connectivity on the left side menu and then select the respective USART used for the serial port, which in the current case is corresponding to the USART2 (Image 2).
From this point multiple configurations are possible, like for example the Baud Rate or the Hardware Flow Control. Once the correct configuration is provided we can click on Generate Code, to generate the project configuration code. STM32 CubeMx will not only create all the necessary code to use the microcontroller and use the serial port, but it will create also a STM32 CubeIDE project which we can now open. Once the IDE is open, we can look at the projects files on left column named Project Explorer and look for a file named main.c (Image 3): this is the file where we will implement our serial port test code.
Coding our Serial Port test
The generated code already provide all the necessary functionalities to operate with the serial port and it’s just enough to write the code which use this functionalities. The main actors of our example are the following two functions:
- HAL_UART_Transmit
- HAL_UART_Receive
As the name already suggest, the first one must be used to send data out to our serial port, while the second must be used to received data in input. The following code snippet, shows how these two functions can be used to start using our serial port.
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/*Receive a character */
if (HAL_UART_Receive(&huart2, &selection, 1, 0u) == HAL_OK)
{
switch (selection)
{
case '1':
HAL_UART_Transmit(&huart2, "Option 1 has been selected\r\n", 28, 1000u);
break;
case '2':
HAL_UART_Transmit(&huart2, "Option 2 has been selected\r\n", 28, 1000u);
break;
case '3':
HAL_UART_Transmit(&huart2, "Option 3 has been selected\r\n", 28, 1000u);
break;
default:
HAL_UART_Transmit(&huart2, "Selected option is not available\r\n", 34, 1000u);
break;
}
}
/* USER CODE BEGIN 3 */
}
The whole code was implemented in the file main.c, inside the while loop, which has been generated. With the first HAL_UART_Receive call we will read one character from the serial port and we will store it into a variable. In the example the variable is named selection and must be declared. A possible position would be just at the start of our main function, as showed in the following example:
...
int main(void)
{
uint8_t selection;
/* USER CODE BEGIN 1 */
...
Once a character is read, is it checked inside a switch statement, providing 3 different possibilities (case) and a default value. In case the number 1, 2 or 3 have been received, then the string “Option x has been selected” will be printed in output, while for every other character, the string “Selected option is not available” will be printed.
Testing our STM32 project
Now we can just connect the board to a PC through the USB port and use a serial port terminal to test our code. For this example I will use Putty, which can be downloaded from here. Once opened, the tool will show the following configuration window.
Once the correct parameter are selected, like the right COM port or the right speed, we can click Open to start the serial port session. Once the serial port terminal is showed, we can start typing some characters and then test our software. Let’s start with 1, 2 and 3 and then type some other random numbers or characters which are not included in the switch statement selection. The following screenshot from Putty shows how the output should look like:
Conclusion
With this example it was provided some basic instructions to activate the STM32 Nucleo-64 serial port to use it for sending and receiving some text data. This can be used as a base for debugging our projects when a debugger is not connected or to allow some configurations to be introduced in our system. Of course, this is a very simple example which has some limitations, like for example that the used functions are blocking and they basically stall the system execution until the data has been transmitted or received completely. In future posts I will show how the serial port can better handled in order to have a debug instrument which can be easily integrated.
in the description you say using mini USB, then in the example you gave is USART2, they are two different thing if I am not mistaken
Hi Geo,
Actually USB and USART are two different protocols, but this is a tutorial which explain how to bridge the microcontroller UART to the external USB mini.
Of course this is different than using the USB as a real native USB.