#include <p33FJ64MC804.h>
#include <stdio.h>
#include <timer.h>
#include <uart.h>
#define _MI2CIF 
#include <i2c.h>
#include <spi.h>
#include "delay.h"
#include "initialisation.h"








//****************************************************************

					//SPI	

void InitSPI1(void)
{			
	unsigned int config1;
    unsigned int config2; 
	unsigned int config3;

   config1 =            ENABLE_SCK_PIN &
			ENABLE_SDO_PIN & //ENABLE_SDOPIN &
			SPI_MODE16_OFF &
			SPI_SMP_OFF &
			SPI_CKE_OFF &
                        CLK_POL_ACTIVE_HIGH &
			SLAVE_ENABLE_OFF &			
			MASTER_ENABLE_ON &
			SEC_PRESCAL_7_1 &
			PRI_PRESCAL_64_1;



	config2 =       FRAME_ENABLE_OFF &
			FRAME_SYNC_OUTPUT &
			FRAME_POL_ACTIVE_HIGH &
			FRAME_SYNC_EDGE_COINCIDE;

	config3 =       SPI_ENABLE &
			SPI_IDLE_CON &
			SPI_RX_OVFLOW_CLR;

 

OpenSPI1(config1, config2, config3);

    
}

void InitSPI2(void)
{
	unsigned int config1;
    unsigned int config2; 
	unsigned int config3;
/* Configure SPI2 interrupt */

ConfigIntSPI2(SPI_INT_DIS );

config1 = 
                ENABLE_SDO_PIN &
		SPI_MODE16_OFF &
		SPI_SMP_OFF&
		SPI_CKE_OFF &
                CLK_POL_ACTIVE_HIGH &
		SLAVE_ENABLE_OFF &
		MASTER_ENABLE_OFF &
		SEC_PRESCAL_7_1 &
		PRI_PRESCAL_64_1;



config2 =       FRAME_ENABLE_OFF& //&
		FRAME_SYNC_OUTPUT &
		FRAME_POL_ACTIVE_HIGH &
		FRAME_SYNC_EDGE_COINCIDE;

 

config3 =       SPI_ENABLE &
		SPI_IDLE_CON& //&
		SPI_RX_OVFLOW_CLR;

 

OpenSPI2(config1, config2, config3);


}

void Initport(void)
{


        //*********SPI1=Master

	TRISCbits.TRISC0=0;   //CLK1
	AD1PCFGLbits.PCFG6=1; //AN6 en digital SCK

        TRISCbits.TRISC3=0;  //SDO1 sortie sur RC3 digital
	AD1PCFGLbits.PCFG7=1; //AN7 en digital
	
	TRISCbits.TRISC2=0;  //SSLAVE1
	AD1PCFGLbits.PCFG8=1;//AN8 en digital
        
        RPINR20bits.SDI1R=0X11; // SDI input sur RP17/AN7/RC1
	_RP19R=7; //SDO sur RP19 broche digitale/RC3
	_RP16R=8; //SCK1 OUT sur RP16/RC0
        //_RP18R=9; //// SSlave1OUT sur RP18/AN8/RC2

        //*********SPI2=Slave

	TRISCbits.TRISC4=1;	//SCK2/RC4/RP20
	TRISCbits.TRISC5=1;	//SDI2/RC5
	TRISCbits.TRISC6=1;	//SSLAVE2/RC6
        TRISCbits.TRISC7=0;	//SDO2 /RC7

        RPINR22bits.SDI2R=21;    //SDI2 sur RP21/RC5
        RPINR22bits.SCK2R=20;    //SCK2 sur RP20
        RPINR23bits.SS2R=22;      //SSLAVE2 sur RP22
	_RP23R=10 ; //SDO2 sur RC7/RP23
}