#ifndef _gf_board_H
#define _gf_board_H

/****************************************************************************/
/* Copyright 1992 to 1996 MBARI                                             */
/****************************************************************************/
/* Summary  : IBC Ground Fault Board definitions                            */
/* Filename : gf_board.h                                                    */
/* Author   : Andrew Pearce                                                 */
/* Project  : Tiburon Data Concentrator                                     */
/* Version  : 1.0                                                           */
/* Created  : 05/19/92                                                      */
/* Modified : 11/18/96                                                      */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/* $Header: /usr/tiburon/.cvsroot/micro/h/gf_board.h,v 1.2 1997/05/07 15:58:38 pean Exp $
 * $Log: gf_board.h,v $
 * Revision 1.2  1997/05/07 15:58:38  pean
 * Cleaned up Include files and makefile for new directory structure.
 *
 * Revision 1.1.1.1  1997/05/02 18:51:36  pean
 * Initial check in of IBC microcontroller software
 *
 * Revision 1.5  96/08/07  15:24:14  15:24:14  pean (Andrew Pearce)
 * Fixed problem with GF scanning stopping when RESTART_SCAN command sent
 * Problem was due to a bug in timer library timer.c. Used read_sysclock instea
 *
 * Revision 1.4  96/02/14  14:28:40  14:28:40  pean (Andrew Pearce)
 * Revised GF/5V Selftest, scan update features, fixed sensor update problems
 *
 * Revision 1.3  96/02/12  09:03:27  09:03:27  pean (Andrew Pearce)
 * Checked in prior to changes for HPS/GF Board interaction
 *
 * Revision 1.2  93/08/06  11:44:33  11:44:33  pean (Andrew Pearce)
 * GF Selftest moved from GF_BoardInit (at power up) to GF_Enable command
 * Therefore, added GF_SELFTEST_STATUS_SRQ to report result
 *
 * Revision 1.1  93/07/02  09:28:05  09:28:05  pean (Andrew Pearce)
 * Initial revision
 *
 * Revision 1.1  92/05/19  14:29:46  14:29:46  pean (Andrew Pearce 408-647-3746)
 * Initial revision
 *
*/
/****************************************************************************/

                                    /* Ground Fault Board Register Offsets  */
#define GF_SWITCH_CTRL_REG       0  /* Relay control register               */
#define GF_AtoD_REG              1  /* A to D result register               */

#define GF_MAX_CHANS             4  /* Number of GF channels per board      */
#define GF_ATOD_TIMEOUT         10  /* A to D conversion timeout (50 usec)  */

                                    /* Default warning & alarm thresholds   */
                                    /* Ground fault current thresholds      */
#define GF_ADVISORY_LEVEL       30  /* Advisory at  30 uAmps                */
#define GF_WARNING_LEVEL       100  /* Warning  at 100 uAmps                */
#define GF_CRITICAL_LEVEL      350  /* Critical at 350 uAmps                */

#define GF_ADVISORY_HYSTERESIS   7  /* Current hysteresis  7 uAmps          */
#define GF_WARNING_HYSTERESIS   20  /* Current hysteresis 20 uAmps          */
#define GF_CRITICAL_HYSTERESIS  70  /* Current hysteresis 70 uAmps          */

#define GF_TEMP_WARNING         50  /* Air temperature warning 50 deg C     */
#define GF_TEMP_ALARM           60  /* Air temperature alarm   50 deg C     */

#define GF_VOLTAGE_WARNING    4750  /* Vicor voltage warning 4.75 V         */
#define GF_VOLTAGE_ALARM      4500  /* Vicor voltage alarm   4.50 V         */

#define GF_TEMP_HYSTERESIS       5  /* Temperature hysteresis: 5 deg C      */
#define GF_VOLTAGE_HYSTERESIS 1000  /* Voltage hysteresis: 1.0 V            */

#define GF_CHARGE_TIME        1000  /* Default charge up time 10 seconds    */

#define GF_QUIESCENT           128  /* Quiescent (NO Fault) GF current      */
#define GF_ATOD_ERROR            6  /* A to D offset error in bits          */
#define GF_SELFTEST_CURRENT    800  /* Selftest relay current level 800 uAmp*/
#define GF_SELFTEST_DELAY       10  /* Charge up time for selftest 100 mSec */
#define GF_CALIBRATE_DELAY      10  /* Relay open delay for cal 100 mSec    */

#define GF_MAX_CURRENT        4096  /* Max fault current measurement 4096uA */
#define GF_MAX_VOLTAGE        6500  /* Max PSU voltage measurement 6500mV   */
#define GF_MAX_TEMP            120  /* Max temperature measurement 120 Deg C*/
#define GF_MAX_RAW_ATOD        256  /* Maximum raw A to D value             */

                                    /* GF Board Relay Control Register bits */
