/* 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 Ultimate Liberty license
  * SLA0044, the "License"; You may not use this file except in compliance with
  * the License. You may obtain a copy of the License at:
  *                             www.st.com/SLA0044
  *
  ******************************************************************************
  */
/* 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 <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include "W25QXX.h"

/* USER CODE END Includes */

/* Exported types ------------------------------------------------------------*/
/* USER CODE BEGIN ET */


//******************************************************************
//Type define / Macro / Utility

#define HTK         //Some utility
#define DEBUG_FUNC  //comment this line when going to build a FORMAL one

//******************************************************************
#ifdef DEBUG_FUNC
//Print out debug message ON/OFF

//#define DISABLE_PRINT_DEBUG_MSG   //Disable print out debug message
//#define DISABLE_PRINT_RTOS_MSG    //Disable print out freeRTOS runtime message

//#define ENABLE_PRINT_IMCP_MSG     //Enable print out update port TX/RX frame message
#ifdef ENABLE_PRINT_IMCP_MSG
#define ENABLE_PRINT_IMCP_SPEC_MSG
#define PRINT_IMCP_MSG_ONLY_ON_UPDATEPORT
#endif //ENABLE_PRINT_IMCP_MSG

#endif //DEBUG_FUNC
//******************************************************************
#ifdef DEBUG_FUNC
//Debug Function

//#define DISABLE_CCID_PROTECT
//#define DISABLE_GROUND_FAULT_PROTECT    //ALARM_GROUND_FAIL
//#define DISABLE_OVP_PROTECT           //for calibration and test
//#define DISABLE_UVP_PROTECT           //for calibration and test
////#define DISABLE_ALARM_CIRCUIT_SHORT
//#define DISABLE_RELAY_WELDING_PROTECT       //ALARM_RELAY_DRIVE_FUALT
//                                            //Threshold value wait to be modified
//#define DEBUG_ALARM_GROUND_FAIL

#endif //DEBUG_FUNC

//******************************************************************
//******************************************************************
//FORMAL Function
#define MODIFY_IMCP
//---------------------------------------------------------------

//---------------------------------------------------------------
//---------------------------------------------------------------
#define MODIFY_COLD_LOAD_PICKUP_DELAY
#ifdef MODIFY_COLD_LOAD_PICKUP_DELAY
#define COLD_LOAD_PICKUP_DELAY_TIME_MAX_RANGE_SEC    (175)
//#define COLD_LOAD_PICKUP_DELAY_TIME_MAX_RANGE_SEC    (10)
#endif
//Prevent BUFFER OVERLOAD
//  @ PROTOCOL_MESSAGE_QUERY_MODEL_NAME
//  @ PROTOCOL_MESSAGE_QUERY_SN
#define MODIFY_UNDEFINED_MODEL_NAME_AND_SN_ISSUE
#define MODIFY_CONFIG_MODEL_NAME
#define MODIFY_CONFIG_SN
//---------------------------------------------------------------

//---------------------------------------------------------------
#define MODIFY_CP_TASK_CTRL_CP_PWM
#define MODIFY_CP_TASK_CTRL_CP_PWM_BELOW_SIX
//---------------------------------------------------------------
#define MODIFY_CPTASK_HEAD

//---------------------------------------------------------------

//******************************************************************

#define FUNC_FW_VER_TRACE

#ifdef FUNC_FW_VER_TRACE

#define FW_VER_TYPE_DEVELOP     (0)
#define FW_VER_TYPE_FORMAL      (1)
#define FW_VER_TYPE_TEST        (2)

//SPECIFY FIRMWARE VERSION
//#define FW_VER_TYPE FW_VER_TYPE_DEVELOP
//#define FW_VER_NUM  (17)    //D0.17

#define FW_VER_TYPE FW_VER_TYPE_FORMAL

//#define FW_VER_NUM  (7000)   //V0.07
//#define FW_VER_NUM  (8000)   //V0.08 20210113
//#define FW_VER_NUM  (9000)   //V0.09 20220415
//#define FW_VER_NUM  (10000)   //V0.10 20220621
//#define FW_VER_NUM  (11000)   //V0.11 20221128
//#define FW_VER_NUM  (12000)   //V0.12 20230210
//#define FW_VER_NUM  (13000)   //V0.13 20230301
//#define FW_VER_NUM  (14000)   //V0.14 20240312
//#define FW_VER_NUM  (15000)   //V0.15 20240321
//#define FW_VER_NUM  (16000)   //V0.16 20240329
//#define FW_VER_NUM  (17000)   //V0.17 20240521
//#define FW_VER_NUM  (18000)   //V0.18 20240613
//#define FW_VER_NUM  (19000)
//#define FW_VER_NUM  (20000)   //V0.20 20241018 EVT2 ATE
//#define FW_VER_NUM  (21000)   //V0.21 20241028 EVT2 Burning
#define FW_VER_NUM  (22000)   //V0.22 20241113


//#define FW_VER_TYPE FW_VER_TYPE_TEST
//#define FW_VER_NUM  (7000)   //T0.07
////#define FW_VER_NUM  (107000)   //T1.07 test for PHA
//---------------------------------------------------------------
#if (FW_VER_NUM >= 7000)  //>= V0.07

#define MODIFY_PROTOCOL_MESSAGE_CONFIG_RELAY_OUTPUT_CHECK_LEGACY_REQUEST
#define MODIFY_PROTOCOL_MESSAGE_CONFIG_RELAY_OUTPUT_CHECK_ALARM_CODE
#define MODIFY_AW_CP_TASK_CSU_0_1_CHARGING_MODE_STOP_WITHOUT_STATE_C

#define MODIFY_CPTASK_CSU_0_1_CHARGE_MODE
#define MODIFY_ADCTASK_CP_STATE_CHANGE
#define MODIFY_ALARMTASK_CP_ERROR_RECOVER
#define MODIFY_MCU_PWM_SET_VALUE_CP

#define FUNC_DEBUG_MSG_SET_CHARGER_MODE
#define FUNC_DEBUG_MSG_CP_STATE

#define FUNC_CURRENT_CP_PWM_DATA

#define FUNC_CHECK_REQ_AND_CP_WHEN_SET_CP_PWM

//-----------------------------------------------
//GB Model:
//Handshake Mode:   CP to D or E => Stop PWM
//Charging Mode:    CP to D or E => Stop PWM
//Stop Mode:        CP to D or E => Stop PWM
#define FUNC_RULE_GB_202108
#ifdef FUNC_RULE_GB_202108
#define FUNC_BLOCK_CSU_CONFIG_CP_PWM_DUTY
#define MODIFY_MODE_HANDSHAKE_STOP_PWM_WITHOUT_OK_STATE
#define MODIFY_MODE_CHARGING_STOP_PWM_WITHOUT_OK_STATE
#define MODIFY_MODE_STOP_STOP_PWM_WITHOUT_OK_STATE
#endif //FUNC_RULE_GB_202108
//-----------------------------------------------

#define MODIFY_CORRECTION_CHECK
//#define FUNC_CSU_CMD_DEBUG

#define TRIM_CCID_SELFTEST

#endif
//---------------------------------------------------------------
#if (FW_VER_NUM >= 8000)  //>= V0.08
#define FIX_FUNC_CHECK_REQ_AND_CP_WHEN_SET_CP_PWM 	//Fixed the problem that the basic model (without CSU) cannot output PWM
#define MODIFY_CP_DETECTION_STATE_F 				//Modify the identifiable CP signal State F (-12V)

#endif
//---------------------------------------------------------------
#if (FW_VER_NUM >= 9000)  //>= V0.09

#define FUNC_BUILD_DATE_TIME

#define MODIFY_POWER_CONSUME_ACCU_TIMER
#ifdef MODIFY_POWER_CONSUME_ACCU_TIMER
#define POWER_CONSUME_ACCU_TIMEOUT_SPEC (10) //ms
#endif

#define MODIFY_MODE_STOP_TO_CHARGE_PROCESS

//Modify from AX
#define MODIFY_CP_STATE_F_SAMPLE_NUM_THRESHOLD
#define CP_STATE_F_SAMPLE_NUM_THRESHOLD     (830)

#define FUNC_CSU_CMD_DEBUG

#define FUNC_CP_VARIABLE_MAX_STATE_COUNTER
#define FUNC_CP_VARIABLE_MAX_STATE_COUNTER_C_TO_D_SET_3_FOR_PASS_TERTEC_TEST
#define FUNC_GET_TRIP_TIME
//-----------------------------------------------

//Testing !
//#define FORCE_VER_WITH_T_PREFIX
//#define DISABLE_CCID_PROTECT

//Testing !

#endif
//---------------------------------------------------------------
#if (FW_VER_NUM >= 10000)  //>= V0.10

////#define FUNC_REPLACE_CORMEX_WITH_VAC_FOR_CNS_MODEL
////#define FUNC_AUTO_MODIFY_CCID_MODULE

////#define DISABLE_OS_ETH_TASK (USB Initial process included)

#define FUNC_RS485_SLAVE //Function of perform a RS485 device (slave) used by  master (ex: DC model) controller
#define FUNC_FORCE_RUN_CSU_MODE_WITH_DC_MODELNAME //For DC Model CSU used via RS485
#define MODIFY_DC_RS485_UPGRADE_ISSUE

#define MODIFY_SET_CP_PWM_DUTY_WHEN_GET_NEW_VAL

#define MODIFY_CP_NEG_JUDGMENT_PROCESS


//-----------------------------------------------
//Testing !
//#define FORCE_VER_WITH_T_PREFIX
//#define DISABLE_OCP_PROTECT
//#define LOCAL_LINK_BLE
//#define DISABLE_UVP_PROTECT
//Testing !
//-----------------------------------------------
#endif

//---------------------------------------------------------------
#if (FW_VER_NUM >= 11000)  //>= V0.11

#define FUNC_BLOCK_CSU_CMD_SET_DUTY_WHEN_ALARM_CP_F_STATE

//OCP Issue
//#define MODIFY_OCP_MAGNIFICATION //RULE CE: fix OCP Magnification
#define FUNC_OCP_WITH_PP
#define MODIFY_OCP_PROC_WITH_MODELNAME_IDX_3_EURO_SPEC

#define MODIFY_CCID_MODULE_CORMEX_RECOVER_AC_DC

//-----------------------------------------------
//Testing !
//#define FORCE_VER_WITH_T_PREFIX
//#define DISABLE_OCP_PROTECT
//Testing !
//-----------------------------------------------
#endif
//---------------------------------------------------------------
#if (FW_VER_NUM >= 12000)  //>= V0.12
//Fix CE OCP specification
//undefine MODIFY_OCP_MAGNIFICATION
//#define FUNC_RELAXSPEC_GROUND_FAULT_PROTECT
//-----------------------------------------------
//Testing !
//#define FORCE_VER_WITH_T_PREFIX
//#define DISABLE_OCP_PROTECT
//Testing !
//-----------------------------------------------
#endif
//---------------------------------------------------------------

