/****************************************************************************/
/* Copyright 1990, 1991, 1992 MBARI                                         */
/****************************************************************************/
/* Summary  : microcontroller support library definitions                   */
/* Filename : microlib.h                                                    */
/* Author   : Andrew Pearce                                                 */
/* Project  : Tiburon ROV                                                   */
/* Version  : 1.0                                                           */
/* Created  : 11/21/90                                                      */
/* Modified : 01/20/92                                                      */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/* $Header: /usr/tiburon/.cvsroot/micro/h/microlib.h,v 1.1.1.1 1997/05/02 17:15:39 pean Exp $
 * $Log: microlib.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.1  92/05/14  09:17:50  09:17:50  pean (Andrew Pearce 408-647-3746)
 * Initial revision
 *
*/
/****************************************************************************/

#ifndef MICROLIB_H
#define MICROLIB_H

#define int_mask1       imask1          /* Int Mask 1 naming inconsistency  */
#define ioc3            t2control       /* Map ioc3 to t2control            */

#define TI_INT_MASK         0x01        /* Transmit Data Interrupt          */
#define RI_INT_MASK         0x02        /* Receive Data Interrupt           */
#define HSI4_INT_MASK       0x04        /* HSI FIFO has four entries Intr   */
#define T2_CAPTURE_MASK     0x08        /* Timer 2 capture Interrupt        */
#define T2_OVERFLOW_MASK    0x10        /* Timer 2 overflow Interrupt       */
#define EXTINT1_MASK        0x20        /* External Interrupt 1             */
#define FIFO_FULL_MASK      0x40        /* HSI FIFO Full Interrupt          */

#define T1_OVFLW_MASK       0x01        /* Timer 1 Overflow Interrupt       */
#define AD_DONE_MASK        0x02        /* A/D Conversion Done Interrupt    */
#define HSI_DATA_MASK       0x04        /* HSI Data Available Interrupt     */
#define HSO_INT_MASK        0x08        /* High Speed Output Interrupt      */
#define HSI_0_INT_MASK      0x10        /* HSI.0 Pin Interrupt              */
#define SW_TIMER_MASK       0x20        /* Software Timer Interrupt         */
#define SER_PORT_MASK       0x40        /* Serial Port Interrupt            */
#define EXTINT_MASK         0x80        /* External Interrupt Enable        */

#define A_TO_D_CHAN_BITS    0x07        /* A to D chan number in AD_RESULT  */
#define A_TO_D_BUSY         0x08        /* A to D conversion Busy/Idle bit  */

#define A_TO_D_START_HSO    0x00        /* Start A to D with HSO command    */
#define A_TO_D_START_NOW    0x08        /* Start A to D conversion now      */
#define A_TO_D_10_BIT       0x00        /* A to D 10 bit conversion         */
#define A_TO_D_8_BIT        0x10        /* A to D  8 bit conversion         */

#define A_TO_D_MASK         0xc0        /* Masks upper byte of 10-bit result*/

#define IOS0_HSO0_STATE     0x01
#define IOS0_HSO1_STATE     0x02
#define IOS0_HSO2_STATE     0x04
#define IOS0_HSO3_STATE     0x08
#define IOS0_HSO4_STATE     0x10
#define IOS0_HSO5_STATE     0x20
#define IOS0_CAM_HR_FULL    0x40
#define IOS0_HOLD_REG_FULL  0x80

#define IOC0_HSI0_ENABLE    0x01
#define IOC0_T2_RESET       0x02
#define IOC0_HSI1_ENABLE    0x04
#define IOC0_T2_EXT_RESET   0x08
#define IOC0_HSI2_ENABLE    0x10
#define IOC0_T2_RST_HSI0    0x20
#define IOC0_HSI3_ENABLE    0x40
#define IOC0_T2_CLK_HSI1    0x80

#define IOC1_PWM_ENABLE     0x01
#define IOC1_EXTINT_ACH7    0x02
#define IOC1_TIMER1_OFLOW   0x04
#define IOC1_TIMER2_OFLOW   0x08
#define IOC1_HSO_4_ENABLE   0x10
#define IOC1_SELECT_TXD     0x20
#define IOC1_HSO_5_ENABLE   0x40
#define IOC1_HSI_INTR_SEL   0x80

#define IOC2_FAST_T2_INC    0x01
#define IOC2_T2_UP_DOWN     0x02
#define IOC2_PWM_PRESCALE   0x04
#define IOC2_196KC_AD_MODE  0x08
#define IOC2_AD_NO_PRESCALE 0x10
#define IOC2_T2_ALT_INT_VEC 0x20
#define IOC2_HSO_CAM_LOCK   0x40
#define IOC2_HSO_CAM_CLEAR  0x80

#define IOC3_T2_INT_CLOCK   0x01
#define IOC3_ENABLE_PWM1    0x04        /* Mask for PWM 1 enable in IOC 3   */
#define IOC3_ENABLE_PWM2    0x08        /* Mask for PWM 2 enable in IOC 3   */

