/********************************************/
/*SPI Code                                  */
/*                                          */
/*PIC32MX675F256L-80I/PF                    */
/********************************************/

#include "xc.h"

#include "MainBrain32.h"

void SPI3_Init(void)
{
    //Interrupts enable
    IEC0bits.SPI3EIE = 0;
    IEC0bits.SPI3RXIE = 0;
    IEC0bits.SPI3TXIE = 0;
    
    //Baud Rate divisor
    SPI3BRG = 100;          //100 = 380 KHz if PBCLK = 80MHz
    
    // clear the Overflow
    SPI3STATbits.SPIROV = 0;
    
    //Master Mode Slave Select Enable
    SPI3CONbits.MSSEN = 0;
    
    //Master mode enable
    SPI3CONbits.MSTEN = 1;
    
    //Mode 8/16/32 bit
    SPI3CONbits.MODE16 = 0;
    SPI3CONbits.MODE32 = 0;
    
    //SDO Polarity
    SPI3CONbits.CKE = 0;
    
    //SS pin SPI or port controlled
    SPI3CONbits.SSEN = 0;
    
    //DEBUG!
    //Set Frame mode for continuous CLK
    SPI3CONbits.FRMEN = 0;
    
    //CS/SS bit bang
    TRISDbits.TRISD14 = 0;
    PORTDbits.RD14 = 1;
    
    //Start the SPI3 Module
    SPI3CONbits.ON = 0;
    
    return;
}

void SPI4_Init(void)
{
    //Interrupts enable
    IEC1bits.SPI4EIE = 0;
    IEC1bits.SPI4RXIE = 0;
    IEC1bits.SPI4TXIE = 0;
    
    //Baud Rate divisor
    SPI4BRG = 85;
    
    // clear the Overflow
    SPI4STATbits.SPIROV = 0;
    
    //Master Mode Slave Select Enable
    SPI4CONbits.MSSEN = 0;
    
    //Master mode enable
    SPI4CONbits.MSTEN = 1;
    
    //Mode 8/16/32 bit
    SPI4CONbits.MODE16 = 0;
    SPI4CONbits.MODE32 = 0;
    
    //SS pin SPI or port controlled
    SPI4CONbits.SSEN = 0;
    
    //CS/SS bit bang
    TRISDbits.TRISD14 = 0;
    PORTDbits.RD14 = 1;
    
    //DEBUG!
    //Set Frame mode for continuous CLK
    SPI4CONbits.FRMEN = 0;
    
    //Start the SPI4 Module
    SPI4CONbits.ON = 0;
    
    return;
}
