/************************************************************************/
/* Copyright 2003-2018 MBARI											*/
/************************************************************************/
/* Summary	: Definitions for the SPI I/O Bits for BEDS2 controller		*/
/* Filename : spi.c														*/
/* Author	: Robert Herlien (rah)										*/
/* Project	: Benthic Event Detection System (BEDS)						*/
/* Revision: 1.0														*/
/* Created	: 10/17/2011 from Respirometer spi.h						*/
/*																			*/
/* 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:												*/
/* 10jan2003 rah - created												*/
/* 08dec2008 rah - modified for respirometer controller					*/ 
/* 17oct2011 rah - modified for BEDS									*/
/* 20feb2018 rah - modified for BEDS2									*/
/************************************************************************/

#ifndef INCspih
#define INCspih 1

typedef enum {SPI_IDLE=0, SPI_SD, SPI_ADC, SPI_RTC, SPI_IMU} SpiSelect;

#define SPI1Write(dat)		while (SPI1STATbits.SPITBF); SPI1BUF = dat;
#define SPI2Write(dat)		while (SPI2STATbits.SPITBF); SPI2BUF = dat;
#define SPI1Read()			while (SPI1STATbits.SPIRBE); SPI1BUF;
#define SPI2Read()			while (SPI2STATbits.SPIRBE); SPI2BUF;

#define SPI1On()			SPI1CONSET = _SPI1CON_ON_MASK;
#define SPI1Off()			SPI1CONCLR = _SPI1CON_ON_MASK;
#define SPI2On()			SPI2CONSET = _SPI2CON_ON_MASK;
#define SPI2Off()			SPI2CONCLR = _SPI2CON_ON_MASK;


/****************************************/
/* Function Declarations				*/
/****************************************/

void	spiInit(Void);
void	spi1WriteRead(BYTE *wp, BYTE *rp, int nBytes);
void	spi2WriteRead(BYTE *wp, BYTE *rp, int nBytes);

void	spiSelectSD(void);
void	spiSetSDBRG(unsigned int brg);
void	spiSelectADC(void);
void	spiSelectRTC(void);
void	spiSelectIMU(void);
void	spi1Unselect(void);
void	spi2Unselect(void);

#endif	/* INCspih */
