/****************************************************************************/
/* Copyright 2015 MBARI                                                     */
/****************************************************************************/
/* Summary  : SPI Routines for OASIS5 on PIC32MX470F512L                    */
/* Filename : spi.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 SPI_H
#define SPI_H

/********************************/
/*	Function Prototypes			*/
/********************************/

void	spiInit(void); 
UINT16  spi1ReadWrite(UINT16 dat);


/********************************/
/*	  Macros                    */
/********************************/

#define spi1Write(dat)              \
{                                   \
    while ( SPI1STATbits.SPITBF )   \
        ;                           \
    SPI1BUF = dat;                  \
}

#define spi1ReadWait()              \
{                                   \
    while ( !SPI1STATbits.SPIRBF )  \
        ;                           \
}

#define spi1Read()          SPI1BUF

#if 0
#if 1
#define spi1ReadWrite(wdat, rdat)   \
{                                   \
    while ( SPI1STATbits.SPITBF )   \
        ;                           \
                                    \
    SPI1BUF = wdat;                 \
                                    \
    while ( !SPI1STATbits.SPIRBF )  \
        ;                           \
                                    \
    rdat = SPI1BUF;                 \
}
#else
#define spi1ReadWrite(wdat, rdat)   \
{                                   \
    int i;                          \
    for (i = 4; SPI1STATbits.SPITBF && i; i-- ) \
        ;                           \
                                    \
    SPI1BUF = wdat;                 \
                                    \
    for (i = 4; !SPI1STATbits.SPIRBF && i; i-- ) \
        ;                           \
                                    \
    rdat = SPI1BUF;                 \
}
#endif
#endif

#endif

