/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2023 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"
#include "adc.h"
#include "dma.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

int spr000;
uint8_t  data_to_send[50];
uint16_t CRC_value 		 = 0;
uint16_t initial_address = 0x00C1;
uint16_t BaudrateValue 	 = 0x0000;
uint16_t Adc2_CalibrationValue ;
uint16_t Adc3_CalibrationValue ;
uint32_t System_version  = Version_sys;

uint8_t	 Rx_Flag 		= 0;
uint8_t  gpioaStatus 	= 0;
uint8_t  gpiobStatus 	= 0;
uint8_t  bufMain[128] 	= {0};
uint8_t	 Rx_Buf[Rx_Max] = {0};	
uint16_t Rx_Len = 0;

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
#ifdef iap_
typedef struct _T2C_RemoteCaliData
{
	uint16_t   FrameHead;   		//帧头 0-1
	uint32_t   AddrExtModule; 	//4字节扩展地址   2--5
	uint16_t   FunctionCode;		//功能码2字节   只能是0x0003、0x0006或者0x0010    6--7
	uint16_t   AddrRegStart;		//寄存器起始地址2字节：合法范围为0x0000~0xffff    8--9
	uint16_t   NumberOfRegs;		//数据个数2字节：最多读取0xffff字节；实际取决于终端限制，目前为256字节  10--11
	uint32_t   ReserveData001;	//保留字4字节 12--15
	uint8_t    PayLoadData[44+32+32];	//有效载荷具体有三种：44，或者76字节，或者108字节；字节 16--59
	uint16_t   ReserveData002;	//保留字2字节    60--61
	uint16_t   CheckSum;				//校验2字节      62--63
}sT2C_RemoteCaliDat;

typedef struct{
	uint16_t USE_RKG;								//控制器占用人孔盖总线
	uint16_t USE_XYF;								//控制器占用卸油阀总线
	uint16_t KZQ_ErrorCnt;					//校验错误计数
	uint16_t KZQ_Error;							//控制器通信错误
	uint8_t sensor_addr;						//操作的传感器地址
	uint8_t sensor_RW;							//操作的传感器读写属性
	uint16_t sensor_reg;							//操作的传感器寄存器地址，即功能码
	uint8_t sensor_datelen;					//操作数据长度
	uint8_t data_buf[70];          //下发数据缓存 
}KZQ_Inf;
#define 	ADDR_Is_In_ElecFence	0x05D0	//	1488		车是否在电子围栏内
 
 sT2C_RemoteCaliDat T2C_RemoteCaliDat001  =
{
 
	0x3901,
	0x9551000,
	0x0003,
	ADDR_Is_In_ElecFence,//0x0000,
	0x0001,        //操作的数据个数
	0x55AA55AA,    //保留字4字节
	{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,\
	25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44},
	0x7788,//保留字2字节
	0x99AA   //校验2字节
};
sT2C_RemoteCaliDat *pT2C_RemoteCaliData = &T2C_RemoteCaliDat001; 

	uint8_t CMD_KZQ[256] =
	{0x39,0x01,0x95,0x50,0x00,0x01,0x00,0x03,
	 0x00,0x00,0x00,0x00,0x95,0x05,0xAA,0xAA,
	 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	};
	int flagU2Rx=0;
	int data_lengthU2;
	uint8_t* ptx = CMD_KZQ;   
	uint16_t ModbusCRC   = 0;
	uint16_t SetSuccess  = 0;
	uint16_t ModbusCRC1  = 0;
	uint16_t SetSuccess1 = 0;
	uint16_t SetSuccess2 = 0;
	uint16_t SetSuccess3 = 0;                 

	int i000;
	uint8_t* send_ptr;
	uint16_t send_len;
	uint32_t tmpU32;
	KZQ_Inf kzq_inf;
	KZQ_Inf* pkzq = &kzq_inf;
	uint8_t USART2_RX_BUF003[128];
	uint8_t F_STATE[70] = {0};
	uint8_t ptxCang01Temp[150];
	uint32_t KZQ_RTerror = 0;
	
void RstCPU(void)
{
  uint16_t j;
  __disable_irq();
	
	while(1)
	{
		__disable_irq();	
		NVIC_SystemReset();  
		while(j --)
		{
	    __nop(); __nop(); __nop(); __nop(); 
		}
	}
}

#endif
/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */
#if	IR_ROM1 == 0x08000000	
	Start_BootLoader();
#else	
	SCB->VTOR = FLASH_BASE | ADD_UPDATE_PROG; 
	__set_PRIMASK(0);

