/****************************************************************************/
/* Copyright 1992 to 1993, MBARI                                            */
/****************************************************************************/
/* Summary  : IBC Regen Controller Board Interface Module for VxWork        */
/* Filename : regen.c                                                       */
/* Author   : Andrew Pearce                                                 */
/* Project  : New ROV                                                       */
/* Version  : Version 1.0                                                   */
/* Created  : 06/09/93                                                      */
/* Modified : 06/09/93                                                      */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/* $Header: regen.c,v 1.1 93/07/01 14:34:32 pean Exp $
 * $Log:        regen.c,v $
 * Revision 1.1  93/07/01  14:34:32  14:34:32  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 "timer.h"                      /* Timer library definitions        */
#include "regen.h"                  /* IBC Regen Controller Card Definitions*/

#include "regenBoard.h"             /* Function Prototypes for regen.c      */

/****************************************************************************/
/* Function    : IBC_regenSetTempThresh                                     */
/* Purpose     : Sets IBC Regen Controller Temperature Warning and Alarm    */
/*               Thresholds                                                 */
/* Inputs      : Sio32 channel, Board Address, Temperature Alarm Thresholds */
/* Outputs     : Returns OK or ERROR                                        */
/****************************************************************************/
    STATUS
IBC_regenSetTempThresh( sio32Chan *IBC_SerialChan, Word boardAddr,
    Int16 tempWarn, Int16 tempAlarm )
{
    Word    status;                 /* Command Result from microcontroller  */

                                    /* Send command to microcontroller      */
    if ( sendMicroCommand( IBC_SerialChan, &status, sizeof(status),
         4, IBC_CPU_APP | REGEN_SET_TEMP_THRESH, (Word) boardAddr,
         tempWarn, tempAlarm ) == ERROR ) return(ERROR);

                                    /* Check command result                  */
    return (wordFromBuf(&status, 0) == CMD_OK ? OK : ERROR);
} /* IBC_regenSetTempThresh() */

/****************************************************************************/
/* Function    : IBC_regenGetTempThresh                                     */
/* Purpose     : Read IBC Regen Controller Temperature Warning and Alarm    */
/*               Thresholds                                                 */
/* Inputs      : Sio32 channel, Board Address, Temperature Alarm Thresholds */
/* Outputs     : Returns OK or ERROR                                        */
/****************************************************************************/
    STATUS
IBC_regenGetTempThresh( sio32Chan *IBC_SerialChan, Word boardAddr,
    Int16 *tempWarn,  Int16 *tempAlarm )
{
    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 | REGEN_GET_TEMP_THRESH, (Word) 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, 3, &cmdStatus, tempWarn,  tempAlarm );

    return (OK);                    /* Return OK                            */
} /* IBC_regenGetTempThresh() */


/****************************************************************************/
/* Function    : IBC_regenSetVoltageThresh                                  */
/* Purpose     : Sets IBC Regen Controller Voltage Warning and Alarm        */
/*               Thresholds                                                 */
/* Inputs      : Sio32 channel, Board Address, Voltage Alarm Thresholds     */
/* Outputs     : Returns OK or ERROR                                        */
/****************************************************************************/
    STATUS
IBC_regenSetVoltageThresh( sio32Chan *IBC_SerialChan, Word boardAddr,
    Int16 voltMinWarn, Int16 voltMinAlarm,
    Int16 voltMaxWarn, Int16 voltMaxAlarm )
{
    Word    status;                 /* Command Result from microcontroller  */

                                    /* Send command to microcontroller      */
    if ( sendMicroCommand( IBC_SerialChan, &status, sizeof(status),
         6, IBC_CPU_APP | REGEN_SET_VOLT_THRESH, (Word) boardAddr,
         voltMinWarn, voltMinAlarm, voltMaxWarn, voltMaxAlarm ) == ERROR )
        return(ERROR);

                                    /* Check command result                  */
    return (wordFromBuf(&status, 0) == CMD_OK ? OK : ERROR);
} /* IBC_regenSetVoltageThresh() */

/****************************************************************************/
/* Function    : IBC_regenGetVoltageThresh                                  */
/* Purpose     : Read IBC Regen Controller Voltage Warning and Alarm        */
/*               Thresholds                                                 */
/* Inputs      : Sio32 channel, Board Address, Voltage Alarm Thresholds     */
/* Outputs     : Returns OK or ERROR                                        */
/****************************************************************************/
    STATUS
IBC_regenGetVoltageThresh( sio32Chan *IBC_SerialChan, Word boardAddr,
    Int16 *voltMinWarn, Int16 *voltMinAlarm,
    Int16 *voltMaxWarn, Int16 *voltMaxAlarm )
{
    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 | REGEN_GET_VOLT_THRESH, (Word) 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, voltMinWarn, voltMinAlarm,
                 voltMaxWarn, voltMaxAlarm );

    return (OK);                    /* Return OK                            */
} /* IBC_regenGetVoltageThresh() */