#if (FW_VER_NUM >= 13000)  //>= V0.13

#define FUNC_ZERO_CURRENT_WHEN_OUTP_OFF

#define FIX_OUTP_PWM_WHEN_ALARM_RECOVER_IN_4SEC

#define FUNC_RELAY_OFF_WHEN_CSU_CMD_TIMEOUT
#ifdef FUNC_RELAY_OFF_WHEN_CSU_CMD_TIMEOUT
#define CSU_CMD_TIMEOUT_SEC (10)
#endif

#define FUNC_FLASH_UPDATE_CONFIG_BOTH_PRIMARY_AND_BACKUP
#define FUNC_FLASH_RESTORE_CONFIG_WHEN_PRIMARY_LOSS
#define FUNC_FLASH_CONFIG_APPEND_CRC

#define FUNC_RELAY_OFF_WHEN_GET_TWICE_OFF_CMD
#define FUNC_REQUEST_OFF_WHEN_GET_TWICE_OFF_CMD
#define FUNC_REQUEST_OFF_WHEN_CSU_CMD_TIMEOUT
#define FUNC_ADD_FW_VER_WITH_GUN_TYPE_NACS
#define FUNC_ADD_FW_VER_WITH_NET_TYPE_WIFI_AND_4G
#define FUNC_SHOW_CSU_CONFIG_LED_ACTION_INFO

#define FUNC_BOOT_TICK

#define FUNC_UPDATE_ROTARY_SWITCH_SETTING_AFTER_PARSE_MODELNAME

#define FUNC_STM32_FLASH_INFO
#define FUNC_STM32_FLASH_OPTION_BYTES

#define MODIFY_UART_TX_LEN_FROM_U8_TO_U16

#define FUNC_UART_IAP_TX_BUF_INIT_ZERO

#define FUNC_ADD_AC_DC_LEAK_TIMER

#define FUNC_TEMP_SENSOR_WITH_NEG_VALUE
#define FUNC_SHOW_CORRECTION_DATA
#define FUNC_SHOW_RELAY_INFO
#define FUNC_MODIFY_LEAK_AC_DISPLAY

#define MODIFY_AC_EVSE_STATUS_MAX_CHARGING_CURRENT_VARIABLE
#define FUNC_MODIFY_CP_PWM_CTRL_20231225

#define MODIFY_FUNC_CRMS_CAL
#define MODIFY_FUNC_GFRMS_CAL
#define MODIFY_FUNC_VRMS_CAL2

#define MODIFY_PROTOCOL_MESSAGE_CONFIG_RELAY_OUTPUT_CHECK_CP_PWM



//******************************* [AWeMP BEG]
//CSU PROJECT: AWeMP
//RFID(ttyS2), PollCharge: LineNo. 5075
#define FUNC_EMPX10_CUSTOMIZE
#ifdef FUNC_EMPX10_CUSTOMIZE
#define FUNC_EMPX10_CUSTOMIZE_RS485_CLIENT_ADDRESS
#define EMPX10_RS485_ADDR_CSU           (0x00)
#define EMPX10_RS485_ADDR_MIN           (0x00)
#define EMPX10_RS485_ADDR_MAX           (0x09)
#define EMPX10_RS485_ADDR_BROADCAST     (0xFF)
#define FUNC_EMPX10_CUSTOMIZE_CP_STATE_OPERATION
#define FUNC_EMPX10_CUSTOMIZE_RUN_CSU_MODE
#define FUNC_EMPX10_SET_FIXED_CURRENT_20A //240V * 20A = 4800W; 4.8kW * 10 = 48kW

#ifdef FUNC_RELAY_OFF_WHEN_CSU_CMD_TIMEOUT
#undef CSU_CMD_TIMEOUT_SEC
#define CSU_CMD_TIMEOUT_SEC (100) //from 10 sec to 100 sec
#endif

//#define DEBUG_EMPX10_COMM

#endif //FUNC_EMPX10_CUSTOMIZE
//******************************* [AWeMP END]

//-----------------------------------------------
//Testing !
//#define FORCE_VER_WITH_T_PREFIX

//#define FUNC_DISABLE_SOME_ALARM_FOR_ATE_SITE_SAMPLE_TEST
//#ifdef FUNC_DISABLE_SOME_ALARM_FOR_ATE_SITE_SAMPLE_TEST
//#define FORCE_VER_WITH_F_PREFIX //F0.13 only test in facrory
//#define DISABLE_CCID_PROTECT
//#define DISABLE_GROUND_FAULT_PROTECT
//#define DISABLE_OVP_PROTECT
//#define DISABLE_UVP_PROTECT
//#define DISABLE_OCP_PROTECT
//#endif

//#ifdef ENABLE_PRINT_IMCP_SPEC_MSG
//#undef ENABLE_PRINT_IMCP_SPEC_MSG
//#endif
//
//#ifdef PRINT_IMCP_MSG_ONLY_ON_UPDATEPORT
//#undef PRINT_IMCP_MSG_ONLY_ON_UPDATEPORT
//#endif

//#define ENABLE_PRINT_IMCP_MSG


//Testing !
//-----------------------------------------------
#endif
//---------------------------------------------------------------
#if (FW_VER_NUM >= 14000)  //>= V0.14

#define FUNC_SIMU_CHARGE_CURRENT_OUTPUT

//-----------------------------------------------
//Testing !
//#define FORCE_VER_WITH_T_PREFIX
//Testing !
//-----------------------------------------------
#endif
//---------------------------------------------------------------
#if (FW_VER_NUM >= 15000)  //>= V0.15

#define FUNC_EMPX10_SEAMUS_MODIFY
#ifdef FUNC_EMPX10_SEAMUS_MODIFY
//#define MODIFY_EMPX10_ALARM_SPEC_RELAY_LN
#define DISABLE_CP_ALARM_PROTECT
#define DISABLE_GROUND_FAULT_PROTECT
#define DISABLE_HANDSHAKE_PROTECT
#define DISABLE_COLD_LOAD_PICKUP
#endif //FUNC_EMPX10_SEAMUS_MODIFY
//-----------------------------------------------
//Testing !
//#define FORCE_VER_WITH_T_PREFIX
//Testing !
//-----------------------------------------------
#endif
//---------------------------------------------------------------
#if (FW_VER_NUM >= 16000)  //>= V0.16

//////#define FUNC_EMPX10_SET_FIXED_CURRENT_20A //240V * 20A = 4800W; 4.8kW * 10 = 48kW

#ifdef FUNC_EMPX10_SET_FIXED_CURRENT_20A
#undef FUNC_EMPX10_SET_FIXED_CURRENT_20A
#define FUNC_EMPX10_SET_FIXED_CURRENT_16A //200V * 16A = 3200W; 3.2kW * 10 = 32kW
#endif

#define FUNC_EMPX10_LED

//-----------------------------------------------
//Testing !
//#define FORCE_VER_WITH_T_PREFIX
//Testing !
//-----------------------------------------------
#endif
//---------------------------------------------------------------
#if (FW_VER_NUM >= 17000)  //>= V0.17

//#define FIX_EMPX10_LED_RX_NG_DATA
#define FIX_RS485_UNKNOW_MSG

//-----------------------------------------------
//Testing !
//#define FORCE_VER_WITH_T_PREFIX
//#define DISABLE_UVP_PROTECT
//Testing !
//-----------------------------------------------
#endif

//---------------------------------------------------------------
#if (FW_VER_NUM >= 18000)  //>= V0.18

#define FUNC_EMPX10_LED_ALARM_FIX_20240730
//-----------------------------------------------
//Testing !
//#define FORCE_VER_WITH_T_PREFIX

//#define DISABLE_UVP_PROTECT
//#define FUNC_TEST_SIMU_LOAD_BUTTON

//for eMP EE Test----------------------
//#define FUNC_TEST_RELAY_ALWAYS_ON
//#define DISABLE_RELAY_WELDING_PROTECT
//-------------------------------------

//Testing !
//-----------------------------------------------
#endif

//---------------------------------------------------------------
#if (FW_VER_NUM >= 20000)  //>= V0.20

#define FUNC_SWITCH_RELAY_WELDING_PROTECT //CCID/OCP

#define FUNC_DISABLE_LED_CMD_CHECK

#define FUNC_EMP_FAN_CTRL_USE_LOCKER_IO
#ifdef FUNC_EMP_FAN_CTRL_USE_LOCKER_IO
//#define OUT_Locker_Lock_Pin GPIO_PIN_11
//#define OUT_Locker_Lock_GPIO_Port GPIOF
#define SET_EMP_FAN(bON)    (HAL_GPIO_WritePin(GPIOF, GPIO_PIN_11, (bON) ? GPIO_PIN_SET : GPIO_PIN_RESET))
#define GET_EMP_FAN         (HAL_GPIO_ReadPin(GPIOF, GPIO_PIN_11) == GPIO_PIN_SET ? ON : OFF)
#endif //FUNC_EMP_FAN_CTRL_USE_LOCKER_IO


//-----------------------------------------------
//Testing !
//#define FORCE_VER_WITH_T_PREFIX
//#define DISABLE_RELAY_WELDING_PROTECT
//#define DISABLE_UVP_PROTECT
//#define FUNC_TEST_SIMU_LOAD_BUTTON
//Testing !
//-----------------------------------------------
#endif

//---------------------------------------------------------------
#if (FW_VER_NUM >= 21000)  //>= V0.21

//-----------------------------------------------
//Testing !
//#define FORCE_VER_WITH_T_PREFIX
//#define DISABLE_RELAY_WELDING_PROTECT
//#define DISABLE_OCP_PROTECT
//#define DISABLE_CCID_PROTECT
//#define DISABLE_UVP_PROTECT
//#define FUNC_TEST_SIMU_LOAD_BUTTON
//Testing !
//-----------------------------------------------
#endif
//---------------------------------------------------------------
#if (FW_VER_NUM >= 22000)  //>= V0.22

#define FUNC_RELAY_OFF_WHEN_CP_NOT_STATE_C

#define FUNC_VERSION_USE_NEW_SPEC_20240926 //define FUNC_VERSION_FOR_LAB_CERTIFICATE_SPEC (xx.xx.A1) else (xx.xx.A0)

#define MODIFY_FLASH_OPERATION_20241104
#define FUNC_CALC_FLASH_CRC_IN_CRITICAL

#define FUNC_RESET_CONNECTOR_PLUGIN_TIMES

//#define FUNC_SHOW_CSU_CONFIG_LED_BRIGHTNESS
#define FUNC_EMP_LED_SET_MIN_BRIGHTNESS
//-----------------------------------------------
//Testing !
#define FORCE_VER_WITH_T_PREFIX
//#define DISABLE_RELAY_WELDING_PROTECT
//#define DISABLE_OCP_PROTECT
//#define DISABLE_CCID_PROTECT
//#define DISABLE_UVP_PROTECT
#define FUNC_TEST_SIMU_LOAD_BUTTON
//Testing !
//-----------------------------------------------
#endif
//---------------------------------------------------------------