#endif	
  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_USART1_UART_Init();
  MX_ADC1_Init();
  MX_TIM2_Init();
  MX_USART3_UART_Init();
  /* USER CODE BEGIN 2 */
  HAL_TIM_Base_Start_IT(&htim2);
  read_new_address(ADDR_FLASH_PAGE_94); 
  updatePbStatus(read_flash_16(ADDR_FLASH_PAGE_104));
  update_baudrate(read_flash_16(ADDR_FLASH_PAGE_114));
  AdcCalibration_init();
  
  spr000 = sprintf((char*)bufMain, "system reboot...");
  HAL_UART_Transmit(&huart1, bufMain, spr000, 300);
  
  HAL_UART_Receive_DMA(&huart1,USART2_RX_BUF,Uart2_BUF_SIZE); 
  __HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE); 
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  { 
	ModBus();
	#ifdef iap_
	if (flagU2Rx == 1) {
		flagU2Rx = 0;
		if ((data_lengthU2 > 300) || ((USART2_RX_BUF002[0] == 0xA5) && (USART2_RX_BUF002[1] == 0x5A)))
		{
			tmpU32 = (USART2_RX_BUF002[0] << 24) | (USART2_RX_BUF002[1] << 16) | (USART2_RX_BUF002[2] << 8) | (USART2_RX_BUF002[3]);
			if (StartBytes_IAP == tmpU32)
			{
				Process_CMD_IAP_Update();
				continue;
			}
		}
	}
#endif
	HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);
	HAL_Delay(1);  
	HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
	
	gpioaStatus = GetPaInputStatus();
	gpiobStatus = GetPbOutputStatus();
	GetADCResults(&hadc1); 
	
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    Error_Handler();
  }
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
  PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */
uint16_t read_flash_16(uint32_t addr){
    uint16_t data = *(volatile uint16_t*)addr;     // 从Flash中读取2个字节
    return data;
}

void read_new_address(uint32_t addr){
		uint16_t init_address = read_flash_16(addr);
	  if(init_address == 0xFFFF) {
			initial_address = initial_address;
		} else {
			initial_address = init_address;  // 读取新地址并赋值给initial_address
		}
}

void erase_flash(uint32_t ADDR_FLASH){                //进行擦除
    FLASH_EraseInitTypeDef erase_init;
    erase_init.TypeErase = FLASH_TYPEERASE_PAGES;  // 擦除类型为页擦除
    erase_init.PageAddress = ADDR_FLASH;  
    erase_init.NbPages = 1;  // 擦除的页数
    uint32_t page_error = 0;
    HAL_FLASH_Unlock();  // 解锁Flash
    HAL_StatusTypeDef status = HAL_FLASHEx_Erase(&erase_init, &page_error);  // 执行擦除操作
    HAL_FLASH_Lock();  // 上锁Flash
    if(status == HAL_OK){
        if(ADDR_FLASH == ADDR_FLASH_PAGE_94) {
            initial_address = 0;         // 将initial_address清零
        }
        else if(ADDR_FLASH == ADDR_FLASH_PAGE_104) {
            gpiobStatus = 0;             // 将pb_status清零
        }
        else if(ADDR_FLASH == ADDR_FLASH_PAGE_114) {
            BaudrateValue = 0xFFFF;  
        }
        else if(ADDR_FLASH == ADDR_FLASH_PAGE_124) {
            Adc2_CalibrationValue = 0x0000;  // 清零
        }
        else if(ADDR_FLASH == ADDR_FLASH_PAGE_134) {
            Adc3_CalibrationValue = 0x0000;  // 清零
        }
        
    }
}

