/****************************************************************************/
/* Copyright 1992 to 1996 MBARI                                             */
/****************************************************************************/
/* Summary  : IBC Low Power Switch Board definitions                        */
/* Filename : low_pwr.h                                                     */
/* Author   : Andrew Pearce                                                 */
/* Project  : Tiburon ROV Data Concentrator                                 */
/* Version  : 1.0                                                           */
/* Created  : 09/17/92                                                      */
/* Modified : 04/03/96                                                      */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/* $Header: /usr/tiburon/.cvsroot/micro/h/low_pwr.h,v 1.1.1.1 1997/05/02 17:15:39 pean Exp $
 * $Log: low_pwr.h,v $
 * Revision 1.1.1.1  1997/05/02 17:15:39  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.3  93/07/02  09:36:13  09:36:13  pean (Andrew Pearce)
 * Minor bug fixes and enhancements for Prototype Vehicle Tests
 *
 * Revision 1.2  93/03/12  11:59:32  11:59:32  pean (Andrew Pearce)
 * Removed raw A to D array. Changed current hysteresis.
 *
 * Revision 1.1  93/03/11  11:57:55  11:57:55  pean (Andrew Pearce)
 * Initial revision
 *
*/
/****************************************************************************/

                                    /* Low Pwr Switch Board Register Offsets*/
#define LPS_SWITCH_CTRL_REG       0 /* Switch control register              */
#define LPS_AtoD_REG              1 /* A to D result register               */

#define LPS_MAX_CHANS             4 /* Number of switch channels per board  */
#define LPS_VICOR                 7 /* Pseudo channel No for vicor control  */
#define LPS_ATOD_TIMEOUT         10 /* A to D conversion timeout (50 usec)  */

                                    /* Default warning & alarm thresholds   */
#define LPS_VICOR_TEMP_WARNING   80 /* Vicor temperature warning 80 deg C   */
#define LPS_VICOR_TEMP_ALARM     90 /* Vicor temperature alarm   90 deg C   */

#define LPS_12V_VOLTAGE_WARN    1150/* 12 Volt LPS Voltage warning 11.5 V   */
#define LPS_12V_VOLTAGE_ALARM   1100/* 12 Volt LPS Voltage warning 11.0 V   */

#define LPS_24V_VOLTAGE_WARN    2350/* 24 Volt LPS Voltage warning 23.5 V   */
#define LPS_24V_VOLTAGE_ALARM   2250/* 24 Volt LPS Voltage warning 22.5 V   */

#define LPS_48V_VOLTAGE_WARN    4200/* 48 Volt LPS Voltage warning 42.0 V   */
#define LPS_48V_VOLTAGE_ALARM   4000/* 48 Volt LPS Voltage warning 40.0 V   */

#define LPS_SWITCH_TEMP_WARNING 100 /* Switch temperature warning 100 deg C */
#define LPS_SWITCH_TEMP_ALARM   108 /* Switch temperature alarm   108 deg C */

#define LPS_TEMP_HYSTERESIS       5 /* Temperature hysteresis: 5 deg C      */
#define LPS_VOLTAGE_HYSTERESIS  100 /* Voltage hysteresis: .1 V             */

#define LPS_CURRENT_WARNING    4000 /* Switch current alarm 4 amps          */
#define LPS_CURRENT_ALARM      5000 /* Switch current alarm 5 amps          */
#define LPS_CURRENT_HYSTERESIS  200 /* Current hystersis 200 mAmps          */
#define LPS_CURRENT_READS        20 /* Current readings to prime filter     */

#define LPS_MAX_TEMP            120 /* Maximum temperature measurement      */
#define LPS_MAX_CURRENT       25000 /* Maximum fault current measurement    */
#define LPS_MAX_VOLTAGE       50000 /* Maximum PSU voltage measurement      */

                                    /* LPS Control Register Bits            */