#define GF_BOARD_LED_ON       0x00  /* Switch board LED On                  */
#define GF_BOARD_LED_OFF      0x40  /* Switch board LED Off                 */

#define GF_RELAY_OPEN          OFF  /* Open GF channel relay                */
#define GF_RELAY_CLOSE          ON  /* Close GF channel relay               */

#define GF_CHAN0_RELAY           0  /* Relay 0 control channel              */
#define GF_CHAN1_RELAY           1  /* Relay 1 control channel              */
#define GF_CHAN2_RELAY           2  /* Relay 2 control channel              */
#define GF_CHAN3_RELAY           3  /* Relay 3 control channel              */
#define GF_SELFTEST_RELAY        4  /* Selftest relay control channel       */

#define GF_AtoD_MAX_CHANS        8  /* Number of A to D channels on board   */
#define GF_AtoD_DIFFERENTIAL  0x00  /* A/D Differential mode command        */
#define GF_AtoD_SINGLE_ENDED  0x08  /* A/D Single Ended mode command        */
#define GF_AtoD_PSEUDO_DIFF   0x18  /* A/D Pseudo-differential mode command */

                                    /* A/D Channel Assignments              */
#define GF_CURRENT_CHAN          0  /* GF Current measurement               */
#define GF_HALFSCALE_CHAN        1  /* Current Low Amplifier Output         */
#define GF_GROUND_REF_CHAN       2  /* A/D Ground reference voltage         */
#define GF_AIR_TEMP_CHAN         6  /* Air Temperature sensor               */
#define GF_VOLTAGE_CHAN          7  /* Vicor output voltage                 */

#define GF_MODEL_RESISTANCE   27000 /* 27K Ohms                             */
#define GF_FAULT_CURRENT_LIMIT 1024 /* 1024 microamps                       */


                                    /* Temp deg C = A/D * 1.505 - 273       */
#define GF_DEGREES(rawTemp)    (( ((Nat32) rawTemp * 1505) / 1000) - 273 )

                                    /* Current(uA) = A/D * 8.07 uA/bit      */
#define GF_MICROAMPS(bits)     (( (Int32) bits * 807) / 100)

                                    /* Voltage(mV) = A/D * 25.2 mV/Bit      */
#define GF_MILLIVOLTS(bits)    (( (Nat32) bits * 252) / 10)

typedef enum                        /* GF Channel charge up states          */
{
    GF_IDLE,                        /* Channel idle - relay open            */
    GF_CHARGING,                    /* Channel charging up - relay closed   */
    GF_CHARGED,                     /* Channel charged - reading in progress*/
    GF_READING1,                    /* Current measurement 1 in progress    */
    GF_READING2,                    /* Current measurement 2 in progress    */
    GF_READING3,                    /* Current measurement 3 in progress    */
    GF_READING4,                    /* Current measurement 4 in progress    */
    GF_READING5,                    /* Current measurement 5 in progress    */
    GF_READING6,                    /* Current measurement 6 in progress    */
    GF_READING7,                    /* Current measurement 7 in progress    */
    GF_READING8,                    /* Current measurement 8 in progress    */
    GF_READING9,                    /* Current measurement 9 in progress    */
    GF_COMPLETE,                    /* Current measurements completed       */
    GF_CAL_START,                   /* All relays open for A/D calibration  */
    GF_CAL_READ,                    /* Relay opening timer expired          */
    GF_SELFTEST_RELAY_OPEN,         /* Selftest: wait for relays to open    */
    GF_SELFTEST_READ_HALFSCALE,     /* Selftest: Read halfscale voltage     */
    GF_SELFTEST_RELAY_CLOSE,        /* Selftest: wait for relays to close   */
    GF_SELFTEST_READ_CURRENT,       /* Selftest: Check GF fault current     */
    GF_SELFTEST_COMPLETE            /* Selftest: Selftest complete          */
} gfState;

typedef enum                        /* Ground Fault Board Selftest status   */
{
    GF_TEST_NOT_RUN,                /* GF Test not executed                 */
    GF_ATOD_FAILED,                 /* Excessive A to D convertor error     */
    GF_SELFTEST_FAILED,             /* No Gnd Flt with selftest relay closed*/
    GF_SELFTEST_PASSED              /* GF selftest passsed OK               */
} gf5v_gfTestStatus;

