/****************************************************************************/
/* 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>
#include <mbariTypes.h>


/********************************/
/*  Function Prototypes         */
/********************************/

void digInit(); 


/********************************/
/*    Macros                    */
/********************************/

#define digSerEnbl()	  LATBCLR = 0x10
#define digSerDisbl()	  LATBSET = 0x10
#define digSerShdn()	  LATBCLR = 0x08
#define digSerNoShdn()	  LATBSET = 0x08
#define digModemEnbl()	  LATBSET = 0x40
#define digModemDisbl()	  LATBCLR = 0x40
#define digModemIsOn()	  LATBbits.LATB6
#define digSensorsEnbl()  LATBSET = 0x80
#define digSensorsDisbl() LATBCLR = 0x80
#define digADCEnbl()	  LATGSET = 0x200
#define digADCDisbl()	  LATGCLR = 0x200
#define digIMUEnbl()	  LATDSET = 0x08
#define digIMUDisbl()	  LATDCLR = 0x08

#define digSD_CSel()      LATCCLR = 8
#define digSD_CUnsel()    LATCSET = 8
#define digRTC_CSel()     LATCCLR = 0x4000
#define digRTC_CUnsel()   LATCSET = 0x4000
#define digADC_CSel()     LATACLR = 1
#define digADC_CUnsel()   LATASET = 1
#define digIMU_CSel()     LATDCLR = 4
#define digIMU_CUnsel()	  LATDSET = 4
#define digAllUnsel()	  LATACLR = 1; LATCCLR = 0x4008; LATDCLR = 4

#define digIMU_IO1Set()	  LATASET = 0x8000
#define digIMU_IO1Clr()	  LATACLR = 0x8000
#define digIMU_IO2Set()	  LATDSET = 0x100
#define digIMU_IO2Clr()	  LATDCLR = 0x100

#define digSDDetect()     PORTCbits.RC2
#define digPwrOnSts()     PORTDbits.RD0
#define digRTCIntSts()    PORTCbits.RC14
#define digIMUSts()       PORTDbits.RD1

#define digTest0On()     LATESET = 1
#define digTest0Off()    LATECLR = 1
#define digTest0Toggle() LATEINV = 1
#define digTest1On()     LATESET = 2
#define digTest1Off()    LATECLR = 2
#define digTest1Toggle() LATEINV = 2
#define digTest2On()     LATESET = 4
#define digTest2Off()    LATECLR = 4
#define digTest2Toggle() LATEINV = 4
#define digTest3On()     LATESET = 8
#define digTest3Off()    LATECLR = 8
#define digTest3Toggle() LATEINV = 8

#define digTest4On()     LATESET = 0x10
#define digTest4Off()    LATECLR = 0x10
#define digTest5On()     LATESET = 0x20
#define digTest5Off()    LATECLR = 0x20
#define digTest6On()     LATESET = 0x40
#define digTest6Off()    LATECLR = 0x40
#define digTest7On()     LATESET = 0x80
#define digTest7Off()    LATECLR = 0x80

#define wdtEnable()		 WDTCONSET = _WDTCON_ON_MASK
#define wdtDisable()	 WDTCONCLR = _WDTCON_ON_MASK
#define wdtPing()		 WDTCONSET = _WDTCON_WDTCLR_MASK

#endif