//---------------------------------------------------------------
#endif //FUNC_FW_VER_TRACE
//******************************************************************

//Type define
#ifdef HTK
#define HTK_USE_XPRINT
#ifdef HTK_USE_XPRINT
#define XP(FMT, ARGS...)    (printf(FMT, ##ARGS))
#else
#define XP(FMT, ARGS...)
#endif

//#define HTK_USE_XPRINT_TEST
#ifdef HTK_USE_XPRINT_TEST
#define XT(FMT, ARGS...)    (printf(FMT, ##ARGS))
#define XRowWidth(c, count) (HTK_PrintRow((c), (count)))
#define XRowS(c)            (HTK_PrintRow((c), (20)))
#define XRowM(c)            (HTK_PrintRow((c), (40)))
#define XRowL(c)            (HTK_PrintRow((c), (80)))
#else
#define XT(FMT, ARGS...)
#define XRowWidth(c, count)
#define XRowS(c)
#define XRowM(c)
#define XRowL(c)
#endif

typedef uint8_t byte;
typedef int8_t sbyte;

typedef int64_t         s64;
typedef int32_t         s32;
typedef int16_t	        s16;
typedef int8_t          s8;
typedef const int64_t   sc64;
typedef const int32_t   sc32;
typedef const int16_t 	sc16;
typedef const int8_t 	sc8;
typedef __IO int64_t    vs64;
typedef __IO int32_t    vs32;
typedef __IO int16_t    vs16;
typedef __IO int8_t     vs8;
typedef __I int64_t     vsc64;
typedef __I int32_t     vsc32;
typedef __I int16_t     vsc16;
typedef __I int8_t 	    vsc8;

typedef uint64_t  	    u64;
typedef uint32_t  	    u32;
typedef uint16_t	    u16;
typedef uint8_t  	    u8;
typedef const uint64_t 	uc64;
typedef const uint32_t 	uc32;
typedef const uint16_t 	uc16;
typedef const uint8_t 	uc8;
typedef __IO uint64_t 	vu64;
typedef __IO uint32_t 	vu32;
typedef __IO uint16_t 	vu16;
typedef __IO uint8_t  	vu8;
typedef __I uint64_t 	vuc64;
typedef __I uint32_t 	vuc32;
typedef __I uint16_t	vuc16;
typedef __I uint8_t 	vuc8;

#define HTK_NULL 	0

#define HTK_UNUSED(X) (void)X      /* To avoid gcc/g++ warnings */

typedef u8 HTK_BOOL;
#define HTK_TRUE 	1
#define HTK_FALSE 	0

#define HTK_ENABLE 	1
#define HTK_DISABLE 0

#define BIT_FLAG_VAL(pos)       (1U << (pos))
#define BIT_SET(x, pos)         ((x) |= BIT_FLAG_VAL(pos))
#define BIT_CLEAR(x, pos)       ((x) &= (~BIT_FLAG_VAL(pos)))
#define BIT_TOGGLE_(x, pos)     ((x) ^= BIT_FLAG_VAL(pos)
#define BIT_CHECK(x, pos)       ((x) & BIT_FLAG_VAL(pos))

#define BIT_GET(x, pos)         (((x) & BIT_FLAG_VAL(pos)) >> (pos))

#define LOOP_SHIFT_U8(x, n)     (((n) >= 0) ? (((x) << (  8 - (n))) | ((x) >> (n))) : (((x) >> (  8 - ((-1) * (n)))) | ((x) << ((-1) * (n)))))
#define LOOP_SHIFT_U16(x, n)    (((n) >= 0) ? (((x) << ( 16 - (n))) | ((x) >> (n))) : (((x) >> ( 16 - ((-1) * (n)))) | ((x) << ((-1) * (n)))))
#define LOOP_SHIFT_U32(x, n)    (((n) >= 0) ? (((x) << ( 32 - (n))) | ((x) >> (n))) : (((x) >> ( 32 - ((-1) * (n)))) | ((x) << ((-1) * (n)))))

u8 GetBit1Num(u32 x);

//Little-Endian
#define HTK_U16_BYTE0(vU16)	    (*(((u8*)&(vU16)) + 0))
#define HTK_U16_BYTE1(vU16)	    (*(((u8*)&(vU16)) + 1))
#define HTK_U16_BYTE(vU16, n)  (*(((u8*)&(vU16)) + (n)))

#define HTK_U16(vU8)		    (*(((u16*)&(vU8)) + 0))

#define HTK_U32_BYTE0(vU32)	    (*(((u8*)&(vU32)) + 0))
#define HTK_U32_BYTE1(vU32)	    (*(((u8*)&(vU32)) + 1))
#define HTK_U32_BYTE2(vU32)	    (*(((u8*)&(vU32)) + 2))
#define HTK_U32_BYTE3(vU32)	    (*(((u8*)&(vU32)) + 3))
#define HTK_U32_BYTE(vU32, n)	(*(((u8*)&(vU32)) + (n)))

#define HTK_U32(vU8)			(*(((u32*)&(vU8)) + 0))

#define HTK_U32_WORD0(vU32)     (*(((u16*)&(vU32)) + 0))
#define HTK_U32_WORD1(vU32)     (*(((u16*)&(vU32)) + 1))
#define HTK_U32_WORD(vU32, n)   (*(((u16*)&(vU32)) + (n)))

//#define HTK_U64_BYTE0(vU64)	    (*(((u8*)&(vU64)) + 0))
//#define HTK_U64_BYTE1(vU64)	    (*(((u8*)&(vU64)) + 1))
//#define HTK_U64_BYTE2(vU64)	    (*(((u8*)&(vU64)) + 2))
//#define HTK_U64_BYTE3(vU64)	    (*(((u8*)&(vU64)) + 3))
//#define HTK_U64_BYTE4(vU64)	    (*(((u8*)&(vU64)) + 4))
//#define HTK_U64_BYTE5(vU64)	    (*(((u8*)&(vU64)) + 5))
//#define HTK_U64_BYTE6(vU64)	    (*(((u8*)&(vU64)) + 6))
//#define HTK_U64_BYTE7(vU64)	    (*(((u8*)&(vU64)) + 7))
//#define HTK_U64_BYTEn(vU64, n)	(*(((u8*)&(vU64)) + (n)))
//
//#define HTK_U64(vU8)			(*(((u64*)&(vU8)) + 0))

//Big-Endian
#define HTK_U16_BE(vU8)		    (                                   \
                                    (HTK_U16_BYTE0(vU8) << 8) |     \
                                    (HTK_U16_BYTE1(vU8) << 0)       \
                                )

#define HTK_U32_BE(vU8)		    (                                   \
                                    (HTK_U32_BYTE0(vU8) << 24) |    \
                                    (HTK_U32_BYTE1(vU8) << 16) |    \
                                    (HTK_U32_BYTE2(vU8) << 8)  |    \
                                    (HTK_U32_BYTE3(vU8) << 0)       \
                                )

#define HTK_GET_VAL_MAX(Val1, Val2)     ((Val1) >= (Val2) ? (Val1) : (Val2))
#define HTK_GET_VAL_MIN(Val1, Val2)     ((Val1) <= (Val2) ? (Val1) : (Val2))
#define HTK_GET_VAL(Bool, Val1, Val2)   ((Bool) ? (Val1) : (Val2))

#define HTK_IS_BETWEEN_MINMAX(v, MinVal, MaxVal)    ((v) >= (MinVal) && (v) <= (MaxVal))
#define HTK_IS_BETWEEN_MIDDIFF(v, MidVal, DiffVal)  (HTK_IS_BETWEEN_MINMAX((v), ((MidVal) - (DiffVal)), ((MidVal) + (DiffVal))))

#define HTK_ROUND_U16(x)    (((u16)(x) + 0.5) > (x) ? ((u16)(x)) : ((u16)(x) + 1))
#define HTK_ROUND_U32(x)    (((u32)(x) + 0.5) > (x) ? ((u32)(x)) : ((u32)(x) + 1))
#define HTK_ROUND_U64(x)    (((u64)(x) + 0.5) > (x) ? ((u64)(x)) : ((u64)(x) + 1))

#define HTK_ROUND_U32_3D(x)             (HTK_ROUND_U32((x) * 1000))
#define HTK_ROUND_U32_3D_TO_FLOAT(x)    (((float)HTK_ROUND_U32_3D(x)) / 1000)

#define GET_BOX_NUM(ItemNum, ItemNumInOneBox)   ((ItemNum) % (ItemNumInOneBox) == 0 ? ((ItemNum) / (ItemNumInOneBox)) : ((ItemNum) / (ItemNumInOneBox)) + 1)

#define MULTI_3D(x)
#define FLOOR_3D(x)      ((float)(((uint32_t)((x) * 1000)) / 1000))

//#define HTK_ABS(x)          (((x) > 0) ? (x) : ((-1) * (x)))
#define HTK_ABS(x)          (((x) >= 0) ? (x) : ((-1) * (x)))

#define HTK_GetPosFromZero(x)   (((x) >= 0) ? (x) : 0)
#define HTK_GetNegFromZero(x)   (((x) <= 0) ? (x) * (-1) : 0)

#define HTK_U8_MAX          (0xFF)
#define HTK_U16_MAX         (0xFFFF)
#define HTK_U32_MAX         (0xFFFFFFFF)
#define HTK_U64_MAX         (0xFFFFFFFFFFFFFFFF)

#define BYTE_TO_BINARY_PATTERN  "%c%c%c%c %c%c%c%c"
#define BYTE_TO_BINARY(byte)  \
  ((byte) & 0x80 ? '1' : '0'), \
  ((byte) & 0x40 ? '1' : '0'), \
  ((byte) & 0x20 ? '1' : '0'), \
  ((byte) & 0x10 ? '1' : '0'), \
  ((byte) & 0x08 ? '1' : '0'), \
  ((byte) & 0x04 ? '1' : '0'), \
  ((byte) & 0x02 ? '1' : '0'), \
  ((byte) & 0x01 ? '1' : '0')

//Codes 0 through 31 and 127 (decimal) are unprintable control characters.
//Code 32 (decimal) is a nonprinting spacing character. Codes 33 through 126 (decimal) are printable graphic characters.
#define HTK_IS_DISP_CHAR(c) ((c) >= 32 && (c) <= 126 ? HTK_TRUE : HTK_FALSE)
#define HTK_IS_CTRL_CHAR(c) (!HTK_IS_DISP_CHAR(c))

