Skip to main content
NSirm.1
Associate II
December 4, 2020
Solved

Hello All I am implementing a simple application using STM32F205 and UART. I have enabled UART as global interrupt. I want the program to detect \r or \n in a string that is received continuously.

  • December 4, 2020
  • 2 replies
  • 1078 views

I wish to print "String recieved" if /r or /n is detecetd

otherwise i wish to print "Error" .The problem is after detecting the first string i receive an output "String Received" After which i also receive error message. I have doubt if i have implemented the __HAL_UART_ENABLE_IT(&huart3, UART_IT_RXNE) and enabled the HAL_UART_IRQ handler correctly,

I have used __HAL_UART_ENABLE_IT(&huart3, UART_IT_RXNE) function after initialising all the configured peripherals in CubeMx

<

/* USER CODE BEGIN PV */

uint8_t g_rx_receive_complete=0;

  uint8_t g_rx_received_bytes=0;

  char g_rx_buffer [10]={0};

  char rx_buffer [10]={0};

/* USER CODE END PV */

/* USER CODE BEGIN PFP */

void check (void)

{

        

 g_rx_buffer[g_rx_received_bytes]=(uint8_t)USART3->DR;

  if((g_rx_buffer[g_rx_received_bytes]=='\n')||(g_rx_buffer[g_rx_received_bytes]=='\r'))

   {

     g_rx_buffer[g_rx_received_bytes]=='\0';

     g_rx_receive_complete= 1;

   }

  else 

   {

    if(g_rx_received_bytes<10)

    g_rx_received_bytes++; 

   }

/* USER CODE END PFP */

 while (1)

 {

  

 if (g_rx_receive_complete)

   {

   memcpy(rx_buffer, g_rx_buffer,sizeof(rx_buffer));

   g_rx_receive_complete=0;

   runCommand(rx_buffer); 

/* USER CODE END WHILE */  

   }

>

In STM32f2xx_it.c I am calling the check function

<

void USART3_IRQHandler(void)

{

 /* USER CODE BEGIN USART3_IRQn 0 */

check();

 /* USER CODE END USART3_IRQn 0 */

 HAL_UART_IRQHandler(&huart3);

 /* USER CODE BEGIN USART3_IRQn 1 */

 /* USER CODE END USART3_IRQn 1 */

}

>

This topic has been closed for replies.
Best answer by Vangelis Fortounas

hello

> "I wish to print "String recieved" if /r or /n is detecetd

otherwise i wish to print "Error" ."

What you mean with "otherwise"? Timeout generaly? timemout with some bytes already received? after a predetermined number of bytes?

What if receive \r and this is not the last byte of the packet?

These questions and maybe more must answered before you write the code

The code you published have issues as TDK mentioned.

Additionaly, code reads DR register and the HAL_UART_IRQHandler reads it again later.

The code that would started receiving again after the first time, is missing.

The macro __HAL_UART_ENABLE_IT enables interrupts only, no need to call it, if you call HAL_UART_Receive_IT(...).

To start RX, there are 3 functions HAL_UART_Receive, HAL_UART_Receive_IT, HAL_UART_Receive_DMA. Call one of them every time you want to receive.

There are working UART examples in the MXCube repository, inside your User folder. IMO are a good starting point to study Serial comm.

2 replies

TDK
Super User
December 4, 2020

There's a lot of stuff here that doesn't make a lot of sense.

You have two variables defined which differ by a single character, and you seem to use them interchangeably:

> uint8_t g_rx_receive_complete;

> uint8_t g_rx_receive_completed=0;

You make a comparison statement instead of an assignment.

> g_rx_buffer[g_rx_received_bytes]=='\0';

You assign a variable one value, then immediately assign it something else.

> g_rx_receive_complete= 1;

> g_rx_receive_complete= 0;

You look up the value on an uninitialized variable

> if (g_rx_receive_complete)

then appear to turn the flag off, but assign a value to another variable instead.

> g_rx_receive_completed=0;

A lot of these issues should have given warnings in the IDE. A lot of them should have been caught when you were debugging the program.

I still think you should follow the advice in your last post on this exact topic.

https://community.st.com/s/question/0D53W00000PqGqDSAV/hello-all-i-am-implementing-a-simple-application-that-reads-data-using-usart-i-want-to-read-10-bytes-of-data-and-if-carriage-return-or-new-line-is-detected-it-must-print-string-received-otherwise-it-must-print-error-i-am-using-stm32f205

"If you feel a post has answered your question, please click ""Accept as Solution""."
NSirm.1
NSirm.1Author
Associate II
December 5, 2020

Hi since I dint have CubeMx while posting the question i made few mistakes i typed the program in editor to post the question. I am facing difficulty after eanbling the HAL_UART_IRQHandler(&huart3) do i have to call it in main.c or do i need to call  HAL_UART_Receive in the main function ?

Vangelis Fortounas
Associate II
December 5, 2020

hello

> "I wish to print "String recieved" if /r or /n is detecetd

otherwise i wish to print "Error" ."

What you mean with "otherwise"? Timeout generaly? timemout with some bytes already received? after a predetermined number of bytes?

What if receive \r and this is not the last byte of the packet?

These questions and maybe more must answered before you write the code

The code you published have issues as TDK mentioned.

Additionaly, code reads DR register and the HAL_UART_IRQHandler reads it again later.

The code that would started receiving again after the first time, is missing.

The macro __HAL_UART_ENABLE_IT enables interrupts only, no need to call it, if you call HAL_UART_Receive_IT(...).

To start RX, there are 3 functions HAL_UART_Receive, HAL_UART_Receive_IT, HAL_UART_Receive_DMA. Call one of them every time you want to receive.

There are working UART examples in the MXCube repository, inside your User folder. IMO are a good starting point to study Serial comm.