Skip to main content
Mani1
Associate III
August 24, 2021
Solved

USART RX and TX issue

I am using STM32F746G discovery board.

~~~

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {

   HAL_UART_Receive_IT (&huart6, Rx_buff, USART_RX_BUFFER_SIZE);

   HAL_Delay(500);

   HAL_UART_Transmit(&huart6, &Rx_buff, USART_RX_BUFFER_SIZE, 1000);

}

~~~

This is the way i am using USART6, TX and RX.

here my doubt is, i fixed my USART_RX_BUFFER_SIZE =10.

I send 10 bytes data via serial terminal(hercules), in this case my usart transmit also works with same 10 bytes data.

But i send 9 bytes data means, the uart transmit is not transmit the 9 bytes data.

my requirement is,

how much amount of bytes received via UART_Receive(); , that amount of bytes, send via UART_Transmit();

Thanks

Manikandan D

This topic has been closed for replies.
Imen GH
Imen GHBest answer
ST Employee
August 24, 2021

10 replies

Imen GH
Imen GHBest answer
ST Employee
August 24, 2021
Tesla DeLorean
Guru
August 24, 2021

You'd need to count the bytes. Perhaps there is an index in the uart instance structure, or you need to accumulate your own buffer one byte at a time.

You should not delay or call blocking functions in the call-back.

If you ask the function to receive 10 bytes before entering the call-back, 9 will not be sufficient.

Watch the definition of Rx_buff

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
Mani1
Mani1Author
Associate III
August 25, 2021

thanks @Community member​  this is very helpful

TDK
August 24, 2021

Use HAL_UARTEx_ReceiveToIdle or HAL_UARTEx_ReceiveToIdle_IT.

/**
 * @brief Receive an amount of data in blocking mode till either the expected number of data
 * is received or an IDLE event occurs.
 * @note HAL_OK is returned if reception is completed (expected number of data has been received)
 * or if reception is stopped after IDLE event (less than the expected number of data has been received)
 * In this case, RxLen output parameter indicates number of data available in reception buffer.
 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
 * the received data is handled as a set of uint16_t. In this case, Size must indicate the number
 * of uint16_t available through pData.
 * @note When FIFO mode is enabled, the RXFNE flag is set as long as the RXFIFO
 * is not empty. Read operations from the RDR register are performed when
 * RXFNE flag is set. From hardware perspective, RXFNE flag and
 * RXNE are mapped on the same bit-field.
 * @param huart UART handle.
 * @param pData Pointer to data buffer (uint8_t or uint16_t data elements).
 * @param Size Amount of data elements (uint8_t or uint16_t) to be received.
 * @param RxLen Number of data elements finally received
 * (could be lower than Size, in case reception ends on IDLE event)
 * @param Timeout Timeout duration expressed in ms (covers the whole reception sequence).
 * @retval HAL status
 */
HAL_StatusTypeDef HAL_UARTEx_ReceiveToIdle(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint16_t *RxLen,
 uint32_t Timeout)
{

"If you feel a post has answered your question, please click ""Accept as Solution""."
Mani1
Mani1Author
Associate III
August 25, 2021

thanks @TDK​ 

Mani1
Mani1Author
Associate III
August 25, 2021

Hello,

I am facing another issue in UART_Receive_DMA();

USART_RX_BUFFER_SIZE = 10

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {

  Rx_ok_flag=1;

  HAL_UART_Receive_DMA (&huart6, Rx_buff, USART_RX_BUFFER_SIZE);

}

I send 10 bytes of String "HIHELLOLCS" via Serial terminal(Hercules).

After sending this string , i read my Rx_buff. But my Rx_buff does not have 10 bytes of data. It's have only Three bytes.

After sending the data i am getting like this, i don't know what's happening.

Rx_buff [0] = 'H' Rx_buff [1] ='\000' Rx_buff [2] ='\000' Rx_buff [3] ='\000' Rx_buff [4] ='I' Rx_buff [5] ='\000' Rx_buff [6] ='\000' Rx_buff [7] ='\000' Rx_buff [8] ='H' Rx_buff [9] ='\000'

Thanks

Manikandan D

Tesla DeLorean
Guru
August 25, 2021

It might be helpful to provide a more complete perspective, these code snippets lack a lot in context.

For example you'd want this flag as a volatile variable, and you'd want to copy the data to a staging buffer because the DMA can overwrite the content of the active buffer at any time.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
TDK
August 25, 2021

Are your DMA memory size and peripheral size both 1 byte?

"If you feel a post has answered your question, please click ""Accept as Solution""."
Mani1
Mani1Author
Associate III
August 26, 2021

Hai,

how to know the DMA memory size.

This issue not only for DMA method. I tried with Interrupt method also. But i am getting same issue.

Thanks

Manikandan D

Mani1
Mani1Author
Associate III
August 26, 2021

Now my doubt is clear. Inside ISR function they are typecasting the Rx_buff.

like,

HAL_DMA_Start_IT(huart->hdmarx, (uint32_t)&huart->Instance->RDR, (uint32_t)huart->pRxBuffPtr, Size)

Because Function declaration of HAL_DMA_Start_IT is,

HAL_StatusTypeDef HAL_DMA_Start_IT(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength);

Thanks for your replies :smiling_face_with_smiling_eyes: