cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 G0 LIN USART automatic baud rate detection. In UART-LIN mode, automatic baud rate detection does not work.

ADalex
Associate

Hello!

In my project, the task is Automatic baud rate detection in LIN UART mode. To test this possibility, I use the NUCLEO G070 board and Lin transceiver TJA1020

I wrote a small code that allows you to receive and transmit data in UART

With a conventional UART, everything turned out to be simple, the data transfer rate is determined automatically. Since I plan to use the LIN UART, the baud rate detection mode 0x55 character frame was selected

static void MX_USART2_UART_Init(void)
{
  huart2.Instance = USART2;
  huart2.Init.BaudRate = 115200;
  huart2.Init.WordLength = UART_WORDLENGTH_8B;
  huart2.Init.StopBits = UART_STOPBITS_1;
  huart2.Init.Parity = UART_PARITY_NONE;
  huart2.Init.Mode = UART_MODE_TX_RX;
  huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  huart2.Init.ClockPrescaler = UART_PRESCALER_DIV1;
  huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_AUTOBAUDRATE_INIT;
  huart2.AdvancedInit.AutoBaudRateEnable = UART_ADVFEATURE_AUTOBAUDRATE_ENABLE;
  huart2.AdvancedInit.AutoBaudRateMode = UART_ADVFEATURE_AUTOBAUDRATE_ON0X55FRAME;
  if (HAL_UART_Init(&huart2) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_UARTEx_SetTxFifoThreshold(&huart2, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_UARTEx_SetRxFifoThreshold(&huart2, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_UARTEx_DisableFifoMode(&huart2) != HAL_OK)
  {
    Error_Handler();
  }
 }

void USART2_IRQHandler(void)
{
  HAL_UART_IRQHandler(&huart2);
  HAL_UARTEx_ReceiveToIdle_IT(&huart2, send_buff1, 16);
}
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size){
	if (huart == &huart2){
		if(send_buff1[0]!=0x55){
		   SET_BIT(huart2.Instance->RQR,  USART_RQR_ABRRQ);
		}
		HAL_UART_Transmit_IT(&huart2, send_buff1, Size);
	  }
}

 From the terminal to the microcontroller in UART, I send the character set U123456789, and the microcontroller sends the same character set back to the terminal, and this happens at different baud rates. Everything works great!!!

  

0693W00000WJiVdQAL.jpg For LIN - UART I changed the code

static void MX_USART2_UART_Init(void)
{
...........................
 huart2.Init.BaudRate = 19200;
...............................
huart2.AdvancedInit.AutoBaudRateMode = UART_ADVFEATURE_AUTOBAUDRATE_ON0X55FRAME;
....................................................
 if (HAL_LIN_Init(&huart2, UART_LINBREAKDETECTLENGTH_11B) != HAL_OK)
  {
    Error_Handler();
  }
}
__HAL_UART_ENABLE_IT(&huart2, UART_IT_LBD);
void USART2_IRQHandler(void)
{
  HAL_UART_IRQHandler(&huart2);
     if(__HAL_UART_GET_FLAG(&huart1,UART_FLAG_LBDF) != RESET){
       SET_BIT(huart1.Instance->ICR,  USART_ICR_LBDCF);
       HAL_UARTEx_ReceiveToIdle_IT(&huart2, send_buff1, 16);
     }
}
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size){
	if (huart == &huart2){
		if(send_buff1[1]=0x55){
			HAL_UART_Transmit_IT(&huart2, SlaveData, 2);
		} else 
                         {
                               if(__HAL_UART_GET_FLAG(&huart2,UART_FLAG_ABRE) != RESET) {
                                    SET_BIT(huart2.Instance->RQR,  USART_RQR_ABRRQ);
                                }                           
                         }
	     }
}

In this mode, the automatic baud rate is not detected.

Please tell me how to correctly determine the data transfer rate in LIN UART mode?

1 REPLY 1
ADalex
Associate

My assumption is that the microcontroller cannot automatically detect the baud rate due to the break field. If you work with a regular yuart, then the frame starts from 0x55 and this one worked. But when working with LIN, there is a break field before the synchronization field, due to which a frame error may occur. Возможно, по �?той причине автоматиче�?кое определение �?коро�?ти передачи не работает в режиме LIN UART.

This is how the frame looks from the master device to the LIN transceiver

0693W00000WJj5qQAD.jpgThis is a frame converted LIN transceiver to UART

0693W00000WJj9TQAT.jpg