/****************************************************************************/
/* Copyright 1992 MBARI                                                     */
/****************************************************************************/
/* Summary  : IBC CPU Microcontroller Board Support Library                 */
/* Filename : sysLib.c                                                      */
/* Author   : Andrew Pearce                                                 */
/* Project  : ROV 1.5 IBC Based Data Concentrator                           */
/* Version  : 1.0                                                           */
/* Created  : 05/05/92                                                      */
/* Modified : 05/11/92                                                      */
/* Archived :                                                               */
/****************************************************************************/

#pragma model(196)
#pragma nosignedchar

#include "C:/C96/INCLUDE/80C196.h"  /* 80196 Register mapping               */
#include "const.h"                  /* Misc. constants - TRUE/FALSE etc     */
#include "types.h"                  /* MBARI style guide declarations       */
#include "microdef.h"               /* Microcontroller Definitions          */
#include "microlib.h"               /* Microcontroller Library decls.       */
#include "./syslib.h"               /* Hardware specific declarations       */
#include "pic.h"                    /* Intel 82C59 Interrupt Controller     */

Extern Word ledState;               /* Front Panel LED Status               */

/****************************************************************************/
/* Function    : initIoport1                                                */
/* Purpose     : Initialize ioport1 on 196 microcontroller to power up state*/
/* Inputs      : None                                                       */
/* Outputs     : None                                                       */
/****************************************************************************/
    Void
initIoport1( Void )
{
                                        /* IBC INIT    = 1, SPARE P1.1  = 1 */
                                        /* PWM 2       = 1, PWM 1       = 1 */
                                        /* LED CONTROL\= 1, SPARE P1.5  = 1 */
                                        /* SPARE P1.6  = 1, SPARE P1.7  = 1 */

    writeIoport1(0xff, SET_PORT);       /* Write value to port 1 and update */
                                        /* the software's internal copy     */
    ledState = OFF;                     /* LED is now off so update state   */
} /* initIoport1() */

/****************************************************************************/
/* Function    : initIoport2                                                */
/* Purpose     : Initialize ioport2 on 196 microcontroller to power up state*/
/* Inputs      : None                                                       */
/* Outputs     : None                                                       */
/****************************************************************************/
    Void
initIoport2( Void )
{
                                        /* TXD         = 1, RXD         = 1 */
                                        /* EXTINT      = 1, SPARE P2.3  = 0 */
                                        /* SPARE P2.4  = 0, TXENAB      = 0 */
                                        /* SPARE P2.6  = 1, SPARE P2.6  = 1 */

    writeIoport2(0xc7, SET_PORT);       /* Write value to port 2 and update */
                                        /* the software's internal copy     */

    ioc1 = 0x20;            /* Select P2.5, EXTINT, TIMER 1 & 2 Int Disable */
                            /* HSO.4 & 5 Disabled, TXD, HSI Hold Reg Loaded */
} /* initIoport2() */

/****************************************************************************/
/* Function    : setBaudRate                                                */
/* Purpose     : Set built in serial port baud rate to specified value      */
/* Inputs      : Desired baud rate                                          */
/* Outputs     : None                                                       */
/****************************************************************************/
    Void
setBaudRate(Nat16 baudRate)
{
                                        /* Calculate Baud Rate              */
                                        /* Write Low Byte of Baud Rate      */
    baud_rate = (CLOCK_FREQ/baudRate/16 - 1) % 256;
                                        /* Write High Byte of Baud Rate     */
    baud_rate = ((CLOCK_FREQ/baudRate/16 - 1) / 256) | 0x80;
} /* setBaudRate() */

/****************************************************************************/
/* Function    : transmitControl                                            */
/* Purpose     : Enable/disable RS485 transmitter                           */
/* Inputs      : Desired state - ON/OFF                                     */
/* Outputs     : None                                                       */
/****************************************************************************/
    Void
transmitControl(Int16 mode)
{
    if (mode == OFF)                    /* Disable 485 transmitter          */
        writeIoport2(~RS485_CONTROL_BIT, AND_PORT);

    else if (mode == ON)                /* Enable 485 transmitter           */
        writeIoport2(RS485_CONTROL_BIT, OR_PORT);
} /* transmitControl() */

/****************************************************************************/
/* Function    : groundFaultTest                                            */
/* Purpose     : Enable/disable Ground fault test mode                      */
/* Inputs      : Desired state - ON/OFF                                     */
/* Outputs     : None                                                       */
/****************************************************************************/
    Void
groundFaultTest(Int16 status)
{
    /* Function not supported by hardware */
} /* groundFaultTest() */

/****************************************************************************/
/* Function    : ledControl                                                 */
/* Purpose     : Set state of on board LED                                  */
/* Inputs      : None                                                       */
/* Outputs     : None                                                       */
/****************************************************************************/
    Void
ledControl(Int16 status)
{
    if (status == OFF)                  /* Set bit 6 (LED_CTRL\) to a 1     */
        writeIoport1(LED_CONTROL_BIT, OR_PORT);
    else
        if (status == ON)               /* Set bit 6 (LED_CTRL\) to a 0     */
            writeIoport1(~LED_CONTROL_BIT, AND_PORT);

} /* ledControl */

/****************************************************************************/
/* Function    : readHumidity                                               */
/* Purpose     : Read on board humidity sensor                              */
/* Inputs      : None                                                       */
/* Outputs     : Returns relative humidity in percent                       */
/****************************************************************************/
    Int16
readHumidity( Void )
{
    Int16   humidity;           /* Raw humidity from sensor                 */
                                /*   0% RH is 0.80 volts = 164 counts       */
                                /* 100% RH is 3.65 volts = 748 counts       */
                                /* Range is 748 - 164 = 584 counts          */

    humidity = readAnalogChannel(HUMIDITY_SENSOR_CHAN);
                                /* subtract 0% RH offset and multiply by    */
                                /* 100 to improve dynamic range for         */
                                /* the conversion to percentage             */
    humidity = (Int16) (( ((Int32) (humidity + 512 - 164)) * 100) / 584);

    return(humidity);           /* Return relative humidity in percent      */
} /* readHumidity() */

/****************************************************************************/

