/****************************************************************************/
/* Copyright 1992 to 1997 MBARI                                             */
/****************************************************************************/
/* Summary  : IBC Regeneration Board Definitions                            */
/* Filename : regen.h                                                       */
/* Author   : Andrew Pearce                                                 */
/* Project  : Tiburon Data Concentrator                                     */
/* Version  : 1.0                                                           */
/* Created  : 09/17/92                                                      */
/* Modified : 04/23/97                                                      */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/* $Header: /usr/tiburon/.cvsroot/micro/h/regen.h,v 1.1.1.1 1997/05/02 17:15:41 pean Exp $
 * $Log: regen.h,v $
 * Revision 1.1.1.1  1997/05/02 17:15:41  pean
 * Initial release of the microcontroller software after Tiburon
 * Moolpool Dive to test IView, Lapboxes, modified Power can
 * GF/5V using bus capacitance mode.
 *
 * Revision 1.2  93/07/02  09:40:57  09:40:57  pean (Andrew Pearce)
 * Minor bug fixes and enhancements for Prototype Vehicle Tests
 *
 * Revision 1.1  93/03/31  17:37:28  17:37:28  pean (Andrew Pearce)
 * Initial revision
 *
 * Initial revision
 *
*/
/****************************************************************************/

                                    /* Regen Control Board Register Offset  */
#define REGEN_AtoD_REG            0 /* A to D result register               */

#define REGEN_ATOD_TIMEOUT       16 /* Approx 50 usecs delay at 10 MHz Clock*/

#define REGEN_LOW_DUTY_CYCLE     35 /* Min Duty Cycle above REGEN_THRESHOLD */
#define REGEN_HIGH_DUTY_CYCLE   100 /* Max Duty Cycle below REGEN_THRESHOLD */
#define REGEN_THRESHOLD         295 /* Threshold where the hardware starts  */

                                    /* dumping regen energy to the heaters  */
#define REGEN_HALF_POWER        300 /* Point where duty cycle should = 50%  */

                                    /* Default warning & alarm thresholds   */
#define REGEN_TEMP_WARNING       59 /* Heatsink temperature warning 59 deg C*/
#define REGEN_TEMP_ALARM         84 /* Heatsink temperature alarm   84 deg C*/

#define REGEN_MIN_VOLTAGE_WARN  210 /* Regen Min Bus Voltage warning 210 V  */
#define REGEN_MIN_VOLTAGE_ALARM 195 /* Regen Min Bus Voltage alarm   195 V  */

#define REGEN_MAX_VOLTAGE_WARN  310 /* Regen Max Bus Voltage warning 310 V  */
#define REGEN_MAX_VOLTAGE_ALARM 320 /* Regen Max Bus Voltage alarm   320 V  */

#define REGEN_TEMP_HYSTERESIS     7 /* Temperature hysteresis: 7 deg C      */
#define REGEN_VOLTAGE_HYSTERESIS  7 /* Voltage hysteresis: 7 V              */

#define REGEN_MAX_TEMP          120 /* Maximum temperature measurement      */
#define REGEN_MAX_DUTY_CYCLE   1024 /* Maximum duty cycle measurement       */
#define REGEN_MAX_VOLTAGE       400 /* Maximum Bus voltage measurement      */

#define REGEN_SAMPLES           250 /* Number of samples for digital scope  */
#define REGEN_AtoD_MAX_CHAN       8 /* Number of A to D channels            */

#define REGEN_AtoD_DIFFERENTIAL 0x00    /* A/D Differential mode command    */
#define REGEN_AtoD_SINGLE_ENDED 0x08    /* A/D Single Ended mode command    */
#define REGEN_AtoD_PSEUDO_DIFF  0x18    /* A/D Pseudo-differential mode     */

                                        /* A/D Channel Assignments          */
#define REGEN_VOLTAGE_CHAN         0    /* Bus Voltage Measurement          */
#define REGEN_DUTY1_CHAN           1    /* Channel 1 duty cycle measurement */
#define REGEN_DUTY2_CHAN           2    /* Channel 2 duty cycle measurement */
#define REGEN_TEMP_CHAN            3    /* Heat sink temperature measurement*/
#define REGEN_GROUND_REF_CHAN      4    /* A/D Ground reference voltage     */

#define REGEN_SAMPLE_TIME          1    /* Every 10 milliseconds            */

                                    /* Converts millivolts to volts         */
#define MILLIVOLTS_TO_VOLTS(mV)    ((Int16) (mV / 1000))

                                    /* Temp deg C = raw A/D * 1.5 - 273     */
#define REGEN_DEGREES(rawTemp)     (( (rawTemp * 15) / 10) - 273 )

                                    /* Voltage(V) = raw A/D * 1.5           */
