How to manage two UART (USART2 and USART1) under Interrupt

If you like it, share it

There are different possibility to use the UART under Interrupt, for instance use PRINTF but this mode use a loot of code or, more efficiency is to use the Call Back.

In this example we use the Call Back modality.

This project was tested on NUCLEO-F030R8, was developed using the Cube-MX and the project is for ATOLLIC.
Thanks to Cube-MX is easy move this project on other STM32 mcu but also generate the same project for other IDE (KEIL, IAR, etc)

For use this project connect the NUCLEO-F030R8 to PC and use a terminal emulator, we suggest TeraTerm.
This because the USART2 is transferred to the PC like a USB virtual com port.

Here there is the Cube-MX project.

In this project we use only the USART2 because it is connected to the PC via the ST-LINK-v2 present on the NUCLEO-F030R8.
The USART1 is already configured for work under Interrupt but is not used.
This project send a buffer (10 characters) via USART2 and receive a buffer (via USART2) from the PC (10 characters).

After the Cube-MX has generated the project, here there is the Cube-MX project, is necessary insert some lines of code for configura the call back and the USART. See below.

NOTE:

  • Use FIREFOX or CHROME for a clear view of the images present in this web site
  • For enlarge the image press: CTRL +
    For reduce the image press: CTRL –

N.1 – After the:

/* USER CODE BEGIN PV */
/* Private variables ———————————————————*/

insert the lines below:

uint8_t buffer_tx[10]={69, 77, 67, 85, 46, 69, 85, 13, 10}; 
uint8_t buffer_rx[10];

/* USER CODE END PV */


N.2 – After the:

/* USER CODE BEGIN 0 */

insert the lines below:

void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
}

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
 if(huart->Instance==USART1)
 {
 while(1);
 }

if(huart->Instance==USART2)
 {
 while(1);
 }
}

/* USER CODE END 0 */


N.3 – After the:

/* USER CODE BEGIN 2 */

insert the lines below:

HAL_UART_Receive_IT(&huart2, buffer_rx, 10);
HAL_UART_Transmit_IT(&huart2, buffer_tx, 10);

/* USER CODE END 2 */


How to test this example

Compile and strat Debug.

Insert two breakpoint in the correspondence of the:
while(1);
see point 1 e 2 below.

Next insert in the watch Expression tab the variable: buffer_rx
see below.

Run your code, see the red arrow below.

Open TeraTerm and configure it as shown below.
Choose a right com port.

Stop your program and restart it, on TeraTerm you must see something like below.

Now type 10 characters in the TeraTerm, for example:
1 2 3 4 5 6 7 8 9 0

The debug mast stop at line n.86 and in the variable buffer_rx must contain the characters that you typed in TeraTerm window.
See below.

That is all, now try you to insert a similar action using USART1.

 


How to get the SW for this project

Please send us an email and ask us the password for: USARTitNU030ATO
Please specify also your country and your city, this are only for our personal statistics.

Get the SW clicking here, but remember to ask us the password for open it.

Note: the project contain also the file for CUBE-MX