Skip to main content
Mmm.1
Associate
December 27, 2020
Question

How to read Data with UART circular DMA and byte-by-byte Data and use two Buffer ? with stm32f407 ? without FreeRTOS and realtime !!

  • December 27, 2020
  • 3 replies
  • 9685 views

Hello Friends

i'm new in STM32

i wanna know how to Read Data Byte-by-Byte from uart Rx Circular Dma and save this data in a buffer ?

and my challenge is i do not know my input data length and type of that (hex, bin, ascii ,...)

theres any sample code ?

i think my code is wrong

uint8_t rxBuffer[10];

... in main :

{

...

HAL_UART_Receive_DMA(&huart1, (int8_t*) rxBuffer, 1);

...

}

..... and i use rx callback :

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)

{

   //

   switch ((uint32_t) huart->Instance)

   {

      case USART1_BASE:

         //

         Receive_Data(rxBuffer);

      break;

   }

}

and my receive data function :

uint8_t Receive_Data(uint8_t* Data)

{

while (Data[0] != '\n')

      {

         //

         buffer[i] = Data[0];

         i++;

         if(i == n)

         {

            i = 0;

            //return buffer;

         }

      }

}

can anyone help me ?

thanks

This topic has been closed for replies.

3 replies

Tesla DeLorean
Guru
December 27, 2020

Doing HAL callbacks for each byte is not how I'd approach this.​

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
Piranha
Principal III
December 27, 2020

HAL USART API is incapable of receiving uninterrupted stream of bytes. It's flawed by design, because of... politics.

https://github.com/MaJerle/stm32-usart-uart-dma-rx-tx

Mmm.1
Mmm.1Author
Associate
December 27, 2020

thanks for your reply

so... we should use LL or other libraries ???

Piranha
Principal III
December 27, 2020

LL is not a real library as it doesn't provide any added value and abstraction. The only significant thing it does - slows the development by doubling the amount of names to learn. Writing your own driver library is the easiest and fastest way to go, and can give the best quality code. The link I gave provides all the required information to start.

When you have USART working, implement data parsing code. For data streams often it's best to do parsing completely independently from receiving.

TDK
Super User
December 27, 2020

What you're doing isn't particularly efficient, but it should work except for the Receive_Data function which doesn't seem to do what you want. You'll need to implement a circular buffer to receive and process data.

Again, not the most efficient, but it should work. See the link provided by Piranha for buffer information.

"If you feel a post has answered your question, please click ""Accept as Solution""."
Tesla DeLorean
Guru
December 27, 2020

One byte requests into a buffer? You'd do better just using interrupts.

For DMA Rx I'd discard the HAL/LL completely and just use DMA as a HW FIFO filling a ring buffer large enough to be managed in a periodic interrupt.

For new designs I'd seriously recommend looking at newer STM32 families where ST discovered how to combine UART+FIFO IP together

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..