cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L4+USART+Rx+HAL-------Distortion of received data

ControlFreak
Associate III

Hello Everyone,

I am using Nucleo-32 board in my project. I am programming USART to receive 10 character data and then transmit this 10 character data back.

Problem that I am observing is that if accidentally 11 character it is suppose to display error message after that every time I enter 10 character string, my response character shift right+1. Can someone

Please find below code:

HAL_Init();

 SystemClock_Config();

 GPIO_Toggle();

 HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, GPIO_PIN_SET);

 __HAL_RCC_USART1_CLK_ENABLE();

 UartHandle.Instance    = USART1;

 UartHandle.Init.BaudRate  = 4800;

 UartHandle.Init.WordLength = UART_WORDLENGTH_8B;

 UartHandle.Init.StopBits  = UART_STOPBITS_1;

 UartHandle.Init.Parity   = UART_PARITY_NONE;

 UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;

 //UartHandle.Init.OverSampling=UART_OVERSAMPLING_16;

 //UartHandle.Init.OneBitSampling=UART_ONE_BIT_SAMPLE_DISABLE;

 UartHandle.Init.Mode    = UART_MODE_TX_RX  ;

 if(HAL_UART_DeInit(&UartHandle) != HAL_OK)

 {

HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, GPIO_PIN_RESET);

 }

 if(HAL_UART_Init(&UartHandle) != HAL_OK)

 {

 HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, GPIO_PIN_RESET);

 }

 LTC_2873();

  HAL_GPIO_WritePin(GPIOB,GPIO_PIN_7,GPIO_PIN_SET); // Mode RS485

  HAL_GPIO_WritePin(GPIOC,GPIO_PIN_14,GPIO_PIN_RESET); // RS485 Terminator Enable

  HAL_GPIO_WritePin(GPIOB,GPIO_PIN_4,GPIO_PIN_SET); // RS485 Driver Enable

  HAL_UART_Transmit_IT( &UartHandle,&aTxBuffer[0],strlen((const char*)aTxBuffer));

  HAL_Delay(1000);

 while(1)

 {

 do

{

   HAL_GPIO_WritePin(GPIOB,GPIO_PIN_4,GPIO_PIN_RESET); // RS485 Receiver Enable

 HAL_Delay(20);

   while(HAL_UART_Receive_IT( &UartHandle,&aRx_Buffer[0],10)!=HAL_OK);

   HAL_Delay(20);

}while(rx_cplt!=1);

  HAL_Delay(50);

if(rx_cplt==1)

{

HAL_GPIO_WritePin(GPIOB,GPIO_PIN_4,GPIO_PIN_SET); // RS485 Driver Enable

HAL_Delay(20);

HAL_UART_Transmit_IT( &UartHandle,&aRx_Buffer[0],10);

HAL_Delay(20);

rx_cplt=0;

  }//end of if

 }//end of while(1)

}

This discussion is locked. Please start a new topic to ask your question.
11 REPLIES 11

Fixed lengths, and sequential rx and then tx, leave very little room for recovery or resync.

I'd approach this differently. Side step HAL and do a stateful byte level management in the IRQ Handler.

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

Can you Please elaborate?

This is what I am seeing as to what is happening

When I send data more than 10 character, HAL Rx_IT rewrites Rx_Buffer[0] with 11 character (understandable)

But at the same time it increment value of my HAL pRxBuffPtr and then when I write again it uses this incremented value.

My question is if I use &Rx_Buffer[0] why it doesnot pick up address of Rx_buffer[0]?

Below is code which gives me better result but I still haven't being able to fix the problem entirely

 do

{

   HAL_GPIO_WritePin(GPIOB,GPIO_PIN_4,GPIO_PIN_RESET); // RS485 Receiver Enable

 HAL_Delay(2);

 HAL_UART_Receive_IT(&UartHandle,dummy_pointer,10);

   HAL_Delay(100);

   //UartHandle.pRxBuffPtr= &aRx_Buffer[0];

   HAL_Delay(50);

}while(rx_cplt!=1);

 UartHandle.pRxBuffPtr=dummy_pointer;

if(rx_cplt==1)

{

  HAL_GPIO_WritePin(GPIOB,GPIO_PIN_4,GPIO_PIN_SET); // RS485 Driver Enable

HAL_Delay(20);

HAL_UART_Transmit_IT(&UartHandle, dummy_pointer,10);

HAL_Delay(100);

HAL_UART_Transmit_IT(&UartHandle,(uint8_t*)"\r\n",strlen((const char*)"\r\n"));

HAL_Delay(20);

rx_cplt=0;

}

I am sorry I am not inclined to used DMA, as I am not 100% sure that I will not see the same problem in DMA

Please help!!