/****************************************************************************/
/* Copyright 1995 to 1996 MBARI                                             */
/****************************************************************************/
/* Summary  : Emergency Recovery Data Concetrator - Main C Source file      */
/* Filename : erec.c                                                        */
/* Author   : Andrew Pearce                                                 */
/* Project  : Tiburon ROV Emergency Recovery System                         */
/* Version  : 1.0                                                           */
/* Created  : 06/09/95                                                      */
/* Modified : 04/05/96                                                      */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/* $Header: ers.h,v 1.5 96/08/15 11:51:25 pean Exp $
 * $Log:	ers.h,v $
 * Revision 1.5  96/08/15  11:51:25  11:51:25  pean (Andrew Pearce)
 * Fixed problem with writes to EEPROM corrupting op-code fetches
 * 
 * Revision 1.4  96/08/07  15:30:06  15:30:06  pean (Andrew Pearce)
 * Fixed problem with GF scanning stopping when RESTART_SCAN command sent
 *
 * Revision 1.3  96/07/10  13:35:11  13:35:11  pean (Andrew Pearce)
 * Included fix for Quad Serial Board RS485 Mode transmitter enable problem
 *
 * Revision 1.2  96/06/11  13:43:38  13:43:38  pean (Andrew Pearce)
 * Fixed VB Level Sensor Scaling
 *
 * Revision 1.1  96/06/10  13:26:15  13:26:15  pean (Andrew Pearce)
 * Initial revision
 *
*/
/****************************************************************************/

#define EEPROM_ENTRIES         10  /* 10 is the max due to 64 byte page size*/
#define EEPROM_MAGIC       0x598d  /* Magic Number indicates Valid EEPROM   */
#define EEPROM_BASE_ADDR   0x6000  /* EEPROM Base Address                   */
#define EEPROM_SIZE        0x8000  /* EEPROM Size in bytes                  */
#define EEPROM_MAX_WRITES   10000  /* Maximum number of writes per cell     */
#define EEPROM_WRITE_TIME       2  /* Write must complete within 20 msecs   */

                                   /* Maps EEPROM absolote address space to */
                                   /* CPU address space                     */
#define EEPROM_REG_ADDR(addr)  ( (addr < 0x8000) ? \
   (((char *) EEPROM_BASE_ADDR) + 0x2000 + addr) : \
   (((char *) EEPROM_BASE_ADDR) + addr) )

                                        /* ERS DCON A/D Channel Assignment  */
#define HUMIDITY_SENSOR_CHAN        0   /* Humidity Sensor channel          */
#define HOUSING_WATER_ALARM_CHAN    1   /* Housing Water Alarm channel      */
#define JBOX_WATER_ALARM_CHAN       2   /* J-Box Water Alarm channel        */
#define HOUSING_WA_EXCITE           3   /* Housing Water Alarm Excitation   */
#define JBOX_WA_EXCITE              4   /* J-Box Water Alarm Excitation     */
#define VEHICLE_IN_WATER_CHAN       5   /* Vehicle in-water sensor channel  */
#define TOOLSLED_LATCHED_CHAN       6   /* Toolsled latched Signal channel  */
#define TOOLSLED_CONNECTED_CHAN     7   /* Toolsled connected Signal channel*/

                                        /* IOPORT 1 Bit Assignments         */
#define BATTERY_LOAD_ENABLE      0x04   /* Battery Load Resistor Enable     */
#define VME_CONSOLE_PORT_CONTROL 0x08   /* VME Console Port Switch Control  */

                                        /* 12V LPS Board Channel Assignment */
#define PRESSURE_SENSOR_PWR_CHAN    0   /* Falmouth Pressure Sensor power   */
#define VB_LEVEL_SENSOR_PWR_CHAN    1   /* VB system level sensor power     */
#define COMP_LEVEL_SENSOR_PWR_CHAN  2   /* Compensation Level Sensor Power  */
#define TOOLSLED_SENSOR_PWR_CHAN    3   /* Toolsled Latch Sensor Power      */

                                        /* 24V LPS0 Board Channel Assignment*/
#define VB_SELECT_SOLENOID_CHAN     0   /* Selects VB Operating Mode        */
#define HYDRAULIC_SOLENOID_CHAN     1   /* Selects Hydraulic Operating Mode */
#define ARM_HYD_SELECT_CHAN         2   /* Enable Arm Hydraulic Power       */
#define TOOLSLED_HYD_SELECT_CHAN    3   /* Enable Toolsled Hydraulic Power  */

                                        /* 24V LPS1 Board Channel Assignment*/
#define VB_FILL_SOLENOID_CHAN       0   /* Enable VB Fill Operating Mode    */
#define DEPLOY_LATCH_RELEASE_CHAN   1   /* Fire Vehicle Deployment Release  */
#define TOOLSLED_LATCH_RELEASE_CHAN 2   /* Fire Toolsled Release            */
#define ACOUSTIC_MODEM_POWER_CHAN   3   /* Acoustic Modem Power             */

                                        /* A to D Board Channel Assignments */
