/****************************************************************************/
/* Copyright 2015 MBARI                                                     */
/****************************************************************************/
/* Summary  : Digital I/O Support for OASIS5 on PIC24FJ128GA406             */
/* 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

/* Structure definition for all board types   */
typedef struct
{
    volatile unsigned int   *portP;
    int                     bit;
} PortBits;

/* Structure definition for Daughter board    */
typedef struct
{
    volatile unsigned int   *setP;
    int                     setBit;
    volatile unsigned int   *resetP;
    int                     resetBit;
} RelayBits;

#define RELAY_TICKS     4

/************************************************************/
/*				Debugging									*/
/* To use the Tx Enable lines as test points, un-comment	*/
/* the line for #define DEBUG_PINS below.  Using these pins	*/
/* as test points is most useful on the proto board, which	*/
/* pinned out the enables for testing						*/
/************************************************************/

//#define DEBUG_PINS

#ifdef DEBUG_PINS
#define spiTxPinOn()    digTx3En()
#define spiTxPinOff()   digTx3Dis()
#define spiRxPinOn()    digTx4En()
#define spiRxPinOff()   digTx4Dis()
#define tickPinOn()		LATBbits.LATB3 = 1
#define tickPinOff()	LATBbits.LATB3 = 0
#define dozePinOn()		digTx2En()
#define dozePinOff()	digTx2Dis()
#else
#define spiTxPinOn()
#define spiTxPinOff()
#define spiRxPinOn()
#define spiRxPinOff()
#define tickPinOn()
#define tickPinOff()
#define dozePinOn()
#define dozePinOff()
#endif


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

void digInit(); 
void digSerEnable(int port);
void digSerDisable(int port);
void digSetSerEnable(int port, int state);
int  digGetSerEnable(int port);
void digPwrSet(int port, int state);
void digPwrVecSet(int vec);
int digPwrIsOn(int bitnum);
int  digPwrGet(void);


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

#define digBlinkyOn()    LATEbits.LATE5 = 1
#define digBlinkyOff()   LATEbits.LATE5 = 0
#define digToggleBlinky() LATEbits.LATE5 = (LATEbits.LATE5 ? 0 : 1)
#define digBlinkyIsOn()  LATEbits.LATE5

#ifdef O5_DB
#define digSnd485()
#define digRcv485()     

#define digRelayEn()    LATEbits.LATE6 = 1
#define digRelayDis()   LATEbits.LATE6 = 0

#define digTx0En()      LATDbits.LATD4  = 1
#define digTx0Dis()     LATDbits.LATD4  = 0
#define digTx1En()      LATBbits.LATB15 = 1
#define digTx1Dis()     LATBbits.LATB15 = 0
#define digTx2En()      LATGbits.LATG6  = 1
#define digTx2Dis()     LATGbits.LATG6  = 0
#define digTx3En()      LATDbits.LATD8  = 1
#define digTx3Dis()     LATDbits.LATD8  = 0
#define digTx4En()      LATBbits.LATB14 = 1
#define digTx4Dis()     LATBbits.LATB14 = 0
#define digTx5En()      LATBbits.LATB8  = 1
#define digTx5Dis()     LATBbits.LATB8  = 0

#define digTx0IsEnabled() LATDbits.LATD4
#define digTx1IsEnabled() LATBbits.LATB15
#define digTx2IsEnabled() LATGbits.LATG6
#define digTx3IsEnabled() LATDbits.LATD8
#define digTx4IsEnabled() LATBbits.LATB14
#define digTx5IsEnabled() LATBbits.LATB8

#else /* Not O5_DB; i.e., 5 port Expander  */

#define digSnd485()     LATEbits.LATE7 = 1
#define digRcv485()     LATEbits.LATE7 = 0

#define digTx0En()      LATDbits.LATD4 = 1
#define digTx0Dis()     LATDbits.LATD4 = 0
#define digTx1En()      LATBbits.LATB15 = 1
#define digTx1Dis()     LATBbits.LATB15 = 0
#define digTx2En()      LATGbits.LATG6 = 1
#define digTx2Dis()     LATGbits.LATG6 = 0
#define digTx3En()      LATBbits.LATB14 = 1
#define digTx3Dis()     LATBbits.LATB14 = 0
#define digTx4En()      LATBbits.LATB8 = 1
#define digTx4Dis()     LATBbits.LATB8 = 0

#define digPwr0En()     LATDbits.LATD5 = 1
#define digPwr0Dis()    LATDbits.LATD5 = 0
#define digPwr1En()     LATDbits.LATD0 = 1
#define digPwr1Dis()    LATDbits.LATD0 = 0
#define digPwr2En()     LATGbits.LATG2 = 1
#define digPwr2Dis()    LATGbits.LATG2 = 0
#define digPwr3En()     LATBbits.LATB15 = 1
#define digPwr3Dis()    LATBbits.LATB15 = 0
#define digPwr4En()     LATDbits.LATD7 = 1
#define digPwr4Dis()    LATDbits.LATD7 = 0

#define digTx0IsEnabled() LATDbits.LATD4
#define digTx1IsEnabled() LATDbits.LATD11
#define digTx2IsEnabled() LATGbits.LATG3
#define digTx3IsEnabled() LATBbits.LATB13
#define digTx4IsEnabled() LATBbits.LATB6

#define digPwr0IsOn()     LATDbits.LATD5
#define digPwr1IsOn()     LATDbits.LATD0
#define digPwr2IsOn()     LATGbits.LATG2
#define digPwr3IsOn()     LATBbits.LATB15
#define digPwr4IsOn()     LATDbits.LATD7
#endif /* O5_DB */

#endif /* DIG_IO_H */
