cancel
Showing results for 
Search instead for 
Did you mean: 

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

Mmm.1
Associate II

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

7 REPLIES 7

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

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Piranha
Chief II

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

thanks for your reply

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

TDK
Guru

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

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.

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

thanks

another question is :

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

with this lib we can manage multi uart rx in circular dma mode ? for example above two or three uart ? and without any lost in input data ?

thanks for your help