2020-12-26 11:46 PM
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
2020-12-27 01:18 AM
Doing HAL callbacks for each byte is not how I'd approach this.
2020-12-27 03:44 AM
HAL USART API is incapable of receiving uninterrupted stream of bytes. It's flawed by design, because of... politics.
2020-12-27 04:39 AM
thanks for your reply
so... we should use LL or other libraries ???
2020-12-27 07:46 AM
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.
2020-12-27 07:59 AM
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.
2020-12-27 08:03 AM
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
2020-12-28 05:18 AM
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