/****************************************************************************/
/* 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                                                  */
/****************************************************************************/

#include <xc.h>
#include <GenericTypeDefs.h>

#include "spi.h"


/************************************************************************/
/* Function    : spiInit                                                */
/* Purpose     : Setup SPI buses                                        */
/* Inputs      : None                                                   */
/* Outputs     : None                                                   */
/************************************************************************/
void spiInit()
{
    REFOCONbits.ACTIVE = 0;
    REFOCONbits.ROSEL = 6;      /* Use USB PLL clock - 48MHz     */
    REFOCONbits.RODIV = 0;
    REFOTRIMbits.ROTRIM = 0;
    REFOCONbits.OE = 0;
    REFOCONbits.ON = 1;
    REFOCONbits.RSLP = 0;
    REFOCONbits.ACTIVE = 1;

    /* Map the pins             */
    SDI1R = 0x03;               /* MISO1 = RPD11, pin 71    */
    RPD9R = 0x08;               /* MOSI1 = RPD9, pin 69     */
    SDI2R = 0x0a;               /* MISO2 = RPC4, pin 9      */
    RPC1R = 0x06;               /* MOSI2 = RPC1, pin 6      */

    /* Setup SPI1 (local) Control Registers */

    SPI1CON = 0;                /* Init SPI1CON to 0        */
    SPI1CONSET = _SPI1CON_MCLKSEL_MASK; /* Use REFCLK       */
//    SPI1CONSET = _SPI1CON_MODE16_MASK;  /* 16 bit mode      */
                                        /* 8 bit mode no bits set*/
    SPI1CONSET = _SPI1CON_CKE_MASK;     /* Out chng on falling edge */
    SPI1CONSET = _SPI1CON_MSTEN_MASK;   /* SPI Master       */

    SPI1CON2 = 0;
    SPI1BRG = 11;                /* 4 MHz                     */

    SPI1STATCLR = _SPI1STAT_SPIROV_MASK; /* Clear rcv ovfl  */
    SPI1CONSET = _SPI1CON_ON_MASK;      /* Turn it on       */

}

UINT16 spi1ReadWrite(UINT16 dat)
{
    while ( SPI1STATbits.SPITBF )   /* Wait for Tx buf available    */
        ;

    SPI1BUF = dat;                  /* put data in SPI TX buffer    */

    while ( !SPI1STATbits.SPIRBF )  /* wait for received byte   */
        ;

    return(SPI1BUF);                /* Return received byte     */
}