#define HSO_CMD_HSO_PIN_0   0x00
#define HSO_CMD_HSO_PIN_1   0x01
#define HSO_CMD_HSO_PIN_2   0x02
#define HSO_CMD_HSO_PIN_3   0x03
#define HSO_CMD_HSO_PIN_4   0x04
#define HSO_CMD_HSO_PIN_5   0x05
#define HSO_CMD_HSO_PINS_01 0x06
#define HSO_CMD_HSO_PINS_23 0x07
#define HSO_CMD_SW_TIMER_0  0x08
#define HSO_CMD_SW_TIMER_1  0x09
#define HSO_CMD_SW_TIMER_2  0x0a
#define HSO_CMD_SW_TIMER_3  0x0b
#define HSO_CMD_HSO_PINS_05 0x0c
#define HSO_CMD_RESET_T2    0x0e
#define HSO_CMD_START_AD    0x0f

#define HSO_CMD_NO_INTR     0x00
#define HSO_CMD_INTERRUPT   0x10
#define HSO_CMD_CLR_PIN     0x00
#define HSO_CMD_SET_PIN     0x20
#define HSO_CMD_USE_T1      0x00
#define HSO_CMD_USE_T2      0x40
#define HSO_CMD_CAM_LOCK    0x80

#define HSO_MIN_STATE_TIME  0x04

#define SP_CON_MODE0        0x00
#define SP_CON_MODE1        0x01
#define SP_CON_MODE2        0x02
#define SP_CON_MODE3        0x03
#define SP_CON_PARITY_ENAB  0x04
#define SP_CON_RX_ENABLE    0x08
#define SP_CON_9BIT_MODE    0x10

#define TI_BIT              0x20        /* Serial port Transmit Ready Bit   */
#define RI_BIT              0x40        /* Serial port Receiver Ready Bit   */
#define TXE_BIT             0x08        /* Serial port Transmit Empty Bit   */

#define intDisable    asm PUSHF         /* Macro to disable all interrupts  */
#define intEnable     asm POPF          /* Macro to enable all interrupts   */

#define int0Enable(bit)  int_mask |= bit  /* Macro enables single interrupt */
#define int0Disable(bit) int_mask &= ~bit /* Macro disables single interrupt*/

#define int1Enable(bit)  imask1 |= bit  /* Macro to enable single interrupt */
#define int1Disable(bit) imask1 &= ~bit /* Macro to disable single interrupt*/

#define setIoport1Bit(bitMask) ioport1 = (ioport1_copy |=  (Byte) bitMask)
#define clrIoport1Bit(bitMask) ioport1 = (ioport1_copy &= ~(Byte) bitMask)

#define setIoport2Bit(bitMask) ioport2 = (ioport2_copy |=  (Byte) bitMask)
#define clrIoport2Bit(bitMask) ioport2 = (ioport2_copy &= ~(Byte) bitMask)

                                    /* Macro to convert two bytes to a word */
#define bytes_to_word(high, low)   ( ((Word) high) << 8) | (low & 0xff)

#define ADC_UNIPOLAR(bits)         (bits + 0x0200)

#define HWINDOW0    0                   /* 80C196 horizontal reg window 0   */
#define HWINDOW1    1                   /* 80C196 horizontal reg window 1   */
#define HWINDOW15   15                  /* 80C196 Horizontal reg window 15  */

#define SET_PORT 0x01                   /* Write to ioport                  */
#define AND_PORT 0x02                   /* AND with ioport                  */
#define OR_PORT  0x03                   /* OR with ioport                   */

#define NUM_CPU_ADC_CHANS       8       /* Number of A/D channels on micro  */
#define MAX_PWM_VALUE         255       /* Maximum PWM control value        */

Extern Reg Byte ioport1_copy;
Extern Reg Byte ioport2_copy;

#ifdef __STDC__                         /* ANSI C function prototypes       */

Word bytesToWord(Byte high, Byte low);

Void writeIoport1(Word portValue, Word andOr);

Void writeIoport2(Word portValue, Word andOr);

Void taskDelay(Nat16 clockTicks);        /* Number of milliseconds to wait   */

Int16 readAnalogChannel(Byte channel);   /* Analog channel number 0 to 7     */

Void microResetCmd( Void );

Void microIdentify( Void );

Void microGetSerNoCmd( Char *serialNumber );

Void microSetSerNoCmd( Char *commandBuf, Int16 commandLen,
         Char *serialNumber, Int16 serNoLen );

Void microSoftwareRevCmd( Void );

Void microStatus( Void );

Void microPing(Byte *commandBuf);

Void microSetGndFltTest(Byte *commandBuf);

Void initBoardStatus( Void );

Void microReadMemory(Byte *commandBuf);

Void microWriteMemory(Byte *commandBuf);

Void toggleLED( Void );

Void processMicroCommands( Char *commandBuf, Int16 commandLen );

Boolean alarmThresholdCheck( Int16 value, Int16 warnThreshold,
    Int16 alarmThreshold, Int16 hysteresis, microAlarm *alarmStatus );

Boolean thresholdTest( Int16 value, Int16 threshold, Int16 hysteresis,
    Boolean *alarmStatus );

Boolean alarmBelowThresholdCheck( Int16 value, Int16 warnThreshold,
    Int16 alarmThreshold, Int16 hysteresis, microAlarm *alarmStatus );

#endif /* __STDC__ */

#endif