//#define HTK_ELAPSEDTICKS_U32(BegTick, EndTick)          ((EndTick) >= (BegTick) ? ((EndTick) - (BegTick)) : ((HTK_U32_MAX) - (BegTick) + (EndTick)))
//#define HTK_IS_TIMEOUT_U32(BegTick, EndTick, MaxTick)   (HTK_ELAPSEDTICKS_U32((BegTick), (EndTick)) > (MaxTick) ? HTK_TRUE : HTK_FALSE)
HTK_BOOL HTK_IsInTime(u32 BegTick, u32 MaxTick);
HTK_BOOL HTK_IsTimeout(u32 BegTick, u32 MaxTick);

typedef struct _HTK_Timer
{
    HTK_BOOL    m_bStart:   1;
    HTK_BOOL    m_bTimeout: 1;
    u32         m_BegTick;
    u32         m_MaxTick;
}
HTK_Timer, *PHTK_Timer;

void HTK_Timer_Reset(PHTK_Timer p);
void HTK_Timer_Start(PHTK_Timer p, u32 MaxTick);
HTK_BOOL HTK_Timer_CheckTimeout(PHTK_Timer p);

uint8_t HTK_CheckSum_Calulate(uint8_t* pBeg, uint32_t len);
HTK_BOOL HTK_CheckSum_Update(uint8_t* pBeg, uint32_t len, uint8_t* pCheckSum);
HTK_BOOL HTK_CheckSum_IsOK(uint8_t* pBeg, uint32_t len, uint8_t* pCheckSum);

void HTK_ByteArray2HexStr(const u8* buf, const u16 BegIdx, const u16 len, char* OutStr, u16 nMaxBytesPerLine, HTK_BOOL bSpace);
void HTK_ByteArray2HexStr_XP(const char* Title, const u8* buf, const u16 BegIdx, const u16 len);
void HTK_ByteArray2HexStr_XT(const char* Title, const u8* buf, const u16 BegIdx, const u16 len);

typedef struct _HTK_MallocHandle
{
    s32 m_Total;
    s32 m_Allocated;
    s32 m_Reserved;
}
HTK_MallocHandle, PHTK_MallocHandle;

void HTK_Malloc_Init(void);
void* HTK_Malloc(s32 size);
s32 HTK_Malloc_BlockTest(s32 BlockSize);
#endif //HTK
//******************************************************************
#ifdef MODIFY_IMCP
#define IMCP_FM_FRAMEID_IDX     (0)
#define IMCP_FM_SRCADDR_IDX     (1)
#define IMCP_FM_DSTADDR_IDX     (2)
#define IMCP_FM_MSGID_IDX       (3)
#define IMCP_FM_DATALEN_LSB_IDX (4) //4: LSB (DATALEN Value: N)
#define IMCP_FM_DATALEN_MSB_IDX (5) //5: MSB
#define IMCP_FM_DATA_BEG_IDX    (6) //6~(6+N) = DATA(1)~DATA(N)
//#define IMCP_FM_DATA_CHECKSUM_IDX   (6 + N + 1) //Variable
#define IMCP_FM_HEAD_LEN        (IMCP_FM_DATALEN_MSB_IDX - IMCP_FM_FRAMEID_IDX + 1)
#define IMCP_FM_TAIL_LEN        (1) //CheckSum
#endif //MODIFY_IMCP
//******************************************************************
#ifdef DISABLE_PRINT_DEBUG_MSG
#define DEBUG_INFO(format, args...)
#define DEBUG_WARN(format, args...)
#define DEBUG_ERROR(format, args...)
#else
#define DEBUG_INFO(format, args...) (Charger.memory.EVSE_Config.data.item.isDispDebug?printf("[%04d-%02d-%02d %02d:%02d:%02d][%s:%d][Info] "format, Charger.memory.EVSE_Config.data.item.SystemDateTime.year, Charger.memory.EVSE_Config.data.item.SystemDateTime.month, Charger.memory.EVSE_Config.data.item.SystemDateTime.day, Charger.memory.EVSE_Config.data.item.SystemDateTime.hour, Charger.memory.EVSE_Config.data.item.SystemDateTime.min, Charger.memory.EVSE_Config.data.item.SystemDateTime.sec, __FUNCTION__, __LINE__, ##args):printf(""))
#define DEBUG_WARN(format, args...) (Charger.memory.EVSE_Config.data.item.isDispDebug?printf("[%04d-%02d-%02d %02d:%02d:%02d][%s:%d][Warn] "format, Charger.memory.EVSE_Config.data.item.SystemDateTime.year, Charger.memory.EVSE_Config.data.item.SystemDateTime.month, Charger.memory.EVSE_Config.data.item.SystemDateTime.day, Charger.memory.EVSE_Config.data.item.SystemDateTime.hour, Charger.memory.EVSE_Config.data.item.SystemDateTime.min, Charger.memory.EVSE_Config.data.item.SystemDateTime.sec, __FUNCTION__, __LINE__, ##args):printf(""))
#define DEBUG_ERROR(format, args...) (Charger.memory.EVSE_Config.data.item.isDispDebug?printf("[%04d-%02d-%02d %02d:%02d:%02d][%s:%d][Error] "format, Charger.memory.EVSE_Config.data.item.SystemDateTime.year, Charger.memory.EVSE_Config.data.item.SystemDateTime.month, Charger.memory.EVSE_Config.data.item.SystemDateTime.day, Charger.memory.EVSE_Config.data.item.SystemDateTime.hour, Charger.memory.EVSE_Config.data.item.SystemDateTime.min, Charger.memory.EVSE_Config.data.item.SystemDateTime.sec, __FUNCTION__, __LINE__, ##args):printf(""))
#endif //DISABLE_PRINT_DEBUG_MSG
//******************************************************************

#define ARRAY_SIZE(A)		(sizeof(A) / sizeof(A[0]))
/* USER CODE END ET */

/* Exported constants --------------------------------------------------------*/
/* USER CODE BEGIN EC */
#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 DEBUG
#define RTOS_STAT
#define BYPASS
#define W25Q16FV_BLOCK_SIZE         0x1000

#define EVSE_CONNECTOR_TYPE         "GBT_MODE3"
#define EVSE_MODEL_CURRENT_RATING   32

#define MCU_CONTROL_MODE_NO_CSU     0
#define MCU_CONTROL_MODE_CSU        1
//#define MCU_PASS_MODE               2 //Not used

#define RFID_SN_TYPE_4BYTE          0
#define RFID_SN_TYPE_7BYTE          1
#define RFID_SN_TYPE_10BYTE         2
#define RFID_SN_TYPE_14443B         3
#define RFID_SN_TYPE_FELICA         4

#define RFID_ENDIAN_LITTLE          0
#define RFID_ENDIAN_BIG             1

//AuthorisationMode
#define AUTH_MODE_OCPP              0x00
#define AUTH_MODE_FREE              0x01

//OfflinePolicy
#define RFID_USER_AUTH_LOCAL_LIST       0x00
#define RFID_USER_AUTH_PH               0x01
#define RFID_USER_AUTH_FREE             0x02
#define RFID_USER_AUTH_NO_CHARGING      0x03

#define MEM_ADDR_EVSE_CONFIG                            0*W25Q16FV_BLOCK_SIZE
#define MEM_ADDR_COLD_LOAD_PICKUP                       1*W25Q16FV_BLOCK_SIZE
#define MEM_ADDR_WHITE_LIST                             2*W25Q16FV_BLOCK_SIZE
#define MEM_ADDR_HIS_ALARM                              3*W25Q16FV_BLOCK_SIZE
#define MEM_ADDR_HIS_CHARGING                           4*W25Q16FV_BLOCK_SIZE
#define MEM_ADDR_BACKUP_EVSE_CONFIG                     20*W25Q16FV_BLOCK_SIZE
#define MEM_ADDR_BACKUP_COLD_LOAD_PICKUP                21*W25Q16FV_BLOCK_SIZE
#define MEM_ADDR_BACKUP_WHITE_LIST                      22*W25Q16FV_BLOCK_SIZE
#define MEM_ADDR_BACKUP_HIS_ALARM                       23*W25Q16FV_BLOCK_SIZE
#define MEM_ADDR_BACKUP_HIS_CHARGING                    24*W25Q16FV_BLOCK_SIZE

#define MODE_INITIAL	    0
#define MODE_IDLE		    1
#define MODE_HANDSHAKE	    2
#define MODE_CHARGING	    3
#define MODE_STOP		    4
#define MODE_ALARM		    5
#define MODE_MAINTAIN	    6
#define MODE_DEBUG          99

#define	LED_MOTION_OFF	    0
#define	LED_MOTION_ON	    1
#define	LED_MOTION_BLINK    2
#define LED_MOTION_BREATHE  3

#define LED_ACTION_INIT                         0
#define LED_ACTION_IDLE                         1
#define LED_ACTION_AUTHED                       2
#define LED_ACTION_CONNECTED                    3
#define LED_ACTION_CHARGING                     4
#define LED_ACTION_STOP                         5
#define LED_ACTION_ALARM                        6
#define LED_ACTION_MAINTAIN                     7
#define LED_ACTION_RFID_PASS                    8
#define LED_ACTION_RFID_FAIL                    9
#define LED_ACTION_BLE_CONNECT                  10
#define LED_ACTION_BLE_DISABLE                  11
#define LED_ACTION_DEBUG                        12
#define LED_ACTION_ALL_OFF                      13
#define LED_RELAY_ON                            14
#define LED_RELAY_OFF                           15
#define LED_ACTION_HANDSHAKE_FAIL               16
#define LED_ACTION_INTERNET_DISCONNECT          17
#define LED_ACTION_RESTORE_SETTING              18

#define LED_RFID_TIME                           3000

#define SPEAKER_NONE                            0
#define SPEAKER_LONG                            1
#define SPEAKER_SHORT                           2
#define SPEAKER_BLINK                           3

#define SPEAK_RESET_RFID_TIME      LED_RFID_TIME-1000

#define ALARM_OVER_VOLTAGE			0x000001
#define ALARM_UNDER_VOLTAGE			0x000002
#define ALARM_OVER_CURRENT			0x000004
#define ALARM_OVER_TEMPERATURE	                0x000008
#define ALARM_GROUND_FAIL			0x000010
#define ALARM_CP_ERROR				0x000020
#define ALARM_CURRENT_LEAK_AC                   0x000040
#define ALARM_CURRENT_LEAK_DC                   0x000080
#define ALARM_MCU_TESTFAIL			0x000100
#define ALARM_HANDSHAKE_TIMEOUT                 0x000200
#define ALARM_EMERGENCY_STOP                    0x000400
#define ALARM_RELAY_STATUS                      0x000800
#define ALARM_LEAK_MODULE_FAIL                  0x001000
#define ALARM_SHUTTER_FAULT                     0x002000
#define ALARM_LOCKER_FAULT                      0x004000
#define ALARM_POWER_DROP                        0x008000
#define ALARM_CIRCUIT_SHORT                     0x010000
#define ALARM_ROTATORY_SWITCH_FAULT             0x020000
#define ALARM_RELAY_DRIVE_FUALT                 0x040000
#define ALARM_BLE_MODULE_BROKEN                 0x080000

