/****************************************************************************/
/* Copyright 1992 to 1993, MBARI                                            */
/****************************************************************************/
/* Summary  : IBC Dual Vicor Board Interface Module for VxWorks             */
/* Filename : vicor.c                                                       */
/* Author   : Andrew Pearce                                                 */
/* Project  : New ROV                                                       */
/* Version  : Version 1.0                                                   */
/* Created  : 04/09/93                                                      */
/* Modified : 04/09/93                                                      */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/* $Header: vicor.c,v 1.1 93/07/01 14:34:34 pean Exp $
 * $Log:        vicor.c,v $
 * Revision 1.1  93/07/01  14:34:34  14:34:34  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 "filter.h"                 /* General Filter Routines              */
#include "vicor.h"                  /* Dual Vicor Board Definitions         */

#include "vicorBoard.h"             /* Function Prototypes for vicor.c      */

/****************************************************************************/
/* Function    : IBC_DualVicorSwitchControl                                 */
/* Purpose     : Sets state of IBC Dual Vicor Power Supply                  */
/* Inputs      : Sio32 channel, IBC Board Address, PSU, Desired state       */
/* Outputs     : Returns OK or ERROR                                        */
/****************************************************************************/
    STATUS
IBC_DualVicorSwitchControl( sio32Chan *IBC_SerialChan, Word boardAddr,
                Nat16 powerSupply, MBool desiredState )
{
    Word    status;                 /* Command Result from microcontroller  */

                                    /* Send command to microcontroller      */
    if ( sendMicroCommand( IBC_SerialChan, &status, sizeof(status),
         4, IBC_CPU_APP | VICOR_PSU_CONTROL, boardAddr,
         powerSupply, desiredState ) == ERROR ) return(ERROR);

                                    /* Check command result                  */
    return (wordFromBuf(&status, 0) == CMD_OK ? OK : ERROR);
} /* IBC_DualVicorSwitchControl() */

/****************************************************************************/
/* Function    : IBC_DualVicorGetStatus                                     */
/* Purpose     : Reads state of IBC Dual Vicor Power Supply                 */
/* Inputs      : Sio32 channel, IBC Board Address, PSU pointer to status    */
/* Outputs     : Returns OK or ERROR                                        */
/****************************************************************************/
    STATUS
IBC_DualVicorGetStatus( sio32Chan *IBC_SerialChan, Word boardAddr,
        Nat16 powerSupply, MBool *vicorStatus )
{
    Word    cmdStatus;
    Word    psuState;
    Word    reply[2];
                                    /* Send command to microcontroller      */
    if ( sendMicroCommand( IBC_SerialChan, reply, sizeof(Word) * 2,
         2, IBC_CPU_APP | VICOR_PSU_STATUS, boardAddr ) == ERROR )
         return(ERROR);
                                    /* Extract arguments from reply buffer   */
    wordsFromBuf(reply, 2, &cmdStatus, &psuState);

                                    /* Check command result                  */
    if (cmdStatus != CMD_OK) return(ERROR);

    if (powerSupply == DUAL_VICOR_PSU0)
        *vicorStatus = (MBool) (psuState | DUAL_VICOR_PSU0_STATUS);

    if (powerSupply == DUAL_VICOR_PSU1)
        *vicorStatus = (MBool) (psuState | DUAL_VICOR_PSU1_STATUS);

    return(OK);
} /* IBC_DualVicorGetStatus() */

/****************************************************************************/
/* Function    : IBC_DualVicorGetTemperature                                */
/* Purpose     : Read IBC Dual Vicor Board Temperatures                     */
/* Inputs      : Sio32 channel, Board Address, Vicor Temperatures           */
/* Outputs     : Returns OK or ERROR                                        */
/****************************************************************************/
    STATUS
IBC_DualVicorGetTemperature( sio32Chan *IBC_SerialChan, Word boardAddr,
         Int16 *vicorPsu0Temp, Int16 *vicorPsu1Temp )
{
    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 | VICOR_GET_TEMPERATURE, 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 temperature     */
    wordsFromBuf(reply, 3, &cmdStatus, vicorPsu0Temp, vicorPsu1Temp);

    return (OK);                    /* Return OK                            */
} /* IBC_DualVicorGetTemperature() */

