#ifndef __USER_NFC_H_
#define __USER_NFC_H_

#ifdef __cplusplus
 extern "C" {
#endif
#include <stdio.h>
#include <stdlib.h>
#include "define.h"
#include "fm175xx.h"

#define USER_NFC_INTERVAL						            10

#define USER_NFC_MAX_COUNT									5

#define USER_NFC_GPIO_RCC_ENABLE					 		RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD, ENABLE);

#define NFC_01_CS_GPIO                  					GPIOB
#define NFC_01_CS_PIN                     					GPIO_Pin_7
#define NFC_01_CS_ON                       					GPIO_WriteBit(NFC_01_CS_GPIO, NFC_01_CS_PIN, Bit_RESET);
#define NFC_01_CS_OFF                      					GPIO_WriteBit(NFC_01_CS_GPIO, NFC_01_CS_PIN, Bit_SET);

#define NFC_01_RST_GPIO                  					GPIOB
#define NFC_01_RST_PIN                     					GPIO_Pin_6
#define NFC_01_RST_DISABLE                       			GPIO_WriteBit(NFC_01_RST_GPIO, NFC_01_RST_PIN, Bit_SET);
#define NFC_01_RST_ENABLE                      				GPIO_WriteBit(NFC_01_RST_GPIO, NFC_01_RST_PIN, Bit_RESET);
	 
#define NFC_02_CS_GPIO                  					GPIOB
#define NFC_02_CS_PIN                     					GPIO_Pin_9
#define NFC_02_CS_ON                       					GPIO_WriteBit(NFC_02_CS_GPIO, NFC_02_CS_PIN, Bit_RESET);
#define NFC_02_CS_OFF                      					GPIO_WriteBit(NFC_02_CS_GPIO, NFC_02_CS_PIN, Bit_SET);
	 
#define NFC_02_RST_GPIO                  					GPIOB
#define NFC_02_RST_PIN                     					GPIO_Pin_8
#define NFC_02_RST_DISABLE                       			GPIO_WriteBit(NFC_02_RST_GPIO, NFC_02_RST_PIN, Bit_SET);
#define NFC_02_RST_ENABLE                      				GPIO_WriteBit(NFC_02_RST_GPIO, NFC_02_RST_PIN, Bit_RESET);

#define NFC_03_CS_GPIO                  					GPIOC
#define NFC_03_CS_PIN                     					GPIO_Pin_5
#define NFC_03_CS_ON                       					GPIO_WriteBit(NFC_03_CS_GPIO, NFC_03_CS_PIN, Bit_RESET);
#define NFC_03_CS_OFF                      					GPIO_WriteBit(NFC_03_CS_GPIO, NFC_03_CS_PIN, Bit_SET);

#define NFC_03_RST_GPIO                  					GPIOC
#define NFC_03_RST_PIN                     					GPIO_Pin_4
#define NFC_03_RST_DISABLE                       			GPIO_WriteBit(NFC_03_RST_GPIO, NFC_03_RST_PIN, Bit_SET);
#define NFC_03_RST_ENABLE                      				GPIO_WriteBit(NFC_03_RST_GPIO, NFC_03_RST_PIN, Bit_RESET);
	 
#define NFC_04_CS_GPIO                  					GPIOB
#define NFC_04_CS_PIN                     					GPIO_Pin_1
#define NFC_04_CS_ON                       					GPIO_WriteBit(NFC_04_CS_GPIO, NFC_04_CS_PIN, Bit_RESET);
#define NFC_04_CS_OFF                      					GPIO_WriteBit(NFC_04_CS_GPIO, NFC_04_CS_PIN, Bit_SET);
	 
#define NFC_04_RST_GPIO                  					GPIOB
#define NFC_04_RST_PIN                     					GPIO_Pin_0
#define NFC_04_RST_DISABLE                       			GPIO_WriteBit(NFC_04_RST_GPIO, NFC_04_RST_PIN, Bit_SET);
#define NFC_04_RST_ENABLE                      				GPIO_WriteBit(NFC_04_RST_GPIO, NFC_04_RST_PIN, Bit_RESET);

#define NFC_05_CS_GPIO                  					GPIOB
#define NFC_05_CS_PIN                     					GPIO_Pin_14
#define NFC_05_CS_ON                       					GPIO_WriteBit(NFC_05_CS_GPIO, NFC_05_CS_PIN, Bit_RESET);
#define NFC_05_CS_OFF                      					GPIO_WriteBit(NFC_05_CS_GPIO, NFC_05_CS_PIN, Bit_SET);
	 
#define NFC_05_RST_GPIO                  					GPIOB
#define NFC_05_RST_PIN                     					GPIO_Pin_12
#define NFC_05_RST_DISABLE                       			GPIO_WriteBit(NFC_05_RST_GPIO, NFC_05_RST_PIN, Bit_SET);
#define NFC_05_RST_ENABLE                      				GPIO_WriteBit(NFC_05_RST_GPIO, NFC_05_RST_PIN, Bit_RESET);

#define ID_LOSE_DELAY_COUNT									2

typedef enum{
	NFC_TYPE_RST_START,
	NFC_TYPE_RST_END,
	NFC_TYPE_INIT,
	NFC_TYPE_CARD_CHECK,
}nfc_work_type_e;

typedef enum{
	NFC_CARD_NULL,
	NFC_CARD_IN,
	NFC_CARD_NO,
}nfc_card_stat_e;

// typedef struct{
// 	nfc_work_type_e work_type;
// 	uint32_t nfc_delay_count;
// 	bool left_init_stat;
// 	bool right_init_stat;
// 	uint8_t left_loss_delay;
// 	uint8_t right_loss_delay;
// 	uint8_t left_ver;
// 	uint32_t left_ID;
// 	uint8_t right_ver;
// 	uint32_t right_ID;
// 	nfc_card_stat_e left_card_stat;
// 	nfc_card_stat_e right_card_stat;
// 	unsigned char left_idA[8];
// 	unsigned char right_idA[8];
// 	unsigned char left_idB[10];
// 	unsigned char right_idB[10];
// 	bool left_charge_stat;
// 	bool right_charge_stat;
// }nfc_work_s;

typedef struct{
	bool init_stat;
	uint8_t version;
	uint8_t loss_delay;
	nfc_card_stat_e card_stat;
	bool elecStat;
	unsigned char idA[8];
	unsigned char idB[10];
}nfc_param_s;

typedef struct{
	nfc_work_type_e work_type;
	uint32_t nfc_delay_count;
	nfc_param_s nfc_param[USER_NFC_MAX_COUNT];
}nfc_work_s;

typedef enum
{
    NFC_QUEUE_TIME		= 0,
}USER_NFC_QUEUE_TYPE;

unsigned char ReadRawRC(unsigned char Address);
void WriteRawRC(unsigned char Address, unsigned char value);
void SetBitMask(unsigned char reg,unsigned char mask);
void ClearBitMask(unsigned char reg,unsigned char mask);

void NFC_HardReset(void);
void NFC_HardPowerdown(uint8_t mode);

typedef void (*vUser_nfc_upload_card_stat_func)(uint8_t idx, bool stat, unsigned char *card);

void vUser_nfc_init(void);
void vUser_nfc_task_start(void);
void vUser_nfc_set_upload_key_stat_func(vUser_nfc_upload_card_stat_func func);
void vUser_nfc_set_charge_stat(uint8_t idx, bool stat);
void vUser_nfc_start_work(uint8_t addr);

#ifdef __cplusplus
}
#endif

#endif /* __USER_NFC_H_ */
