/****************************************************************************/
/* Copyright 2013 MBARI.                                                    */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
/* Written by Bob Herlien for SensorNode for xFOCE, May 2013                */
/****************************************************************************/
#ifndef ADC_H
#define ADC_H

#include <mb.h>
#include <port.h>

#define ADC_HOLDING_START 0x0020
#define ADC_HOLDING_END   0x003F

#define ADC_CHANS	   8
#define ADC_CONVERT_START ADC_HOLDING_START
#define ADC_CONVERT_END1   (ADC_CONVERT_START+ADC_CHANS)
#define ADC_CONVERT_END    (ADC_CONVERT_START+ADC_CHANS-1)

#define ADC_PARM_START    0x0030
#define ADC_PARM_END      ADC_HOLDING_END


/* PIC ADC Definitions		*/
#define FIRST_PIC_ADC_CHAN 10

/* ADS8344 Bit Definitions	*/
#define ADS8344START	0x8000
#define ADS8344SGL	0x0400
#define ADS8344EXT	0x0300
#define ADS8344INT	0x0200
#define ADS8344PDFull	0x0000


typedef union				/************************************/
{					/* Union to hold ADC parameter regs */	
    struct adcChanStruct		/* Structure for NumAvg and AvgMs   */	
    {					/*				    */
	unsigned short	adcNumAvg;	/* Number of samples to average     */
	unsigned short	adcAvgMs;	/* Ms to wait between samples       */
    } adcChan[ADC_CHANS];               /* One for each ADC channel         */
					/*				    */
    unsigned short adcRegs[2 * ADC_CHANS]; /* Regs in an array		    */
} ADCRegs;				/************************************/

typedef struct				/************************************/
{					/* Structure for accumulating A/D Results*/
    unsigned long	accum;		/* Accumulator for avging samples   */
    unsigned short	nSamples;	/* Num samples accumulated	    */
    unsigned short	nextMs;		/* Time of next sample in ms since start*/
} ADCWork;				/************************************/


/* init registers */
void adcInit(); 

/* Modbus registers for ADC		*/
void adcConvert(USHORT address, USHORT nRegs);
USHORT adcReadReg(USHORT address);
eMBErrorCode adcWriteRegs(UCHAR *pRegBuffer, USHORT address, USHORT nRegs);

#endif