#define HYDRAULIC_PRESSURE_CHAN     0   /* Hydraulic Pressure Sensor        */
#define VB_LEVEL_SENSOR_CHAN        1   /* VB Level Sensor                  */
#define COMPENSATION_LEVEL_CHAN     2   /* Compensation Level Sensor        */
#define EREC_BATTERY_CHAN           3   /* EREC DCon Batter Current Sensor  */

                                        /* Quad Serial Board Chan Assignment*/
#define DATA_SWITCH_SERIAL_CHAN     0   /* Data Switch (VME console) Channel*/
#define ACOUSTIC_MODEM_SERIAL_CHAN  1   /* Acoustic Modem Channel           */
#define PRESSURE_SENSOR_SERIAL_CHAN 2   /* Falmouth Pressure Sensor Channel */
#define SPARE_SERIAL_CHANNEL        3

#define HOUSING_WATER_ALARM_BIT  0x01   /* Housing Water Alarm Status Bit   */
#define JBOX_WATER_ALARM_BIT     0x02   /* J-Box Water Alarm Status Bit     */

#define NUM_IN_WATER_READS          3   /* Number reads for switch debounce */
#define NUM_TOOLSLED_READS          3   /* Number reads for switch debounce */
#define TOOLSLED_LATCHED_BIT     0x01   /* Toolsled latched status bit      */
#define TOOLSLED_CONNECTED_BIT   0x02   /* Toolsled connected status bit    */

#define DEPLOYMENT_LATCH_ARM_CODE  (('D' << 8) | 'L')
#define DEPLOYMENT_LATCH_MAX_TIME  18 * SYSCLOCK_RATE
#define DEPLOYMENT_LATCH_MANUAL     0

#define TOOLSLED_LATCH_ARM_CODE    (('T' << 8) | 'L')
#define TOOLSLED_LATCH_MAX_TIME     60 * SYSCLOCK_RATE
#define TOOLSLED_LATCH_MANUAL       0
#define TOOLSLED_DROP_CMD_TIMEOUT   2 * 60 * SYSCLOCK_RATE
#define TOOLSLED_DROP_CMD_COUNT     3
                                        /* Water Alarm Threshold is 0.5V    */
#define WATER_ALARM_THRESHOLD     103   /* 103 = 2^10 * 0.5/5.0             */
#define WATER_ALARM_HYSTERESIS     10   /* 10 counts = 0.049 volts          */

#define COMP_LEVEL_WARN_THRESHOLD  75   /* Default Comp Level Thresholds    */
#define COMP_LEVEL_ALARM_THRESHOLD 60
#define COMP_LEVEL_HYSTERESIS       5   /* Compensation Level Hysteresis    */
#define COMP_LEVEL_SENSOR_MAX     110   /* Maximum compensation level %     */
#define COMP_LEVEL_FILT_MAX       150   /* Max comp level filter value      */

#define COMP_LEVEL_PERCENT(bits) \
             (Int16) ((((Nat32) bits) * 1000L * 5) / (65536L * 16))

#define HYD_PRES_WARN_THRESHOLD  2300   /* Default Hydraulic Pressure       */
#define HYD_PRES_ALARM_THRESHOLD 1500
#define HYD_PRES_HYSTERESIS       100   /* Hydraulic Pressure Hysteresis    */
#define HYD_PRES_MAX             4000   /* Maximum hydraulic pressure       */

                                        /* Convert bits to PSI              */
#define HYD_PRES_PSI(bits)          (Int16) ((((Nat32) bits) * 3000) / 65536)

                                       /* Convert bits to microamps         */
#define BATTERY_MICROAMPS(bits)     ((((Nat32) bits) * 763) / 10)
#define BATTERY_MILLIAMPS(bits)     ((((Nat32) (bits / 1000)) * 763) / 10)

#define MICROAMPS_TO_MILLIAMPS(microamps) ((Int16) (microamps / 1000))
#define MA_SEC_TO_DECI_AH(masec) ((Int16) ((masec * 10) / (60L * 60L * 1000L)))

                                        /* Convert current to millivolts    */
#define BATTERY_MILLIVOLTS(current) ((Int16) ((current * 330) / 100))

                                       /* 0.400 mm per LSB                  */
#define VB_LEVEL_DECI_MM(bits)     ((Int16) (bits << 2))

#define BATTERY_VOLTS_MAX         60
#define BATTERY_VOLT_WARN_THRESH  45
#define BATTERY_VOLT_ALARM_THRESH 35
#define BATTERY_HYSTERESIS         3

#define BATTERY_REPORT_TIME       60    /* Update EEPROM every 60 seconds   */
#define BATT_THRESHOLD_CURRENT 50000    /* Battery threshold current 50mA   */

#define BATTERY_CAPACITY (27 * 10)      /* Capacity in Amp-hours * 10       */

#define BATTERY_LIFE_WARN_THRESH   ((BATTERY_CAPACITY * 34) / 100)
#define BATTERY_LIFE_ALARM_THRESH  ((BATTERY_CAPACITY * 90) / 100)
#define BATTERY_LIFE_HYSTERESIS      2  /* 0.2 Amp-hours                    */