#define REGEN_VOLTS(rawVolts)      ( (rawVolts * 15) / 10)

                                    /* Voltage(V) = raw A/D * 1.5           */
#define REGEN_MILLIVOLTS(rawVolts) (((Nat32) rawVolts) * 1500)

                                    /* Duty Cycle(%) = 109 - (raw A/D * .43)*/
#define REGEN_DUTY_CYCLE(rawDuty)  ( 109 - (rawDuty * 43) / 100)

typedef struct                      /* Regen Controlller Data Structure     */
{
    IBC_BoardEntry IBC_Board;           /* IBC Board Entry structure        */

    Int16 voltageMinWarning;            /* Voltage Min warning thresh       */
    Int16 voltageMinAlarm;              /* Voltage Min alarm thresh         */
    Int16 voltageMaxWarning;            /* Voltage Max warning thresh       */
    Int16 voltageMaxAlarm;              /* Voltage Max alarm thresh         */

    Byte  tempWarning;                  /* Temperature warning thresh       */
    Byte  tempAlarm;                    /* Temperature alarm thresh         */

    struct                              /* Warning and alarm status bits    */
    {
        unsigned voltageMinAlarm : 2;   /* Voltage Minimum alarm            */
        unsigned voltageMaxAlarm : 2;   /* Voltage Maximum alarm            */
        unsigned tempAlarm       : 2;   /* Temperature alarm                */
        unsigned openHeaterAlarm : 1;   /* Open Heater Coil Hardware Failure*/
        unsigned dutyCycleAlarm  : 1;   /* Bad Duty Cycle Hardware Failure  */
    } alarmStatus;

    Int16 filteredDutyCycle1;           /* Filtered duty cycle 1            */
    Int16 filteredDutyCycle2;           /* Filtered duty cycle 2            */
    Int16 filteredBusVoltage;           /* Filtered Bus Voltage             */
    Int16 filteredTemperature;          /* Filtered Regen temperature       */

    filterParam dutyCycle1Filter;       /* Duty Cycle 1 Filter Parameters   */
    filterParam dutyCycle2Filter;       /* Duty Cycle 2 Filter Parameters   */
    filterParam voltageFilter;          /* Voltage Filter Parameters        */
    filterParam temperatureFilter;      /* Temperature Filter Parameters    */

    Nat32 busVoltage;                   /* Raw Bus Voltage millivolts       */
    Int16 minBusVoltage;                /* Minimum Bus Voltage measured     */
    Int16 maxBusVoltage;                /* Maximum Bus Voltage measured     */

    timerId atodSampleTimer;            /* Timer for A/D Sampling           */
    MBool   atodReadInProgress;         /* Normal A/D reading in progress   */
    MBool   atodReadDeferred;           /* A to D reading was deferred      */

    Void   (*busVoltageReadHook) ();    /* Func to call when voltage is read*/
    Int16  busVoltageReadHookParam;     /* param to pass to function        */
} regenBoardEntry;


#ifdef __STDC__

Int16 regenBoardSearch( IBC_BoardEntry *IBC_Boards[], Int16 maxCards );

regenBoardEntry* regenBoardInit( Byte *addr );

Void regenSetTempThreshCmd( IBC_BoardEntry* IBC_CardTable[],
    Int16 IBC_CardCount, Byte *commandBuf );

Void regenGetTempThreshCmd( IBC_BoardEntry* IBC_CardTable[],
    Int16 IBC_CardCount, Byte *commandBuf );

Void regenSetVoltageThreshCmd( IBC_BoardEntry* IBC_CardTable[],
    Int16 IBC_CardCount, Byte *commandBuf );

Void regenGetVoltageThreshCmd( IBC_BoardEntry* IBC_CardTable[],
    Int16 IBC_CardCount, Byte *commandBuf );

Void regenGetStatusCmd( IBC_BoardEntry* IBC_CardTable[],
    Int16 IBC_CardCount, Byte *commandBuf );

Void regenResetMinMaxCmd( IBC_BoardEntry* IBC_CardTable[],
    Int16 IBC_CardCount, Byte *commandBuf );

Void regenGetAlarmStatusCmd( IBC_BoardEntry* IBC_CardTable[],
    Int16 IBC_CardCount, Byte *commandBuf );

Void regenDigitalScopeCmd( IBC_BoardEntry* IBC_CardTable[],
    Int16 IBC_CardCount, Byte *commandBuf );

Int16 regenBoardBusVoltageReadHookAdd( regenBoardEntry *regenBoard,
    Void (*functionPtr) (), Int16 param );

Void regenBoardFunctions( regenBoardEntry *regenBoard );

#endif

