/****************************************************************************/
/* Copyright 1990, 1991, 1992 MBARI                                         */
/****************************************************************************/
/* Summary  : hardware declarations for SIO32 microcontroller               */
/* Filename : microlib.h                                                    */
/* Author   : Andrew Pearce                                                 */
/* Project  : ROV 1.5 SIO32 Serial Communications Board Rev 1.0             */
/* Version  : 1.0                                                           */
/* Created  : 11/21/90                                                      */
/* Modified : 05/11/92                                                      */
/* Archived :                                                               */
/****************************************************************************/

#define CLOCK_FREQ  10000000        /* CPU Clock Frequency is 10MHz         */

                                    /* IO Port 0 bit functions              */
#define EXT_GND_FLT_CHANNEL      0  /* External Fault Analog Channel        */
#define LOCAL_GND_FLT_CHANNEL    1  /* Ground Fault Analog Channel          */

                                    /* IO port 1 function bits              */
#define LED_CONTROL_BIT       0x10  /* On board LED control bit             */
#define GF_TEST_CONTROL_BIT   0x20  /* Ground Fault Test enable bit         */
#define GF_ENABLE_CONTROL_BIT 0x08  /* Ground Fault PSU enable bit          */

                                    /* IO port 2 function bits              */
#define RS485_CONTROL_BIT     0x20  /* RS 485 transmitter control bit       */

#define TEMP_SENSOR_CHANNEL      0  /* Temperature Sensor Analog Channel    */
#define GND_FLT_CHANNEL          1  /* Ground Fault Analog Channel          */

#define MAX_RELAYS              23  /* Max. number of breakout board relays */
#define CLOSE_ALL_RELAYS 0x00000000 /* Mask for closing all relays          */
#define OPEN_ALL_RELAYS  0xffffffff /* Mask for opening all relays          */

#define OPEN                    1   /* Open Relay Control                   */
#define CLOSE                   0   /* Close Relay Control                  */

                                    /* Ground fault current thresholds      */
#define GF_ADVISORY_LEVEL      30
#define GF_WARNING_LEVEL      100
#define GF_CRITICAL_LEVEL     350
                                    /* Extrn. Ground Fault Level (microamps)*/
#define GF_TEST_LEVEL         300
#define GF_HYSTERESIS           5   /* Ground Fault Detector Hystereis      */

#define GF_AVG_READS           15   /* No of readings for GF Value average  */
                            /* Convert raw A/D bits to microamps            */
                            /* raw A/D * 5.0 volts * 1000000 (microamps/amp) /
                               2^10 (10 bit A/D) * 2490 ohms                */
#define BITS_TO_MICROAMPS(bits)   \
        ( (((Nat32) bits) * 500000) / ( ((Nat32) 1024) * 249))

#ifdef __STDC__                     /* ANSI C function prototypes           */

Void initIoport1( Void );
Void initIoport2( Void );
Nat16 readLocalGroundFault( Void );
Void groundFaultTest(Int16 status);
Void checkGroundFaults( gfStatus *localGfAlarmStatus );
Void setBaudRate(Nat16 baudRate);
Void transmitControl(Int16 mode);
Void ledControl(Int16 status);
Int16 readTemperature( Void );
Void initRelayControl( Void );
Void relayOutputCtrl(Word status);
Void writeRelayControl(Nat32 relayCtrl);
Int16 extRelayControl(Word relay, Word mode);
DWord extRelayStatus( Void );
gfTestStatus testGroundFaults( Void );
Void gfEnableControl(Nat16 mode);

#endif /* __STDC__ */