/****************************************************************************/
/* Function    : IBC_regenResetMinMax                                       */
/* Purpose     : Sets IBC Regen Controller Reset Voltage Min/Max Values     */
/* Inputs      : Sio32 channel, Board Address                               */
/* Outputs     : Returns OK or ERROR                                        */
/****************************************************************************/
    STATUS
IBC_regenResetMinMax( sio32Chan *IBC_SerialChan, Word boardAddr )
{
    Word    status;                 /* Command Result from microcontroller  */

                                    /* Send command to microcontroller      */
    if ( sendMicroCommand( IBC_SerialChan, &status, sizeof(status),
         2, IBC_CPU_APP | REGEN_RESET_MIN_MAX, (Word) boardAddr) == ERROR )
         return(ERROR);

                                    /* Check command result                  */
    return (wordFromBuf(&status, 0) == CMD_OK ? OK : ERROR);
} /* IBC_regenResetMinMax() */


/****************************************************************************/
/* Function    : IBC_regenGetMeasurements                                   */
/* Purpose     : Read IBC Regen Controller Voltage, duty cycles, temperature*/
/* Inputs      : Sio32 channel, Board Address, measured parameters          */
/* Outputs     : Returns OK or ERROR                                        */
/****************************************************************************/
    STATUS
IBC_regenGetMeasurements( sio32Chan *IBC_SerialChan, Word boardAddr,
                Int16 *dutyCycle1, Int16 *dutyCycle2,
                Int16 *busVoltage, Int16 *minBusVoltage, Int16 *maxBusVoltage,
                Int16 *temperature )
{
    Int16   cmdStatus;              /* Command Status                       */
    Word    reply[7];               /* Command Result from microcontroller  */

                                    /* Send command to microcontroller      */
    if ( sendMicroCommand( IBC_SerialChan, &reply, sizeof(Word) * 7,
         2, IBC_CPU_APP | REGEN_GET_STATUS, (Word) 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, 7, &cmdStatus, dutyCycle1, dutyCycle2,
                 busVoltage, minBusVoltage, maxBusVoltage, temperature );

    return (OK);                    /* Return OK                            */
} /* IBC_regenGetMeasurements() */

/****************************************************************************/
/* Function    : IBC_regenGetAlarmStatus                                    */
/* Purpose     : Read IBC Regen Controller Board Alarms                     */
/* Inputs      : Sio32 channel, Board Address, Various Alarms               */
/* Outputs     : Returns OK or ERROR                                        */
/****************************************************************************/
    STATUS
IBC_regenGetAlarmStatus( sio32Chan *IBC_SerialChan, Word boardAddr,
                ibcAlarm *heatsinkTempAlarm,
                ibcAlarm *minBusVoltAlarm, ibcAlarm *maxBusVoltAlarm,
                MBool *openHeaterAlarm, MBool *dutyCycleAlarm )
{
    Int16   cmdStatus;              /* Command Status                       */
    Word    reply[6];               /* Command Result from microcontroller  */

                                    /* Send command to microcontroller      */
    if ( sendMicroCommand( IBC_SerialChan, &reply, sizeof(Word) * 6,
         2, IBC_CPU_APP | REGEN_GET_ALARM_STATUS, (Word) 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 alarms          */
    *heatsinkTempAlarm = (ibcAlarm) wordFromBuf(&reply, sizeof(Word));
    *minBusVoltAlarm   = (ibcAlarm) wordFromBuf(&reply, sizeof(Word) * 2);
    *maxBusVoltAlarm   = (ibcAlarm) wordFromBuf(&reply, sizeof(Word) * 3);
    *openHeaterAlarm   = (MBool)    wordFromBuf(&reply, sizeof(Word) * 4);
    *dutyCycleAlarm    = (MBool)    wordFromBuf(&reply, sizeof(Word) * 5);

    return (OK);                    /* Return OK                            */
} /* IBC_regenGetAlarmStatus() */

/****************************************************************************/
/* Function    : IBC_regenDigitalScope                                      */
/* Purpose     : Read IBC Regen Controller Digital Scope Data               */
/* Inputs      : Sio32 channel, Board Address, analog chan, arrary of values*/
/* Outputs     : Returns OK or ERROR                                        */
/****************************************************************************/
    STATUS
IBC_regenDigitalScope( sio32Chan *IBC_SerialChan, Word boardAddr,
                         Word analogChan, Byte values[REGEN_SAMPLES] )
{
    Int16 cmdStatus;                /* Command Status                       */
    Word  reply[REGEN_SAMPLES + 1]; /* Command Result from microcontroller  */

                                    /* Send command to microcontroller      */
    if ( sendMicroCommand( IBC_SerialChan, &reply,
         sizeof(Word) * REGEN_SAMPLES + 1, 3, IBC_CPU_APP | REGEN_DIGITAL_SCOPE,
         (Word) boardAddr, analogChan ) == 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 copy readings          */
    bcopy(reply + 1, values, REGEN_SAMPLES);

    return (OK);                    /* Return OK                            */
} /* IBC_regenDigitalScope() */

