/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file         stm32f1xx_hal_msp.c
  * @brief        This file provides code for the MSP Initialization
  *               and de-Initialization codes.
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "main.h"

void HAL_MspInit(void) {
  /* USER CODE BEGIN MspInit 0 */

  /* USER CODE END MspInit 0 */

  __HAL_RCC_AFIO_CLK_ENABLE();
  __HAL_RCC_PWR_CLK_ENABLE();

  /* System interrupt init*/

  /** NOJTAG: JTAG-DP Disabled and SW-DP Enabled
  */
  __HAL_AFIO_REMAP_SWJ_NOJTAG();

  /* USER CODE BEGIN MspInit 1 */

  /* USER CODE END MspInit 1 */
}

/*----------------------------------------------------------*/
/*函数名：中止接收完成回调函数                              */
/*参  数：huart：串口配置句柄                               */
/*返回值：无                                                */
/*----------------------------------------------------------*/
void HAL_UART_AbortReceiveCpltCallback(UART_HandleTypeDef *huart) {
  if(huart->Instance==USART3) {                                                                                         //判断是串口几的回调
    UART3_ControlCB.UsartRxDataInPtr->EndPtr = &U3_RxBuff[UART3_ControlCB.Usart_RxCounter];   		                  //标记本次接收的结束位置
    UART3_ControlCB.UsartRxDataInPtr->Timecounter = HAL_GetTick();                                                    //记录接收的时基
    UART3_ControlCB.UsartRxDataInPtr++;                                                                               //接收缓冲区数据IN指针下移
    if(UART3_ControlCB.UsartRxDataInPtr==UART3_ControlCB.UsartRxDataEndPtr)                                           //如果指针下移到结尾标志指针的时候，进入if
      UART3_ControlCB.UsartRxDataInPtr = &UART3_ControlCB.Usart_RxLocation[0];                                      //接收缓冲区 In插入指针归位到 位置结构体数组0号成员
    if(U3_RXBUFF_SIZE - UART3_ControlCB.Usart_RxCounter >= U3_RXMAX_SIZE) {                                           //计算，如果剩余空间大于 单次接收的数据量 进入if
      UART3_ControlCB.UsartRxDataInPtr->StartPtr = &U3_RxBuff[UART3_ControlCB.Usart_RxCounter];                     //记录下次接收的起始位置
    } else {                                                                                                           //如果剩余空间不够单次接收量 进入else
      UART3_ControlCB.Usart_RxCounter = 0;                                                                          //累计接收缓冲区存放的数据量清空
      UART3_ControlCB.UsartRxDataInPtr->StartPtr = U3_RxBuff;                                                       //接收位置，重新回到缓冲区的起始位置
    }
    HAL_UART_Receive_DMA(&UART3_ControlCB.USART_Handler,UART3_ControlCB.UsartRxDataInPtr->StartPtr,U3_RXMAX_SIZE);    //设置新一次的 DMA接收 指定接收位置
  }
  if(huart->Instance==USART2) {                                                                                         //判断是串口几的回调
    UART2_ControlCB.UsartRxDataInPtr->EndPtr = &U2_RxBuff[UART2_ControlCB.Usart_RxCounter];   		                  //标记本次接收的结束位置
    UART2_ControlCB.UsartRxDataInPtr++;                                                                               //接收缓冲区数据IN指针下移
    if(UART2_ControlCB.UsartRxDataInPtr==UART2_ControlCB.UsartRxDataEndPtr)                                           //如果指针下移到结尾标志指针的时候，进入if
      UART2_ControlCB.UsartRxDataInPtr = &UART2_ControlCB.Usart_RxLocation[0];                                      //接收缓冲区 In插入指针归位到 位置结构体数组0号成员
    if(U2_RXBUFF_SIZE - UART2_ControlCB.Usart_RxCounter >= U2_RXMAX_SIZE) {                                           //计算，如果剩余空间大于 单次接收的数据量 进入if
      UART2_ControlCB.UsartRxDataInPtr->StartPtr = &U2_RxBuff[UART2_ControlCB.Usart_RxCounter];                     //记录下次接收的起始位置
    } else {                                                                                                           //如果剩余空间不够单次接收量 进入else
      UART2_ControlCB.Usart_RxCounter = 0;                                                                          //累计接收缓冲区存放的数据量清空
      UART2_ControlCB.UsartRxDataInPtr->StartPtr = U2_RxBuff;                                                       //接收位置，重新回到缓冲区的起始位置
    }
    HAL_UART_Receive_DMA(&UART2_ControlCB.USART_Handler,UART2_ControlCB.UsartRxDataInPtr->StartPtr,U2_RXMAX_SIZE);    //设置新一次的 DMA接收 指定接收位置
  }
}
/*----------------------------------------------------------*/
/*函数名：数据接收完成回调函数                              */
/*参  数：huart：串口配置句柄                               */
/*返回值：无                                                */
/*----------------------------------------------------------*/
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
  if(huart->Instance==USART3) {                                                                                         //判断是串口几的回调
    UART3_ControlCB.Usart_RxCounter += U3_RXMAX_SIZE;                                                                 //累计当前缓冲区已经存放的数据量
    UART3_ControlCB.UsartRxDataInPtr->EndPtr = &U3_RxBuff[UART3_ControlCB.Usart_RxCounter];   		                  //标记本次接收的结束位置
    UART3_ControlCB.UsartRxDataInPtr->Timecounter = HAL_GetTick();                                                    //记录接收的时基
    UART3_ControlCB.UsartRxDataInPtr++;                                                                               //接收缓冲区数据IN指针下移
    if(UART3_ControlCB.UsartRxDataInPtr==UART3_ControlCB.UsartRxDataEndPtr)                                           //如果指针下移到结尾标志指针的时候，进入if
      UART3_ControlCB.UsartRxDataInPtr = &UART3_ControlCB.Usart_RxLocation[0];                                      //接收缓冲区 In插入指针归位到 位置结构体数组0号成员
    if(U3_RXBUFF_SIZE - UART3_ControlCB.Usart_RxCounter >= U3_RXMAX_SIZE) {                                           //计算，如果剩余空间大于 单次接收的数据量 进入if
      UART3_ControlCB.UsartRxDataInPtr->StartPtr = &U3_RxBuff[UART3_ControlCB.Usart_RxCounter];                     //记录下次接收的起始位置
    } else {                                                                                                           //如果剩余空间不够单次接收量 进入else
      UART3_ControlCB.Usart_RxCounter = 0;                                                                          //累计接收缓冲区存放的数据量清空
      UART3_ControlCB.UsartRxDataInPtr->StartPtr = U3_RxBuff;                                                       //接收位置，重新回到缓冲区的起始位置
    }
    HAL_UART_Receive_DMA(&UART3_ControlCB.USART_Handler,UART3_ControlCB.UsartRxDataInPtr->StartPtr,U3_RXMAX_SIZE);     //设置新一次的 DMA接收 指定接收位置
  }
  if(huart->Instance==USART2) {                                                                                         //判断是串口几的回调
    UART2_ControlCB.Usart_RxCounter += U2_RXMAX_SIZE;                                                                 //累计当前缓冲区已经存放的数据量
    UART2_ControlCB.UsartRxDataInPtr->EndPtr = &U2_RxBuff[UART2_ControlCB.Usart_RxCounter];   		                  //标记本次接收的结束位置
    UART2_ControlCB.UsartRxDataInPtr++;                                                                               //接收缓冲区数据IN指针下移
    if(UART2_ControlCB.UsartRxDataInPtr==UART2_ControlCB.UsartRxDataEndPtr)                                           //如果指针下移到结尾标志指针的时候，进入if
      UART2_ControlCB.UsartRxDataInPtr = &UART2_ControlCB.Usart_RxLocation[0];                                      //接收缓冲区 In插入指针归位到 位置结构体数组0号成员
    if(U2_RXBUFF_SIZE - UART2_ControlCB.Usart_RxCounter >= U2_RXMAX_SIZE) {                                           //计算，如果剩余空间大于 单次接收的数据量 进入if
      UART2_ControlCB.UsartRxDataInPtr->StartPtr = &U2_RxBuff[UART2_ControlCB.Usart_RxCounter];                     //记录下次接收的起始位置
    } else {                                                                                                           //如果剩余空间不够单次接收量 进入else
      UART2_ControlCB.Usart_RxCounter = 0;                                                                          //累计接收缓冲区存放的数据量清空
      UART2_ControlCB.UsartRxDataInPtr->StartPtr = U2_RxBuff;                                                       //接收位置，重新回到缓冲区的起始位置
    }
    HAL_UART_Receive_DMA(&UART2_ControlCB.USART_Handler,UART2_ControlCB.UsartRxDataInPtr->StartPtr,U2_RXMAX_SIZE);     //设置新一次的 DMA接收 指定接收位置
  }
}