#define BATTERY_CURRENT_UPDATE    0x01
#define BATTERY_VOLTAGE_UPDATE    0x02
#define BATTERY_LIFE_UPDATE       0x04

#define VB_LEVEL_MAX             65535
#define VB_LEVEL_COUNT_OFFSET      131

#define ROVNAV_CMD_LEN   4
#define ROVNAV_REPLY_LEN 4
                                        /* ** Sensor IBC Serial Commands ** */
#define GET_WATER_ALARM           0x01  /* Get Water Alarm Status Word      */
#define GET_WATER_ALARM_THRESH    0x02  /* Get Water Alarm Threshold Value  */
#define SET_WATER_ALARM_THRESH    0x03  /* Set Water Alarm Threshold Value  */
#define GET_VEHICLE_IN_WATER      0x04  /* Get Vehicle In-Water Status      */
#define GET_ERS_STATE             0x05  /* Get ERS System State             */
#define GET_TELEMETRY             0x06  /* Get Telemetry Values             */
#define ARM_DEPLOYMENT_LATCH      0x07
#define RELEASE_DEPLOYMENT_LATCH  0x08
#define ARM_TOOLSLED_LATCH        0x09
#define RELEASE_TOOLSLED_LATCH    0x0a
#define GET_COMP_LEVEL            0x0b  /* Get Compensator Level Command    */
#define GET_COMP_LEVEL_ALARM      0x0c  /* Get Compensator Level Alarm Cmd  */
#define GET_COMP_ALARM_LEVELS     0x0d  /* Get Compensator Alarm Levels Cmd */
#define SET_COMP_ALARM_LEVELS     0x0e  /* Set Compensator Alarm Levels Cmd */
#define GET_COMP_LEVEL_SCALING    0x0f  /* Get Compensator Level Scaling Cmd*/
#define SET_COMP_LEVEL_SCALING    0x10  /* Set Compensator Level Scaling Cmd*/
#define GET_HYD_PRESS_ALARM       0x11  /* Get Hydraulic Pressure Alarm Cmd */
#define SET_HYD_PRESS_ALARM_LVLS  0x12  /* Set Hyd Pressure Alarm Levels Cmd*/
#define GET_HYD_PRESS_ALARM_LVLS  0x13  /* Get Hyd Pressure Alarm Levels Cmd*/
#define READ_BATTERY_VOLTAGE      0x14  /* Measure Battery Voltage Cmd      */
#define GET_BATTERY_ALARMS        0x15  /* Get Battery Alarms Cmd           */
#define SET_BAT_VOLT_ALARM_LEVELS 0x16  /* Set Battery Voltage Alarm Levels */
#define GET_BAT_VOLT_ALARM_LEVELS 0x17  /* Get Battery Voltage Alarm Levels */
#define SET_BAT_LIFE_ALARM_LEVELS 0x18  /* Set Battery Life Alarm Levels    */
#define GET_BAT_LIFE_ALARM_LEVELS 0x19  /* Get Battery Life Alarm Levels    */

#define CAN_WATER_ALARM_ON_SRQ    APPLIC_SRQ_BASE | 0xC0
#define CAN_WATER_ALARM_OFF_SRQ   APPLIC_SRQ_BASE | 0xC1

#define JBOX_WATER_ALARM_ON_SRQ   APPLIC_SRQ_BASE | 0xC2
#define JBOX_WATER_ALARM_OFF_SRQ  APPLIC_SRQ_BASE | 0xC3

#define ERS_MODE_SRQ              APPLIC_SRQ_BASE | 0xC4
#define VEHICLE_IN_WATER_SRQ      APPLIC_SRQ_BASE | 0xC5

#define HYDRAULIC_PRES_ALARM_SRQ  APPLIC_SRQ_BASE | 0xC6
#define COMP_LEVEL_ALARM_SRQ      APPLIC_SRQ_BASE | 0xC7
#define BATTERY_VOLTAGE_ALARM_SRQ APPLIC_SRQ_BASE | 0xC8
#define BATTERY_LIFE_SRQ          APPLIC_SRQ_BASE | 0xC9
#define TOOLSLED_LATCH_STATE_SRQ  APPLIC_SRQ_BASE | 0xCA
#define TOOLSLED_LATCH_RUN_SRQ    APPLIC_SRQ_BASE | 0xCB
#define DEPLOYMENT_LATCH_RUN_SRQ  APPLIC_SRQ_BASE | 0xCC

#define ROVNAV_MSG_SRQ  	  APPLIC_SRQ_BASE | 0xCD


typedef enum
{
    OPERATING_MODE,
    EMERGENCY_MODE,
    ERS_POWER_OFF
} ersOperatingMode;

typedef enum
{
    STOP_DATA_REPORT,
    FAST_DATA_REPORT,
    SLOW_DATA_REPORT,
    SINGLE_DATA_REPORT
} ersDataReportMode;

typedef enum
{
    CONSOLE_TO_IBC,
    CONSOLE_TO_TELEM
} consoleRouteSelect;

