cancel
Showing results for 
Search instead for 
Did you mean: 

USART receive thorugh interrupt by using LL in stm32f030

Abhishek Gupta
Associate III

I have to receive a string in stm32f030 from USART using LL library through the interrupt.

11 REPLIES 11
T J
Lead

do you have demo board ?

search for serial port example for that board.

did you use the Cube ?

Abhishek Gupta
Associate III

I have a evaluation Kit but i need to know how to receive a string from USART since i am able to receive byte data at a time.

T J
Lead

can you show me your code where you receive a byte ?

then I will be able to show you how to save a string.

Abhishek Gupta
Associate III

void receive_usart1()

{

 if(LL_USART_IsActiveFlag_RXNE(USART1) && LL_USART_IsEnabledIT_RXNE(USART1))

  {

rx_data_2=LL_USART_ReceiveData8(USART1);

}

// In interrupt

void USART2_IRQHandler(void)

{

 /* USER CODE BEGIN USART1_IRQn 0 */

receive_usart1();

 /* USER CODE END USART1_IRQn 0 */

 HAL_UART_IRQHandler(&huart2);

 /* USER CODE BEGIN USART1_IRQn 1 */

 /* USER CODE END USART1_IRQn 1 */

}

T J
Lead

just declare a buffer globally U1RxBuffer[256]; and a pointer U1RxBufferPtrIN

simply

void receive_usart1()
 
{
 
 if(LL_USART_IsActiveFlag_RXNE(USART1) && LL_USART_IsEnabledIT_RXNE(USART1))
 
  {
 
Usart1RxDMABuffer[U1RxBufferPtrIN++] = LL_USART_ReceiveData8(USART1);;
    if (U1RxBufferPtrIN >= U1RxBufSize) U1RxBufferPtrIN = 0;
 
}

that will get it into a buffer..

or else use DMAs

#define U1RxBufSize 256
#define U1TxBufSize 2048
     
char Usart1TxDMABuffer[U1TxBufSize];
char Usart1RxDMABuffer[U1RxBufSize];
int16_t U1RxBufferPtrIN, U1RxBufferPtrOUT, U1TxBufferPtrIN, U1TxBufferPtrOUT;
char   TxDMA1BufHasData, DMA1BufAlmostFull;
 

in the cube, if you enable the DMA in circular mode, it will fill the Usart1RxDMABuffer directly.

I run this once at startup:

void initUart1RxDMABuffer(void) {  // stream directly into RxBuffer
    if (HAL_UART_Receive_DMA(&huart2, (uint8_t *)Usart1RxDMABuffer, U1RxBufSize) != HAL_OK)
    {
        // Transfer error in reception process 
        //_Error_Handler(__FILE__, __LINE__);
        printf("initUart1RxDMABuffer Failed\n");
    }
    else
        printf("initUart1RxDMABuffer OK!\n");
}

that will get it into the same buffer, but under DMA control,

that way, you wont lose any data if you are tardy...

Abhishek Gupta
Associate III

I am a bit confused since what are you telling is relating with the DMA control and i only doing in transmit and reception mode.

I need to transmit a string and what the received string is i need to cut and modify it and than again transmit it.

The received string is of length approximately 100.

And please tell me the flow to write "void initUart1RxDMABuffer(void) " this function if necessary.

T J
Lead

initialise the RxDma stuff before while(1)

T J
Lead

did you initialise the DMA circular buffer in the cube ?

void receive_usart1()

{

 if(LL_USART_IsActiveFlag_RXNE(USART1) && LL_USART_IsEnabledIT_RXNE(USART1))

  {

uint8_t rxdata =LL_USART_ReceiveData8(USART1);

...

}

So what's to stop you putting the data into a buffer, or processing it with a state machine?

If the data has some type of form. ie GPS NMEA sentences, you can watch for a start character to resynchronize the buffer, and a <CR><LF> pair to terminate the buffer and hand the string off to a processing routine.

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