#ifndef __TFDC2214_H__
#define __TFDC2214_H__
#include <stdint.h>


//#include "bianliang.h"
#define TPIT               3.1415926
#define TClockFrequency   40000000 //时钟频率

#define TSWAP16(data)    (uint32_t)((((uint16_t)(data) & (0xFF<<0 ))<<8)|(((uint32_t)(data) & (0xFF<<8))>>8))
#define TFDC2214_Addr                 (0x2A)
#define TFDC2214_DATA_CH0             (0x00)
#define TFDC2214_DATA_LSB_CH0         (0x01)
#define TFDC2214_DATA_CH1             (0x02)
#define TFDC2214_DATA_LSB_CH1         (0x03)
#define TFDC2214_DATA_CH2             (0x04)
#define TFDC2214_DATA_LSB_CH2         (0x05)
#define TFDC2214_DATA_CH3             (0x06)
#define TFDC2214_DATA_LSB_CH3         (0x07)
#define TFDC2214_RCOUNT_CH0           (0x08)
#define TFDC2214_RCOUNT_CH1           (0x09)
#define TFDC2214_RCOUNT_CH2           (0x0A)
#define TFDC2214_RCOUNT_CH3           (0x0B)
#define TFDC2214_SETTLECOUNT_CH0      (0x10)
#define TFDC2214_SETTLECOUNT_CH1      (0x11)
#define TFDC2214_SETTLECOUNT_CH2      (0x12)
#define TFDC2214_SETTLECOUNT_CH3      (0x13)
#define TFDC2214_CLOCK_DIVIDERS_CH0   (0x14)
#define TFDC2214_CLOCK_DIVIDERS_CH1   (0x15)
#define TFDC2214_CLOCK_DIVIDERS_CH2   (0x16)
#define TFDC2214_CLOCK_DIVIDERS_CH3   (0x17)
#define TFDC2214_STATUS               (0x18)
#define TFDC2214_ERROR_CONFIG         (0x19)
#define TFDC2214_CONFIG               (0x1A)
#define TFDC2214_MUX_CONFIG           (0x1B)
#define TFDC2214_RESET_DEV            (0x1C)
#define TFDC2214_DRIVE_CURRENT_CH0    (0x1E)
#define TFDC2214_DRIVE_CURRENT_CH1    (0x1F)
#define TFDC2214_DRIVE_CURRENT_CH2    (0x20)
#define TFDC2214_DRIVE_CURRENT_CH3    (0x21)
#define TFDC2214_MANUFACTURER_ID      (0x7E)
#define TFDC2214_DEVICE_ID            (0x7F)

#define TFDC2214_ID                   (0x3055)


typedef enum
{
    TFDC2214_Channel_0 = 0x00,
    TFDC2214_Channel_1 = 0x01,
    TFDC2214_Channel_2 = 0x02,
    TFDC2214_Channel_3 = 0x03
} TFDC2214_channel_t;


typedef enum
{
    TFDC2214_Channel_Sequence_0_1      = 0x00,
    TFDC2214_Channel_Sequence_0_1_2    = 0x01,
    TFDC2214_Channel_Sequence_0_1_2_3  = 0x02,
} TFDC2214_channel_sequence_t;


typedef enum
{
    TFDC2214_Bandwidth_1M   = 0x01, //1MHz
    TFDC2214_Bandwidth_3_3M = 0x04, //3.3MHz
    TFDC2214_Bandwidth_10M  = 0x05, //10MHz
    TFDC2214_Bandwidth_33M  = 0x07  //33MHz
} TFDC2214_filter_bandwidth_t;


