#ifndef _COMMON_H_
#define _COMMON_H_

#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include "gd32f10x.h"
#include "gd32f10x_libopt.h"
#include "delay.h"

#define DEBUG_MODE    (1)
 
#define CONFIG_CAN_IAP
#define USART_BAUND    (38400)
#define NVIC_OFFSET    (0x00)
#define THER_NUM_CON		2u


#define SN_FLASH_ADDRESS   (2048)
#define SYLC_FLASH_ADDRESS   (SN_FLASH_ADDRESS + 1024)
#define HARD_TEST_FLASH_ADDRESS   (SYLC_FLASH_ADDRESS + 1024)

#define PS100_SERIAL_NUM_SIZE                      18
#define PS100_SOFTWARE_SIZE                        20
extern const uint8_t soft_version[];
extern uint8_t sn[PS100_SERIAL_NUM_SIZE];

typedef struct
{
	uint8_t set;
	uint16_t count;
}DELAY_COMMON;

typedef struct
{
	uint8_t enable;
	uint8_t set;
	uint16_t count;
}CTR_DELAY_COMMON;

enum
{
	WS_BATTER_BING_LIAN,
	WS_BATTER_CHUAN_LIAN,

	
	WS_BATTER_MAX
};



#define NO_EVENT                     0x00000000UL
#define RS485_RECEIVE_END_EVENT      0x00000001UL
#define EVENT_CAN_RECEIVE_FINISH	 0x00000002UL
#define RS485_2_RECEIVE_END_EVENT    0x00000004UL
#define SUB_BMS_1_RS485_DISC_EVENT   0x00000008UL
#define SUB_BMS_2_RS485_DISC_EVENT   0x00000010UL
#define MEASURE_TEMPERATURE_EVENT    0x00000020UL
#define SUB_BMS_1_SEND_CMD_EVENT     0x00000040UL
#define SUB_BMS_2_SEND_CMD_EVENT     0x00000080UL
#define BMS_1_RESEND_CMD_EVENT       0x00000100UL
#define BMS_2_RESEND_CMD_EVENT       0x00000200UL
#define END_CTR_RESEND_CMD_EVENT     0x00000400UL
#define ADAS_RESEND_CMD_EVENT        0x00000800UL
#define BMS_1_SELF_TIMEOUT_EVENT     0x00001000UL
#define BMS_2_SELF_TIMEOUT_EVENT     0x00002000UL
#define END_CTR_SELF_TIMEOUT_EVENT   0x00004000UL
#define ADAS_SELF_TIMEOUT_EVENT      0x00008000UL
#define ADAS_PWM_1_TIMEOUT_EVENT     0x00010000UL
#define ADAS_PWM_2_TIMEOUT_EVENT     0x00020000UL
#define ADAS_MEAS_1_FINISH_EVENT     0x00040000UL
#define ADAS_MEAS_2_FINISH_EVENT     0x00080000UL
#define RE_INITIAL_CAN_EVENT         0x00100000UL
#define ENTER_SLEEP_EVENT            0x00200000UL
#define DEVICE_REBOOT_EVENT          0x00400000UL
#define SAVE_PARAM_EVENT             0x00800000UL

extern uint32_t g_event;



#define ST_NORMAL						(0x0000u)
#define ST_OVRCHRG_VOL					(0x0001u)
#define ST_OVRDISCHRG_VOL				(0x0002u)
#define ST_SMALL_CURRENT_OVER			(0x0004u)
#define ST_SHORT_CUR					(0x0008u)

#define ST_CHRG_CUR						(0x0010u)
#define ST_DISCHRG_CUR					(0x0020u)
#define ST_OVRCHRG_CUR					(0x0040u)
#define ST_OVRDISCHRG_CUR				(0x0080u)

#define ST_TOTAL_CHRG					(0x0100u)
#define ST_CELLDIFF_CHRG				(0x0200u)
#define ST_PSAVE						(0x0400u)
#define ST_PDOWN						(0x0800u)

#define ST_UDR_TEMPE_CHRG				(0x1000u)//Detect the charge inhibit temperature (high)
#define ST_UDR_TEMPE_DISCHRG			(0x2000u)
#define ST_OVR_TEMPE_CHRG				(0x4000u)
#define ST_OVR_TEMPE_DISCHRG			(0x8000u)

//#define HAN_GUO_VERSION

uint8_t Get_RS485_CRC(uint8_t *data,uint16_t size);

void Writer_SN(uint8_t *data);


#endif