#define LPS_BOARD_LED_ON       0x00 /* Switch board LED On                  */
#define LPS_BOARD_LED_OFF      0x40 /* Switch board LED Off                 */
#define LPS_BOARD_VICOR_ON     0x80 /* Switch Vicor Power Supply On         */
#define LPS_BOARD_VICOR_OFF    0x00 /* Switch Vicor Power Supply Off        */

#define LPS_AtoD_MAX_CHANS        8 /* Number of A to D channels on board   */
#define LPS_AtoD_DIFFERENTIAL  0x00 /* A/D Differential mode command        */
#define LPS_AtoD_SINGLE_ENDED  0x08 /* A/D Single Ended mode command        */
#define LPS_AtoD_PSEUDO_DIFF   0x18 /* A/D Pseudo-differential mode command */

                                    /* A/D Channel Assignments              */
#define LPS_SW0_CURRENT_CHAN      0 /* Channel 0 current measurement        */
#define LPS_SW1_CURRENT_CHAN      1 /* Channel 1 current measurement        */
#define LPS_SW2_CURRENT_CHAN      2 /* Channel 2 current measurement        */
#define LPS_SW3_CURRENT_CHAN      3 /* Channel 3 current measurement        */
#define LPS_VICOR_TEMP_CHAN       4 /* Vicor temperature sensor             */
#define LPS_SW3_TEMP_CHAN         5 /* Switch 3 temperature sensor          */
#define LPS_VICOR_VOLTAGE_CHAN    6 /* Vicor output voltage                 */
#define LPS_GROUND_REF_CHAN       7 /* A/D Ground reference voltage         */

                                        /* Temp deg C = raw A/D * 1.5 - 273 */
#define LPS_DEGREES(rawTemp)    (( (rawTemp * 15) / 10) - 273 )

                                        /* Current(mA) = A/D * 96.9 mA/bit  */
#define LPS_MILLIAMPS(bits)     (( (Nat32) bits * 969) / 10)

                                        /* Voltage(mV) = A/D * 250 mV/Bit   */
#define LPS_VOLTS(bits)         ( (Nat32) bits * 250)

typedef enum
{
    LPS_CURRENT,
    LPS_VICOR_TEMP,
    LPS_SW3_TEMP,
    LPS_VICOR_VOLTAGE
} lpsState;

typedef struct                          /* Low Power Switch Data Structure  */
{
    IBC_BoardEntry IBC_Board;           /* IBC Board Entry structure        */

    lpsState state;                     /* Measurement State Machine State  */
    Byte  switchState;                  /* Copy of switch control register  */

    Byte  vicorTempWarning;             /* Vicor temperature warning thresh */
    Byte  vicorTempAlarm;               /* Vicor temperature alarm thresh   */
    Byte  switchTempWarning;            /* Switch temperature warning thresh*/
    Byte  switchTempAlarm;              /* Switch temperature alarm thresh  */

    Int16 voltageWarning;               /* Vicor Voltage Warning thresh     */
    Int16 voltageAlarm;                 /* Vicor Voltage Alarm thresh       */

    struct                              /* Warning and alarm status bits    */
    {
        unsigned vicorTempAlarm    : 2; /* Vicor temperature alarm status   */
        unsigned vicorVoltageAlarm : 2; /* Vicor voltage alarm              */
        unsigned sw3TempAlarm      : 2; /* Switch 3 temperature alarm       */
    } alarmStatus;

    struct                              /* LPS Channel specific data struct */
    {
        Int16 filteredCurrent;          /* Filtered Current Measurement mA  */
        filterParam filter;             /* Current Filter Parameters        */

        Int16 currentWarning;           /* Current warning thresholds mAmps */
        Int16 currentAlarm;             /* Current alarm thresholds mAmps   */

        microAlarm currentAlarmStatus;  /* Current Alarm Status             */

        MBool checkCurrent;             /* Flag prevents current checking   */
        MBool gangOutputs;              /* Gang LPS outputs 0 & 1, 2 & 3    */
    } lpsChan[LPS_MAX_CHANS];           /* Channel specific parameters      */

    Int16 filteredVicorTemp;            /* Filtered Vicor temperature       */
    filterParam vicorTempFilter;        /* Vicor Temp Filter Parameters     */

    Int16 filteredVicorVoltage;         /* Filtered Vicor output voltage    */
    filterParam vicorVoltageFilter;     /* Voltage Filter Parameters        */

    Int16 filteredSw3Temp;              /* Filtered Switch 3 temperature    */
    filterParam sw3TempFilter;          /* Switch 3 Temp Filter Parameters  */

    timerId switchOnTimer;              /* Delays voltage measurements      */
    Nat16   switchOnDelay;              /* Time for switch on delay         */
    MBool   checkVoltage;               /* Flag prevents voltage checking   */
} LPS_BoardEntry;