#define END_STATUS_CODE_NORMAL                  0x013600
#define END_STATUS_CODE_TIMEUP                  0x013601
#define END_STATUS_CODE_DROP                    0x012212

#ifdef MODIFY_CP_TASK_CTRL_CP_PWM
uint16_t GetCpPwmDuty(uint16_t Curr);
#else
#define PWM_DUTY_651(AMP)           (uint16_t)((AMP/0.6)*10)        // 6<=currewnt<=51 PWM duty resolution in 0.1
#endif

#define PWM_DUTY_FULL               1000                            // Stop PWM duty resolution in 0.1
#define PWM_DUTY_ZERO               0                               // Zero PWM duty resolution in 0.1

#define START_METHOD_NONE           0
#define START_METHOD_CP             1
#define START_METHOD_RFID           2
#define START_METHOD_BLE            3

#define RESERVATION_DISABLE         0
#define RESERVATION_ONCE            1
#define RESERVATION_ALWAYS          2

#define ALARM_SPEC_POWER_DROP                   7200            // unit 0.01V >> 240V * 30%
#define ALARM_SPEC_OV                           27500           // unit 0.01V
#define ALARM_SPEC_UV                           16000//16000    // unit 0.01V
#define ALARM_SPEC_OUV_HYSTERESIS               1000            // unit 0.01V
#define ALARM_SPEC_OT_1                         90              // unit C
#define ALARM_SPEC_OT_2                         100             // unit C
#define ALARM_SPEC_OT_HYSTERESIS                20
#define ALARM_SPEC_GF                           3100            //2.5V
#define ALARM_SPEC_GF_HYSTERESIS                2110            //2.5V-"1.7V"=0.8V

#ifdef MODIFY_EMPX10_ALARM_SPEC_RELAY_LN
#define ALARM_SPEC_RELAY_LN_DRIVE_FAULT         100             // unit 0.01V
#define ALARM_SPEC_RELAY_LN_WELDING             150//140        // unit 0.01V
#else
#define ALARM_SPEC_RELAY_LN_DRIVE_FAULT         140             // unit 0.01V
#define ALARM_SPEC_RELAY_LN_WELDING             250//140        // unit 0.01V
#endif
#define ALARM_SPEC_RELAY_LN_HYSTERESIS          20
#define ALARM_SPEC_RELAY_LL                     80 //24         // unit 0.01V
#define ALARM_SPEC_CURRENT_LEAK_HYSTERESIS      300

#define ALARM_RETRY_COUNT           1
#define ALARM_OC_RETRY_COUNT        4
#define ALARM_LEAK_RETRY_COUNT      1
#define ALARM_RETRY_INTERVAL_OC     60000       //  60000 ma  >> 1min
#define ALARM_RETRY_INTERVAL_LEAK   15000       //  900000 ms >> 15min
#define ALARM_RETRY_INTERVAL_OT     15000       //  900000 ms >> 15min

#define WHITE_LIST_TYPE_RFID        0x00
#define WHITE_LIST_TYPE_BLE         0x01

#define AM3352_REQ_PIN_DEBOUNCE     30

#define BLE_LOGIN_ROLE_UNKOWN       0x00
#define BLE_LOGIN_ROLE_ROOT         0x01
#define BLE_LOGIN_ROLE_USER         0x02
#define BLE_LOGIN_ROLE_VISITOR      0x03

#define WIFI_LOGIN_ROLE_UNKOWN              0x00
#define WIFI_LOGIN_ROLE_ROOT                0x01
#define WIFI_LOGIN_ROLE_USER                0x02
#define WIFI_LOGIN_ROLE_VISITOR             0x03

#define MCU_DATA      0
#define METER_DATA    1

#define CORR_GROUP_ARRAY        3

#define GROUNGING_SYSTEM_LL     0
#define GROUNGING_SYSTEM_LN     1

#define CCID_MODULE_CORMEX      0
#define CCID_MODULE_VAC         1
#define CCID_MODULE_UNKNOW      0xFF

#ifdef FUNC_AUTO_MODIFY_CCID_MODULE
#define CCID_MODULE_TYPE_STR    ((Charger.CCID_Module_Type != CCID_MODULE_CORMEX && Charger.CCID_Module_Type != CCID_MODULE_VAC) ? "UNKNOW" : (Charger.CCID_Module_Type == CCID_MODULE_CORMEX ? "CORMEX" : "VAC"))
#endif

//#define LOCAL_LINK_BLE
//#define LOCAL_LINK_WIFI

#ifdef FUNC_TEMP_SENSOR_WITH_NEG_VALUE
typedef struct TEMPERATURE
{
    int16_t SystemAmbientTemp;
    int16_t CcsConnectorTemp;


}Temperature;
#else //FUNC_TEMP_SENSOR_WITH_NEG_VALUE
typedef struct TEMPERATURE
{
    uint8_t SystemAmbientTemp;
    uint8_t CcsConnectorTemp;


}Temperature;
#endif //FUNC_TEMP_SENSOR_WITH_NEG_VALUE

typedef struct BLE_CONFIG
{
    uint8_t puk[8];
    uint8_t idRoot[32];
    uint8_t isRegPuk:1;
    uint8_t isGenPin:1;
}Ble_Config;

typedef struct WIFI_CONFIG
{
    uint8_t puk[8];
    uint8_t idRoot[32];
    uint8_t passwd[32];
    uint8_t isRegPuk:1;
    uint8_t isGenPin:1;
}Wifi_Config;

typedef struct TM
{
  uint16_t  year;
  uint8_t   month;
  uint8_t   day;
  uint8_t   hour;
  uint8_t   min;
  uint8_t   sec;
}DT;

typedef struct COEFFICIENT
{
  float     gain_v[CORR_GROUP_ARRAY];
  float     offset_v[CORR_GROUP_ARRAY];
  float     gain_c[CORR_GROUP_ARRAY];
  float     offset_c[CORR_GROUP_ARRAY];
  float     gain_leak[CORR_GROUP_ARRAY];
  float     offset_leak[CORR_GROUP_ARRAY];
  float     gain_gmi[CORR_GROUP_ARRAY];
  float     offset_gmi[CORR_GROUP_ARRAY];


  uint8_t   offsetisPosOrNeg_v[CORR_GROUP_ARRAY];
  uint8_t   offsetisPosOrNeg_c[CORR_GROUP_ARRAY];
  uint8_t   offsetisPosOrNeg_leak[CORR_GROUP_ARRAY];
  uint8_t   offsetisPosOrNeg_gmi[CORR_GROUP_ARRAY];
}Coefficient;

typedef struct ALARM_DEF
{
    uint16_t    fail;
    uint16_t    restore ;
    uint8_t     retry;
    uint8_t     isOccurInCharging;
    uint8_t     isLatch;
}Alarm_Def;

typedef struct COUNTER
{
    Alarm_Def    OV;
    Alarm_Def    UV;
    Alarm_Def    OC;
    Alarm_Def    OT;
    Alarm_Def    GF;
    Alarm_Def    LEAK;      // CORMEX
    Alarm_Def    LEAK_AC ;  // VAC
    Alarm_Def    LEAK_DC ;  // VAC
    Alarm_Def    EMO;
    Alarm_Def    RELAY;
    Alarm_Def    RELAY_Drive_Fualt;
    Alarm_Def    CP;
    Alarm_Def    LEAK_MODULE;
    Alarm_Def    AC_Drip;
    uint16_t     RST_CFG;
    uint16_t     WatchDogLeak ;
}Counter;

typedef struct LED
{
  uint8_t 	motion;                 // LED motion command reference const declare
}Led;

typedef struct SPEAKER
{
  uint8_t 	motion;                 // LED motion command reference const declare
}Speaker;

typedef struct CYCLE_INFO
{
    DT          startDateTime;      // Charging start dqte timeinfo
    uint8_t     userId[32];         // Start user ID
    uint32_t    startTimeTick;      // Time tick in million second which get from HAL_GetTick()
    uint32_t    endTimeTick;        // Same as startTimeTick
    uint32_t    meterStart;         // Cycle start meter read value;
    uint32_t    Power_Consumption;  // Charging total power(KWH) resolution is 0.01
    uint32_t    Duration;           // Charging duration unit: ms
    uint8_t     StartType;          // 0: CP detect     1: RFID     2: BLE      3: Backend
}Cycle_Info;

typedef struct RFID
{
    uint8_t	data_Sn[16];        // RFID serial number data
    uint8_t	data_Block[16];     // RFID operation block data
    uint8_t	currentCard[16];    // Current charging cycle start card number
    uint8_t	nowCard[16];        // now card number
    uint8_t     snType;             // 0: 4 bytes   1: 7 bytes  2:  10 Bytes
    uint8_t     targetBlock;        // RFID read/write target block
    uint8_t     key[6];             // RFID access key
    uint8_t     keyType;            // 0: Key-A     1: Key-B
    uint8_t     cardType;           // 1: 14443     2: 15693   3:felica
    uint8_t	Charge_data_Sn[16];        // RFID serial number data
    struct
    {
        uint8_t reqBlockRead:1;     // Read block data request flag
        uint8_t reqBlockWrite:1;    // Write block data request flag
        uint8_t reqHalt:1;          // Halt card request flag
        uint8_t reqStart:1;         // RFID start charging cycle request flag
        uint8_t reqStop:1;          // RFID stop charging cycle request flag
        uint8_t isBlockReadOK:1;    // RFID block data read ok flag
        uint8_t isBlockWriteOK:1;   // RFID block data write ok flag
        uint8_t isFelicaDelay:1;    //
    }op_bits;
}Rfid;

typedef struct BLE
{
    uint8_t initSeq;                // BLE module initial sequence
    uint8_t peripheral_mac[13];     // BLE module mac address
    uint8_t loginRole;              // BLE login role, 0: Unkown    1: Root     2: User     3: Visitor
    uint8_t loginId[32];           // BLE central login id
    uint8_t isRequestOn:1;          // BLE start charging cycle request
    uint8_t isRequestOff:1;         // BLE stop charging cycle request
    uint8_t isDataMode:1;           // BLE module is in data mode
}Ble;

typedef struct WIFI
{
    uint8_t initSeq;                // WIFI module intiail sequence
    uint8_t	module_mac[13];         // WIFI module mac address
    uint8_t loginRole;              // WIFI login role, 0: Unkown    1: Root     2: User     3: Visitor
    uint8_t loginId[32];            // WIFI central login id
	uint8_t isRequestOn:1;          // WIFI start charging cycle request
	uint8_t isRequestOff:1;         // WIFI stop charging cycle request
    uint8_t isDataMode:1;           // WIFI module is in data mode
}Wifi;

