/* 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 "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 */
int fputc(int ch, FILE *f)
{
    uint8_t temp[10] = {ch};
    HAL_UART_Transmit(&huart1, temp,10, 50);
    HAL_UART_Transmit(&huart3, temp,10, 50);
    return ch;
}
/* USER CODE END PD */

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

/* USER CODE END PM */

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

/* USER CODE BEGIN PV */
uint8_t data_to_send[50];
uint16_t CRC_value = 0;
uint16_t initial_address = 0x00C1;
uint32_t System_version  = 0x23051600;
uint16_t BaudrateValue = 0x0000;
uint16_t Adc2_CalibrationValue ;
uint16_t Adc3_CalibrationValue ;
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */

/* 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 */

  /* 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_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_from_flash(ADDR_FLASH_PAGE_61); 
  updatePbStatus(read_flash_16(ADDR_FLASH_PAGE_62));
  update_baudrate(read_flash_16(ADDR_FLASH_PAGE_63));
  AdcCalibration_init();
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
#if Modbus
		if(flag == 1 && (calculate_crc(receive_buffer,6) == (receive_buffer[7] << 8 | receive_buffer[6]))) {	
			
			if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x03 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x01 && receive_buffer[4] == 0x00 && receive_buffer[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_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 15);
					if(status == HAL_OK) {
							memset(receive_buffer, 0, sizeof(receive_buffer)); 
							flag = 0;
					} else {
							memset(receive_buffer, 0, sizeof(receive_buffer)); 
							__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE);
							__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE);
					}
					HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
			}

			else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x03 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x02 && receive_buffer[4] == 0x00 && receive_buffer[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_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 15);
					if(status == HAL_OK) {
							memset(receive_buffer, 0, sizeof(receive_buffer)); 
							flag = 0;
					} else {
							memset(receive_buffer, 0, sizeof(receive_buffer)); 
							__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE);
							__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE);
					}
					HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
			}
					
			else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x03 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x03 && receive_buffer[4] == 0x00 && receive_buffer[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_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 15);
					if(status == HAL_OK) {
							memset(receive_buffer, 0, sizeof(receive_buffer)); 
							flag = 0;
					} else {
							memset(receive_buffer, 0, sizeof(receive_buffer)); 
							__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE);
							__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE);
					}
					HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
			}
					
			else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x03 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x04 && receive_buffer[4] == 0x00 && receive_buffer[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_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 15);
					if(status == HAL_OK) {
							memset(receive_buffer, 0, sizeof(receive_buffer)); 
							flag = 0;
					} else {
							memset(receive_buffer, 0, sizeof(receive_buffer)); 
							__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE);
							__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE);
					}
					HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
			}
					
			else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x03 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x05 && receive_buffer[4] == 0x00 && receive_buffer[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, 15);
					if(status == HAL_OK) {
							memset(receive_buffer, 0, sizeof(receive_buffer)); 
							flag = 0;
					} else {
							memset(receive_buffer, 0, sizeof(receive_buffer)); 
							__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE);
							__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE);
					}
					HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
			}
			
			else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x03 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0xBB && receive_buffer[4] == 0x00 && receive_buffer[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, 15);
					if(status == HAL_OK) {
							memset(receive_buffer, 0, sizeof(receive_buffer)); 
							flag = 0;
					} else {
							memset(receive_buffer, 0, sizeof(receive_buffer)); 
							__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE);
							__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE);
					}
					HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
			}

			else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x03 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x01 && receive_buffer[4] == 0x00 && receive_buffer[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, 15);
					if(status == HAL_OK) {
							memset(receive_buffer, 0, sizeof(receive_buffer)); 
							flag = 0;
					} else {
							memset(receive_buffer, 0, sizeof(receive_buffer)); 
							__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE);
							__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE);
					}
					HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
			}
			
			else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x03 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x01 && receive_buffer[4] == 0x00 && receive_buffer[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, 15);
					if(status == HAL_OK) {
							memset(receive_buffer, 0, sizeof(receive_buffer)); 
							flag = 0;
					} else {
							memset(receive_buffer, 0, sizeof(receive_buffer)); 
							__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE);
							__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE);
					}
					HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
			}

			else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x03 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x01 && receive_buffer[4] == 0x00 && receive_buffer[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, 15);
					if(status == HAL_OK) {
							memset(receive_buffer, 0, sizeof(receive_buffer)); 
							flag = 0;
					} else {
							memset(receive_buffer, 0, sizeof(receive_buffer)); 
							__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE);
							__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE);
					}
					HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
			}
					
			else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x03 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x01 && receive_buffer[4] == 0x00 && receive_buffer[5] == 0x05){
					uint8_t data_to_send[] = {(uint8_t)initial_address, 0x03, 0x08, 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, 15);
					if(status == HAL_OK) {
							memset(receive_buffer, 0, sizeof(receive_buffer)); 
							flag = 0;
					} else {
							memset(receive_buffer, 0, sizeof(receive_buffer)); 
							__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE);
							__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE);
					}
					HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
			}  
			
			else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x03 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x02 && receive_buffer[4] == 0x00 && receive_buffer[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, 15);
					if(status == HAL_OK) {
							memset(receive_buffer, 0, sizeof(receive_buffer)); 
							flag = 0;
					} else {
							memset(receive_buffer, 0, sizeof(receive_buffer)); 
							__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE);
							__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE);
					}
					HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
			}  

			else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x03 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x02 && receive_buffer[4] == 0x00 && receive_buffer[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, 15);
					if(status == HAL_OK) {
							memset(receive_buffer, 0, sizeof(receive_buffer)); 
							flag = 0;
					} else {
							memset(receive_buffer, 0, sizeof(receive_buffer)); 
							__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE);
							__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE);
					}
					HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
			}  
			
			else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x03 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x02 && receive_buffer[4] == 0x00 && receive_buffer[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, 15);
					if(status == HAL_OK) {
							memset(receive_buffer, 0, sizeof(receive_buffer)); 
							flag = 0;
					} else {
							memset(receive_buffer, 0, sizeof(receive_buffer)); 
							__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE);
							__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE);
					}
					HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
			}       
					
			else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x03 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x03 && receive_buffer[4] == 0x00 && receive_buffer[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, 15);
					if(status == HAL_OK) {
							memset(receive_buffer, 0, sizeof(receive_buffer)); 
							flag = 0;
					} else {
							memset(receive_buffer, 0, sizeof(receive_buffer)); 
							__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE);
							__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE);
					}
					HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
			}  
			
			else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x03 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x03 && receive_buffer[4] == 0x00 && receive_buffer[5] == 0x03){
					uint8_t data_to_send[] = {(uint8_t)initial_address, 0x03, 0x06, adc2_byte1, adc2_byte2, adc3_byte1, adc3_byte2, adc1_byte1, adc1_byte1, 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, 15);
					if(status == HAL_OK) {
							memset(receive_buffer, 0, sizeof(receive_buffer)); 
							flag = 0;
					} else {
							memset(receive_buffer, 0, sizeof(receive_buffer)); 
							__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE);
							__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE);
					}
					HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
			} 

			else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x03 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x04 && receive_buffer[4] == 0x00 && receive_buffer[5] == 0x02){
					uint8_t data_to_send[] = {(uint8_t)initial_address, 0x03, 0x04, adc3_byte1, adc3_byte2, adc1_byte1, adc1_byte1, 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, 15);
					if(status == HAL_OK) {
							memset(receive_buffer, 0, sizeof(receive_buffer)); 
							flag = 0;
					} else {
							memset(receive_buffer, 0, sizeof(receive_buffer)); 
							__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE);
							__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE);
					}
					HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
			}   
			
			else if((receive_buffer[0] == (uint8_t)initial_address || receive_buffer[0] == 0xFA) && receive_buffer[1] == 0x03 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0xAA && receive_buffer[4] == 0x00 && receive_buffer[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, 15);
					if(status == HAL_OK) {
							memset(receive_buffer, 0, sizeof(receive_buffer)); 
							flag = 0;
					} else {
							memset(receive_buffer, 0, sizeof(receive_buffer)); 
							__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE);
							__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE);
					}
					HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
			} 
			else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x06 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x02 && receive_buffer[4] == 0x00){
					updatePbStatus(receive_buffer[5]);
					erase_flash(ADDR_FLASH_PAGE_62);
					write_flash_PbStatus(receive_buffer);  // 写入Flash
					uint8_t data_to_send[] = {(uint8_t)initial_address, 0x06, 0x02, 0x00, receive_buffer[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, 15);
					if(status == HAL_OK) {
							memset(receive_buffer, 0, sizeof(receive_buffer)); 
							flag = 0;
					} else {
							memset(receive_buffer, 0, sizeof(receive_buffer)); 
							__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE);
							__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE);
					}
					HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
			} 
			else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x06 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x0A && receive_buffer[4] == 0x00 && receive_buffer[5] == 0x01){
					erase_flash(ADDR_FLASH_PAGE_64);
					write_flash_autoAdc(ADDR_FLASH_PAGE_64);
					uint16_t now_calibrationValue = Adc2_CalibrationValue;
					CoverADC(ADDR_FLASH_PAGE_64);
					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, 15);
					if(status == HAL_OK) {
							memset(receive_buffer, 0, sizeof(receive_buffer)); 
							flag = 0;
					} else {
							memset(receive_buffer, 0, sizeof(receive_buffer)); 
							__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE);
							__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE);
					}
					HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
			}    
					
			else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x06 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x0B && receive_buffer[4] == 0x00 && receive_buffer[5] == 0x01){
					erase_flash(ADDR_FLASH_PAGE_65);
					write_flash_autoAdc(ADDR_FLASH_PAGE_65);
					uint16_t now_calibrationValue = Adc3_CalibrationValue;
					CoverADC(ADDR_FLASH_PAGE_65);
					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, 15);
					if(status == HAL_OK) {
							memset(receive_buffer, 0, sizeof(receive_buffer)); 
							flag = 0;
					} else {
							memset(receive_buffer, 0, sizeof(receive_buffer)); 
							__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE);
							__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE);
					}
					HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
			}   
			
			else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x06 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x0C && receive_buffer[4] == 0x00){
					update_baudrate(receive_buffer[5]);
					erase_flash(ADDR_FLASH_PAGE_63);
					write_flash_Baudrate(receive_buffer);  // 写入Flash
					uint8_t data_to_send[] = {(uint8_t)initial_address, 0x06, 0x00, 0x0C, 0x00, receive_buffer[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, 15);
					if(status == HAL_OK) {
							memset(receive_buffer, 0, sizeof(receive_buffer)); 
							flag = 0;
					} else {
							memset(receive_buffer, 0, sizeof(receive_buffer)); 
							__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE);
							__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE);
					}
					HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
			}  
			else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x06 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0xAA && receive_buffer[4] == 0x00){
					erase_flash(ADDR_FLASH_PAGE_61);
					write_flash_address(receive_buffer);  // 写入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, 15);
					if(status == HAL_OK) {
							memset(receive_buffer, 0, sizeof(receive_buffer)); 
							flag = 0;
					} else {
							memset(receive_buffer, 0, sizeof(receive_buffer)); 
							__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE);
							__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE);
					}
					HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
			}   

			else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x06 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0xCC && receive_buffer[4] == 0xA5 && receive_buffer[5] == 0x5A){
					flag = 0; // 将标志位重新置为0
					memset(receive_buffer,0,sizeof(receive_buffer));
					HAL_NVIC_SystemReset();
			}	
			else {
				memset(receive_buffer, 0, sizeof(receive_buffer)); 
				__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE); // 清除接收溢出标志位
				__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE); // 清除接收中断标志位
				HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
			}
			
    }
		gpioaStatus = GetPaInputStatus();
    gpiobStatus = GetPbOutputStatus();
    GetADCResults(&hadc1);
		__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE); // 清除接收溢出标志位
		__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE); // 清除接收中断标志位
		HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);
		HAL_Delay(1);  
		HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
		HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
		#endif
    /* 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 flash_address){
    uint16_t data = *(volatile uint16_t*)flash_address;     // 从Flash中读取2个字节
    return data;
}

void read_new_address_from_flash(uint32_t flash_address){
		uint16_t init_address = read_flash_16(flash_address);
	  if(init_address == 0xFFFF) {
			initial_address = initial_address;
		} else {
			initial_address = read_flash_16(flash_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_61) {
            initial_address = 0;         // 将initial_address清零
        }
        if(ADDR_FLASH == ADDR_FLASH_PAGE_62) {
            gpiobStatus = 0;           // 将pb_status清零
        }
        if(ADDR_FLASH == ADDR_FLASH_PAGE_64) {
            Adc2_CalibrationValue = 0x0000;  // 清零
        }
        if(ADDR_FLASH == ADDR_FLASH_PAGE_65) {
            Adc3_CalibrationValue = 0x0000;  // 清零
        }
        
        if(ADDR_FLASH == ADDR_FLASH_PAGE_63) {
            BaudrateValue = 0xFFFF;  
        }
    }
}

void write_flash_address(uint8_t* rx_buffer) { //数组位置5进行写入——地址
    uint8_t newAddress = rx_buffer[5];
    HAL_FLASH_Unlock();  // 解锁Flash
    HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, ADDR_FLASH_PAGE_61, newAddress);  // 写入新地址到Flash
    HAL_FLASH_Lock();  // 上锁Flash
    if(status == HAL_OK){
        initial_address = newAddress;  // 将新地址赋值给initial_address
    }
}

void write_flash_PbStatus(uint8_t* rx_buffer){ //数组位置5进行写入——PB
    uint8_t newStatus = rx_buffer[5];
    HAL_FLASH_Unlock();  // 解锁Flash
    HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, ADDR_FLASH_PAGE_62, newStatus);  // 写入PbStatus到Flash
    HAL_FLASH_Lock();  // 上锁Flash
    if(status == HAL_OK){
        gpiobStatus = newStatus;  // 将新状态赋值给pb_status
    }
}
void write_flash_autoAdc(uint32_t ADDR_FLASH){
    uint16_t Standard_value;
    Standard_value = (0.6/(3.3/4096));
    uint16_t newValue = 0;
    if(ADDR_FLASH == ADDR_FLASH_PAGE_64) {
        if(adc2_rawValue >= Standard_value) {
            newValue = adc2_rawValue - Standard_value;
        } else {
            newValue = Standard_value - adc2_rawValue;
            newValue |= 0x8000;
        }
        HAL_FLASH_Unlock();  // 解锁Flash
        HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, ADDR_FLASH, newValue); 
        HAL_FLASH_Lock();  // 上锁Flash
        if(status == HAL_OK){
            Adc2_CalibrationValue = newValue & 0x7FFF;
        }
    } 
    if(ADDR_FLASH == ADDR_FLASH_PAGE_65) {
        if(adc3_rawValue >= Standard_value) {
            newValue = adc3_rawValue - Standard_value;
        } else {
            newValue = Standard_value - adc3_rawValue;
            newValue |= 0x8000;
        }
        HAL_FLASH_Unlock(); // 解锁Flash
        HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, ADDR_FLASH, newValue); 
        HAL_FLASH_Lock(); // 上锁Flash
        if(status == HAL_OK){
            Adc3_CalibrationValue = newValue & 0x7FFF;
        }
    }
}

void CoverADC(uint32_t ADDR) {
    uint16_t Overwrite_Value = read_flash_16(ADDR);
    if (ADDR == ADDR_FLASH_PAGE_64) {
        Value_old_addr2 = Overwrite_Value;
    }
    if (ADDR == ADDR_FLASH_PAGE_65) {
        Value_old_addr3 = Overwrite_Value;
    }
}

void write_flash_Baudrate(uint8_t* rx_buffer){ //数组位置5进行写入——Baudrate
    uint8_t newStatus = rx_buffer[5];
    HAL_FLASH_Unlock();  // 解锁Flash
    HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, ADDR_FLASH_PAGE_63, newStatus);  // 写入PbStatus到Flash
    HAL_FLASH_Lock();  // 上锁Flash
    if(status == HAL_OK){
        BaudrateValue = newStatus;  // 将新状态赋值给pb_status
    }
}
/* 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 */
