cancel
Showing results for 
Search instead for 
Did you mean: 

UART LIN DMA HAL

AMele.1
Associate

Hello, second day try to launch UART in LIN mode.

Task: waiting break, when break flag set up, switch on DMA receiver.

Transmitter works fine and send 100us break before frame.

But receiver can't set UP Break Flag.

And there are not everywhere examples =(

UART initialization:

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.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_LIN_Init(&huart2, UART_LINBREAKDETECTLENGTH_10B) != HAL_OK)
  {
    Error_Handler();
  }
	__HAL_UART_ENABLE_IT(&huart2, UART_IT_LBD);
	HAL_UART_Receive_DMA(&huart2,(uint8_t*)UART2_RxBuffer,UART2_RX_BUFFER_SIZE);
}

DMA + GPIO initialization:

__HAL_RCC_USART2_CLK_ENABLE();
  
    __HAL_RCC_GPIOA_CLK_ENABLE();
    /**USART2 GPIO Configuration    
    PA2     ------> USART2_TX
    PA3     ------> USART2_RX 
    */
    GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 
    /* USART2_RX Init */
    hdma_usart2_rx.Instance = DMA1_Channel6;
    hdma_usart2_rx.Init.Request = DMA_REQUEST_2;
    hdma_usart2_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
    hdma_usart2_rx.Init.PeriphInc = DMA_PINC_DISABLE;
    hdma_usart2_rx.Init.MemInc = DMA_MINC_ENABLE;
    hdma_usart2_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
    hdma_usart2_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
    hdma_usart2_rx.Init.Mode = DMA_NORMAL;
    hdma_usart2_rx.Init.Priority = DMA_PRIORITY_LOW;
    if (HAL_DMA_Init(&hdma_usart2_rx) != HAL_OK)
    {
      Error_Handler();
    }
 
    __HAL_LINKDMA(uartHandle,hdmarx,hdma_usart2_rx);
 
    /* USART2 interrupt Init */
    HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(USART2_IRQn);

Interrupt and launch DMA

void USART2_IRQHandler(void)
{
  /* USER CODE BEGIN USART2_IRQn 0 */
	if(__HAL_UART_GET_FLAG(&huart2,UART_FLAG_LBDF))
	{
		HAL_UART_Receive_DMA(&huart2,(uint8_t*)UART2_RxBuffer,UART2_RX_BUFFER_SIZE);
		__HAL_UART_CLEAR_FLAG(&huart2,UART_FLAG_LBDF);
		UART2_PacketReceived=true;		
	}
  /* USER CODE END USART2_IRQn 0 */
  HAL_UART_IRQHandler(&huart2);
  /* USER CODE BEGIN USART2_IRQn 1 */
 
  /* USER CODE END USART2_IRQn 1 */
}

Main:

  while (1)
  {
    /* USER CODE END WHILE */
		if(UART2_PacketReceived==true)
		{
		__HAL_UART_DISABLE_IT(&huart2, UART_IT_LBD);
			HAL_Delay(10);
			HAL_UART_DMAStop (&huart2);
			UART2_PacketReceived=false;
			__HAL_UART_ENABLE_IT(&huart2, UART_IT_LBD);
		}
    /* USER CODE BEGIN 3 */
  }

Currently interrupt work, DMA also work but UART receiver lost a lot of bytes or skip a lot of frames.

Please help me :sad_but_relieved_face:

0 REPLIES 0