typedef struct                      /* Ground Fault Board Data Structure    */
{
    IBC_BoardEntry IBC_Board;       /* IBC Board Entry structure            */
    Byte  relayState;               /* Copy of relay control register       */

    Byte    gfChannel;              /* GF Channel being measured            */
    Byte    gfChanUpdate;           /* Bit vector flags when channel updates*/
    gfState gfChanState;            /* Measurement state machine status     */

    Nat16   timerTicks;
    Nat16   timerState;
    Boolean gfEnabled;              /* Enable/disable GF fault monitoring   */

    Int16   offsetVoltage;          /* Amplifier offset voltage             */

    Int16  tempWarning;             /* Air temperature warning thresh       */
    Int16  tempAlarm;               /* Air temperature alarm thresh         */

    Int16  voltageWarning;          /* Voltage warning threshold            */
    Int16  voltageAlarm;            /* Voltage alarm threshold              */

    struct                          /* Warning and alarm status bits        */
    {
        unsigned tempAlarm    : 2;  /* Vicor temperature alarm              */
        unsigned voltageAlarm : 2;  /* Vicor voltage alarm                  */
    } alarmStatus;

    Boolean gfSelftest;             /* Set on command to run selftest       */
    gf5v_gfTestStatus testStatus;   /* GF board selftest status             */

    struct                          /* GF Channel specific data struct      */
    {
        Int16 filteredCurrent;      /* Filtered Current Measurement uA      */
        filterParam filter;         /* Current Filter Parameters            */
        gfStatus gfAlarmStatus;     /* GF Current Alarm Status              */
        Nat16 chargeTime;           /* Charge time in 0.01 sec steps        */
    } gfChan[GF_MAX_CHANS];         /* Channel specific parameters          */

    Int16 filteredTemp;             /* Filtered Vicor temperature           */
    filterParam tempFilter;         /* Vicor Temp Filter Parameters         */

    Int16 filteredVicorVoltage;     /* Filtered Vicor output voltage        */
    filterParam vicorVoltageFilter; /* Voltage Filter Parameters            */

    Nat16   busCapacitance;         /* Bus Capacitance for model uF         */
    Int32   capacitorVoltage;       /* Capacitor voltage for model mV       */
    Int16   capacitorCurrent;       /* Capacitor current for model uA       */
    Int16   filteredCapCurrent;     /* Filtered Capacitor Current uA        */
    filterParam capCurrentFilter;   /* Capacitor Current Filter Params      */
} GF_BoardEntry;

#ifdef __STDC__

Int16 GF_BoardSearch( IBC_BoardEntry *IBC_Boards[], Int16 maxCards );

                                     /* GF Board Initialization Routine     */
GF_BoardEntry* GF_BoardInit( Byte *boardBaseAddr );

Void GF_BoardInterruptOn( GF_BoardEntry *GF_Board );

Void GF_BoardInterruptOff( GF_BoardEntry *GF_Board );

Void GF_FaultMonitorCmd( IBC_BoardEntry* IBC_CardTable[],
        Int16 IBC_CardCount, Byte *commandBuf );

Void GF_SelftestCmd( IBC_BoardEntry* IBC_CardTable[],
        Int16 IBC_CardCount, Byte *commandBuf );

Void GF_RestartChanChargeCmd( IBC_BoardEntry* IBC_CardTable[],
        Int16 IBC_CardCount, Byte *commandBuf );

Void GF_SetChargeTimesCmd( IBC_BoardEntry* IBC_CardTable[],
        Int16 IBC_CardCount, Byte *commandBuf );

Void GF_GetChargeTimesCmd( IBC_BoardEntry* IBC_CardTable[],
        Int16 IBC_CardCount, Byte *commandBuf );

Void GF_GetTemperatureCmd( IBC_BoardEntry* IBC_CardTable[],
        Int16 IBC_CardCount, Byte *commandBuf );

Void GF_SetTempThreshCmd( IBC_BoardEntry* IBC_CardTable[],
        Int16 IBC_CardCount, Byte *commandBuf );

Void GF_GetTempThreshCmd( IBC_BoardEntry* IBC_CardTable[],
        Int16 IBC_CardCount, Byte *commandBuf );

Void GF_SetVoltageThreshCmd( IBC_BoardEntry* IBC_CardTable[],
        Int16 IBC_CardCount, Byte *commandBuf );

Void GF_GetVoltageThreshCmd( IBC_BoardEntry* IBC_CardTable[],
        Int16 IBC_CardCount, Byte *commandBuf );

Void GF_GetCurrentsCmd( IBC_BoardEntry* IBC_CardTable[],
        Int16 IBC_CardCount, Byte *commandBuf );

Void GF_GetStatusCmd( IBC_BoardEntry* IBC_CardTable[],
        Int16 IBC_CardCount, Byte *commandBuf );

Void GF_SetBusCapacitanceCmd( IBC_BoardEntry* IBC_CardTable[],
         Int16 IBC_CardCount, Byte *commandBuf );

Void GF_BoardFunctions( GF_BoardEntry *GF_Board );

#endif



#endif /* _gf_board_H */