//edward 2019_08_28
typedef struct AM3352
{
    uint16_t RequestPinDebounceCnt; // CSU request action debounce counter
    uint8_t LedActionState;         // CSU LED action state
    uint32_t LedAlarmState;         // CSU LED alarm state
    uint8_t RotarySwitchState;      // CSU rotary switch
    uint16_t BreatheLedAuthdFadeIn;   //Authd breathe led fade in time
    uint16_t BreatheLedAuthdFadeOut;   //Authd breathe led fade out time
    uint16_t BreatheLedConnectFadeIn;   //Connect breathe led fade in time
    uint16_t BreatheLedConnectFadeOut;   //Connect breathe led fade out time
    uint16_t BreatheLedChargeFadeIn;   //Charge breathe led fade in time
    uint16_t BreatheLedChargeFadeOut;   //Charge breathe led fade out time

    uint8_t isPWMOn:1;              // CSU PWM action status, 0: Off    1: Off
    uint8_t isRequestOn:1;          // CSU relay action request on
    uint8_t isRequestOff:1;         // CSU relay action request off
}Am3352;

typedef struct RESERVATION
{
    uint8_t mode;                   // Reservation mode, 0:Disable     1:Once      2:Always
    uint8_t hour;                   // Reservation hour
    uint8_t min;                    // Reservation minute
}Reservation;

#ifdef MODIFY_UNDEFINED_MODEL_NAME_AND_SN_ISSUE
#define MODEL_NAME_LEN  (64)
#define SERIAL_NUM_LEN  (64)
#define SYSTEM_ID_LEN   (MODEL_NAME_LEN + SERIAL_NUM_LEN)
#endif
typedef struct MEM
{
    struct
    {
        union
        {
            uint8_t value[W25Q16FV_BLOCK_SIZE];             // 4096 byte
#ifdef FUNC_FLASH_CONFIG_APPEND_CRC
            struct
            {
                uint8_t buf[W25Q16FV_BLOCK_SIZE - 4];
                uint32_t crc32;
            } verify;
#endif
            struct
            {
#ifdef MODIFY_UNDEFINED_MODEL_NAME_AND_SN_ISSUE
                char        ModelName[MODEL_NAME_LEN];      // 32 Byte -> EVSE model name max 64 byte in ASCII
                char        SerialNumber[SERIAL_NUM_LEN];   // 32 Byte -> EVSE serial number max 64 byte in ASCII
                char        System_ID[SYSTEM_ID_LEN];       // 64 Byte -> Byte EVSE system ID max 128 bytes in ASCII
#else
                char        ModelName[64];                  // 32 Byte -> EVSE model name max 64 byte in ASCII
                char        SerialNumber[64];               // 32 Byte -> EVSE serial number max 64 byte in ASCII
                char        System_ID[128];                 // 64 Byte -> Byte EVSE system ID max 128 bytes in ASCII
#endif
                DT          SystemDateTime;                 // 7 Byte  -> EVSE system dat & time info, sync from RTC
                uint8_t     AuthMode;                       // 1 Byte  -> 0: OCPP backend , 1: free mode
                uint8_t     MCU_Control_Mode;               // 0: without CSU   1: with CSU     2: by pass
                uint16_t    MaxChargingEnergy;              // 2 Byte  -> 0: No limit      Other: Limit max energy per cycle in KWH
                uint16_t    MaxChargingCurrent;             // 2 Byte  -> 0: Rating value  Other: Linit max current output per cycle
                uint16_t    MaxChargingDuration;            // 2 Byte  -> 0: No limit      Other: Limit max duration per cycle in minutes
                uint16_t    Correction_VL1[4][2];           // 16 Byte -> Voltage L1 correction point data(adc & meter) [point][adc(0) & meter(1)]
                uint16_t    Correction_VL2[2][2];           // 8 Byte  -> Voltage L2 correction point data(adc & meter) [point][adc(0) & meter(1)]
                uint16_t    Correction_VL3[2][2];           // 8 Byte  -> Voltage L3 correction point data(adc & meter) [point][adc(0) & meter(1)]
                uint16_t    Correction_CL1[4][2];           // 16 Byte -> Current L1 correction point data(adc & meter) [point][adc(0) & meter(1)]
                uint16_t    Correction_CL2[2][2];           // 8 Byte  -> Current L2 correction point data(adc & meter) [point][adc(0) & meter(1)]
                uint16_t    Correction_CL3[2][2];           // 8 Byte  -> Current L3 correction point data(adc & meter) [point][adc(0) & meter(1)]
                uint16_t    Correction_Leak[4][2];          // 16 Byte -> Leak Current correction point data(adc & meter) [point][adc(0) & meter(1)]
                uint16_t    Correction_GMI[4][2];           // 16 Byte -> GMI correction point data(adc & meter) [point][adc(0) & meter(1)]
                uint32_t    Power_Consumption_Cumulative;   // 4 Byte  -> Power consumption keep data, resolution 0.0001 KWH (0.1W)    // V*I*3600000ms = WH  ,   0.1W *10 = W
                uint32_t    AcPlugInTimes;                  // 4 Byte  -> Connector plug counter
                uint8_t     isRestoreFactory:1;             // 1 Byte  -> 0: No action 1: Trigger EVSE restore factory value
                uint8_t     RFID_SN_Endian:1;               // 1 Byte  -> 0: Little    1: Big
                uint8_t     isAuthEnable:1;                 // 1 Byte  -> 0: User authorization disable    1: User authorizing enable
                uint8_t     isDispDebug:1;                  // 1 Byte  -> 0: Non-display debug info        1: Display debug info
                Reservation reservation;                    // 3 Byte  -> EVSE reservation config
                Ble_Config  bleConfig;                      // 41 Byte -> BLE PUK & PIN config
                Wifi_Config wifiConfig;                     // 73 Byte -> WIFI config
                uint8_t     OfflinePolicy;                  // 1 Byte  -> 0: local list , 1: Phihong RFID tag , 2: free charging , 3: no charging
            }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 backup:1;                               // Backup data to external flash request
            uint8_t restore:1;                              // Restore backup data from external flash request
        }op_bits;
    }EVSE_Config;

    struct
    {
        union
        {
            uint8_t value[W25Q16FV_BLOCK_SIZE];
            struct
            {
                uint8_t     isEnable:1;                     // Cold load pick up function is enable
                DT          DateTime;                       // Charging start date time info
                uint8_t     user[32];                       // Start user account / card number / mac address
                char        PlugType[16];                   // Plug type max 16 bytes in ASCII
                uint32_t    Duration;                       // Charging duration unit: ms
                uint16_t    powerSum;                       // Charging total power(KWH) resolution is 0.01
                uint8_t     startType;                      // 0: CP detect     1: RFID     2: BLE      3: Backend
            }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 backup:1;                               // Backup data to external flash request
            uint8_t restore:1;                              // Restore backup data from external flash request
        }op_bits;
    }coldLoadPickUp;

    struct
    {
        union
        {
            uint8_t value[W25Q16FV_BLOCK_SIZE];
            struct
            {
                uint8_t     listType;                       // 0: RFID  1: BLE
                uint8_t     pin[6];                         // Bonding pin code, max 6 digital in ASCII
                uint8_t     listID[32];                     // list ID
                uint8_t     isReg;                          // PIN already registe flag  0: non-registe     1: registed
            }item[W25Q16FV_BLOCK_SIZE>>6];                  // Sector max record only 4096/64 = 64 records.
        }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
        }op_bits;
    }whiteList;

    struct
    {
        union
        {
            uint8_t value[W25Q16FV_BLOCK_SIZE];
            struct
            {
                DT          DateTime;                       // Charging start date time info
                uint32_t    alarmCode;                      // Please refence code specification document
                uint8_t     mode;                           // EVSE last system mode before alarm occur
                uint8_t     isRecover;                      // 0: occur     1: recover
            }item[W25Q16FV_BLOCK_SIZE>>4];                  // Sector max record only 4096/16 = 256 records.
        }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 backup:1;                               // Backup data to external flash request
            uint8_t restore:1;                              // Restore backup data from external flash request
        }op_bits;
    }hisAlarm;

    struct
    {
        union
        {
            uint8_t value[W25Q16FV_BLOCK_SIZE];
            struct
            {
                DT          DateTime;                       // Charging start date time info
                uint8_t     user[32];                       // Start user account / card number / mac address
                char        PlugType[12];                   // Plug type max 16 bytes in ASCII
                uint32_t    Duration;                       // Charging duration unit: ms
                uint16_t    powerSum;                       // Charging total power(KWH) resolution is 0.01
                uint32_t    stopStatusCode;                 // Charging stop reason as status code
                uint8_t     startType;                      // 0: CP detect     1: RFID     2: BLE      3: Backend
            }item[W25Q16FV_BLOCK_SIZE>>6];                  // Sector max record only 4096/64 = 64 records.
        }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 backup:1;                               // Backup data to external flash request
            uint8_t restore:1;                              // Restore backup data from external flash request
        }op_bits;
    }hisCharging;
}Mem;

typedef struct BUTTON
{
    uint32_t count;
    uint8_t display;
}button;

typedef struct ALARMSECP
{
    uint16_t Current_LEAK_AC;

}AlarmSpec;


/* 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 */
extern volatile unsigned long  ulHighFrequencyTimerTicks;
extern void SystemClock_Config(void);
#ifdef MODIFY_CPTASK_HEAD
void Proc_CpTaskHead(HTK_BOOL bCSU);
#endif
/* USER CODE END EFP */

