/* 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 "cmsis_os.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 rebootTip;
uint8_t  data_to_send[50];
uint16_t CRC_value 		 = 0;
uint16_t initial_address = 0x00C1;
uint16_t BaudrateValue 	 = 0x0000;
uint32_t System_version  = 0x23080700;
uint8_t  bufMain[128] 	= {0};



/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
void MX_FREERTOS_Init(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_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);
//更新标定数据  
  AdcCalibration_init();
  updateiapMd5(StartMode_Addr);
  update_iaplength(LENGTH_ADDR);
  read_new_address(addr_FLASH_ADDR); 
  update_sysVersion(StartMode_Addr+14);
  updatePbStatus(read_flash_16(pbstatus_FLASH_ADDR));
  update_baudrate(read_flash_16(baudrate_FLASH_ADDR));
//更新结束  
#if	IR_ROM1 == 0x08000000	
   	Start_BootLoader();
#else	
	SCB->VTOR = 0x08020000;
	__set_PRIMASK(0);
#endif	
  

//  rebootTip = sprintf((char*)bufMain, "reboot ok");
//  HAL_UART_Transmit(&huart1, bufMain, rebootTip, 300);
  HAL_UART_Receive_DMA(&huart1,USART_PUB_RX,RX_BUF_SIZE); 
  __HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE); 
  /* USER CODE END 2 */

  /* Call init function for freertos objects (in freertos.c) */
  MX_FREERTOS_Init();

  /* Start scheduler */
  osKernelStart();
  /* We should never get here as control is now taken by the scheduler */
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  { 


	
    /* 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_MUL2;
  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_DIV2;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  {
    Error_Handler();
  }
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
  PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV4;
  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;
}

uint32_t read_flash_32(uint32_t addr){
    uint32_t data = *(volatile uint32_t*)addr;
    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_ADDR) {
            initial_address = 0;         // 将initial_address清零
        }
        else if(ADDR_FLASH == pbstatus_FLASH_ADDR) {
            gpiobStatus = 0;             // 将pb_status清零
        }
        else if(ADDR_FLASH == baudrate_FLASH_ADDR) {
            BaudrateValue = 0xFFFF;  
        }
        else if(ADDR_FLASH == ADC1_FLASH_ADDR) {
            Adc2_CalibrationValue = 0x0000;  // 清零
        }
        else if(ADDR_FLASH == ADC2_FLASH_ADDR) {
            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_ADDR) {  // 写入地址到Flash
        newValue = rx_buffer[buffer_index];
        HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, addr_FLASH_ADDR, newValue);
        if (status == HAL_OK) {
            initial_address = newValue;  // 将新地址赋值给initial_address
        }
    } 
	else if (addr == pbstatus_FLASH_ADDR) {  // 写入PB状态到Flash
        newValue = rx_buffer[buffer_index];
        HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, pbstatus_FLASH_ADDR, newValue);
        if (status == HAL_OK) {
            gpiobStatus = newValue;  // 将新状态赋值给gpiobStatus
        }
    } 
	else if (addr == baudrate_FLASH_ADDR) {  // 写入波特率到Flash
        newValue = rx_buffer[buffer_index];
        HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, baudrate_FLASH_ADDR, newValue);
        if (status == HAL_OK) {
            BaudrateValue = newValue;  // 将新状态赋值给BaudrateValue
        }
    } 
	else if (addr == ADC1_FLASH_ADDR || addr == ADC2_FLASH_ADDR) {  // 写入ADC校准值到Flash
        uint16_t Standard_value = (0.6 / (3.3 / 4096));
        if (addr == ADC1_FLASH_ADDR) {
            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, ADC1_FLASH_ADDR, newValue);
            if (status == HAL_OK) {
                Adc2_CalibrationValue = newValue & 0x7FFF;
							  Value_old_addr2 = newValue;
            }
        } 
	else if (addr == ADC2_FLASH_ADDR) {
            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, ADC2_FLASH_ADDR, newValue);
            if (status == HAL_OK) {
                Adc3_CalibrationValue = newValue & 0x7FFF;
							  Value_old_addr3 = newValue;
            }
        }
    }
	HAL_FLASH_Lock();  // 上锁Flash
}



void clear_buf(void){
	memset(USART_MODBUS_RX, 0, sizeof(USART_MODBUS_RX)); 
	memset(USART_PUB_RX, 0, RX_BUF_SIZE); 
	USART_MODBUS_FLAG = 0;	
	USART_IAP_FLAG = 0;
}

void re_receive(void){
	HAL_UART_AbortReceive(&huart1);
	HAL_UART_Receive_DMA(&huart1, USART_PUB_RX, RX_BUF_SIZE); 
}


void receive_modbus_7(uint16_t addr, uint8_t funcode, uint8_t byteNum, uint16_t byte, int byteSize){
	uint8_t data_to_send[] = {(uint8_t)addr, funcode, byteNum, (uint8_t)(byte >> 8), (uint8_t)(byte & 0xFF), 0x00, 0x00};
	uint16_t CRC_value = calculate_crc(data_to_send, (byteSize - 2));
	data_to_send[(byteSize - 2)] = (uint8_t)(CRC_value & 0xFF);
	data_to_send[(byteSize - 1)] = (uint8_t)(CRC_value >> 8);
	HAL_UART_Transmit(&huart1, data_to_send, byteSize, byteSize);
	clear_buf();
	re_receive();
}

void receive_modbus_8(uint16_t addr, uint8_t funcode, uint16_t startaddr, uint16_t reg){
	
	uint8_t data_to_send[] = {(uint8_t)addr, funcode, (uint8_t)(startaddr >> 8), (uint8_t)(startaddr & 0xFF), \
	(uint8_t)(reg >> 8), (uint8_t)(reg & 0xFF), 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_UART_Transmit(&huart1, data_to_send, 8, 7);
	clear_buf();
	re_receive();
}

void receive_modbus_9_1(uint16_t addr, uint8_t funcode, uint16_t startaddr, uint16_t reg, uint8_t byteNum){
	
	uint8_t data_to_send[] = {(uint8_t)addr, funcode, (uint8_t)(startaddr >> 8), (uint8_t)(startaddr & 0xFF), \
	(uint8_t)(reg >> 8), (uint8_t)(reg & 0xFF), byteNum, 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_UART_Transmit(&huart1, data_to_send, 9, 9);

	clear_buf();
	re_receive();
}

void receive_modbus_9(uint16_t addr, uint8_t funcode, uint8_t byteNum, uint16_t byte1, uint16_t byte2, int byteSize){
	uint8_t data_to_send[] = {(uint8_t)addr, funcode, byteNum, (uint8_t)(byte1 >> 8), (uint8_t)(byte1 & 0xFF), (uint8_t)(byte2 >> 8), \
	(uint8_t)(byte2 & 0xFF), 0x00, 0x00};
	uint16_t CRC_value = calculate_crc(data_to_send, (byteSize - 2));
	data_to_send[(byteSize - 2)] = (uint8_t)(CRC_value & 0xFF);
	data_to_send[(byteSize - 1)] = (uint8_t)(CRC_value >> 8);
	HAL_UART_Transmit(&huart1, data_to_send, byteSize, (byteSize - 1));
	clear_buf();
	re_receive();
}

void receive_modbus_11(uint16_t addr, uint8_t funcode, uint8_t byteNum, uint16_t byte1, uint16_t byte2, uint16_t byte3, int byteSize){
	uint8_t data_to_send[] = {(uint8_t)addr, funcode, byteNum, (uint8_t)(byte1 >> 8), (uint8_t)(byte1 & 0xFF), (uint8_t)(byte2 >> 8), \
	(uint8_t)(byte2 & 0xFF), (uint8_t)(byte3 >> 8), (uint8_t)(byte3 & 0xFF), 0x00, 0x00};
	uint16_t CRC_value = calculate_crc(data_to_send, (byteSize - 2));
	data_to_send[(byteSize - 2)] = (uint8_t)(CRC_value & 0xFF);
	data_to_send[(byteSize - 1)] = (uint8_t)(CRC_value >> 8);
	HAL_UART_Transmit(&huart1, data_to_send, byteSize, (byteSize - 1));
	clear_buf();
	re_receive();
}

void receive_modbus_13(uint16_t addr, uint8_t funcode, uint8_t byteNum, uint16_t byte1, uint16_t byte2, uint16_t byte3, uint16_t byte4, int byteSize){
	uint8_t data_to_send[] = {(uint8_t)addr, funcode, byteNum, (uint8_t)(byte1 >> 8), (uint8_t)(byte1 & 0xFF), (uint8_t)(byte2 >> 8), \
		(uint8_t)(byte2 & 0xFF), (uint8_t)(byte3 >> 8), (uint8_t)(byte3 & 0xFF), (uint8_t)(byte4 >> 8), (uint8_t)(byte4 & 0xFF), 0x00, 0x00};
	uint16_t CRC_value = calculate_crc(data_to_send, (byteSize - 2));
	data_to_send[(byteSize - 2)] = (uint8_t)(CRC_value & 0xFF);
	data_to_send[(byteSize - 1)] = (uint8_t)(CRC_value >> 8);
	HAL_UART_Transmit(&huart1, data_to_send, byteSize, (byteSize - 1));
	clear_buf();
	re_receive();
}

void receive_modbus_15(uint16_t addr, uint8_t funcode, uint8_t byteNum, uint16_t byte1, uint16_t byte2, uint16_t byte3, uint16_t byte4, uint16_t byte5, int byteSize){
	uint8_t data_to_send[] = {(uint8_t)addr, funcode, byteNum, (uint8_t)(byte1 >> 8), (uint8_t)(byte1 & 0xFF), (uint8_t)(byte2 >> 8), \
		(uint8_t)(byte2 & 0xFF), (uint8_t)(byte3 >> 8), (uint8_t)(byte3 & 0xFF), (uint8_t)(byte4 >> 8), (uint8_t)(byte4 & 0xFF), (uint8_t)(byte5 >> 8),\
		(uint8_t)(byte5 & 0xFF), 0x00, 0x00};
	uint16_t CRC_value = calculate_crc(data_to_send, (byteSize - 2));
	data_to_send[(byteSize - 2)] = (uint8_t)(CRC_value & 0xFF);
	data_to_send[(byteSize - 1)] = (uint8_t)(CRC_value >> 8);
	HAL_UART_Transmit(&huart1, data_to_send, byteSize, (byteSize - 1));
	clear_buf();
	re_receive();
}



void ModBus(void) {
	
	if (USART_MODBUS_FLAG == 1 && (calculate_crc(USART_MODBUS_RX,6) == (USART_MODBUS_RX[7] << 8 | USART_MODBUS_RX[6])) && USART_PUB_LENGTH < 10) {		
		if(USART_MODBUS_RX[0] == (uint8_t)initial_address && USART_MODBUS_RX[1] == 0x03 && USART_MODBUS_RX[2] == 0x00 \
			&& USART_MODBUS_RX[3] == 0x01 && USART_MODBUS_RX[4] == 0x00 && USART_MODBUS_RX[5] == 0x01){
				
			receive_modbus_7(initial_address, 0x03, 0x02, gpioaStatus, 7);
		}

		else if(USART_MODBUS_RX[0] == (uint8_t)initial_address && USART_MODBUS_RX[1] == 0x03 && USART_MODBUS_RX[2] == 0x00 \
			&& USART_MODBUS_RX[3] == 0x02 && USART_MODBUS_RX[4] == 0x00 && USART_MODBUS_RX[5] == 0x01){
				
			receive_modbus_7(initial_address, 0x03, 0x02, gpiobStatus, 7);
				
		}
				
		else if(USART_MODBUS_RX[0] == (uint8_t)initial_address && USART_MODBUS_RX[1] == 0x03 && USART_MODBUS_RX[2] == 0x00 \
			&& USART_MODBUS_RX[3] == 0x03 && USART_MODBUS_RX[4] == 0x00 && USART_MODBUS_RX[5] == 0x01){	
			
			receive_modbus_7(initial_address, 0x03, 0x02, adc2_filtered, 7);
		
		}
				
		else if(USART_MODBUS_RX[0] == (uint8_t)initial_address && USART_MODBUS_RX[1] == 0x03 && USART_MODBUS_RX[2] == 0x00 \
			&& USART_MODBUS_RX[3] == 0x04 && USART_MODBUS_RX[4] == 0x00 && USART_MODBUS_RX[5] == 0x01){
			
			receive_modbus_7(initial_address, 0x03, 0x02, adc3_filtered, 7);
		}
				
		else if(USART_MODBUS_RX[0] == (uint8_t)initial_address && USART_MODBUS_RX[1] == 0x03 && USART_MODBUS_RX[2] == 0x00 \
			&& USART_MODBUS_RX[3] == 0x05 && USART_MODBUS_RX[4] == 0x00 && USART_MODBUS_RX[5] == 0x01){

			receive_modbus_7(initial_address, 0x03, 0x02, adc1_filtered, 7);
		}

		else if(USART_MODBUS_RX[0] == (uint8_t)initial_address && USART_MODBUS_RX[1] == 0x03 && USART_MODBUS_RX[2] == 0x00 \
			&& USART_MODBUS_RX[3] == 0x01 && USART_MODBUS_RX[4] == 0x00 && USART_MODBUS_RX[5] == 0x02){
			
			receive_modbus_9(initial_address, 0x03, 0x04, gpioaStatus, gpiobStatus, 9);
		}
		
		else if(USART_MODBUS_RX[0] == (uint8_t)initial_address && USART_MODBUS_RX[1] == 0x03 && USART_MODBUS_RX[2] == 0x00 \
			&& USART_MODBUS_RX[3] == 0x01 && USART_MODBUS_RX[4] == 0x00 && USART_MODBUS_RX[5] == 0x03){
			
			receive_modbus_11(initial_address, 0x03, 0x06, gpioaStatus, gpiobStatus, adc2_filtered, 11);

		}

		else if(USART_MODBUS_RX[0] == (uint8_t)initial_address && USART_MODBUS_RX[1] == 0x03 && USART_MODBUS_RX[2] == 0x00 \
			&& USART_MODBUS_RX[3] == 0x01 && USART_MODBUS_RX[4] == 0x00 && USART_MODBUS_RX[5] == 0x04){
				
			receive_modbus_13(initial_address, 0x03, 0x08, gpioaStatus, gpiobStatus, adc2_filtered, adc3_filtered, 13);

		}
				
		else if(USART_MODBUS_RX[0] == (uint8_t)initial_address && USART_MODBUS_RX[1] == 0x03 && USART_MODBUS_RX[2] == 0x00 \
			&& USART_MODBUS_RX[3] == 0x01 && USART_MODBUS_RX[4] == 0x00 && USART_MODBUS_RX[5] == 0x05){
				
			receive_modbus_15(initial_address, 0x03, 0x0A, gpioaStatus, gpiobStatus, adc2_filtered, adc3_filtered, adc1_filtered, 15);

		}  
		
		else if(USART_MODBUS_RX[0] == (uint8_t)initial_address && USART_MODBUS_RX[1] == 0x03 && USART_MODBUS_RX[2] == 0x00 \
			&& USART_MODBUS_RX[3] == 0x02 && USART_MODBUS_RX[4] == 0x00 && USART_MODBUS_RX[5] == 0x02){
			
			receive_modbus_9(initial_address, 0x03, 0x04, gpiobStatus, adc2_filtered, 9);
		}  

		else if(USART_MODBUS_RX[0] == (uint8_t)initial_address && USART_MODBUS_RX[1] == 0x03 && USART_MODBUS_RX[2] == 0x00 \
			&& USART_MODBUS_RX[3] == 0x02 && USART_MODBUS_RX[4] == 0x00 && USART_MODBUS_RX[5] == 0x03){
			
			receive_modbus_11(initial_address, 0x03, 0x06, gpiobStatus, adc2_filtered, adc3_filtered, 11);
		}  
		
		else if(USART_MODBUS_RX[0] == (uint8_t)initial_address && USART_MODBUS_RX[1] == 0x03 && USART_MODBUS_RX[2] == 0x00 \
			&& USART_MODBUS_RX[3] == 0x02 && USART_MODBUS_RX[4] == 0x00 && USART_MODBUS_RX[5] == 0x04){
			
			receive_modbus_13(initial_address, 0x03, 0x08, gpiobStatus, adc2_filtered, adc3_filtered, adc1_filtered, 13);
		}       
				
		else if(USART_MODBUS_RX[0] == (uint8_t)initial_address && USART_MODBUS_RX[1] == 0x03 && USART_MODBUS_RX[2] == 0x00 \
			&& USART_MODBUS_RX[3] == 0x03 && USART_MODBUS_RX[4] == 0x00 && USART_MODBUS_RX[5] == 0x02){
				
			receive_modbus_9(initial_address, 0x03, 0x04, adc2_filtered, adc3_filtered, 9);
				
		}  
		
		else if(USART_MODBUS_RX[0] == (uint8_t)initial_address && USART_MODBUS_RX[1] == 0x03 && USART_MODBUS_RX[2] == 0x00 \
			&& USART_MODBUS_RX[3] == 0x03 && USART_MODBUS_RX[4] == 0x00 && USART_MODBUS_RX[5] == 0x03){
			
			receive_modbus_11(initial_address, 0x03, 0x06, adc2_filtered, adc3_filtered, adc1_filtered, 11);
		} 

		else if(USART_MODBUS_RX[0] == (uint8_t)initial_address && USART_MODBUS_RX[1] == 0x03 && USART_MODBUS_RX[2] == 0x00 \
			&& USART_MODBUS_RX[3] == 0x04 && USART_MODBUS_RX[4] == 0x00 && USART_MODBUS_RX[5] == 0x02){
			
			receive_modbus_9(initial_address, 0x03, 0x04, adc3_filtered, adc1_filtered, 9);
		}   
		
		else if(USART_MODBUS_RX[0] == 0xFA && USART_MODBUS_RX[1] == 0x03 && USART_MODBUS_RX[2] == 0x00 && USART_MODBUS_RX[3] == 0xAA \
			&& USART_MODBUS_RX[4] == 0x00 && USART_MODBUS_RX[5] == 0x01){
			
			receive_modbus_7(0xFA, 0x03, 0x02, initial_address, 7);
		} 
		
		else if(USART_MODBUS_RX[0] == (uint8_t)initial_address && USART_MODBUS_RX[1] == 0x03 && USART_MODBUS_RX[2] == 0x50 \
			&& USART_MODBUS_RX[3] == 0x00 && USART_MODBUS_RX[4] == 0x00 && USART_MODBUS_RX[5] == 0x02){

			receive_modbus_9(initial_address, 0x03, 0x04,  ((System_version >> 16) & 0xFFFF), (System_version & 0xFFFF), 9);
		}

		else if(USART_MODBUS_RX[0] == (uint8_t)initial_address && USART_MODBUS_RX[1] == 0x06 && USART_MODBUS_RX[2] == 0x00 \
			&& USART_MODBUS_RX[3] == 0x02 && USART_MODBUS_RX[4] == 0x00){
			
			updatePbStatus(USART_MODBUS_RX[5]);
			Write_Information(pbstatus_FLASH_ADDR, USART_MODBUS_RX, 5); 
			receive_modbus_8(initial_address, USART_MODBUS_RX[1], ((uint16_t)USART_MODBUS_RX[2] << 8) | USART_MODBUS_RX[3], \
				((uint16_t)USART_MODBUS_RX[4] << 8) | USART_MODBUS_RX[5]);
		} 
		
		else if(USART_MODBUS_RX[0] == (uint8_t)initial_address && USART_MODBUS_RX[1] == 0x06 && USART_MODBUS_RX[2] == 0x00 \
			&& USART_MODBUS_RX[3] == 0x0A && USART_MODBUS_RX[4] == 0x00 && USART_MODBUS_RX[5] == 0x01){
			Write_Information(ADC1_FLASH_ADDR, USART_MODBUS_RX, 0);
			receive_modbus_8(initial_address, USART_MODBUS_RX[1], ((uint16_t)USART_MODBUS_RX[2] << 8) | USART_MODBUS_RX[3], \
				((uint16_t)USART_MODBUS_RX[4] << 8) | USART_MODBUS_RX[5]);
		}    
				
		else if(USART_MODBUS_RX[0] == (uint8_t)initial_address && USART_MODBUS_RX[1] == 0x06 && USART_MODBUS_RX[2] == 0x00 \
			&& USART_MODBUS_RX[3] == 0x0B && USART_MODBUS_RX[4] == 0x00 && USART_MODBUS_RX[5] == 0x01){
			Write_Information(ADC2_FLASH_ADDR, USART_MODBUS_RX, 0);
			receive_modbus_8(initial_address, USART_MODBUS_RX[1], ((uint16_t)USART_MODBUS_RX[2] << 8) | USART_MODBUS_RX[3], \
				((uint16_t)USART_MODBUS_RX[4] << 8) | USART_MODBUS_RX[5]);
		}   
		
		else if(USART_MODBUS_RX[0] == (uint8_t)initial_address && USART_MODBUS_RX[1] == 0x06 && USART_MODBUS_RX[2] == 0x00 \
			&& USART_MODBUS_RX[3] == 0x0C && USART_MODBUS_RX[4] == 0x00){
			update_baudrate(USART_MODBUS_RX[5]);
			Write_Information(baudrate_FLASH_ADDR, USART_MODBUS_RX, 5); 
			receive_modbus_8(initial_address, USART_MODBUS_RX[1], ((uint16_t)USART_MODBUS_RX[2] << 8) | USART_MODBUS_RX[3], \
				((uint16_t)USART_MODBUS_RX[4] << 8) | USART_MODBUS_RX[5]);
		}  
		
		else if(USART_MODBUS_RX[0] == (uint8_t)initial_address && USART_MODBUS_RX[1] == 0x06 && USART_MODBUS_RX[2] == 0x00 \
			&& USART_MODBUS_RX[3] == 0xAA && USART_MODBUS_RX[4] == 0x00){
			Write_Information(addr_FLASH_ADDR, USART_MODBUS_RX, 5); 
			receive_modbus_8(initial_address, USART_MODBUS_RX[1], ((uint16_t)USART_MODBUS_RX[2] << 8) | USART_MODBUS_RX[3], \
				((uint16_t)USART_MODBUS_RX[4] << 8) | USART_MODBUS_RX[5]);
		}   

		else if(USART_MODBUS_RX[0] == (uint8_t)initial_address && USART_MODBUS_RX[1] == 0x06 && USART_MODBUS_RX[2] == 0x00 \
			&& USART_MODBUS_RX[3] == 0xCC && USART_MODBUS_RX[4] == 0xA5 && USART_MODBUS_RX[5] == 0x5A){
			clear_buf();
			HAL_NVIC_SystemReset();
		}	
			
			
		else {
			clear_buf();
			re_receive();
		}
	}

}


/* USER CODE END 4 */

/**
  * @brief  Period elapsed callback in non blocking mode
  * @note   This function is called  when TIM3 interrupt took place, inside
  * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
  * a global variable "uwTick" used as application time base.
  * @param  htim : TIM handle
  * @retval None
  */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
  /* USER CODE BEGIN Callback 0 */

  /* USER CODE END Callback 0 */
  if (htim->Instance == TIM3) {
    HAL_IncTick();
  }
  /* USER CODE BEGIN Callback 1 */

  /* USER CODE END Callback 1 */
}

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