/****************************************************************************/
/* Copyright 2016 MBARI                                                     */
/****************************************************************************/
/* Summary  : ModBus over SPI Routines for OASIS5 on PIC32MX470F512L        */
/* Filename : modspi.c                                                      */
/* Author   : Robert Herlien (rah)                                          */
/* Project  : OASIS Mooring Replacement (OASIS5)                            */
/* Revision: 1.0                                                            */
/* Created  : 07/23/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:                                                    */
/* 23jul2015 rah - created                                                  */
/****************************************************************************/

#ifndef MODSPI_H
#define MODSPI_H

#include <xc.h>
#include <timer.h>
#include <mbariTypes.h>
#include <modbus.h>


/********************************/
/*  Typedefs and defines        */
/********************************/

#define spi1XmitAvail() (SPI1STATbits.SPITBF == 0)
#define spi1RcvAvail()  (SPI1STATbits.SPIRBE == 0)
#define spi1On()        SPI1CONSET = _SPI1CON_ON_MASK
#define spi1Off()       SPI1CONCLR = _SPI1CON_ON_MASK

#define modTimerExpired()  IFS0bits.T2IF

#define SPI_TMOUT       (TICKS_PER_SEC/4)

#ifdef DEBUG_MODSPI
#define spiIntPinOn()   digTest1On()
#define spiIntPinOff()  digTest1Off()
#define mbSentPinOn()   digTest2On()
#define mbSentPinOff()  digTest2Off()
#define spiTransactPinOn()  digTest3On()
#define spiTransactPinOff() digTest3Off()
#else
#define spiIntPinOn()
#define spiIntPinOff()
#define mbSentPinOn()
#define mbSentPinOff()
#define spiTransactPinOn()
#define spiTransactPinOff()
#endif


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

void		modSpiInit(void); 
Errno		modSpiIO(ModBusHandle mh, Nat16 len);
ModBusHandle modBusSpiOpen(Port_t port);
ModBusStruct *getSpiModStruct(void);



#endif