/* Private defines -----------------------------------------------------------*/
#define NoneUse_Pin1_GPIO_PE2_Pin GPIO_PIN_2
#define NoneUse_Pin1_GPIO_PE2_GPIO_Port GPIOE
#define OUT_BLE_RESET_Pin GPIO_PIN_3
#define OUT_BLE_RESET_GPIO_Port GPIOE
#define NoneUse_Pin3_GPIO_PE4_Pin GPIO_PIN_4
#define NoneUse_Pin3_GPIO_PE4_GPIO_Port GPIOE
#define TIM9_CH1_PWM_LED_R_Pin GPIO_PIN_5
#define TIM9_CH1_PWM_LED_R_GPIO_Port GPIOE
#define TIM9_CH2_PWM_LED_G_Pin GPIO_PIN_6
#define TIM9_CH2_PWM_LED_G_GPIO_Port GPIOE
#define OUT_PHY_Reset_Pin GPIO_PIN_13
#define OUT_PHY_Reset_GPIO_Port GPIOC
#define IN_MEM_Erase_Pin GPIO_PIN_2
#define IN_MEM_Erase_GPIO_Port GPIOF
#define ADC3_IN9_Voltage_L1_Pin GPIO_PIN_3
#define ADC3_IN9_Voltage_L1_GPIO_Port GPIOF
#define ADC3_IN14_1998_Pin GPIO_PIN_4
#define ADC3_IN14_1998_GPIO_Port GPIOF
#define ADC3_IN15_Temp_Pin GPIO_PIN_5
#define ADC3_IN15_Temp_GPIO_Port GPIOF
#define ADC3_IN4_GMI_VL1_Pin GPIO_PIN_6
#define ADC3_IN4_GMI_VL1_GPIO_Port GPIOF
#define NoneUse_Pin19_GPIO_PF7_Pin GPIO_PIN_7
#define NoneUse_Pin19_GPIO_PF7_GPIO_Port GPIOF
#define NoneUse_Pin20_GPIO_PF8_Pin GPIO_PIN_8
#define NoneUse_Pin20_GPIO_PF8_GPIO_Port GPIOF
#define ADC3_IN7_Current_L1_Pin GPIO_PIN_9
#define ADC3_IN7_Current_L1_GPIO_Port GPIOF
#define NoneUse_Pin22_GPIO_PF10_Pin GPIO_PIN_10
#define NoneUse_Pin22_GPIO_PF10_GPIO_Port GPIOF
#define NoneUse_Pin26_GPIO_PC0_Pin GPIO_PIN_0
#define NoneUse_Pin26_GPIO_PC0_GPIO_Port GPIOC
#define NoneUse_Pin27_GPIO_PC1_Pin GPIO_PIN_1
#define NoneUse_Pin27_GPIO_PC1_GPIO_Port GPIOC
#define NoneUse_Pin28_GPIO_PC2_Pin GPIO_PIN_2
#define NoneUse_Pin28_GPIO_PC2_GPIO_Port GPIOC
#define NoneUse_Pin29_GPIO_PC3_Pin GPIO_PIN_3
#define NoneUse_Pin29_GPIO_PC3_GPIO_Port GPIOC
#define NoneUse_Pin34_GPIO_PA0_Pin GPIO_PIN_0
#define NoneUse_Pin34_GPIO_PA0_GPIO_Port GPIOA
#define NoneUse_Pin35_GPIO_PA1_Pin GPIO_PIN_1
#define NoneUse_Pin35_GPIO_PA1_GPIO_Port GPIOA
#define NoneUse_Pin36_GPIO_PA2_Pin GPIO_PIN_2
#define NoneUse_Pin36_GPIO_PA2_GPIO_Port GPIOA
#define NoneUse_Pin37_GPIO_PA3_Pin GPIO_PIN_3
#define NoneUse_Pin37_GPIO_PA3_GPIO_Port GPIOA
#define ADC1_IN4_CP_Pin GPIO_PIN_4
#define ADC1_IN4_CP_GPIO_Port GPIOA
#define ADC2_IN5_Welding_Pin GPIO_PIN_5
#define ADC2_IN5_Welding_GPIO_Port GPIOA
#define ADC2_IN6_GF_Pin GPIO_PIN_6
#define ADC2_IN6_GF_GPIO_Port GPIOA
#define NoneUse_Pin43_GPIO_PA7_Pin GPIO_PIN_7
#define NoneUse_Pin43_GPIO_PA7_GPIO_Port GPIOA
#define NoneUse_Pin44_GPIO_PC4_Pin GPIO_PIN_4
#define NoneUse_Pin44_GPIO_PC4_GPIO_Port GPIOC
#define NoneUse_Pin45_GPIO_PC5_Pin GPIO_PIN_5
#define NoneUse_Pin45_GPIO_PC5_GPIO_Port GPIOC
#define NoneUse_Pin46_GPIO_PB0_Pin GPIO_PIN_0
#define NoneUse_Pin46_GPIO_PB0_GPIO_Port GPIOB
#define NoneUse_Pin47_GPIO_PB1_Pin GPIO_PIN_1
#define NoneUse_Pin47_GPIO_PB1_GPIO_Port GPIOB
#define NoneUse_Pin48_GPIO_PB2_Pin GPIO_PIN_2
#define NoneUse_Pin48_GPIO_PB2_GPIO_Port GPIOB
#define OUT_Locker_Lock_Pin GPIO_PIN_11
#define OUT_Locker_Lock_GPIO_Port GPIOF
#define IN_LOCK_Status_Pin GPIO_PIN_12
#define IN_LOCK_Status_GPIO_Port GPIOF
#define IN_CCS_HW_ID1_Pin GPIO_PIN_13
#define IN_CCS_HW_ID1_GPIO_Port GPIOF
#define IN_CCS_HW_ID2_Pin GPIO_PIN_14
#define IN_CCS_HW_ID2_GPIO_Port GPIOF
#define IN_CCS_I_O1_Pin GPIO_PIN_15
#define IN_CCS_I_O1_GPIO_Port GPIOF
#define IN_CCS_I_O2_Pin GPIO_PIN_0
#define IN_CCS_I_O2_GPIO_Port GPIOG
#define IN_GridType_Select_Pin GPIO_PIN_1
#define IN_GridType_Select_GPIO_Port GPIOG
#define IN_Grid_Groundding_Pin GPIO_PIN_7
#define IN_Grid_Groundding_GPIO_Port GPIOE
#define IN_AC_Current_Set4_Pin GPIO_PIN_8
#define IN_AC_Current_Set4_GPIO_Port GPIOE
#define IN_AC_Current_Set3_Pin GPIO_PIN_9
#define IN_AC_Current_Set3_GPIO_Port GPIOE
#define IN_AC_Current_Set2_Pin GPIO_PIN_10
#define IN_AC_Current_Set2_GPIO_Port GPIOE
#define IN_AC_Current_Set1_Pin GPIO_PIN_11
#define IN_AC_Current_Set1_GPIO_Port GPIOE
#define IN_CCS_Proximity_Pin GPIO_PIN_12
#define IN_CCS_Proximity_GPIO_Port GPIOE
#define NoneUse_Pin66_GPIO_PE13_Pin GPIO_PIN_13
#define NoneUse_Pin66_GPIO_PE13_GPIO_Port GPIOE
#define TIM1_CH4_PWM_CP_Pin GPIO_PIN_14
#define TIM1_CH4_PWM_CP_GPIO_Port GPIOE
#define OUT_SPI1_CS_Pin GPIO_PIN_15
#define OUT_SPI1_CS_GPIO_Port GPIOE
#define NoneUse_Pin69_GPIO_PB10_Pin GPIO_PIN_10
#define NoneUse_Pin69_GPIO_PB10_GPIO_Port GPIOB
#define NoneUse_Pin70_GPIO_PB11_Pin GPIO_PIN_11
#define NoneUse_Pin70_GPIO_PB11_GPIO_Port GPIOB
#define IN_GFCI_Reset_Pin GPIO_PIN_12
#define IN_GFCI_Reset_GPIO_Port GPIOB
#define NoneUse_Pin74_GPIO_PB13_Pin GPIO_PIN_13
#define NoneUse_Pin74_GPIO_PB13_GPIO_Port GPIOB
#define TIM12_CH1_PWM_GFCI_Test_Pin GPIO_PIN_14
#define TIM12_CH1_PWM_GFCI_Test_GPIO_Port GPIOB
#define OUT_Leak_Test_Pin GPIO_PIN_15
#define OUT_Leak_Test_GPIO_Port GPIOB
#define OUT_Speaker_Pin GPIO_PIN_10
#define OUT_Speaker_GPIO_Port GPIOD
#define IN_Emergency_Pin GPIO_PIN_11
#define IN_Emergency_GPIO_Port GPIOD
#define ICP_TIM4_CH1_LEAK_PWM_Pin GPIO_PIN_12
#define ICP_TIM4_CH1_LEAK_PWM_GPIO_Port GPIOD
#define NoneUse_Pin82_GPIO_PD13_Pin GPIO_PIN_13
#define NoneUse_Pin82_GPIO_PD13_GPIO_Port GPIOD
#define NoneUse_Pin85_GPIO_PD14_Pin GPIO_PIN_14
#define NoneUse_Pin85_GPIO_PD14_GPIO_Port GPIOD
#define NoneUse_Pin86_GPIO_PD15_Pin GPIO_PIN_15
#define NoneUse_Pin86_GPIO_PD15_GPIO_Port GPIOD
#define IN_BLE_DTR_Pin GPIO_PIN_2
#define IN_BLE_DTR_GPIO_Port GPIOG
#define OUT_BLE_DSR_Pin GPIO_PIN_3
#define OUT_BLE_DSR_GPIO_Port GPIOG
#define NoneUse_Pin89_GPIO_PG4_Pin GPIO_PIN_4
#define NoneUse_Pin89_GPIO_PG4_GPIO_Port GPIOG
#define NoneUse_Pin90_GPIO_PG5_Pin GPIO_PIN_5
#define NoneUse_Pin90_GPIO_PG5_GPIO_Port GPIOG
#define IN_RFID_ICC_Pin GPIO_PIN_6
#define IN_RFID_ICC_GPIO_Port GPIOG
#define NoneUse_Pin92_GPIO_PG7_Pin GPIO_PIN_7
#define NoneUse_Pin92_GPIO_PG7_GPIO_Port GPIOG
#define OUT_RFID_Reset_Pin GPIO_PIN_8
#define OUT_RFID_Reset_GPIO_Port GPIOG
#define IN_Leak_Error_Pin GPIO_PIN_6
#define IN_Leak_Error_GPIO_Port GPIOC
#define IN_Leak_AC_Pin GPIO_PIN_7
#define IN_Leak_AC_GPIO_Port GPIOC
#define IN_Leak_DC_Pin GPIO_PIN_8
#define IN_Leak_DC_GPIO_Port GPIOC
#define OUT_RelayDrv_L1_Pin GPIO_PIN_9
#define OUT_RelayDrv_L1_GPIO_Port GPIOC
#define OUT_RelayHold_L1_Pin GPIO_PIN_8
#define OUT_RelayHold_L1_GPIO_Port GPIOA
#define OUT_RelayDrv_L2_Pin GPIO_PIN_9
#define OUT_RelayDrv_L2_GPIO_Port GPIOA
#define OUT_RelayHold_L2_Pin GPIO_PIN_10
#define OUT_RelayHold_L2_GPIO_Port GPIOA
#define IN_1998_Pin GPIO_PIN_15
#define IN_1998_GPIO_Port GPIOA
#define OUT_1998_Pin GPIO_PIN_10
#define OUT_1998_GPIO_Port GPIOC
#define OUT_Meter_485_RE_Pin GPIO_PIN_11
#define OUT_Meter_485_RE_GPIO_Port GPIOC
#define NoneUse_Pin114_GPIO_PD0_Pin GPIO_PIN_0
#define NoneUse_Pin114_GPIO_PD0_GPIO_Port GPIOD
#define NoneUse_Pin115_GPIO_PD1_Pin GPIO_PIN_1
#define NoneUse_Pin115_GPIO_PD1_GPIO_Port GPIOD
#define OUT_USB_DriveBus_Pin GPIO_PIN_3
#define OUT_USB_DriveBus_GPIO_Port GPIOD
#define OUT_Meter_485_DE_Pin GPIO_PIN_4
#define OUT_Meter_485_DE_GPIO_Port GPIOD
#define IN_ACT_REQ_Pin GPIO_PIN_7
#define IN_ACT_REQ_GPIO_Port GPIOD
#define IN_USB_OC_Pin GPIO_PIN_10
#define IN_USB_OC_GPIO_Port GPIOG
#define NoneUse_Pin126_GPIO_PG11_Pin GPIO_PIN_11
#define NoneUse_Pin126_GPIO_PG11_GPIO_Port GPIOG
#define OUT_IPP_TRI_Pin GPIO_PIN_12
#define OUT_IPP_TRI_GPIO_Port GPIOG
#define NoneUse_Pin128_GPIO_PG13_Pin GPIO_PIN_13
#define NoneUse_Pin128_GPIO_PG13_GPIO_Port GPIOG
#define IN_Shutter_Status_Pin GPIO_PIN_15
#define IN_Shutter_Status_GPIO_Port GPIOG
#define TIM10_CH1_PWM_LED_B_Pin GPIO_PIN_8
#define TIM10_CH1_PWM_LED_B_GPIO_Port GPIOB
#define TIM11_CH1_PWM_LED_W_Pin GPIO_PIN_9
#define TIM11_CH1_PWM_LED_W_GPIO_Port GPIOB
#define OUT_StateE_Pin GPIO_PIN_0
#define OUT_StateE_GPIO_Port GPIOE
#define OUT_LED_Breathe_Pin GPIO_PIN_1
#define OUT_LED_Breathe_GPIO_Port GPIOE
/* USER CODE BEGIN Private defines */
extern struct EVSE
{
    char        Ver_FW[32];                     // Firmware version max 32 byte in ASCII
    char        Ver_HW[32];                     // Hardware version max 32 byte in ASCII
    uint8_t     Mode;                           // System mode
    uint8_t     Mode_Previous;                  // Previous system mode
    uint8_t     Mode_Before_Alarm;              // System mode before enter alarm mode
    uint32_t    AC_DripisTure;  	        // AC Drip is happen.  0:None  1:is happen AC Drip
    uint32_t    Alarm_Code;  	                // System alarm code
    uint32_t    Alarm_LED;  	                // System alarm LED
    uint32_t    Alarm_Code_Previous;            // Previous system alarm code
    uint8_t     CP_State;                       // Control pilot state
    uint16_t    Voltage[3];                     // EVSE input voltage resolution 0.01V
    uint16_t    Current[3];                     // EVSE output current resolution 0.01A
    uint16_t    Leak_Current;                   // EVSE output leak current resolution  mA
    uint16_t    Gmi_Voltage;                    // EVSE output Gmi_Voltage V
    uint8_t     maxRatingCurrent;               // EVSE Max rating current resolution 1A
    uint16_t    MaxChargingCurrent_Previous;    // EVSE previous current limit resolution 1A
    uint8_t     Relay_Action;                   // EVSE output relay action
    uint8_t     Relay_isOperationCompleted;     // EVSE output relay action is Operation completed
    uint8_t     is_RFIDKeepCharger;             // in ocpp mode no RFID.reqStop . CP C->B->C , charger can be charge
    float       GmiMagn100K ;                   // Gmi alarm 100k VAC*Magnification
    float       GmiMagn50K ;                    // Gmi alarm 50k VAC*Magnification
    Temperature temperature;                    // EVSE temperature resolution 1 degree
    Coefficient coefficient;                    // ADC corrention coefficient
    Cycle_Info  cycle_info;                     // Charging cycle info
    Rfid rfid;                                  // RFID related info
    Ble ble;                                    // BLE related info
    Wifi wifi;                                  // WIFI related info
    Am3352 am3352;                              // CSU info
    Led led_R;                                  // LED red info
    Led led_G;                                  // LED green info
    Led led_B;                                  // LED blue info
    Led led_W;                                  // LED white info
    Led led_Breathe;                            // LED breathe info
    Speaker speaker;                            // Speaker info
    Mem memory;                                 // External flash memory info
    Counter counter;                            // Alarm delay counter
    AlarmSpec   alarm_spec;                      // alarm spec data for modelname Decide , kaho 20200303 add
    uint8_t     Led_Mode;                       // set led mode >> idle , handshaking , charging , stop , alarm
    uint8_t     Led_temp_motion;                // OFF : RFID led prompt OFF , ON :RFID LED prompt ON
    uint8_t     Led_Brightness[12];
    uint16_t    led_pwm_duty[4];
    uint8_t     GroundingSystem;                // different grounding -> LL , LN
    uint8_t     GroundingDetect;                // Ground Detect On-> 1, Off-> 0
    uint8_t     isSetStartTime;                 // 1 : need set Charger.cycle_info.startTimeTick , 0 : Useless Charger.cycle_info.startTimeTick
    uint8_t     CCID_Module_Type;               // 0 : Cormex , 1 : VAC
    uint8_t     Test_LeakModuleisPass;          // 0 : fail , 1 : success
    uint8_t     is_BLEKeepCharger;              // in occp mode no ble.reqStop . CP C->B->C , charger can be charge
    uint32_t    Alarm_Code_before;              // last time system alarm code
    uint32_t    OCP_Magnification;              // EC satet use 1.25 , UL GB use 1.1


    uint16_t    isDebugEnable:1;                // EVSE enable debug mode   0: Disable      1: Enable
    uint16_t    isAlarmSetPWM:1;                // Alarm -> charger set pwm  0: PWM is never output   1: PWM is output
    uint16_t    isRotarySwitchError:1;          // Rotary Switch set 9~E is Error 0: Error       1: OK
    uint16_t    isTestLeakModule:1;             // 0: don't test leak module , 1: do test leak module
    uint16_t    isDebugModeCPtest:1;            // in debug mode use CP Simulator simulate CP -> 0: CP test off , 1: CP tset on
    uint16_t    istoHandShakeMode:1;            // for rfid MODE -> MODE_IDLE to MODE_HANDSHAKE flag
    uint16_t    isCP_in_B:1;                    // CP status had been in B mode
    uint16_t    CSUisRequest:1 ;                // 1 : CSU is Request , 0 : Request No  Request // when Charger.am3352.isRequestOn 1->0 PWM pull 100%
    uint16_t    ModelReadisOK:1 ;               // 0 : NG  , 1 : OK
    uint16_t    isBle_disable:1;                //
    uint16_t    isUpgrading:1;                  //
    uint16_t    isSelfTesting:1;                //
    uint16_t    CSUisReady:1;                   // CSU is reboot OK (init -> idle)
#ifdef FUNC_RS485_SLAVE
    uint16_t    m_bRS485SlaveForDCModel:1;      // 0: Not for DC Model; 1: for DC Model
#endif

#ifdef FUNC_FORCE_RUN_CSU_MODE_WITH_DC_MODELNAME
    uint16_t    m_bModelNameDC:1;               //0: Not DC ModelName; 1: DC ModelName
    uint16_t    m_bModelNameDCReadOK:1;
#endif

#ifdef MODIFY_OCP_PROC_WITH_MODELNAME_IDX_3_EURO_SPEC
    uint16_t    m_bModelNameWithEuroSpecOCP:1;
#endif

//#ifdef NEW_CP_SPEC
    uint16_t    m_bCpDetectModuleInitOK: 1; //0: CpDatectModule not init; 1: CpDatectModule init OK
//#endif

#ifdef FUNC_RULE_GB_202108
    uint16_t    m_bSafetyRegulationGB:1;
#endif

#ifdef FUNC_BLOCK_CSU_CONFIG_CP_PWM_DUTY
    uint16_t    m_bBlockCsuCpPwmDuty: 1;
#endif

#ifdef FUNC_BLOCK_CSU_CMD_SET_DUTY_WHEN_ALARM_CP_F_STATE
    uint16_t    m_bBlockCsuCpPwmDutyWhenAlarmCpF: 1;
#endif

#ifdef FUNC_SIMU_CHARGE_CURRENT_OUTPUT
    uint16_t    m_bEnableSimuChargeCurrent: 1;
#endif

#ifdef FUNC_SWITCH_RELAY_WELDING_PROTECT
    uint16_t    m_bDisableRelayWelding: 1;
    uint16_t    m_bDisableCCID: 1;
    uint16_t    m_bSetOCP_32A: 1;
#endif

#ifdef FUNC_RS485_SLAVE
    uint8_t m_RS485SlaveAddr;
#endif
#ifdef FUNC_CURRENT_CP_PWM_DATA
    uint16_t m_CP_CurPWM;
#endif

#ifdef FUNC_OCP_WITH_PP
    uint16_t m_OCP_CurMaxCurr;      //A
    uint16_t m_OCP_BegThreshold;    //Occur:    0.01A
    uint16_t m_OCP_EndThreshold;    //Recover:  0.01A
#endif

#ifdef MODIFY_AC_EVSE_STATUS_MAX_CHARGING_CURRENT_VARIABLE
    uint16_t    AC_MaxChargingCurrentOrDuty; //AC Only
#endif

#ifdef FUNC_GET_TRIP_TIME
    uint32_t m_TripBeg;
    uint32_t m_TripEnd;
#endif

#ifdef FUNC_AUTO_MODIFY_CCID_MODULE
    HTK_BOOL m_bCCID_MustModify;
    uint8_t m_CCID_ModuleTestOK;
#endif

#ifdef FUNC_RELAY_OFF_WHEN_CSU_CMD_TIMEOUT
    u32 m_CSU_RxTick;
#endif

#ifdef FUNC_BOOT_TICK
    uint32_t    m_BootTick;
#endif

#ifdef FUNC_EMPX10_CUSTOMIZE_RS485_CLIENT_ADDRESS
    u8 m_EMPX10_RS485_ClientAddr;
#endif

}Charger;

