cancel
Showing results for 
Search instead for 
Did you mean: 

UART RX ambiguities

AMerc.4
Senior

0693W000003PrD0QAK.pngHello,

i'm trying to use UART with STM32F767ZI Nucleo board. I'm using the following simple code (the essential parts):

/* USER CODE BEGIN PV */
uint8_t a[] = {0x81, 0x00, 0x00, 0x00, 0x00};
uint8_t b[] = {0x00, 0x00, 0x00, 0x00, 0x00};
uint8_t error[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
 
uint8_t uart_buf[5];
/* USER CODE END PV */  
 
while (1)
  {
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
	  HAL_UART_Transmit(&huart4, a, 5, 100);
	  HAL_UART_Transmit(&huart4, b, 5, 100);
 
	  if(HAL_UART_Receive(&huart4, uart_buf, 5, 500) == HAL_OK)
	  {
		  HAL_UART_Transmit(&huart3, uart_buf, 5, 100);
	  } else
	  {
		  HAL_UART_Transmit(&huart3, error, 5, 100);
	  }
  }

The communication is between STM32 and an USB-TTL converter at 9600 baud.

The program send some bytes to the USB-TTL converter, the latter sends {0x05, 0x04, 0x03, 0x02, 0x01} and {0x0A, 0x09, 0x08, 0x07, 0x06} in repetition. The STM32 trys to receive the signal, if this is ok it sends to the ST-LINK uart the received pack, otherwise it sends to ST-LINK uart {0xFF, 0xFF, 0xFF, 0xFF, 0xFF}.

The STM32 receive the first pack of bytes, but than it returns always {0xFF, 0xFF, 0xFF, 0xFF, 0xFF} (ERROR). If you see the image, on the left there are the bytes sended by STM32, on the right the bytes sended through ST-LINK debug.

I can see the correct byte only when i reset the MCU, after the first read it return always error.

I don't know what is the problem, and i hope you can fix it.

1 REPLY 1
TDK
Guru

HAL_UART_Transmit and HAL_UART_Receive are both blocking. The device can only receive data when it's inside of the HAL_UART_Receive routine. If your other device sends data outside of this time, your program won't work correctly.

You can either work on the timing between the two to ensure data is sent only when the STM32 is inside the HAL_UART_Receive function, or you can implement nonblocking receive routines.

Putting a logic analyzer on the lines would show you exactly what's happening.

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