cancel
Showing results for 
Search instead for 
Did you mean: 

How can i handle two UART ports in one STM32?

Sungjae Kim
Associate

Hello,

I have questions about handling multiple UART ports.

Connections are;

PC <--USB VCP--> STM32(1) <--UART1--> STM32(2) <--UART2--> Target device

Two STM32 are both L412K series,

And I wanna make a application which forwards data to the target device which sends from PC.

I found STM32(1) works properly.

When the data is coming through USB VCP, it repeats data to STM32(2).

However, STM32(2) does not send character to UART2.

I think it receives the data through UART1, but not forwarding it to UART2.

Here is the code which I wrote for STM32(2).​

uint8_t data;
 
int main(void)
{
  HAL_Init();
 
  SystemClock_Config();
 
  MX_GPIO_Init();
  HAL_Delay(100);
  MX_USART1_UART_Init();
  HAL_Delay(500);
  MX_USART2_UART_Init();
  HAL_Delay(500);
  HAL_UART_Receive_IT(&huart1, &data,1);
 
  while (1)
  {
  }
 
}
 
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart){
	if(huart->Instance == huart1.Instance){
		HAL_UART_Transmit(&huart2, &data, 1, 10);
		}
	HAL_UART_Receive_IT(&huart1, &data, 1);
	}

(all the unnecessary codes are removed)

Is this cannot be implemented in this simple way?

Please let me know.

Thank you.

4 REPLIES 4
TDK
Guru

> HAL_UART_Receive_IT(&huart2, &data, 1, 10);

Since you want to transmit data, shouldn't this be HAL_UART_Transmit_IT?

If you feel a post has answered your question, please click "Accept as Solution".

Thank you for reply. It was typo.

At the program, I applied "HAL_UART_Transmit(&huart2, &data, 1, 10);" at there and it is not working.

>I think it receives the data through UART1, but not forwarding it to UART2.

Try to make it certainty. Observe the signals using oscilloscope/logic analyzer.

Rx overrun due to Cube/HAL inefficiency? Use compiler optimization and insert delays between transmitted characters in the first STM32.

JW

MAziz.1
Associate II

Hello Kim ,

Did you find the answer? As I am also using stm32f446ze and want to use multiple USARTS, (USART3 which is the default is working while USART2 is not working for me). I am observing the serial data on real term.

Thanks.