//---------------------------------------------------------------

#ifndef DISABLE_CCID_PROTECT
#define CCID_PROTECT
#endif

#ifndef DISABLE_OTP_PROTECT
#define OTP_PROTECT
#endif

#ifndef DISABLE_CP_ALARM_PROTECT
#define CP_ALARM_PROTECT
#endif

#ifndef DISABLE_GROUND_FAULT_PROTECT
#define GROUND_FAULT_PROTECT
#endif

#define EMC_BUTTON_PROTECT

#ifndef DISABLE_RELAY_WELDING_PROTECT
#define RELAY_WELDING_PROTECT
#endif

#ifndef DISABLE_OVP_PROTECT
#define OVP_PROTECT
#endif

#ifndef DISABLE_UVP_PROTECT
#define UVP_PROTECT
#endif

#ifndef DISABLE_OCP_PROTECT
#define OCP_PROTECT
#endif

#ifndef DISABLE_HANDSHAKE_PROTECT
#define HANDSHAKE_PROTECT
#endif

#define ROTATE_SWITCH_CHECK

#ifndef DISABLE_COLD_LOAD_PICKUP
#define COLD_LOAD_PICKUP
#endif


//---------------------------------------------------------------

/* USER CODE END Private defines */

#ifdef __cplusplus
}
#endif

#endif /* __MAIN_H */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
