/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.h
  * @brief          : Header for main.c file.
  *                   This file contains the common defines of the application.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __MAIN_H
#define __MAIN_H

#ifdef __cplusplus
extern "C" {
#endif

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

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "string.h"
#include "iwdg.h"
/* USER CODE END Includes */

/* Exported types ------------------------------------------------------------*/
/* USER CODE BEGIN ET */
#define DEBUG_INFO(format, args...) printf("[%s:%d][Info] "format, __FUNCTION__, __LINE__, ##args)
#define DEBUG_WARN(format, args...) printf("[%s:%d][Warn] "format, __FUNCTION__, __LINE__, ##args)
#define DEBUG_ERROR(format, args...) printf("[%s:%d][Error] "format, __FUNCTION__, __LINE__, ##args)
#define ARRAY_SIZE(A)		(sizeof(A) / sizeof(A[0]))
#define MEMORY_LENGTH_CONFIG    0x1000 
#define MEM_REAL_LENGTH			27         //27 two word = 52 word	 
#define MEM_MODELNAME_ADDRESS	100
#define MEM_MODELNAME_LENGTH	4
#define bool char
//#define DEBUG

enum 
{
	Fan1 = 0,
	Fan2,
	Fan3,
	Fan4,
	Fan5,
	Fan6,
	Fan7,
	Fan8
};

enum
{
	AC_L1_GAIN = 0,
	AC_L2_GAIN,
	AC_L3_GAIN,
	DC_SMR1_GAIN,
	DC_SMR2_GAIN
};

enum 
{
	MainRelay1 = 0x10,
	MainRelay2 = 0x11,
	GunRelay = 0x12,
	MainBridge1 = 0x13,	
	MainBridge2 = 0x14,
	MainBridge3 = 0x15,
	MainBridge4 = 0x17,
};

enum
{
	Input_L1_AC_voltage = 0x01,
	Input_L2_AC_voltage,
	Input_L3_AC_voltage,
	
	Output_L1_AC_current = 0x11,
	Output_L2_AC_current,
	Output_L3_AC_current,
	
	Leak_current = 0x21,
	GMI_voltage = 0x31,	
	
	GFD_Resister_Left	= 0x46,
	GFD_Resister_Right	= 0x47,		
	
	Battery1_input_voltage = 0x51,
	Battery2_input_voltage,

	SMR1_output_voltage = 0x61,
	SMR2_output_voltage,
	SMR3_output_voltage,
	SMR4_output_voltage,
	SMR5_output_voltage,
	SMR6_output_voltage,
};

enum 
{
	Parallel_relay_n = 0,
	Parallel_relay_p,
	SMR1_relay_n,
	SMR1_relay_p,
	SMR2_relay_n,
	SMR2_relay_p,
	
	LED1,
	Chdemo_Delay,
	Reserved,
};

typedef union
{
    uint64_t All;
	uint8_t Status[8];
    struct __attribute__ ((packed))
    {
    	uint8_t AC_Contactor	:1;    	
		uint8_t	Precharge1		:1;
		uint8_t	Precharge2		:1;
		uint8_t Reserved_0		:5;		
			
		uint8_t SMR1_relay_n	:1;
		uint8_t SMR1_relay_p	:1;
		uint8_t Reserved_1		:6;

		uint8_t SMR2_relay_n	:1;
		uint8_t SMR2_relay_p	:1;
		uint8_t Reserved_2		:6;

		uint8_t SMR3_relay_n	:1;
		uint8_t SMR3_relay_p	:1;
		uint8_t Reserved_3		:6;   

		uint8_t SMR4_relay_n	:1;
		uint8_t SMR4_relay_p	:1;
		uint8_t Reserved_4		:6;

		uint8_t SMR5_relay_n	:1;
		uint8_t SMR5_relay_p	:1;
		uint8_t Reserved_5		:6;

		uint8_t SMR6_relay_n	:1;
		uint8_t SMR6_relay_p	:1;
		uint8_t Reserved_6		:6;   	

		uint8_t Reserved_7		:8; 	  	
    } flags;
} RLY_STATUS;

typedef struct
{  
	uint8_t AC_Contactor	:1;    	
	uint8_t	Precharge1		:1;
	uint8_t	Precharge2		:1;
	uint8_t Reserved_0		:5;	
	
	uint8_t SMR1_relay_n	:1;
	uint8_t SMR1_relay_p	:1;
	uint8_t Reserved_1		:6;

	uint8_t SMR2_relay_n	:1;
	uint8_t SMR2_relay_p	:1;
	uint8_t Reserved_2		:6;

	uint8_t SMR3_relay_n	:1;
	uint8_t SMR3_relay_p	:1;
	uint8_t Reserved_3		:6;   

	uint8_t SMR4_relay_n	:1;
	uint8_t SMR4_relay_p	:1;
	uint8_t Reserved_4		:6;

	uint8_t SMR5_relay_n	:1;
	uint8_t SMR5_relay_p	:1;
	uint8_t Reserved_5		:6;

	uint8_t SMR6_relay_n	:1;
	uint8_t SMR6_relay_p	:1;
	uint8_t Reserved_6		:6;   		
} RLY_ST;

typedef union
{
    uint64_t All;
    struct __attribute__ ((packed))
    {
    	uint8_t AC_Contactor	:1;    	
		uint8_t	Precharge1		:1;
		uint8_t	Precharge2		:1;
    	uint8_t Reserved_0		:5;
    		
    	uint8_t SMR1_relay_n	:1;
    	uint8_t SMR1_relay_p	:1;
		uint8_t Reserved_1		:6;

		uint8_t SMR2_relay_n	:1;
    	uint8_t SMR2_relay_p	:1;
		uint8_t Reserved_2		:6;

		uint8_t SMR3_relay_n	:1;
    	uint8_t SMR3_relay_p	:1;
		uint8_t Reserved_3		:6;   

		uint8_t SMR4_relay_n	:1;
    	uint8_t SMR4_relay_p	:1;
		uint8_t Reserved_4		:6;

		uint8_t SMR5_relay_n	:1;
    	uint8_t SMR5_relay_p	:1;
		uint8_t Reserved_5		:6;

		uint8_t SMR6_relay_n	:1;
    	uint8_t SMR6_relay_p	:1;
		uint8_t Reserved_6		:6;   	

		uint8_t Reserved_7		:8;      			    	
    } flags;
} RLY_GPIO;

typedef union
{
    uint16_t All;
    struct __attribute__ ((packed))
    {    		    	
    	uint8_t Relay1_Fail		:1;
    	uint8_t Relay2_Fail		:1;
    	uint8_t Relay3_Fail		:1;
    	uint8_t Relay4_Fail		:1;
    	uint8_t Relay5_Fail		:1;
    	uint8_t Relay6_Fail		:1;	
    	uint8_t Relay7_Fail		:1;
    	uint8_t Relay8_Fail		:1;
    	
    	uint8_t Gfd_Alarm_0		:1;
    	uint8_t Gfd_Alarm_1		:1;
    	uint8_t Reserved_1		:6;
    } flags;
} ALARM;

typedef union
{
    uint8_t All;
    struct __attribute__ ((packed))
    {
    	// LSB to begin
    	uint8_t Fan_Enable:1;
    	uint8_t Drv_Up:1;
    	uint8_t Drv_Down:1;
    	uint8_t Reversed:5;
    	// MSB to End
    } flags;
} _GPIO_;

typedef union
{
	uint8_t Mode;
	struct __attribute__ ((packed))
    {
    	// LSB to begin
    	uint8_t SW0:1;
    	uint8_t SW1:1;
		uint8_t SW2:1;
    	uint8_t Reversed:5;
    	// MSB to End
    } flags;
} _DIP_SW_;

typedef struct COEFFICIENT
{
  float     gain_volt;
  float     offset_volt;        
} Coefficient;

typedef struct SF_TEST
{
	struct 
	{
		uint8_t SF_test_status;	
		uint8_t SF_Act;
		uint8_t	SF_State;
		union
		{
			uint64_t value;
			struct
			{
				uint8_t test_item1		:1;
    			uint8_t test_item2		:1;
    			uint8_t test_item3		:1;
    			uint8_t test_item4		:1;
    			uint8_t test_item5		:1;
    			uint8_t test_item6		:1;	
    			uint8_t test_item7		:1;
    			uint8_t test_item8		:1;

				uint8_t test_item9		:1;
    			uint8_t test_item10		:1;
    			uint8_t test_item11		:1;
    			uint8_t test_item12		:1;
    			uint8_t test_item13		:1;
    			uint8_t test_item14		:1;	
    			uint8_t test_item15		:1;
    			uint8_t test_item16		:1;

				uint8_t test_item17		:1;
    			uint8_t test_item18		:1;
    			uint8_t test_item19		:1;
    			uint8_t test_item20		:1;
    			uint8_t test_item21		:1;
    			uint8_t test_item22		:1;	
    			uint8_t test_item23		:1;
    			uint8_t test_item24		:1;

				uint8_t test_item25		:1;
    			uint8_t test_item26		:1;
    			uint8_t test_item27		:1;
    			uint8_t test_item28		:1;
    			uint8_t test_item29		:1;
    			uint8_t test_item30		:1;	
    			uint8_t test_item31		:1;
    			uint8_t test_item32		:1;

				uint8_t test_item33		:1;
    			uint8_t test_item34		:1;
			} items;
		} data;
	} SF_Config;
} SF_test;

// V18 memory structure.
typedef struct MEM
{
    struct
    {
        union
        { 
        	// 0x1000 = 10240 byte , uint32_t = 4byte , array size = 10240/4 = 1000 
            uint32_t value[MEMORY_LENGTH_CONFIG];
               		
            struct                                              
            {   
            	// 16 Byte -> Voltage correction point data(adc & meter) [point][adc & meter], unit: 0.1V                                       
                uint16_t    Correction_Volt[9][2]; 				// [L1-point1~L3-point3][ADC,Set Value]
                uint16_t	DC_Correction_Volt[12][2];			// [SMR1 point1~SMR6 point2][ADC,Set Value]
                uint16_t    GFD_Correction_Resistor[4][2];		// [L-GFD point1~R-GFD point2][ADC,Set Value]
				uint16_t	DCIn_Correction_Volt[2][2];		    // [DC In 2 point][ADC,Set Value]  
                uint32_t	spare[73];							// 

				uint8_t Modelname[16];     						// from 100
            } item;  
        } data;
      
        struct
        {
            uint8_t update:1;                               // Update ram content to external flash request
            uint8_t clear:1;                                // Clear ram & external flash content request
            uint8_t read:1;                                 // Read external flash content to ram request
			uint8_t modelname:1;                            // Update modelname to external flash request
            uint8_t reserved:4;                        
        } op_bits; 
           
    } Module_Config;
}Mem;

struct Detet_Fan_IO
{
	uint32_t Difference;
	uint32_t Frequency;
	uint32_t ubFan_RPM;
	uint8_t	 Rpm_Idx;
	uint16_t Second;
	uint8_t  Fan_Fail;
};

struct Gun_Gfd_Check
{
	uint8_t 	Rfd_State;
	uint8_t		Csu_State;
	uint8_t 	Gfd_Running_Count;
	uint8_t		Gfd_Warning_Count;
	uint8_t 	bResult_Gfd;
	uint32_t 	R_GFD;
	uint32_t    R_GFD_v;
	uint32_t 	R_GFD_Fail;
	uint8_t		Rfd_State_Fail;
	uint32_t	SMR_Voltage_Fail;
	uint8_t		bFirstGfd;
	float   	 UP_BRIDGE_OP_V;
	float        DN_BRIDGE_OP_V;
	float        U1_V;
	float        U2_V;
	uint32_t 	P2PE_GFD;
	uint32_t 	N2PE_GFD;	
};

extern const uint16_t   nRPM_Tbl[11][2];
extern uint16_t serial_n;
extern SF_test sf_t;

extern float Avg_offset_v;
extern uint16_t Freq_CLC_Grid_v;
extern uint8_t bGfd_Correct[2];
extern uint32_t  nGfd_Temp[2][17];
extern uint32_t R_GFD_Total[2];
extern uint8_t	nGfd_Idx[2];
extern uint16_t pwmVal;
extern uint8_t dir;
extern uint8_t bRelayFeedback;
extern uint8_t nBoard_Addr;
extern float c_vadc[2];
extern uint8_t nGun;
extern uint8_t ModelName[15];
extern bool  GainCaliFlag;

// #define vsense1 	            24.42			// for ADC1 (330 / 4095 / 0.0033) = 24.42
#define vsense1 	            36.63			// for ADC1 (330 / 4095 / 0.0022) = 36.63	2024/11/4
#define vsense2 	            0.002919
//#define vsense3					4.118			// for current (330 / 4095 / 0.01957 ) = 4.118
#define vsense3					24448.7			// 5000 / (5.1*4.01) * 100 = 24448.7 (5000:CT �I��v  5.1:�q���q��  20.5K/5.11K=4.01 OP���v)
#define v_div_1K	            0.8059			// 3300 / 4095
#define v_div		            0.0008059		// 3.3 / 4095 

#define FAN_RPM_DEF		        6200
#define FAN_PWM_PERIOD	        4200
#define FAN_TOLERANCE	        50			   // 50 Percent
#define PWM_Limit		        9980
#define OFF	                    0
#define	ON	                    1
#define FAIL 	                0
#define PASS	                1
#define NO		                0
#define YES		                1
#define true                    1
#define false                   0 

#define SMR1_Relay_Shift_V	        0		// 27
#define SMR1_Fuse_Shift_V 	        0
#define SMR2_Relay_Shift_V	        0		// 14
#define SMR2_Fuse_Shift_V	        0

#define LINE_VOLTAGE_POINT	        100
#define LINE_VOLTAGE_TOLERANCE	    200
#define V165_OFFSET	                7

//<<<<<<< HEAD
#define DC_LOW_GAIN		            1
#define DC_HIGH_GAIN	            1	// 1.0035
//=======
//#define DC_LOW_GAIN		        1.0314
//#define DC_HIGH_GAIN	            1.02		// 1.0035
//>>>>>>> be39927da1874c206c78d2be3fcbaa101e72d2ea

#define GFD_UNKNOW	                0
#define GFD_PASS	                1
#define GFD_FAIL	                2
#define GFD_WARNING	                3
//<<<<<<< HEAD
#define GFD_BALANCE_TEST_COUNT      3
#define GFD_UNBALANCE_TEST_COUNT	0
#define GFD_UNBALANCE_CABLECHK_TEST_COUNT	1
#define GFD_SLEF_TEST_RESISTOR_TOLERANCE	45000		// 30K -> 45K ohm
//#define GFD_SELF_TEST_RESISTOR      55000
#define GFD_SELF_TEST_RESISTOR      (50000 + GFD_SLEF_TEST_RESISTOR_TOLERANCE)	// 95K ohm
#define GFD_FILTER_LIMIT			16
#define IDLE						0
#define CABLE_CHECK					1
#define PRECHARGE					2
#define CHARGE					    3
//=======
//#define GFD_OPT_COUP_RESISTER	        35000
//#define GFD_TEST_TIMES	            15
//>>>>>>> be39927da1874c206c78d2be3fcbaa101e72d2ea

#define ISOLATION_TEST_VOLTAGE	        5000
#define WAIT_FOR_RESISTER_CALC	        30		// mini_second
#define WAIT_FOR_RESISTER_CALC_LONG		50
#define WAIT_FOR_RESISTER_CALC_FAST		30	
#define WAIT_FOR_GFD_CALC	    	   	1000
#define GFD_SWITCH_TIME		    	   	18

#define MCU_DATA                        0  
#define METER_DATA                      1 

#define PREVENT_REPEAT_OFF				1
#define DEBUG_PRINTF					0
#define GFD_SELF_TEST					1

#define AC_INPUT_1ST_POINT				2800	// OVP +10
#define AC_INPUT_2ND_POINT				2200	// UVP -10
#define AC_INPUT_3RD_POINT				1600	// UVP -10
#define DC_OUTPUT_1ST_POINT				9500
#define DC_OUTPUT_2ND_POINT				1500

#define GFD_RESISTOR_FAULT				100
#define GFD_RESISTOR_WARNING			500
#define EVSE_MAX_OUTPUT_VOLTAGE			950
#define GFD_TOLERANCE_RESISTOR			5000

#define GFD_RESISTOR_COEFFICIENT		73.8 	// 73.8 , 51.3
#define GFD_SENSE_VOLTAGE_UP_LIMIT		1.75	// 1.75
#define GFD_SENSE_VOLTAGE_DOWN_LIMIT	1.55	// 1.55
#define GFD_SENSE_CENTER_VOLTAGE		1.65
#define GFD_WORKING_VOLTAGE				1400	// 200v, for Safety Test : 1400

#define Rsense							18	
#define R1  							100000

#define GFD_OFF							0x00
#define GFD_BALANCE						0x01
#define GFD_UNBALANCE					0x02

//#define AC_CORRECT_GAIN_MAX_NUM			6
#define DC_CORRECT_GAIN_MAX_NUM			6    
#define GFD_CORRECT_GAIN_MAX_NUM		2  
//#define DC_CORR_2ND_SHIFT               7


//#define DEFAULT_COFF_3					1.00768
//#define DEFAULT_OFFST_3					-15.556
//#define DEFAULT_COFF_4					1.00553
//#define DEFAULT_OFFST_4					-2.262
//
//#define DEFAULT_COFF_5					1.086
//#define DEFAULT_OFFST_5					-358.322
//#define DEFAULT_COFF_6					1.241
//#define DEFAULT_OFFST_6					-481.362

/* USER CODE END ET */

/* Exported constants --------------------------------------------------------*/
/* USER CODE BEGIN EC */

/* USER CODE END EC */

/* Exported macro ------------------------------------------------------------*/
/* USER CODE BEGIN EM */

/* USER CODE END EM */

/* Exported functions prototypes ---------------------------------------------*/
void Error_Handler(void);

/* USER CODE BEGIN EFP */
#define PWM_CH_FAN_START	0
//void user_pwm_setvalue(uint8_t ch, uint16_t value);
/* USER CODE END EFP */

/* Private defines -----------------------------------------------------------*/
#define CP0_EXTI_Pin GPIO_PIN_0
#define CP0_EXTI_GPIO_Port GPIOF
#define CP0_EXTI_EXTI_IRQn EXTI0_IRQn
#define CP1_EXTI_Pin GPIO_PIN_1
#define CP1_EXTI_GPIO_Port GPIOF
#define CP1_EXTI_EXTI_IRQn EXTI1_IRQn
#define CP2_EXTI_Pin GPIO_PIN_2
#define CP2_EXTI_GPIO_Port GPIOF
#define CP2_EXTI_EXTI_IRQn EXTI2_IRQn
#define CP3_EXTI_Pin GPIO_PIN_3
#define CP3_EXTI_GPIO_Port GPIOF
#define CP3_EXTI_EXTI_IRQn EXTI3_IRQn
#define CP4_EXTI_Pin GPIO_PIN_4
#define CP4_EXTI_GPIO_Port GPIOF
#define CP4_EXTI_EXTI_IRQn EXTI4_IRQn
#define CP5_EXTI_Pin GPIO_PIN_5
#define CP5_EXTI_GPIO_Port GPIOF
#define CP5_EXTI_EXTI_IRQn EXTI9_5_IRQn
#define SMR1_Gfd_Sense_Pin GPIO_PIN_6
#define SMR1_Gfd_Sense_GPIO_Port GPIOF
#define SMR2_Gfd_Sense_Pin GPIO_PIN_7
#define SMR2_Gfd_Sense_GPIO_Port GPIOF
#define DC_In_Pin GPIO_PIN_10
#define DC_In_GPIO_Port GPIOF
#define SMR3_Relay_Voltage_Pin GPIO_PIN_1
#define SMR3_Relay_Voltage_GPIO_Port GPIOC
#define SMR4_Relay_Voltage_Pin GPIO_PIN_2
#define SMR4_Relay_Voltage_GPIO_Port GPIOC
#define SMR5_Relay_Voltage_Pin GPIO_PIN_3
#define SMR5_Relay_Voltage_GPIO_Port GPIOC
#define SMR6_Relay_Voltage_Pin GPIO_PIN_0
#define SMR6_Relay_Voltage_GPIO_Port GPIOA
#define CSRHB_VER_Pin GPIO_PIN_3
#define CSRHB_VER_GPIO_Port GPIOA
#define SMR1_Relay_Voltage_Pin GPIO_PIN_4
#define SMR1_Relay_Voltage_GPIO_Port GPIOA
#define SMR1_Current_Pin GPIO_PIN_5
#define SMR1_Current_GPIO_Port GPIOA
#define SMR2_Relay_Voltage_Pin GPIO_PIN_6
#define SMR2_Relay_Voltage_GPIO_Port GPIOA
#define SMR2_Current_Pin GPIO_PIN_7
#define SMR2_Current_GPIO_Port GPIOA
#define AC_Input_L1_Pin GPIO_PIN_4
#define AC_Input_L1_GPIO_Port GPIOC
#define AC_Input_L2_Pin GPIO_PIN_5
#define AC_Input_L2_GPIO_Port GPIOC
#define AC_Input_L3_Pin GPIO_PIN_0
#define AC_Input_L3_GPIO_Port GPIOB
#define Vref_165_Pin GPIO_PIN_1
#define Vref_165_GPIO_Port GPIOB
#define Contactor_Enable_Pin GPIO_PIN_13
#define Contactor_Enable_GPIO_Port GPIOF
#define PSU_Enable1_Pin GPIO_PIN_14
#define PSU_Enable1_GPIO_Port GPIOF
#define PSU_Enable2_Pin GPIO_PIN_15
#define PSU_Enable2_GPIO_Port GPIOF
#define SW1_Pin GPIO_PIN_1
#define SW1_GPIO_Port GPIOG
#define SW0_Pin GPIO_PIN_7
#define SW0_GPIO_Port GPIOE
#define SW2_Pin GPIO_PIN_8
#define SW2_GPIO_Port GPIOE
#define SMR5_Relay_p_Ret_Pin GPIO_PIN_13
#define SMR5_Relay_p_Ret_GPIO_Port GPIOB
#define SMR5_Relay_n_Ret_Pin GPIO_PIN_14
#define SMR5_Relay_n_Ret_GPIO_Port GPIOB
#define SMR6_Relay_p_Ret_Pin GPIO_PIN_8
#define SMR6_Relay_p_Ret_GPIO_Port GPIOD
#define SMR6_Relay_n_Ret_Pin GPIO_PIN_9
#define SMR6_Relay_n_Ret_GPIO_Port GPIOD
#define Sys2_Self_Test_DC_n_Pin GPIO_PIN_10
#define Sys2_Self_Test_DC_n_GPIO_Port GPIOD
#define Sys2_Self_Test_DC_p_Pin GPIO_PIN_11
#define Sys2_Self_Test_DC_p_GPIO_Port GPIOD
#define Drv_Down_2_Pin GPIO_PIN_12
#define Drv_Down_2_GPIO_Port GPIOD
#define Drv_Up_2_Pin GPIO_PIN_13
#define Drv_Up_2_GPIO_Port GPIOD
#define SMR4_Relay_p_Ret_Pin GPIO_PIN_14
#define SMR4_Relay_p_Ret_GPIO_Port GPIOD
#define SMR4_Relay_n_Ret_Pin GPIO_PIN_15
#define SMR4_Relay_n_Ret_GPIO_Port GPIOD
#define AC_Contactor_Ret_Pin GPIO_PIN_2
#define AC_Contactor_Ret_GPIO_Port GPIOG
#define SMR1_Relay_p_Ret_Pin GPIO_PIN_3
#define SMR1_Relay_p_Ret_GPIO_Port GPIOG
#define SMR1_Relay_n_Ret_Pin GPIO_PIN_4
#define SMR1_Relay_n_Ret_GPIO_Port GPIOG
#define SMR2_Relay_p_Ret_Pin GPIO_PIN_5
#define SMR2_Relay_p_Ret_GPIO_Port GPIOG
#define SMR2_Relay_n_Ret_Pin GPIO_PIN_6
#define SMR2_Relay_n_Ret_GPIO_Port GPIOG
#define SMR3_Relay_p_Ret_Pin GPIO_PIN_7
#define SMR3_Relay_p_Ret_GPIO_Port GPIOG
#define SMR3_Relay_n_Ret_Pin GPIO_PIN_8
#define SMR3_Relay_n_Ret_GPIO_Port GPIOG
#define Precharge1_Ret_Pin GPIO_PIN_6
#define Precharge1_Ret_GPIO_Port GPIOC
#define DBG_RX_Pin GPIO_PIN_7
#define DBG_RX_GPIO_Port GPIOC
#define Precharge2_Ret_Pin GPIO_PIN_8
#define Precharge2_Ret_GPIO_Port GPIOC
#define RS485_DE_Pin GPIO_PIN_9
#define RS485_DE_GPIO_Port GPIOC
#define RS485_RE_Pin GPIO_PIN_8
#define RS485_RE_GPIO_Port GPIOA
#define Sys1_Self_Test_DC_p_Pin GPIO_PIN_12
#define Sys1_Self_Test_DC_p_GPIO_Port GPIOA
#define SMR1_RLY_p_Enable_Pin GPIO_PIN_15
#define SMR1_RLY_p_Enable_GPIO_Port GPIOA
#define SMR1_RLY_n_Enable_Pin GPIO_PIN_10
#define SMR1_RLY_n_Enable_GPIO_Port GPIOC
#define SMR3_RLY_p_Enable_Pin GPIO_PIN_11
#define SMR3_RLY_p_Enable_GPIO_Port GPIOC
#define SMR2_RLY_p_Enable_Pin GPIO_PIN_12
#define SMR2_RLY_p_Enable_GPIO_Port GPIOC
#define SMR2_RLY_n_Enable_Pin GPIO_PIN_0
#define SMR2_RLY_n_Enable_GPIO_Port GPIOD
#define Precharge1_Enable_Pin GPIO_PIN_1
#define Precharge1_Enable_GPIO_Port GPIOD
#define Drv_Up_Pin GPIO_PIN_2
#define Drv_Up_GPIO_Port GPIOD
#define Drv_Down_Pin GPIO_PIN_3
#define Drv_Down_GPIO_Port GPIOD
#define SMR3_RLY_n_Enable_Pin GPIO_PIN_4
#define SMR3_RLY_n_Enable_GPIO_Port GPIOD
#define Precharge2_Enable_Pin GPIO_PIN_5
#define Precharge2_Enable_GPIO_Port GPIOD
#define SMR4_RLY_p_Enable_Pin GPIO_PIN_6
#define SMR4_RLY_p_Enable_GPIO_Port GPIOD
#define SMR4_RLY_n_Enable_Pin GPIO_PIN_7
#define SMR4_RLY_n_Enable_GPIO_Port GPIOD
#define SMR5_RLY_p_Enable_Pin GPIO_PIN_10
#define SMR5_RLY_p_Enable_GPIO_Port GPIOG
#define SMR5_RLY_n_Enable_Pin GPIO_PIN_11
#define SMR5_RLY_n_Enable_GPIO_Port GPIOG
#define SMR6_RLY_p_Enable_Pin GPIO_PIN_12
#define SMR6_RLY_p_Enable_GPIO_Port GPIOG
#define SMR6_RLY_n_Enable_Pin GPIO_PIN_13
#define SMR6_RLY_n_Enable_GPIO_Port GPIOG
#define DBG_TX_Pin GPIO_PIN_14
#define DBG_TX_GPIO_Port GPIOG
#define Sys1_Self_Test_DC_n_Pin GPIO_PIN_5
#define Sys1_Self_Test_DC_n_GPIO_Port GPIOB
#define LED1_Pin GPIO_PIN_7
#define LED1_GPIO_Port GPIOB
#define LED2_Pin GPIO_PIN_0
#define LED2_GPIO_Port GPIOE
#define LED3_Pin GPIO_PIN_1
#define LED3_GPIO_Port GPIOE
/* USER CODE BEGIN Private defines */
extern struct MODULE_INFO
{
    // Board info
    uint8_t                 Soft_Ver_Ptr[16];
    uint8_t                 Hard_Ver_Ptr[16];
    uint8_t                 SN[20];
 
 	// Dip_Switch Status
 	_DIP_SW_				Dip_status;
 	
    // Relay DC voltage info
    uint32_t                SMR1_Relay_V;
    uint32_t                SMR1_Relay_C;    
    uint32_t                SMR2_Relay_V;    
    uint32_t                SMR2_Relay_C;  
	uint32_t                SMR3_Relay_V; 
	uint32_t                SMR4_Relay_V; 
	uint32_t                SMR5_Relay_V; 
	uint32_t                SMR6_Relay_V;    
    uint32_t                Verf_165_V; 	
	uint32_t                BAT_Voltage;

    // Ground sense info
    float                   Vref_165;
    float                   SMR_Gfd_Sense[2];
    float				    SMR_Gfd_Diff[2];
    uint32_t                R_GFD;
    struct Gun_Gfd_Check    gfd_chk[2];
    
    // GPIO info
    RLY_STATUS	            Relay_Status;
    RLY_GPIO		        Relay_IO;    
    _GPIO_                  Gpio_status;
    struct Detet_Fan_IO     fan[1];
    
    // Memory info
    Mem                     memory;
    
    // Coefficient info
    Coefficient             ACVcoeff[3];       // 0~2:AC,3~8:DC SMR1~SMR6,9~10:GRD
	Coefficient             DCVcoeff[6];       
	Coefficient             GFDcoeff[2];       
    Coefficient             DCINcoeff;
    // Alarm info
    ALARM		    Alarm_CSU;
}Module_Info;

extern RLY_ST	RelayStatus;

extern struct COUNTER
{
    uint8_t          nAC_Drop;
    uint8_t          nInterLock;
    uint8_t          nRelay_Timeout[8];
    
}Counter;

extern struct OP_FLAG
{
    uint8_t          bRelay_Config_Change:1;
    uint8_t          bInterLock:1;
    
}OpFlag;

extern struct GFD
{    
	uint32_t	 IsolationVoltage;
	uint8_t		 GfdAvgCount[2];
	uint8_t 	 ToggleBridge[2];	
	uint8_t		 operation[2];
	uint8_t		 GfdMode[2];
	uint8_t		 GfdOldMode[2];
	uint8_t		 Gfdcount[2];
	uint8_t		 SCBwaitfg[2];			//self check banlance mdoe wait flag
	uint8_t		 SCBwaitcount[2];		
	uint8_t		 Gfdtimer;		
	uint8_t		 GfdSwitch;
	float   	 TotalGFDvalue[2];
	float		 UpBridgeMaxV[2];
	float		 UpBridgeMinV[2];
	float		 DnBridgeMaxV[2];
	float		 DnBridgeMinV[2];	
	uint32_t 	 MaxP2PE[2];
	uint32_t 	 MinP2PE[2];
	uint32_t 	 MaxN2PE[2];
	uint32_t 	 MinN2PE[2];
	uint8_t 	 maxcount[2];
	uint8_t 	 mincount[2];
	float 	 	 maxgfdcount[2];
	float 		 mingfdcount[2];		
}Gfd;

extern struct EXT_inupt
{	
	bool  EXTI_SMR1_Flag:1;
	bool  EXTI_SMR2_Flag:1;
	bool  EXTI_SMR3_Flag:1;
	bool  EXTI_SMR4_Flag:1;
	bool  EXTI_SMR5_Flag:1;
	bool  EXTI_SMR6_Flag:1;
	bool  reserve       :2;
	
}Exti;    

extern uint8_t EXTI_SMR1_Count;
extern uint8_t EXTI_SMR2_Count;
extern uint8_t EXTI_SMR3_Count;
extern uint8_t EXTI_SMR4_Count;
extern uint8_t EXTI_SMR5_Count;
extern uint8_t EXTI_SMR6_Count;
extern uint8_t EXTI_Test_Count;

extern uint8_t EXTI_TestFlag1;
extern uint8_t EXTI_TestFlag2;
extern uint8_t EXTI_TestFlag3;
extern uint8_t EXTI_TestFlag4;
extern uint8_t EXTI_TestFlag5;
extern uint8_t EXTI_TestFlag6;

extern CAN_TxHeaderTypeDef TxHeader;
extern CAN_RxHeaderTypeDef RxHeader;
extern uint8_t TxData[8];
extern uint8_t RxData[8];
extern uint32_t TxMailbox;
extern uint32_t canError;

extern uint8_t temp1,temp2,temp3,temp4;
extern float temp5,temp6,temp7,temp8;
extern uint8_t Cmdcount; 
/* USER CODE END Private defines */

#ifdef __cplusplus
}
#endif

#endif /* __MAIN_H */