cancel
Showing results for 
Search instead for 
Did you mean: 

USART RX and TX issue

Mani1
Associate II

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

1 ACCEPTED SOLUTION

Accepted Solutions
Imen GH
ST Employee
10 REPLIES 10
Imen GH
ST Employee

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
Up vote any posts that you find helpful, it shows what's working..
TDK
Guru

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".

thanks @Community member​  this is very helpful

Mani1
Associate II

thanks @TDK​ 

Mani1
Associate II

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

TDK
Guru

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".

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
Up vote any posts that you find helpful, it shows what's working..
Mani1
Associate II

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