/*****************************************************************************/
/* Copyright 1991 to 1999 MBARI                                              */
/*****************************************************************************/
/* Summary  : Telemetry Card SIO32 UART Driver for vxWorks                   */
/* Filename : telem_card.c                                                   */
/* Author   : Andrew Pearce                                                  */
/* Project  : Tiburon ROV                                                    */
/* Version  : Version 1.0                                                    */
/* Created  : 04/03/95                                                       */
/* Modified : 06/04/99                                                       */
/* Archived :                                                                */
/*****************************************************************************/

#include "vxWorks.h"               /* vxWorks system declarations            */
#include "mbariTypes.h"            /* MBARI style guide type declarations    */
#include "mbariConst.h"            /* Miscellaneous constants                */
#include "ModNum.h"                /* MBARI module numbers for error code    */
#include "sio32Drv.h"              /* SIO32 Driver information               */
#include "telem_card.h"            /* Telem Card  hardware information       */

#include "arch/mc68k/ivMc68k.h"    /* vxWorks 68K definitions                */

/*****************************************************************************/
/* Function    : sio32BoardProbe                                             */
/* Purpose     : checks for the presence of a telemetry board                */
/* Inputs      : NONE                                                        */
/* Outputs     : Returns OK or ERROR                                         */
/*****************************************************************************/
    STATUS
sio32BoardProbe( Nat32 uart, MBool sio32Debug )
{
    char        probeVal;           /* location used by vxMemProbe           */

                                    /* Look for memory at Vector Register Add*/
    if (vxMemProbe ((char*) SIO32_VECT_REG, VX_READ, sizeof(probeVal),
       &probeVal) == OK)
    {
        if (sio32Debug)             /* Everything checks out OK              */
            printf("Telemetry card found at address 0x%x\n", SIO32_VECT_REG);
        return(OK);                 /* So return OK                          */
    } /* if */

    if (sio32Debug)
       printf("Telemetry board not found at 0x%x\n", SIO32_VECT_REG);

    errnoSet(ESIO32_NO_CARD);       /* Set task error number                 */
    return (ERROR);
} /* sio32BoardProbe() */

/*****************************************************************************/
/* Function    : sio32InitInterrupts                                         */
/* Purpose     : Initialize interrupts for telemetry card UART               */
/* Inputs      : Interrupt vector base and interrupt level                   */
/* Outputs     : None                                                        */
/*               The 68155 interrupt controller is set up to select Local    */
/*               Interrupt 5 level triggered and the interrupt is enabled.   */
/*****************************************************************************/
    Void
sio32InitInterrupts(Nat16 sio32IntVecBase, Nat16 sio32IntLevel)
{
    FAST    Nat16   chan;           /* Channel counter                       */
    FAST    Nat16   uart;           /* Uart Counter                          */
    FAST    Nat16   intVector;      /* Interrupt Vector Number               */
    FAST    Nat16   channel;        /* channel index for intConnect          */
                                    /* Attach interrupt handlers for hardware*/
    char        probeVal;           /* location used by vxMemProbe           */

    intVector = sio32IntVecBase;    /* Point to Interrupt Vector Base        */

                                   /* Look for memory at Vector Register Addr*/
    if (vxMemProbe ((char*) SIO32_VECT_REG, VX_READ, sizeof(probeVal),
       &probeVal) != OK)
      return;
                                    /* Attach ISR for each UART channel pair */
    for (uart = 0, channel = 0; uart < SIO32_MAX_UARTS; uart++)
        if (sio32BoardProbe(uart, FALSE) == OK)
            for (chan = 0; chan < SIO32_CHAN_PER_UART; chan+=2 )
            {                       /* Attach interrupt handlers (16 Max)    */
                intConnect(INUM_TO_IVEC((Char) intVector++), sio32Interrupt,
                    channel);       /* Channel number is passed to ISR       */
                channel += 2;
             } /* for */

                                    /* Set Interrupt Vector on SIO32 board   */
    *SIO32_VECT_REG = (Char) sio32IntVecBase;

    sio32IntEnable(sio32IntLevel);  /* enable interrupt level for SIO32      */
} /* sio32InitInterrupts() */

/*****************************************************************************/
/* Function    : sio32IntEnable                                              */
/* Purpose     : Enable VME Bus interrupt assigned to telemetry card         */
/* Inputs      : interrupt Level                                             */
/* Outputs     : None                                                        */
/*****************************************************************************/
    Void
sio32IntEnable(Nat16 sio32IntLevel)
{
    sysIntEnable(sio32IntLevel);
} /* sio32IntEnable() */

/*****************************************************************************/
/* Function    : sio32IntDisable                                             */
/* Purpose     : Disable VME Bus interrupt assigned to telemetry card        */
/* Inputs      : interrupt Level                                             */
/* Outputs     : None                                                        */
/*****************************************************************************/
    Void
sio32IntDisable(Nat16 sio32IntLevel)
{
    sysIntDisable(sio32IntLevel);
} /* sio32IntDisable() */