#ifdef __STDC__

Int16 LPC_BoardSearch( IBC_BoardEntry *IBC_Boards[], Int16 maxCards );

                                          /*LPS_Board Initialization Routine*/
LPS_BoardEntry* LPS_BoardInit( Byte *boardBaseAddr );

Void LPS_AtoD_ISR( LPS_BoardEntry *LPS_Board );

Void LPS_BoardInterruptOn( LPS_BoardEntry *LPS_Board );

Void LPS_BoardInterruptOff( LPS_BoardEntry *LPS_Board );

MBool LPS_BoardSwitchState( LPS_BoardEntry *LPS_Board, Nat16 chan );

Int16 LPS_BoardVicorCtrl( LPS_BoardEntry *LPS_Board, MBool state);

Int16 LPS_BoardSwitch( LPS_BoardEntry *LPS_Board, Byte chan, MBool state);

Void LPS_VicorControlCmd( IBC_BoardEntry* IBC_CardTable[],
       Int16 IBC_CardCount, Byte *commandBuf );

Void LPS_SwitchControlCmd( IBC_BoardEntry* IBC_CardTable[],
       Int16 IBC_CardCount, Byte *commandBuf );

Void LPS_SwitchStatusCmd( IBC_BoardEntry* IBC_CardTable[],
       Int16 IBC_CardCount, Byte *commandBuf );

Void LPS_SetCurrentThreshCmd( IBC_BoardEntry* IBC_CardTable[],
       Int16 IBC_CardCount, Byte *commandBuf );

Void LPS_GetCurrentThreshCmd( IBC_BoardEntry* IBC_CardTable[],
       Int16 IBC_CardCount, Byte *commandBuf );

Void LPS_SetTempThreshCmd( IBC_BoardEntry* IBC_CardTable[],
       Int16 IBC_CardCount, Byte *commandBuf );

Void LPS_GetTempThreshCmd( IBC_BoardEntry* IBC_CardTable[],
       Int16 IBC_CardCount, Byte *commandBuf );

Void LPS_GetTemperaturesCmd( IBC_BoardEntry* IBC_CardTable[],
       Int16 IBC_CardCount, Byte *commandBuf );

Void LPS_SetVoltageThreshCmd( IBC_BoardEntry* IBC_CardTable[],
       Int16 IBC_CardCount, Byte *commandBuf );

Void LPS_GetVoltageThreshCmd( IBC_BoardEntry* IBC_CardTable[],
       Int16 IBC_CardCount, Byte *commandBuf );

Void LPS_GetVoltageCmd( IBC_BoardEntry* IBC_CardTable[],
       Int16 IBC_CardCount, Byte *commandBuf );

Void LPS_GetCurrentsCmd( IBC_BoardEntry* IBC_CardTable[],
       Int16 IBC_CardCount, Byte *commandBuf );

Void LPS_GetAlarmStatusCmd( IBC_BoardEntry* IBC_CardTable[],
       Int16 IBC_CardCount, Byte *commandBuf );

Void LPS_GangOutputsCmd( IBC_BoardEntry* IBC_CardTable[],
       Int16 IBC_CardCount, Byte *commandBuf );

Void LPS_SetSwitchDelayCmd( IBC_BoardEntry* IBC_CardTable[],
       Int16 IBC_CardCount, Byte *commandBuf );

Void LPS_BoardFunctions( LPS_BoardEntry *LPS_Board );

#endif

