/****************************************************************************/
/* Copyright 1992 to 1993, MBARI                                            */
/****************************************************************************/
/* Summary  : IBC High Power Switch Interface Module for VxWorks            */
/* Filename : hps.c                                                         */
/* Author   : Andrew Pearce                                                 */
/* Project  : New ROV                                                       */
/* Version  : Version 1.0                                                   */
/* Created  : 04/09/93                                                      */
/* Modified : 04/09/93                                                      */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/* $Header: hps.c,v 1.1 93/07/01 14:34:27 pean Exp $
 * $Log:        hps.c,v $
 * Revision 1.1  93/07/01  14:34:27  14:34:27  pean (Andrew Pearce)
 * Initial revision
 *
 *
 */
/****************************************************************************/

#include <vxWorks.h>                /* vxWorks system declarations          */
#include <semLib.h>                 /* vxWorks semaphore functions          */
#include <wdLib.h>                  /* vxWorks watch dog timer support decls*/
#include <msgQLib.h>                /* vxWorks Message Queue Library        */

#include <mbariTypes.h>             /* mbari style guide type declarations  */
#include <mbariConst.h>             /* Miscellaneous constants              */

#include <sio32.h>                  /* SIO32 hardware and driver information*/
#include <sio32Server.h>            /* Sio32 server communication services  */
#include <sio32Client.h>            /* Sio32 client communication services  */
#include <microCmd.h>               /* Microcontoller command functions     */

#include "applic.h"                 /* Microcontroller applications         */
#include "microcmd.h"               /* Microcontroller commands             */
#include "ibc_card.h"               /* IBC Card Types                       */
#include "ibc_cmd.h"                /* IBC Card Serial Commands             */

#include "hps.h"                    /* Function Prototypes for hps.c        */

/****************************************************************************/
/* Function    : IBC_HPS_SwitchControl                                      */
/* Purpose     : Sets state of IBC High Power Switch                        */
/* Inputs      : Sio32 channel, IBC Board Address, Desired state            */
/* Outputs     : Returns OK or ERROR                                        */
/****************************************************************************/
    STATUS
IBC_HPS_SwitchControl( sio32Chan *IBC_SerialChan, Word boardAddr,
                 MBool desiredState )
{
    Word    status;                 /* Command Result from microcontroller  */

                                    /* Send command to microcontroller      */
    if ( sendMicroCommand( IBC_SerialChan, &status, sizeof(status),
         3, IBC_CPU_APP | HPS_SWITCH_CTRL,  boardAddr,
         desiredState ) == ERROR ) return(ERROR);

                                    /* Check command result                  */
    return (wordFromBuf(&status, 0) == CMD_OK ? OK : ERROR);
} /* IBC_HPS_SwitchControl() */

/****************************************************************************/
/* Function    : IBC_HPS_ReadStatus                                         */
/* Purpose     : Reads status of IBC High Power Switch                      */
/* Inputs      : Sio32 channel, IBC Board Address, Switch Status            */
/* Outputs     : Returns OK or ERROR                                        */
/****************************************************************************/
    STATUS
IBC_HPS_ReadStatus( sio32Chan *IBC_SerialChan, Word boardAddr,
    Nat16 *switchState )
{
    Int16   cmdStatus;              /* Command Status                       */
    Word    reply[2];               /* Command Result from microcontroller  */

                                    /* Send command to microcontroller      */
    if ( sendMicroCommand( IBC_SerialChan, &reply, sizeof(Word) * 2,
         2, IBC_CPU_APP | HPS_READ_STATUS,  boardAddr )
         == ERROR ) return(ERROR);

                                    /* Check if command result is CMD OK    */
    if (wordFromBuf(&reply, 0) != CMD_OK)
        return(ERROR);              /* Command failed so return ERROR       */

                                    /* Command OK so update temeratures     */
    wordsFromBuf(reply, 2, &cmdStatus, switchState);

    return (OK);                    /* Return OK                            */
} /* IBC_HPS_SwitchStatus() */

/****************************************************************************/
/* Function    : IBC_HPS_GetCurrent                                         */
/* Purpose     : Read IBC High Power Switch Current                         */
/* Inputs      : Sio32 channel, Board Address, Switch Current               */
/* Outputs     : Returns OK or ERROR                                        */
/****************************************************************************/
    STATUS