void Write_Information(uint32_t addr, uint8_t* rx_buffer, uint8_t buffer_index) {
    uint16_t newValue = 0;
	  erase_flash(addr);
    HAL_FLASH_Unlock();  // 解锁Flash
	
    if (addr == ADDR_FLASH_PAGE_94) {  // 写入地址到Flash
        newValue = rx_buffer[buffer_index];
        HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, ADDR_FLASH_PAGE_94, newValue);
        if (status == HAL_OK) {
            initial_address = newValue;  // 将新地址赋值给initial_address
        }
    } 
		else if (addr == ADDR_FLASH_PAGE_104) {  // 写入PB状态到Flash
        newValue = rx_buffer[buffer_index];
        HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, ADDR_FLASH_PAGE_104, newValue);
        if (status == HAL_OK) {
            gpiobStatus = newValue;  // 将新状态赋值给gpiobStatus
        }
    } 
		else if (addr == ADDR_FLASH_PAGE_114) {  // 写入波特率到Flash
        newValue = rx_buffer[buffer_index];
        HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, ADDR_FLASH_PAGE_114, newValue);
        if (status == HAL_OK) {
            BaudrateValue = newValue;  // 将新状态赋值给BaudrateValue
        }
    } 
		else if (addr == ADDR_FLASH_PAGE_124 || addr == ADDR_FLASH_PAGE_134) {  // 写入ADC校准值到Flash
        uint16_t Standard_value = (0.6 / (3.3 / 4096));
        if (addr == ADDR_FLASH_PAGE_124) {
            if (adc2_rawValue >= Standard_value) {
                newValue = adc2_rawValue - Standard_value;
            } else {
                newValue = Standard_value - adc2_rawValue;
                newValue |= 0x8000;
            }
            HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, ADDR_FLASH_PAGE_124, newValue);
            if (status == HAL_OK) {
                Adc2_CalibrationValue = newValue & 0x7FFF;
							  Value_old_addr2 = newValue;
            }
        } else if (addr == ADDR_FLASH_PAGE_134) {
            if (adc3_rawValue >= Standard_value) {
                newValue = adc3_rawValue - Standard_value;
            } else {
                newValue = Standard_value - adc3_rawValue;
                newValue |= 0x8000;
            }
            HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, ADDR_FLASH_PAGE_134, newValue);
            if (status == HAL_OK) {
                Adc3_CalibrationValue = newValue & 0x7FFF;
							  Value_old_addr3 = newValue;
            }
        }
    }
    
		HAL_FLASH_Lock();  // 上锁Flash
}


void delay_sys_us(uint32_t Delay)//1个delay，大概1.5us
{
	uint32_t cnt = Delay * 8;  
    uint32_t i = 0;
    for(i = 0; i < cnt; i++)__NOP();
}


