#ifndef DURAADC_INC
#define DURAADC_INC		1

#include	<msc_config.h>

//External Vars
extern uchar sys_message_enable;
extern uchar dura_led_enable;
extern uchar dura_atod_pwr;
extern double SHHCoefs[5];

//External Prototypes
void 	DelaySecs( ushort secs );
void 	DelayMilliSecs( ushort msecs );
double 	rtot(double r, double a[]);
short 	Sleep(ulong us, bool absolute);
int 	LogErrorMsg(char *emsg, int ecode, uchar outp);
void 	FloatVectorMeanStdDev(ushort end, float data[], float *mean, float *stdev);

// Cirrus Logic CS5506 20Bit AtoD - defines

#define CS5506_VREF         2.048
#define CS5506_BIPOLAR_DIV  0x7FFFF             // 2^19 - 1 COUNTS
#define CS5506_UNIPOLAR_DIV 0xFFFFF             // 2^20 - 1 COUNTS
#define CS5506_CONV_TIMEOUT 75000     			// 75ms Data Conversion Timeout (CF2 elapsed timer func uses us)

// TCA6424 Port0 Bit Values to select A/D Mux Channel
#define CS5506_AIN1         0x00
#define CS5506_AIN2         0x01
#define CS5506_AIN3         0x02
#define CS5506_AIN4         0x03

// TCA6424 Port0 Bit Value to select A/D Sleep Mode
#define CS5506_ACTIVE       0x01
#define CS5506_SLEEP        0x02

// TCA6424 Port0 Bit Value to select A/D Bipolar/Unipolar Mode
#define CS5506_UNIPOLAR     0x01
#define CS5506_BIPOLAR      0x02

// Analog Devices ADG1609 4ch Mux - defines 

// TCA6424 Port1 Bit Values to select Analog Mux Channel
#define ADG1609_AIN1        0x00
#define ADG1609_AIN2        0x01
#define ADG1609_AIN3        0x02
#define ADG1609_AIN4        0x03

// Linear Tech LTC2050 and LTC1152 Zero-Drift OpAmps with High Z Shutdown 

#define VK_BUFFER           0x01
#define VRS_BUFFER          0x02
#define TEMP_BUFFER         0x03

// Linear Tech LTC6915 Zero-Drift Instrumentation Amp with Programmable Gain and Shutdown 

// TCA6424 Port1 Bit Values to select IAmp Gain
#define LTC6915_G0          0x00
#define LTC6915_G1          0x08
#define LTC6915_G2          0x10
#define LTC6915_G4          0x18
#define LTC6915_G8          0x20
#define LTC6915_G16         0x28
#define LTC6915_G32         0x30
#define LTC6915_G64         0x38
#define LTC6915_G128        0x40
#define LTC6915_G256        0x48
#define LTC6915_G512        0x50
#define LTC6915_G1024       0x58
#define LTC6915_G2048       0x60
#define LTC6915_G4096       0x68