/*----------------------------------------------------------*/
/*函数名：数据发送完成回调函数                              */
/*参  数：huart：串口配置句柄                               */
/*返回值：无                                                */
/*----------------------------------------------------------*/
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) {
  if(huart->Instance==USART3) {                        //判断是串口几的回调
    UART3_ControlCB.Usart_TxCpltflag = 0;            //发送空闲
    UART3_ControlCB.Usart_TxTimer = HAL_GetTick();   //记录当前时间
  }
  if(huart->Instance==USART2) {                        //判断是串口几的回调
    UART2_ControlCB.Usart_TxCpltflag = 0;            //发送空闲
    UART2_ControlCB.Usart_TxTimer = HAL_GetTick();   //记录当前时间
  }
}

/**
* @brief UART MSP De-Initialization
* This function freeze the hardware resources used in this example
* @param huart: UART handle pointer
* @retval None
*/
void HAL_UART_MspDeInit(UART_HandleTypeDef* huart) {
  if(huart->Instance==USART1) {
    /* USER CODE BEGIN USART1_MspDeInit 0 */

    /* USER CODE END USART1_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_USART1_CLK_DISABLE();

    /**USART1 GPIO Configuration
    PA9     ------> USART1_TX
    PA10     ------> USART1_RX
    */
    HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10);

    /* USER CODE BEGIN USART1_MspDeInit 1 */

    /* USER CODE END USART1_MspDeInit 1 */
  } else if(huart->Instance==USART2) {
    /* USER CODE BEGIN USART2_MspDeInit 0 */

    /* USER CODE END USART2_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_USART2_CLK_DISABLE();

    /**USART2 GPIO Configuration
    PA2     ------> USART2_TX
    PA3     ------> USART2_RX
    */
    HAL_GPIO_DeInit(GPIOA, GPIO_PIN_2|GPIO_PIN_3);

    /* USART2 DMA DeInit */
    HAL_DMA_DeInit(huart->hdmarx);
    HAL_DMA_DeInit(huart->hdmatx);
    /* USER CODE BEGIN USART2_MspDeInit 1 */

    /* USER CODE END USART2_MspDeInit 1 */
  } else if(huart->Instance==USART3) {
    /* USER CODE BEGIN USART3_MspDeInit 0 */

    /* USER CODE END USART3_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_USART3_CLK_DISABLE();

    /**USART3 GPIO Configuration
    PB10     ------> USART3_TX
    PB11     ------> USART3_RX
    */
    HAL_GPIO_DeInit(GPIOB, GPIO_PIN_10|GPIO_PIN_11);

    /* USART3 DMA DeInit */
    HAL_DMA_DeInit(huart->hdmarx);
    HAL_DMA_DeInit(huart->hdmatx);

    /* USART3 interrupt DeInit */
    HAL_NVIC_DisableIRQ(USART3_IRQn);
    /* USER CODE BEGIN USART3_MspDeInit 1 */

    /* USER CODE END USART3_MspDeInit 1 */
  }
}

/* USER CODE BEGIN 1 */

/* USER CODE END 1 */