void ModBus(void) {
	if (Rx_Flag == 1 && (calculate_crc(Rx_Buf,6) == (Rx_Buf[7] << 8 | Rx_Buf[6])) && data_lengthU2 < 10) {		
		if(Rx_Buf[0] == (uint8_t)initial_address && Rx_Buf[1] == 0x03 && Rx_Buf[2] == 0x00 && Rx_Buf[3] == 0x01 && Rx_Buf[4] == 0x00 && Rx_Buf[5] == 0x01){
			uint8_t data_to_send[] = {(uint8_t)initial_address, 0x03, 0x02, 0x00, gpioaStatus, 0x00, 0x00};
			uint16_t CRC_value = calculate_crc(data_to_send, 5);
			data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
			data_to_send[6] = (uint8_t)(CRC_value >> 8);
			HAL_UART_Transmit(&huart1, data_to_send, 7, 6);
			memset(Rx_Buf, 0, sizeof(Rx_Buf)); 
			memset(USART2_RX_BUF, 0, Uart2_BUF_SIZE); 
			Rx_Flag = 0;	
			HAL_UART_AbortReceive(&huart1);
			HAL_UART_Receive_DMA(&huart1, USART2_RX_BUF, Uart2_BUF_SIZE); 
		}

		else if(Rx_Buf[0] == (uint8_t)initial_address && Rx_Buf[1] == 0x03 && Rx_Buf[2] == 0x00 && Rx_Buf[3] == 0x02 && Rx_Buf[4] == 0x00 && Rx_Buf[5] == 0x01){
			uint8_t data_to_send[] = {(uint8_t)initial_address, 0x03, 0x02, 0x00, gpiobStatus, 0x00, 0x00};
			uint16_t CRC_value = calculate_crc(data_to_send, 5);
			data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
			data_to_send[6] = (uint8_t)(CRC_value >> 8);
			HAL_UART_Transmit(&huart1, data_to_send, 7, 6);
			memset(Rx_Buf, 0, sizeof(Rx_Buf)); 
			memset(USART2_RX_BUF, 0, Uart2_BUF_SIZE); 
			Rx_Flag = 0;	
			HAL_UART_AbortReceive(&huart1);
			HAL_UART_Receive_DMA(&huart1, USART2_RX_BUF, Uart2_BUF_SIZE); 
		}
				
		else if(Rx_Buf[0] == (uint8_t)initial_address && Rx_Buf[1] == 0x03 && Rx_Buf[2] == 0x00 && Rx_Buf[3] == 0x03 && Rx_Buf[4] == 0x00 && Rx_Buf[5] == 0x01){	
			uint8_t data_to_send[] = {(uint8_t)initial_address, 0x03, 0x02, adc2_byte1, adc2_byte2, 0x00, 0x00};
			uint16_t CRC_value = calculate_crc(data_to_send, 5);
			data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
			data_to_send[6] = (uint8_t)(CRC_value >> 8);
			HAL_UART_Transmit(&huart1, data_to_send, 7, 6);
			memset(Rx_Buf, 0, sizeof(Rx_Buf)); 
			memset(USART2_RX_BUF, 0, Uart2_BUF_SIZE); 
			Rx_Flag = 0;	
			HAL_UART_AbortReceive(&huart1);
			HAL_UART_Receive_DMA(&huart1, USART2_RX_BUF, Uart2_BUF_SIZE); 
		}
				
		else if(Rx_Buf[0] == (uint8_t)initial_address && Rx_Buf[1] == 0x03 && Rx_Buf[2] == 0x00 && Rx_Buf[3] == 0x04 && Rx_Buf[4] == 0x00 && Rx_Buf[5] == 0x01){
			uint8_t data_to_send[] = {(uint8_t)initial_address, 0x03, 0x02, adc3_byte1, adc3_byte2, 0x00, 0x00};
			uint16_t CRC_value = calculate_crc(data_to_send, 5);
			data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
			data_to_send[6] = (uint8_t)(CRC_value >> 8);
			HAL_UART_Transmit(&huart1, data_to_send, 7, 6);
			memset(Rx_Buf, 0, sizeof(Rx_Buf)); 
			memset(USART2_RX_BUF, 0, Uart2_BUF_SIZE); 
			Rx_Flag = 0;	
			HAL_UART_AbortReceive(&huart1);
			HAL_UART_Receive_DMA(&huart1, USART2_RX_BUF, Uart2_BUF_SIZE); 
		}
				
		else if(Rx_Buf[0] == (uint8_t)initial_address && Rx_Buf[1] == 0x03 && Rx_Buf[2] == 0x00 && Rx_Buf[3] == 0x05 && Rx_Buf[4] == 0x00 && Rx_Buf[5] == 0x01){
			uint8_t data_to_send[] = {(uint8_t)initial_address, 0x03, 0x02, adc1_byte1, adc1_byte2, 0x00, 0x00};
			uint16_t CRC_value = calculate_crc(data_to_send, 5);
			data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
			data_to_send[6] = (uint8_t)(CRC_value >> 8);
			HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 6);
			memset(Rx_Buf, 0, sizeof(Rx_Buf)); 
			memset(USART2_RX_BUF, 0, Uart2_BUF_SIZE); 
			Rx_Flag = 0;	
			HAL_UART_AbortReceive(&huart1);
			HAL_UART_Receive_DMA(&huart1, USART2_RX_BUF, Uart2_BUF_SIZE); 
		}

		else if(Rx_Buf[0] == (uint8_t)initial_address && Rx_Buf[1] == 0x03 && Rx_Buf[2] == 0x00 && Rx_Buf[3] == 0x01 && Rx_Buf[4] == 0x00 && Rx_Buf[5] == 0x02){
			uint8_t data_to_send[] = {(uint8_t)initial_address, 0x03, 0x04, 0x00, gpioaStatus, 0x00, gpiobStatus, 0x00, 0x00};
			uint16_t CRC_value = calculate_crc(data_to_send, 7);
			data_to_send[7] = (uint8_t)(CRC_value & 0xFF);
			data_to_send[8] = (uint8_t)(CRC_value >> 8);
			HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 9, 8);
			memset(Rx_Buf, 0, sizeof(Rx_Buf)); 
			memset(USART2_RX_BUF, 0, Uart2_BUF_SIZE); 
			Rx_Flag = 0;	
			HAL_UART_AbortReceive(&huart1);
			HAL_UART_Receive_DMA(&huart1, USART2_RX_BUF, Uart2_BUF_SIZE); 
		}
		
		else if(Rx_Buf[0] == (uint8_t)initial_address && Rx_Buf[1] == 0x03 && Rx_Buf[2] == 0x00 && Rx_Buf[3] == 0x01 && Rx_Buf[4] == 0x00 && Rx_Buf[5] == 0x03){
			uint8_t data_to_send[] = {(uint8_t)initial_address, 0x03, 0x06, 0x00, gpioaStatus, 0x00, gpiobStatus, adc2_byte1, adc2_byte2, 0x00, 0x00};
			uint16_t CRC_value = calculate_crc(data_to_send, 9);
			data_to_send[9] = (uint8_t)(CRC_value & 0xFF);
			data_to_send[10] = (uint8_t)(CRC_value >> 8);
			HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 11, 10);
			memset(Rx_Buf, 0, sizeof(Rx_Buf)); 
			memset(USART2_RX_BUF, 0, Uart2_BUF_SIZE); 
			Rx_Flag = 0;	
			HAL_UART_AbortReceive(&huart1);
			HAL_UART_Receive_DMA(&huart1, USART2_RX_BUF, Uart2_BUF_SIZE); 
		}

		else if(Rx_Buf[0] == (uint8_t)initial_address && Rx_Buf[1] == 0x03 && Rx_Buf[2] == 0x00 && Rx_Buf[3] == 0x01 && Rx_Buf[4] == 0x00 && Rx_Buf[5] == 0x04){
			uint8_t data_to_send[] = {(uint8_t)initial_address, 0x03, 0x08, 0x00, gpioaStatus, 0x00, gpiobStatus, adc2_byte1, adc2_byte2, adc3_byte1, adc3_byte2, 0x00, 0x00};
			uint16_t CRC_value = calculate_crc(data_to_send, 11);
			data_to_send[11] = (uint8_t)(CRC_value & 0xFF);
			data_to_send[12] = (uint8_t)(CRC_value >> 8);
			HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 13, 12);
			memset(Rx_Buf, 0, sizeof(Rx_Buf)); 
			memset(USART2_RX_BUF, 0, Uart2_BUF_SIZE); 
			Rx_Flag = 0;	
			HAL_UART_AbortReceive(&huart1);
			HAL_UART_Receive_DMA(&huart1, USART2_RX_BUF, Uart2_BUF_SIZE); 
		}
				
		else if(Rx_Buf[0] == (uint8_t)initial_address && Rx_Buf[1] == 0x03 && Rx_Buf[2] == 0x00 && Rx_Buf[3] == 0x01 && Rx_Buf[4] == 0x00 && Rx_Buf[5] == 0x05){
			uint8_t data_to_send[] = {(uint8_t)initial_address, 0x03, 0x0A, 0x00, gpioaStatus, 0x00, gpiobStatus, adc2_byte1, adc2_byte2, adc3_byte1, adc3_byte2, adc1_byte1, adc1_byte2, 0x00, 0x00};
			uint16_t CRC_value = calculate_crc(data_to_send, 13);
			data_to_send[13] = (uint8_t)(CRC_value & 0xFF);
			data_to_send[14] = (uint8_t)(CRC_value >> 8);
			HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 15, 14);
			memset(Rx_Buf, 0, sizeof(Rx_Buf)); 
			memset(USART2_RX_BUF, 0, Uart2_BUF_SIZE); 
			Rx_Flag = 0;	
			HAL_UART_AbortReceive(&huart1);
			HAL_UART_Receive_DMA(&huart1, USART2_RX_BUF, Uart2_BUF_SIZE); 
		}  
		
		else if(Rx_Buf[0] == (uint8_t)initial_address && Rx_Buf[1] == 0x03 && Rx_Buf[2] == 0x00 && Rx_Buf[3] == 0x02 && Rx_Buf[4] == 0x00 && Rx_Buf[5] == 0x02){
			uint8_t data_to_send[] = {(uint8_t)initial_address, 0x03, 0x04, 0x00, gpiobStatus, adc2_byte1, adc2_byte2, 0x00, 0x00};
			uint16_t CRC_value = calculate_crc(data_to_send, 7);
			data_to_send[7] = (uint8_t)(CRC_value & 0xFF);
			data_to_send[8] = (uint8_t)(CRC_value >> 8);
			HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 9, 8);
			memset(Rx_Buf, 0, sizeof(Rx_Buf)); 
			memset(USART2_RX_BUF, 0, Uart2_BUF_SIZE); 
			Rx_Flag = 0;	
			HAL_UART_AbortReceive(&huart1);
			HAL_UART_Receive_DMA(&huart1, USART2_RX_BUF, Uart2_BUF_SIZE); 
		}  

		else if(Rx_Buf[0] == (uint8_t)initial_address && Rx_Buf[1] == 0x03 && Rx_Buf[2] == 0x00 && Rx_Buf[3] == 0x02 && Rx_Buf[4] == 0x00 && Rx_Buf[5] == 0x03){
			uint8_t data_to_send[] = {(uint8_t)initial_address, 0x03, 0x06, 0x00, gpiobStatus, adc2_byte1, adc2_byte2, adc3_byte1, adc3_byte2, 0x00, 0x00};
			uint16_t CRC_value = calculate_crc(data_to_send, 9);
			data_to_send[9] = (uint8_t)(CRC_value & 0xFF);
			data_to_send[10] = (uint8_t)(CRC_value >> 8);
			HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 11, 10);
			memset(Rx_Buf, 0, sizeof(Rx_Buf)); 
			memset(USART2_RX_BUF, 0, Uart2_BUF_SIZE); 
			Rx_Flag = 0;	
			HAL_UART_AbortReceive(&huart1);
			HAL_UART_Receive_DMA(&huart1, USART2_RX_BUF, Uart2_BUF_SIZE); 
		}  
		
		else if(Rx_Buf[0] == (uint8_t)initial_address && Rx_Buf[1] == 0x03 && Rx_Buf[2] == 0x00 && Rx_Buf[3] == 0x02 && Rx_Buf[4] == 0x00 && Rx_Buf[5] == 0x04){
			uint8_t data_to_send[] = {(uint8_t)initial_address, 0x03, 0x08, 0x00, gpiobStatus, adc2_byte1, adc2_byte2, adc3_byte1, adc3_byte2, adc1_byte1, adc1_byte2, 0x00, 0x00};
			uint16_t CRC_value = calculate_crc(data_to_send, 11);
			data_to_send[11] = (uint8_t)(CRC_value & 0xFF);
			data_to_send[12] = (uint8_t)(CRC_value >> 8);
			HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 13, 12);
			memset(Rx_Buf, 0, sizeof(Rx_Buf)); 
			memset(USART2_RX_BUF, 0, Uart2_BUF_SIZE); 
			Rx_Flag = 0;	
			HAL_UART_AbortReceive(&huart1);
			HAL_UART_Receive_DMA(&huart1, USART2_RX_BUF, Uart2_BUF_SIZE); 
		}       
				
		else if(Rx_Buf[0] == (uint8_t)initial_address && Rx_Buf[1] == 0x03 && Rx_Buf[2] == 0x00 && Rx_Buf[3] == 0x03 && Rx_Buf[4] == 0x00 && Rx_Buf[5] == 0x02){
			uint8_t data_to_send[] = {(uint8_t)initial_address, 0x03, 0x04, adc2_byte1, adc2_byte2, adc3_byte1, adc3_byte2, 0x00, 0x00};
			uint16_t CRC_value = calculate_crc(data_to_send, 7);
			data_to_send[7] = (uint8_t)(CRC_value & 0xFF);
			data_to_send[8] = (uint8_t)(CRC_value >> 8);
			HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 9, 8);
			memset(Rx_Buf, 0, sizeof(Rx_Buf)); 
			memset(USART2_RX_BUF, 0, Uart2_BUF_SIZE); 
			Rx_Flag = 0;	
			HAL_UART_AbortReceive(&huart1);
			HAL_UART_Receive_DMA(&huart1, USART2_RX_BUF, Uart2_BUF_SIZE); 
		}  
		
		else if(Rx_Buf[0] == (uint8_t)initial_address && Rx_Buf[1] == 0x03 && Rx_Buf[2] == 0x00 && Rx_Buf[3] == 0x03 && Rx_Buf[4] == 0x00 && Rx_Buf[5] == 0x03){
			uint8_t data_to_send[] = {(uint8_t)initial_address, 0x03, 0x06, adc2_byte1, adc2_byte2, adc3_byte1, adc3_byte2, adc1_byte1, adc1_byte2, 0x00, 0x00};
			uint16_t CRC_value = calculate_crc(data_to_send, 9);
			data_to_send[9] = (uint8_t)(CRC_value & 0xFF);
			data_to_send[10] = (uint8_t)(CRC_value >> 8);
			HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 11, 10);
			memset(Rx_Buf, 0, sizeof(Rx_Buf)); 
			memset(USART2_RX_BUF, 0, Uart2_BUF_SIZE); 
			Rx_Flag = 0;	
			HAL_UART_AbortReceive(&huart1);
			HAL_UART_Receive_DMA(&huart1, USART2_RX_BUF, Uart2_BUF_SIZE); 
		} 

		else if(Rx_Buf[0] == (uint8_t)initial_address && Rx_Buf[1] == 0x03 && Rx_Buf[2] == 0x00 && Rx_Buf[3] == 0x04 && Rx_Buf[4] == 0x00 && Rx_Buf[5] == 0x02){
			uint8_t data_to_send[] = {(uint8_t)initial_address, 0x03, 0x04, adc3_byte1, adc3_byte2, adc1_byte1, adc1_byte2, 0x00, 0x00};
			uint16_t CRC_value = calculate_crc(data_to_send, 7);
			data_to_send[7] = (uint8_t)(CRC_value & 0xFF);
			data_to_send[8] = (uint8_t)(CRC_value >> 8);
			HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 9, 8);
			memset(Rx_Buf, 0, sizeof(Rx_Buf)); 
			memset(USART2_RX_BUF, 0, Uart2_BUF_SIZE); 
			Rx_Flag = 0;	
			HAL_UART_AbortReceive(&huart1);
			HAL_UART_Receive_DMA(&huart1, USART2_RX_BUF, Uart2_BUF_SIZE); 
		}   
		
		else if(Rx_Buf[0] == 0xFA && Rx_Buf[1] == 0x03 && Rx_Buf[2] == 0x00 && Rx_Buf[3] == 0xAA && Rx_Buf[4] == 0x00 && Rx_Buf[5] == 0x01){
			uint8_t data_to_send[] = {0xFA, 0x03, 0x02, 0x00, (uint8_t)initial_address, 0x00, 0x00};
			uint16_t CRC_value = calculate_crc(data_to_send, 5);
			data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
			data_to_send[6] = (uint8_t)(CRC_value >> 8);
			HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 6);
			memset(Rx_Buf, 0, sizeof(Rx_Buf)); 
			memset(USART2_RX_BUF, 0, Uart2_BUF_SIZE); 
			Rx_Flag = 0;	
			HAL_UART_AbortReceive(&huart1);
			HAL_UART_Receive_DMA(&huart1, USART2_RX_BUF, Uart2_BUF_SIZE); 
		} 
		
		else if(Rx_Buf[0] == (uint8_t)initial_address && Rx_Buf[1] == 0x03 && Rx_Buf[2] == 0x00 && Rx_Buf[3] == 0xBB && Rx_Buf[4] == 0x00 && Rx_Buf[5] == 0x02){
			uint8_t data_to_send[] = {(uint8_t)initial_address, 0x03, 0x04, (uint8_t)(System_version >> 24), (uint8_t)(System_version >> 16), (uint8_t)(System_version >> 8), (uint8_t)(System_version ), 0x00, 0x00};
			uint16_t CRC_value = calculate_crc(data_to_send, 7);
			data_to_send[7] = (uint8_t)(CRC_value & 0xFF);
			data_to_send[8] = (uint8_t)(CRC_value >> 8);
			HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 9, 8);
			memset(Rx_Buf, 0, sizeof(Rx_Buf)); 
			memset(USART2_RX_BUF, 0, Uart2_BUF_SIZE); 
			Rx_Flag = 0;	
			HAL_UART_AbortReceive(&huart1);
			HAL_UART_Receive_DMA(&huart1, USART2_RX_BUF, Uart2_BUF_SIZE); 
		}

		else if(Rx_Buf[0] == (uint8_t)initial_address && Rx_Buf[1] == 0x06 && Rx_Buf[2] == 0x00 && Rx_Buf[3] == 0x02 && Rx_Buf[4] == 0x00){
			updatePbStatus(Rx_Buf[5]);
			Write_Information(ADDR_FLASH_PAGE_104, Rx_Buf, 5);  // 写入Flash
			uint8_t data_to_send[] = {(uint8_t)initial_address, 0x06, 0x02, 0x00, Rx_Buf[5], 0x00, 0x00};
			uint16_t CRC_value = calculate_crc(data_to_send, 5);
			data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
			data_to_send[6] = (uint8_t)(CRC_value >> 8);
			HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 7);
			memset(Rx_Buf, 0, sizeof(Rx_Buf)); 
			memset(USART2_RX_BUF, 0, Uart2_BUF_SIZE); 
			Rx_Flag = 0;	
			HAL_UART_AbortReceive(&huart1);
			HAL_UART_Receive_DMA(&huart1, USART2_RX_BUF, Uart2_BUF_SIZE); 
		} 
		else if(Rx_Buf[0] == (uint8_t)initial_address && Rx_Buf[1] == 0x06 && Rx_Buf[2] == 0x00 && Rx_Buf[3] == 0x0A && Rx_Buf[4] == 0x00 && Rx_Buf[5] == 0x01){
			Write_Information(ADDR_FLASH_PAGE_124, Rx_Buf, 0);
			uint16_t now_calibrationValue = Adc2_CalibrationValue;
			uint8_t data_to_send[] = {(uint8_t)initial_address, 0x06, 0x02, (uint8_t)((now_calibrationValue >> 8) & 0xFF), (uint8_t)(now_calibrationValue & 0xFF), 0x00, 0x00};
			uint16_t CRC_value = calculate_crc(data_to_send, 5);
			data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
			data_to_send[6] = (uint8_t)(CRC_value >> 8);
			HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 7);
			memset(Rx_Buf, 0, sizeof(Rx_Buf)); 
			memset(USART2_RX_BUF, 0, Uart2_BUF_SIZE); 
			Rx_Flag = 0;	
			HAL_UART_AbortReceive(&huart1);
			HAL_UART_Receive_DMA(&huart1, USART2_RX_BUF, Uart2_BUF_SIZE); 
		}    
				
		else if(Rx_Buf[0] == (uint8_t)initial_address && Rx_Buf[1] == 0x06 && Rx_Buf[2] == 0x00 && Rx_Buf[3] == 0x0B && Rx_Buf[4] == 0x00 && Rx_Buf[5] == 0x01){
			Write_Information(ADDR_FLASH_PAGE_134, Rx_Buf, 0);
			uint16_t now_calibrationValue = Adc3_CalibrationValue;
			uint8_t data_to_send[] = {(uint8_t)initial_address, 0x06, 0x02, (uint8_t)((now_calibrationValue >> 8) & 0xFF), (uint8_t)(now_calibrationValue & 0xFF), 0x00, 0x00};
			uint16_t CRC_value = calculate_crc(data_to_send, 5);
			data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
			data_to_send[6] = (uint8_t)(CRC_value >> 8);
			HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 7);
			memset(Rx_Buf, 0, sizeof(Rx_Buf)); 
			memset(USART2_RX_BUF, 0, Uart2_BUF_SIZE); 
			Rx_Flag = 0;	
			HAL_UART_AbortReceive(&huart1);
			HAL_UART_Receive_DMA(&huart1, USART2_RX_BUF, Uart2_BUF_SIZE); 
		}   
		
		else if(Rx_Buf[0] == (uint8_t)initial_address && Rx_Buf[1] == 0x06 && Rx_Buf[2] == 0x00 && Rx_Buf[3] == 0x0C && Rx_Buf[4] == 0x00){
			update_baudrate(Rx_Buf[5]);
			Write_Information(ADDR_FLASH_PAGE_114, Rx_Buf, 5);  // 写入Flash
			uint8_t data_to_send[] = {(uint8_t)initial_address, 0x06, 0x00, 0x0C, 0x00, Rx_Buf[5], 0x00, 0x00};
			uint16_t CRC_value = calculate_crc(data_to_send, 6);
			data_to_send[6] = (uint8_t)(CRC_value & 0xFF);
			data_to_send[7] = (uint8_t)(CRC_value >> 8);
			HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 8, 8);
			memset(Rx_Buf, 0, sizeof(Rx_Buf)); 
			memset(USART2_RX_BUF, 0, Uart2_BUF_SIZE); 
			Rx_Flag = 0;	
			HAL_UART_AbortReceive(&huart1);
			HAL_UART_Receive_DMA(&huart1, USART2_RX_BUF, Uart2_BUF_SIZE); 
		}  
		
		else if(Rx_Buf[0] == (uint8_t)initial_address && Rx_Buf[1] == 0x06 && Rx_Buf[2] == 0x00 && Rx_Buf[3] == 0xAA && Rx_Buf[4] == 0x00){
			Write_Information(ADDR_FLASH_PAGE_94, Rx_Buf, 5);  // 写入Flash
			uint8_t data_to_send[] = {(uint8_t)initial_address, 0x06, 0x02, 0x00, (uint8_t)initial_address, 0x00, 0x00};
			uint16_t CRC_value = calculate_crc(data_to_send, 5);
			data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
			data_to_send[6] = (uint8_t)(CRC_value >> 8);
			HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 7);
			memset(Rx_Buf, 0, sizeof(Rx_Buf));
			memset(USART2_RX_BUF, 0, Uart2_BUF_SIZE); 
			Rx_Flag = 0;	
			HAL_UART_AbortReceive(&huart1);
			HAL_UART_Receive_DMA(&huart1, USART2_RX_BUF, Uart2_BUF_SIZE); 
		}   

		else if(Rx_Buf[0] == (uint8_t)initial_address && Rx_Buf[1] == 0x06 && Rx_Buf[2] == 0x00 && Rx_Buf[3] == 0xCC && Rx_Buf[4] == 0xA5 && Rx_Buf[5] == 0x5A){
			Rx_Flag = 0; // 将标志位重新置为0
			memset(Rx_Buf,0,sizeof(Rx_Buf));
			memset(USART2_RX_BUF, 0, Uart2_BUF_SIZE); 
			HAL_NVIC_SystemReset();
		}	
		else {
			memset(Rx_Buf, 0, sizeof(Rx_Buf));
			memset(USART2_RX_BUF, 0, Uart2_BUF_SIZE);  
			Rx_Flag = 0;	
			HAL_UART_AbortReceive(&huart1);
			HAL_UART_Receive_DMA(&huart1, USART2_RX_BUF, Uart2_BUF_SIZE); 
		}
	}
}
/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