// MBARI DURAFET INTERFACE ENUMERATED ATOD VALUES
#define DURAFET_VRS_EXT     0x01    //Ext Reference Electrode To Source Voltage (bipolar reading)
#define DURAFET_VRS_INT     0x02    //Int Reference Electrode To Source Voltage (bipolar reading)
#define DURAFET_VK          0x03    //Counter Electrode Output Voltage (bipolar reading)
#define DURAFET_TEMP        0x04    //Thermistor Voltage Divider (unipolar reading with G=1)
#define DURAFET_BATT        0x05    //Backup Battery Voltage Divider (unipolar reading with G=1)
#define DURAFET_1WIRE_TEMP  0x06    //Remote Temperature Reading of DuraFET Die for High Pressure Version
#define DURAFET_IK_4096     0x07    //Counter Electrode Output Current (source or sink, i.e., bipolar reading with G=4096, 31.25nA FS)
#define DURAFET_IB_4096     0x08    //DuraFET Die Substrate Current (source or sink, i.e., bipolar reading with G=4096, 31.25nA FS)
#define DURAFET_IK_2048     0x09    //Counter Electrode Output Current (source or sink, i.e., bipolar reading with G=2048, 62.5nA FS)
#define DURAFET_IB_2048     0x0A    //DuraFET Die Substrate Current (source or sink, i.e., bipolar reading with G=2048, 62.5nA FS)
#define DURAFET_IK_1024     0x0B    //Counter Electrode Output Current (source or sink, i.e., bipolar reading with G=1024, 125nA FS)
#define DURAFET_IB_1024     0x0C    //DuraFET Die Substrate Current (source or sink, i.e., bipolar reading with G=1024, 125nA FS)
#define DURAFET_IK_512      0x0D    //Counter Electrode Output Current (source or sink, i.e., bipolar reading with G=512, 250nA FS)
#define DURAFET_IB_512      0x0E    //DuraFET Die Substrate Current (source or sink, i.e., bipolar reading with G=512, 250nA FS)
#define DURAFET_IK_256      0x0F    //Counter Electrode Output Current (source or sink, i.e., bipolar reading with G=256, 500nA FS)
#define DURAFET_IB_256      0x10    //DuraFET Die Substrate Current (source or sink, i.e., bipolar reading with G=256, 500nA FS)
#define DURAFET_IK_128      0x11    //Counter Electrode Output Current (source or sink, i.e., bipolar reading with G=128, 1uA FS)
#define DURAFET_IB_128      0x12    //DuraFET Die Substrate Current (source or sink, i.e., bipolar reading with G=128, 1uA FS)
#define DURAFET_IK_64       0x13    //Counter Electrode Output Current (source or sink, i.e., bipolar reading with G=64, 2uA FS)
#define DURAFET_IB_64       0x14    //DuraFET Die Substrate Current (source or sink, i.e., bipolar reading with G=64, 2uA FS)
#define DURAFET_IK_32       0x15    //Counter Electrode Output Current (source or sink, i.e., bipolar reading with G=32, 4uA FS)
#define DURAFET_IB_32       0x16    //DuraFET Die Substrate Current (source or sink, i.e., bipolar reading with G=32, 4uA FS)
#define DURAFET_IK_16       0x17    //Counter Electrode Output Current (source or sink, i.e., bipolar reading with G=16, 8uA FS)
#define DURAFET_IB_16       0x18    //DuraFET Die Substrate Current (source or sink, i.e., bipolar reading with G=16, 8uA FS)
#define DURAFET_IK_8        0x19    //Counter Electrode Output Current (source or sink, i.e., bipolar reading with G=8, 16uA FS)
#define DURAFET_IB_8        0x1A    //DuraFET Die Substrate Current (source or sink, i.e., bipolar reading with G=8, 16uA FS)
#define DURAFET_IK_4        0x1B    //Counter Electrode Output Current (source or sink, i.e., bipolar reading with G=4, 32uA FS)
#define DURAFET_IB_4        0x1C    //DuraFET Die Substrate Current (source or sink, i.e., bipolar reading with G=4, 32uA FS)
#define DURAFET_IK_2        0x1D    //Counter Electrode Output Current (source or sink, i.e., bipolar reading with G=2, 64uA FS)
#define DURAFET_IB_2        0x1E    //DuraFET Die Substrate Current (source or sink, i.e., bipolar reading with G=2, 64uA FS)
#define DURAFET_IK_1        0x1F    //Counter Electrode Output Current (source or sink, i.e., bipolar reading with G=1, 128uA FS)
#define DURAFET_IB_1        0x20    //DuraFET Die Substrate Current (source or sink, i.e., bipolar reading with G=1, 128uA FS)

// MBARI DURAFET IK VALUES
#define IK_V_MAX            2.00    //Maximum voltage threshold for an over-range condition
#define IK_ERANGE_POS       +1.0    //Positive Ik Range Error Return Value
#define IK_ERANGE_NEG       -1.0    //Negative Ik Range Error Return Value

// MBARI DURAFET IB VALUES
#define IB_V_MAX            2.00    //Maximum voltage threshold for an over-range condition
#define IB_ERANGE_POS       +1.0    //Positive Ib Range Error Return Value
#define IB_ERANGE_NEG       -1.0    //Negative Ib Range Error Return Value

// MISC...
#define ATOD_RESET_RETRIES  1       //Number of AtoD Resets if AtoD returns a timeout, if one doesn't do it, then probably more won't either
#define TEN_HZ_LOOP			100000	//Collect A/D data at 10Hz rate

// ATOD FUNCTION PROTOTYPES
int AtodPowerOn(void);
int AtodPowerOff(void);
int AtodInit(void);
int AtodReset(void);
int AtodConversion(ulong *Counts, float *Volts);
int AtodCalibrate(void);
int AtodSleep(uchar State);
int AtodChannel(uchar Chan);
int AtodPolarity(uchar State);
int AtodIamp(uchar Gain, uchar Enable);
int AtodAnalogSw(uchar Chan, uchar Enable);
int AtodHighZBuffers(uchar Device, uchar Enable);
int AtodEnable(void);
int AtodDisable(void);
int AtodSelectValue(ushort C);
int AtodProcessValue(ushort C, float *V);
int AtodReadValue(ushort C, ushort D, ushort N, float *V, float *S);

#endif