/****************************************************************************/
/* Function    : IBC_DualVicorSetTempThresh                                 */
/* Purpose     : Sets IBC Dual Vicor Board Temperature Alarm Thresholds     */
/* Inputs      : Sio32 channel, Board Address, Temperature Alarm Thresholds */
/* Outputs     : Returns OK or ERROR                                        */
/****************************************************************************/
    STATUS
IBC_DualVicorSetTempThresh( sio32Chan *IBC_SerialChan, Word boardAddr,
                Int16 psu0TempWarn, Int16 psu0TempAlarm,
                Int16 psu1TempWarn, Int16 psu1TempAlarm )
{
    Word    status;                 /* Command Result from microcontroller  */

                                    /* Send command to microcontroller      */
    if ( sendMicroCommand( IBC_SerialChan, &status, sizeof(status),
         6, IBC_CPU_APP | VICOR_SET_TEMP_THRESH, boardAddr,
         psu0TempWarn, psu0TempAlarm, psu1TempWarn, psu1TempWarn ) == ERROR )
         return(ERROR);

                                    /* Check command result                  */
    return (wordFromBuf(&status, 0) == CMD_OK ? OK : ERROR);
} /* IBC_DualVicorSetTempThresh() */

/****************************************************************************/
/* Function    : IBC_DualVicorGetTempThresh                                 */
/* Purpose     : Read IBC Dual Vicor Board Temperature Alarm Thresholds     */
/* Inputs      : Sio32 channel, Board Address, Temperature Alarm Thresholds */
/* Outputs     : Returns OK or ERROR                                        */
/****************************************************************************/
    STATUS
IBC_DualVicorGetTempThresh( sio32Chan *IBC_SerialChan, Word boardAddr,
                Int16 *psu0TempWarn,  Int16 *psu0TempAlarm,
                Int16 *psu1TempWarn,  Int16 *psu1TempAlarm )
{
    Int16   cmdStatus;              /* Command Status                       */
    Word    reply[5];               /* Command Result from microcontroller  */

                                    /* Send command to microcontroller      */
    if ( sendMicroCommand( IBC_SerialChan, &reply, sizeof(Word) * 5,
         2, IBC_CPU_APP | VICOR_GET_TEMP_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 thresholds      */
    wordsFromBuf(reply, 5, &cmdStatus, psu0TempWarn,  psu0TempAlarm,
                 psu1TempWarn, psu1TempAlarm );

    return (OK);                    /* Return OK                            */
} /* IBC_DualVicorGetTempThresh() */


/****************************************************************************/
/* Function    : IBC_DualVicorGetAlarmStatus                                */
/* Purpose     : Read IBC Dual Vicor Board Alarm Status                     */
/* Inputs      : Sio32 channel, Board Address, Various Alarm                */
/* Outputs     : Returns OK or ERROR                                        */
/****************************************************************************/
    STATUS
IBC_DualVicorGetAlarmStatus( sio32Chan *IBC_SerialChan, Word boardAddr,
                Int16 *psu0TempAlarm,  Int16 *psu1TempAlarm,
                Int16 *psu0ErrorAlarm, Int16 *psu1ErrorAlarm )

{
    Int16   cmdStatus;              /* Command Status                       */
    Word    reply[5];               /* Command Result from microcontroller  */

                                    /* Send command to microcontroller      */
    if ( sendMicroCommand( IBC_SerialChan, &reply, sizeof(Word) * 5,
         2, IBC_CPU_APP | VICOR_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 thresholds      */
    wordsFromBuf(reply, 5, &cmdStatus, psu0TempAlarm,  psu1TempAlarm,
                 psu0ErrorAlarm, psu1ErrorAlarm );

    return (OK);                    /* Return OK                            */
} /* IBC_DualVicorGetAlarmStatus() */








