text: /**
    ******************************************************************************
    * @file    dma.c
    * @brief   This file provides code for the configuration
    *          of all the requested memory to memory DMA transfers.
    ******************************************************************************
    * @attention
    *
    * <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
    * All rights reserved.</center></h2>
    *
    * This software component is licensed by ST under BSD 3-Clause license,
    * the "License"; You may not use this file except in compliance with the
    * License. You may obtain a copy of the License at:
    *                        opensource.org/licenses/BSD-3-Clause
    *
    ******************************************************************************
    */
  
  /* Includes ------------------------------------------------------------------*/
  #include "dma.h"
  
  /* USER CODE BEGIN 0 */
  #include "usart.h"
  extern uint8_t MPUBuffer[22];
  /* USER CODE END 0 */
  
  /*----------------------------------------------------------------------------*/
  /* Configure DMA                                                              */
  /*----------------------------------------------------------------------------*/
  
  /* USER CODE BEGIN 1 */
  
  /* USER CODE END 1 */
  
  /**
    * Enable DMA controller clock
    */
  void MX_DMA_Init(void)
  {
  
    /* DMA controller clock enable */
    __HAL_RCC_DMA2_CLK_ENABLE();
    __HAL_RCC_DMA1_CLK_ENABLE();
  
    /* DMA interrupt init */
    /* DMA1_Stream6_IRQn interrupt configuration */
    HAL_NVIC_SetPriority(DMA1_Stream6_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(DMA1_Stream6_IRQn);
    /* DMA2_Stream2_IRQn interrupt configuration */
    HAL_NVIC_SetPriority(DMA2_Stream2_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(DMA2_Stream2_IRQn);
  
  }
  
  /* USER CODE BEGIN 2 */
  
  int UART_Receive_DMA_No_IT(UART_HandleTypeDef* huart, uint8_t* pData, uint32_t Size)
  {
    uint32_t tmp1 = 0;
  
    tmp1 = huart->RxState;
  	
  	if (tmp1 == HAL_UART_STATE_READY)
  	{
  		if ((pData == NULL) || (Size == 0))
  		{
  			return HAL_ERROR;
  		}
  
  		huart->pRxBuffPtr = pData;
  		huart->RxXferSize = Size;
  		huart->ErrorCode  = HAL_UART_ERROR_NONE;
  
  		/* Enable the DMA Stream */
  		HAL_DMA_Start(huart->hdmarx, (uint32_t)&huart->Instance->DR, (uint32_t)pData, Size);
  
  		/* 
  		 * Enable the DMA transfer for the receiver request by setting the DMAR bit
  		 * in the UART CR3 register 
  		 */
  		SET_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  
  		return HAL_OK;
  	}
  	else
  	{
  		return HAL_BUSY;
  	}
  }
  void My_UART_RESET_RX_CALLBACK(UART_HandleTypeDef *huart)
  {
      if(__HAL_UART_GET_FLAG(huart, UART_FLAG_IDLE))
      {
          __HAL_UART_CLEAR_IDLEFLAG(huart);
          //clear idle it flag
          uint32_t DMA_FLAGS = __HAL_DMA_GET_TC_FLAG_INDEX(huart->hdmarx);
              
          __HAL_DMA_DISABLE(huart->hdmarx);
          __HAL_DMA_CLEAR_FLAG(huart->hdmarx,DMA_FLAGS);
          __HAL_DMA_ENABLE(huart->hdmarx);
      }
  }
  void My_UART_DMA_Init(void)
  {
      __HAL_UART_CLEAR_IDLEFLAG(&huart1);
      __HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);
  
      UART_Receive_DMA_No_IT(&huart1, MPUBuffer, sizeof(MPUBuffer));
  }
  
  /* USER CODE END 2 */
  
  /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  