typedef enum
{
    TFDC2214_Drive_Current_0_016 = 0x00, //0.016mA
    TFDC2214_Drive_Current_0_018 = 0x01, //0.018mA
    TFDC2214_Drive_Current_0_021 = 0x02, //0.021mA
    TFDC2214_Drive_Current_0_025 = 0x03, //0.025mA
    TFDC2214_Drive_Current_0_028 = 0x04, //0.028mA
    TFDC2214_Drive_Current_0_033 = 0x05, //0.033mA
    TFDC2214_Drive_Current_0_038 = 0x06, //0.038mA
    TFDC2214_Drive_Current_0_044 = 0x07, //0.044mA
    TFDC2214_Drive_Current_0_052 = 0x08, //0.052mA
    TFDC2214_Drive_Current_0_060 = 0x09, //0.060mA
    TFDC2214_Drive_Current_0_069 = 0x0A, //0.069mA
    TFDC2214_Drive_Current_0_081 = 0x0B, //0.081mA
    TFDC2214_Drive_Current_0_093 = 0x0C, //0.093mA
    TFDC2214_Drive_Current_0_108 = 0x0D, //0.108mA
    TFDC2214_Drive_Current_0_126 = 0x0E, //0.126mA
    TFDC2214_Drive_Current_0_146 = 0x0F, //0.146mA
    TFDC2214_Drive_Current_0_169 = 0x10, //0.169mA
    TFDC2214_Drive_Current_0_196 = 0x11, //0.196mA
    TFDC2214_Drive_Current_0_228 = 0x12, //0.228mA
    TFDC2214_Drive_Current_0_264 = 0x13, //0.264mA
    TFDC2214_Drive_Current_0_307 = 0x14, //0.307mA
    TFDC2214_Drive_Current_0_356 = 0x15, //0.356mA
    TFDC2214_Drive_Current_0_413 = 0x16, //0.413mA
    TFDC2214_Drive_Current_0_479 = 0x17, //0.479mA
    TFDC2214_Drive_Current_0_555 = 0x18, //0.555mA
    TFDC2214_Drive_Current_0_644 = 0x19, //0.644mA
    TFDC2214_Drive_Current_0_747 = 0x1A, //0.747mA
    TFDC2214_Drive_Current_0_867 = 0x1B, //0.867mA
    TFDC2214_Drive_Current_1_006 = 0x1C, //1.006mA
    TFDC2214_Drive_Current_1_167 = 0x1D, //1.167mA
    TFDC2214_Drive_Current_1_354 = 0x1E, //1.354mA
    TFDC2214_Drive_Current_1_571 = 0x1F  //1.571mA
} TFDC2214_drive_current_t;

extern uint32_t ch0,ch1,ch2,ch3;
uint8_t TFDC2214_Init(void);
void TFDC2214_Write16(uint8_t Slve_Addr, uint8_t reg, uint16_t data);
uint16_t TFDC2214_Read16(uint8_t Slve_Addr, uint8_t reg);
uint8_t TFDC2214_GetChannelData(TFDC2214_channel_t channel, uint32_t *data);
void TFDC2214_SetRcount(TFDC2214_channel_t channel, uint16_t rcount);
void TFDC2214_SetSettleCount(TFDC2214_channel_t channel, uint16_t count);
void TFDC2214_SetChannelClock(TFDC2214_channel_t channel, uint8_t frequency_select, uint16_t divider);
void TFDC2214_SetINTB(uint8_t mode);
void TFDC2214_SetActiveChannel(TFDC2214_channel_t channel);
void TFDC2214_SetSleepMode(uint8_t mode);
void TFDC2214_SetCurrentMode(uint8_t mode);
void TFDC2214_SetClockSource(uint8_t src);
void TFDC2214_SetHighCurrentMode(uint8_t mode);
void TFDC2214_SetMUX_CONFIG(uint8_t autoscan, TFDC2214_channel_sequence_t channels, TFDC2214_filter_bandwidth_t bandwidth);
void TFDC2214_Reset(void);
void TFDC2214_SetDriveCurrent(TFDC2214_channel_t channel, TFDC2214_drive_current_t current);
double TFDC2214_CalculateFrequency(TFDC2214_channel_t channel, uint32_t datax);
double TFDC2214_CalculateCapacitance(double frequency, float inductance, float capacitance);
double Tgetcap(uint8_t channel,double dg,double dr);

#endif
