/*
 * Diamond Systems Corporation DMM16 A/D Converter Board
 *
 *
 *****  USED THIS FILE AS LINUX SHELL TO WRITE ALL
 *****  dmm16 functions needed
 *****  Janet Estabridis    18 May 1999
 
 DMM16 specific code
 
 */

//********* DEFINES 

#define BYTE unsigned char
#define WORD unsigned short
#define DWORD unsigned long

#define BOOL long
#define TRUE ((BOOL)1)
#define FALSE ((BOOL)0)


/* board types */
#define DSC_DMM16   0


/*  REGISTER OFFSETS  */

#define ADCHANREG  0x2    //   read/write reg - high & low channel for scan

#define INTFFREG   0x8    //   write only Interrupt Flip Flop Reg
#define STATREG    0x8    //   READ only Status Reg

#define CNTRLREG   0x9    //   read/write reg - DMM-16 board configuration
#define COUNTTREG   0xA    //   WRITE only counter/timer enable/select reg
#define ACONFIGREG 0x0B   //   read/write reg - A/D converter configuration

#define CT0_DATAREG 0x0C  //read/write counter/timer 0 data reg
#define CT1_DATAREG 0x0d  //read/write counter/timer 1 data reg
#define CT2_DATAREG 0x0e  //read/write counter/timer 2 data reg
#define CT_CNTRLREG 0x0f  //read/write counter/timer control register


//////////////////////////////////////////


#define BIPOLAR 0
#define UNIPOLAR 1


typedef struct
{
      WORD baseAddress;  /* 0x100 to 0x3f0 */
      BYTE intLevel;     /* 2-7 */
      BYTE dmaLevel;     /* 1 or 3 */
      DWORD clockFreq;   /* 1 MHz or 10 MHz */
      BYTE polarity;      /* BIPOLAR or UNIPOLAR */
 
} boardInfo;


/* Analog/Digital input */

#define GAIN_1 0
#define GAIN_2 1
#define GAIN_4 2
#define GAIN_8 3

#define RANGE_BIPOLAR_5 0
#define RANGE_BIPOLAR_10 1
#define RANGE_UNIPOLAR_10 2

typedef struct
{
   BYTE currentChannel;
   BYTE lowChannel;
   BYTE highChannel;
   BYTE gain;
   BYTE range;
   BYTE polarity;
   BYTE loadCal;
} DSCADSETTINGS;

typedef struct
{
   BYTE low_channel;
   BYTE high_channel;
   WORD *sample_values;
} DSCADSCAN;

typedef struct
{
   BYTE address;
   BYTE data;
} REGPARAMS;


/* Counter/Timer control */
typedef struct
{
   WORD value;
   BYTE status;
} DSCCS;  //Counter readback

typedef struct
{
   BYTE control_code;
   BYTE counter_number;
   WORD counter_data; // Counter write data
   DSCCS counter0;  // Counter read data
   DSCCS counter1;
   DSCCS counter2;
} DSCCR;

#define OMM_SOURCE_PIN 0
#define OMM_SOURCE_CLOCK 1
#define OMM_SOURCE_OUT 2

/* returns the string describing the error */
/* *****************************  error codes */
#define DE_NONE 0             /* no error */
#define DE_HW_FAILURE 1       /* hardware failed */
#define DE_SW_FAILURE 2       /* software failed */
#define DE_HW_NOT_SUPPORTED 3 /* could not perform operation on this board */
#define DE_SW_NOT_SUPPORTED 4 /* software does not yet support this operation */
#define DE_INVALID_PARM 5     /* a parameter to the function is invalid */
#define DE_ALTERNATE_IN_PROGRESS 6 /* alternate interrupt function in progress */
#define DE_NONE_IN_PROGRESS 7 /* no interrupt function in progress to pause/cancel/resume */
#define DE_BUFFER_ROLLOVER 8  /* pointer passed in + sizeof data to be written would roll over a segment */
#define DE_ALREADY_PAUSED 10  /* can't pause an operation--it's already paused */
#define DE_OVERFLOW       11  /* Am9513A counter function overflowed */
#define DE_INVALID_FUNC   12  // Function number not recognized by board type
#define DE_DSCUDH_INVALID 13  // Header - Library version mismatch


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////


BYTE DMM16Init(boardInfo *b);
BYTE DMM16ADSetSettings(boardInfo *b,DSCADSETTINGS *dscadsettings);

/* interrupt callback routines */
void DMM16StartInterrupts(boardInfo *b);
void DMM16ClearInterrupt(boardInfo *b);
void DMM16EndInterrupts(boardInfo *b);

//  ********* FROM MISC code - Read or Write to any 
//  ********* register on any board

BYTE RegisterWrite(boardInfo *b, REGPARAMS *params );
BYTE RegisterRead(boardInfo *b, REGPARAMS *params );


