cancel
Showing results for 
Search instead for 
Did you mean: 

UART Reception Until Newline Character on STM32F767 with TeraTerm Using CMSIS v2

Ajaykumar_V
Associate

Hello ST Community,

I’m working on a project with the Nucleo F767ZI development board, and I’m using CMSIS v2 for the RTOS. My goal is to implement UART communication where the system continuously receives characters until a newline character ("\n") is detected. Once "\n" is received, I want to process the received string and transmit it back over UART.

What I've Done:

UART Configuration:

  1. Baud Rate: 115200
  2. Data Bits: 8
  3. Parity: None
  4. Stop Bits: 1
  5. Flow Control: None

Task Setup:

  • I’ve created a uartTask thread that initiates UART reception using HAL_UART_Receive_IT, receiving one character at a time:

 

void uartTask(void *argument) {
    while (1) {
        if (huart3.gState == HAL_UART_STATE_READY) {
            HAL_UART_Receive_IT(&huart3, &rxBuffer[rxIndex], 1);
        }
        osDelay(100);
    }
}

 

Callback Function:

  • In HAL_UART_RxCpltCallback, I check for the newline character and, upon detection, transmit the entire buffer back and reset the index:

 

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
    if (huart->Instance == USART3) {
        if (rxBuffer[rxIndex] == '\n') {
            HAL_UART_Transmit(huart, rxBuffer, rxIndex + 1, HAL_MAX_DELAY);
            rxIndex = 0;
        } else {
            rxIndex++;
        }

        HAL_UART_Receive_IT(huart, &rxBuffer[rxIndex], 1);
    }
}

 

Issue:

I’m using TeraTerm to test the UART communication. I configured TeraTerm with the following settings:

  • Correct COM port for the Nucleo F767ZI
  • Baud Rate: 115200
  • Data Bits: 8
  • Parity: None
  • Stop Bits: 1
  • Flow Control: None

I’m typing characters directly into TeraTerm and pressing Enter to send the newline character (\r\n). However, the callback function doesn’t always seem to trigger as expected. The characters are not being received continuously until "\n", and sometimes the callback isn’t triggered at all.

Here’s what I’ve already checked:

  • UART Interrupts: They are enabled in the NVIC for USART3.
  • UART State: I ensure the UART state is ready before initiating reception.
  • Echo Test: I tried a simple echo test, and it’s consistent.

Has anyone else experienced similar issues or have suggestions on what might be going wrong? Any help or insights would be greatly appreciated.
Thanks,
Ajaykumar V

2 REPLIES 2
TDK
Guru

HAL_UART_Transmit will block until it's complete, which means you can lose characters that are being received during this time. It would be better to call this asynchronously, which means you'd need another buffer.

Code seems fine, excepting the one caveat above.

When it doesn't work, what is the software state of huart3? Consider the possibility that TeraTerm is buffering before it sends out data. Monitor the actual lines if you can. What characters are you sending and what characters does it receive instead?

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

Can you elaborate more on what "sometimes the callback isn’t triggered at all" means? 

Are you saying that when you type some characters and then hit return, you get no interrupt at all on any one character?

 

Though not recommended to transmit from within an interrupt in blocking mode, simply typing and hitting return would not cause any issues with your current code. Only if you're receiving stream of packets back to back, then you'll run into issues.

 

 

 

Tips and Tricks with TimerCallback https://www.youtube.com/@eebykarl
If you find my solution useful, please click the Accept as Solution so others see the solution.