/****************************************************************************/
/* Copyright 2015 MBARI                                                     */
/****************************************************************************/
/* Summary  : Digital I/O Support for OASIS5 on PIC32MX470F512L             */
/* Filename : dig_io.h                                                      */
/* Author   : Robert Herlien (rah)                                          */
/* Project  : OASIS Mooring Replacement (OASIS5)                            */
/* Revision: 1.0                                                            */
/* Created  : 07/13/2015                                                    */
/*                                                                          */
/* MBARI provides this documentation and code "as is", with no warranty,    */
/* express or implied, of its quality or consistency. It is provided without*/
/* support and without obligation on the part of the Monterey Bay Aquarium  */
/* Research Institute to assist in its use, correction, modification, or    */
/* enhancement. This information should not be published or distributed to  */
/* third parties without specific written permission from MBARI.            */
/*                                                                          */
/****************************************************************************/
/* Modification History:                                                    */
/* 13jul2015 rah - created                                                  */
/****************************************************************************/

#ifndef DIG_IO_H
#define DIG_IO_H

#include <xc.h>

#define RELAY_DELAY_MS   30


/********************************/
/*  Function Prototypes         */
/********************************/

void digInit(); 
void digSerEnable(int port);
void digSerDisable(int port);
int  digGetSerEnable(int port);
void digSnd485(int port);
void digRcv485(int port);
int  digSense485(int port);
void digPwrOn(int port);
void digPwrOff(int port);
void digPwrSet(int port, int state);
int	 digPwrVecGet(void);
void digPwrVecSet(int state);
void digPwrAllOff(void);


/********************************/
/*    Macros                    */
/********************************/

#define digBlinkyOn()     LATFSET = 0x1000
#define digBlinkyOff()    LATFCLR = 0x1000
#define digToggleBlinky() LATFINV = 0x1000
#define digBlinkyIsOn()   LATFbits.LATF12

#define dig12vEnable()    LATBSET = 0x1000
#define dig12vDisable()   LATBCLR = 0x1000
#define digEnvEnable()    LATFSET = 0x2000
#define digEnvDisable()   LATFCLR = 0x2000

#define digVBusOn()       LATBSET = 0x20
#define digVBusOff()      LATBCLR = 0x20
#define digGFLowOn()      LATASET = 2
#define digGFLowOff()     LATACLR = 2
#define digGFHighOn()     LATBSET = 0x800
#define digGFHighOff()    LATBCLR = 0x800
#define digSD_CSel();     LATCCLR = 8
#define digSD_CUnsel();   LATCSET = 8
#define digSDDetect()     LATCbits.LATC2

#define dig1Snd485()    LATDSET = 0x40
#define dig1Rcv485()    LATDCLR = 0x40
#define dig2Snd485()    LATDSET = 0x80
#define dig2Rcv485()    LATDCLR = 0x80
#define dig3Snd485()    LATFSET = 1
#define dig3Rcv485()    LATFCLR = 1
#define dig4Snd485()    LATFSET = 2
#define dig4Rcv485()    LATFCLR = 2

#define digTx0En()      LATDSET = 0x20
#define digTx0Dis()     LATDCLR = 0x20
#define digTx1En()      LATGSET = 2
#define digTx1Dis()     LATGCLR = 2
#define digTx2En()      LATGSET = 1
#define digTx2Dis()     LATGCLR = 1
#define digTx3En()      LATASET = 0x40
#define digTx3Dis()     LATACLR = 0x40
#define digTx4En()      LATASET = 0x80
#define digTx4Dis()     LATACLR = 0x80

#define dig1Is485()     LATBbits.LATB2
#define dig2Is485()     LATBbits.LATB3
#define dig3Is485()     LATBbits.LATB4
#define dig4Is485()     LATEbits.LATE9

#define digTx0IsEnabled() LATDbits.LATD5
#define digTx1IsEnabled() LATGbits.LATG1
#define digTx2IsEnabled() LATGbits.LATG9
#define digTx3IsEnabled() LATAbits.LATA6
#define digTx4IsEnabled() LATAbits.LATA7

#define relay1SetOn()    LATGSET = 0x2000
#define relay1SetOff()   LATGCLR = 0x2000
#define relay2SetOn()    LATESET = 4
#define relay2SetOff()   LATECLR = 4
#define relay3SetOn()    LATESET = 8
#define relay3SetOff()   LATECLR = 8
#define relay4SetOn()    LATESET = 0x10
#define relay4SetOff()   LATECLR = 0x10

#define relay1ResetOn()  LATESET = 1
#define relay1ResetOff() LATECLR = 1
#define relay2ResetOn()  LATESET = 2
#define relay2ResetOff() LATECLR = 2
#define relay3ResetOn()  LATGSET = 0x4000
#define relay3ResetOff() LATGCLR = 0x4000
#define relay4ResetOn()  LATGSET = 0x1000
#define relay4ResetOff() LATGCLR = 0x1000

#define clearRelayCoils() {LATECLR = 0x1f; LATGCLR = 0x7000;}

#define digTest0On()     LATGSET = 0x8000
#define digTest0Off()    LATGCLR = 0x8000
#define digTest0Toggle() LATGINV = 0x8000
#define digTest1On()     LATESET = 0x20
#define digTest1Off()    LATECLR = 0x20
#define digTest1Toggle() LATEINV = 0x20
#define digTest2On()     LATESET = 0x40
#define digTest2Off()    LATECLR = 0x40
#define digTest2Toggle() LATEINV = 0x40
#define digTest3On()     LATESET = 0x80
#define digTest3Off()    LATECLR = 0x80
#define digTest3Toggle() LATEINV = 0x80

#endif