IBC_HPS_GetCurrent( sio32Chan *IBC_SerialChan, Word boardAddr,
    Nat16 *hpsCurrent)
{
    Int16   cmdStatus;              /* Command Status                       */
    Word    reply[2];               /* Command Result from microcontroller  */

                                    /* Send command to microcontroller      */
    if ( sendMicroCommand( IBC_SerialChan, &reply, sizeof(Word) * 2,
         2, IBC_CPU_APP | HPS_READ_CURRENT,  boardAddr )
         == ERROR ) return(ERROR);
                                    /* Check if command result is CMD OK    */
    if (wordFromBuf(&reply, 0) != CMD_OK)
        return(ERROR);              /* Command failed so return ERROR       */

                                    /* Command OK so update currents        */
    wordsFromBuf(reply, 2, &cmdStatus, hpsCurrent );

    return (OK);                    /* Return OK                            */
} /* IBC_HPS_GetCurrent() */

/****************************************************************************/
/* Function    : IBC_HPS_GetCurrentThresh                                   */
/* Purpose     : Read IBC High Power Switch Current Alarm Thresholds        */
/* Inputs      : Sio32 channel, Board Address, Current Alarm Thresholds     */
/* Outputs     : Returns OK or ERROR                                        */
/****************************************************************************/
    STATUS
IBC_HPS_GetCurrentThresh( sio32Chan *IBC_SerialChan, Word boardAddr,
    Nat16 *warnThresh, Nat16 *alarmThresh )
{
    Int16   cmdStatus;              /* Command Status                       */
    Word    reply[3];               /* Command Result from microcontroller  */

                                    /* Send command to microcontroller      */
    if ( sendMicroCommand( IBC_SerialChan, &reply, sizeof(Word) * 3,
         2, IBC_CPU_APP | HPS_GET_CURRENT_THRESH,  boardAddr )
         == ERROR ) return(ERROR);

                                    /* Check if command result is CMD OK    */
    if (wordFromBuf(&reply, 0) != CMD_OK)
        return(ERROR);              /* Command failed so return ERROR       */

                                    /* Command OK so update currents        */
    wordsFromBuf(reply, 3, &cmdStatus, warnThresh, alarmThresh);

    return (OK);                    /* Return OK                            */
} /* IBC_HPS_GetCurrentThresh() */

/****************************************************************************/
/* Function    : IBC_HPS_SetCurrentThresh                                   */
/* Purpose     : Sets IBC High Power Switch Current Warning and Alarm       */
/*               Thresholds                                                 */
/* Inputs      : Sio32 channel, Board Address, Warning & Alarm Thresholds   */
/* Outputs     : Returns OK or ERROR                                        */
/****************************************************************************/
    STATUS
IBC_HPS_SetCurrentThresh( sio32Chan *IBC_SerialChan, Word boardAddr,
    Nat16 warnThresh,  Nat16 alarmThresh)
{
    Word    status;                 /* Command Result from microcontroller  */

                                    /* Send command to microcontroller      */
    if ( sendMicroCommand( IBC_SerialChan, &status, sizeof(status),
         4, IBC_CPU_APP | HPS_SET_CURRENT_THRESH, boardAddr,
         warnThresh, alarmThresh ) == ERROR )
         return(ERROR);

                                    /* Check command result                  */
    return (wordFromBuf(&status, 0) == CMD_OK ? OK : ERROR);
} /* IBC_HPS_SetCurrentThresh() */

/****************************************************************************/
/* Function    : IBC_HPS_GetAlarmStatus                                     */
/* Purpose     : Read IBC High Power Switch Board Alarms                    */
/* Inputs      : Sio32 channel, Board Address, Current Alarms               */
/* Outputs     : Returns OK or ERROR                                        */
/****************************************************************************/
    STATUS
IBC_HPS_GetAlarmStatus( sio32Chan *IBC_SerialChan, Word boardAddr,
                ibcAlarm *currentAlarm )
{
    Word    cmdStatus;              /* Command Status                       */
    Word    reply[2];               /* Command Result from microcontroller  */

                                    /* Send command to microcontroller      */
    if ( sendMicroCommand( IBC_SerialChan, &reply, sizeof(Word) * 2,
         2, IBC_CPU_APP | HPS_GET_ALARM_STATUS, boardAddr )
         == ERROR ) return(ERROR);

                                    /* Check if command result is CMD OK    */
    if (wordFromBuf(&reply, 0) != CMD_OK)
        return(ERROR);              /* Command failed so return ERROR       */

                                    /* Command OK so update current alarm   */
    *currentAlarm = (ibcAlarm) wordFromBuf(&reply, sizeof(Word));

    return (OK);                    /* Return OK                            */
} /* IBC_LPS_GetAlarmStatus() */


