/*******************************************************************************
* Device      : AD18F06
* Author      : XAD Application Team
* Version     : V0.0.1
* Date        : 202012001
* Description :
* Note		  :
********************************************************************************/

/********************************INCLUDES*************************************/
#include <stdio.h>
#include "AD18F06A.h"

__CONFIG(1,_FINTOSC_DIV1_1L   & _LVREN_OFF_1L      & _LVR24V_1L                                                                                          );
__CONFIG(2,_FLASH_CP7_OFF_2L  & _FLASH_CP6_OFF_2L  & _FLASH_CP5_OFF_2L   & _FLASH_CP4_OFF_2L   & _FLASH_CP3_OFF_2L
         & _FLASH_CP2_OFF_2L  & _FLASH_CP1_OFF_2L  & _FLASH_CP0_OFF_2L   & _CRYLEN_OFF_2L      & _CRYHEN_OFF_2L
  	     & _IDIS_ON_2L        & _CKSUM_OFF_2L      & _CBP_OFF_2L         & _RESETE_OFF_2L      & _RSEL_LOW_2L     & _STV_ON_2L              	         );
__CONFIG(3,_DBG_OFF_3L        & _PSUT_8_3L         & _FCPU_2T_3L         & _WDTEN_OFF_3L       & _TWDT_111_3L                                            );
__CONFIG(4,_OSCM_HIRC_16M_4L  & _ENVDDL_ON_4L      & _EEDIS_OFF_4L       & _FLDIS_OFF_4L       & _FCMEN_OFF_4L    & _IESO_OFF_4L&  _DBGS_IOSEL_1_4L      );


void putch(unsigned char tmp)
{
	while(!TXIF)continue;
	TXREG = tmp;
	TXIF = 0;
}


unsigned char getch(void)
{
	while(!RCIF)continue;
	RCIF = 0;
	return RCREG;
}


#define SS_EN_SEL                 0
#define SS_Dis_SEL                1

#define SPI_Slave_MODE            0
#define SPI_Master_MODE           1

#define SPI_MODE0                 0   // 空闲时是低电平，第1个跳变沿是上升沿，第2个跳变沿是下降沿,数据在第1个跳变沿（上升沿）采样
#define SPI_MODE1                 1   // 空闲时是低电平，第1个跳变沿是上升沿，第2个跳变沿是下降沿,数据在第2个跳变沿（下降沿）采样


#define SPI_Speed_Fsys_2          0
#define SPI_Speed_Fsys_4          1
#define SPI_Speed_Fsys_8          2
#define SPI_Speed_Fsys_16         3
#define SPI_Speed_Fsys_32         4
#define SPI_Speed_Fsys_64         5
#define SPI_Speed_Fsys_128        6
#define SPI_NONE_CLK              7

#define SPI_BUS_GPIO_MODE0        0   // SPI_SCK:PA2  SPI_MOSI:PA1  SPI_MISO:PA0
#define SPI_BUS_GPIO_MODE1        1   // SPI_SCK:PD5  SPI_MOSI:PD6  SPI_MISO:PD7


#define SPI_SSN_PA3               0   // SPI_SSN:PA3
#define SPI_SSN_PA2               1   // SPI_SSN:PA2
#define SPI_SSN_PD0               2   // SPI_SSN:PD0


#define SPI_Master_CS1            PA3
#define SPI_Master_CS2            PA4

void SPI_INIT(unsigned char SSEN_SEL,unsigned char SPI_SSN_GPIO_SEL,unsigned char SPI_GPIO_SEL,unsigned char SPI_MODE_t,unsigned char SPI_BUS_t,unsigned char SPI_Speed_SEL)
{
	unsigned char SPCON_TEMP = 0;
    unsigned char SPSTA_TEMP = 0;

	SPCON_TEMP |= (SSEN_SEL   << 5);
	SPCON_TEMP |= (SPI_MODE_t << 4);

	switch(SPI_BUS_t)
	{
		case SPI_MODE0:{SPCON_TEMP |= (0 << 3);SPCON_TEMP |= (0 << 2);}break;
		case SPI_MODE1:{SPCON_TEMP |= (0 << 3);SPCON_TEMP |= (1 << 2);}break;
		default:{SPCON_TEMP |= (0 << 3);SPCON_TEMP |= (0 << 2);}break;
	}
	switch(SPI_Speed_SEL)
	{
		case SPI_Speed_Fsys_2  :{SPCON_TEMP |= (0 << 0);}break;
        case SPI_Speed_Fsys_4  :{SPCON_TEMP |= (1 << 0);}break;
		case SPI_Speed_Fsys_8  :{SPCON_TEMP |= (2 << 0);}break;
		case SPI_Speed_Fsys_16 :{SPCON_TEMP |= (3 << 0);}break;
		case SPI_Speed_Fsys_32 :{SPCON_TEMP |= 0x80;SPCON_TEMP |= (0 << 0);}break;
        case SPI_Speed_Fsys_64 :{SPCON_TEMP |= 0x80;SPCON_TEMP |= (1 << 0);}break;
		case SPI_Speed_Fsys_128:{SPCON_TEMP |= 0x80;SPCON_TEMP |= (2 << 0);}break;
		case SPI_NONE_CLK:{SPCON_TEMP |= 0x80;SPCON_TEMP |= (3 << 0);}break;
		default:{SPCON_TEMP |= (3 << 0);}break;
	}

	if(SPI_MODE_t == SPI_Slave_MODE)
	{
		switch(SPI_SSN_GPIO_SEL)
		{
			case SPI_SSN_PA3:{SPSTA_TEMP |= (0 << 2);TRISA3 = 1;PA3PU = 1;PA3PD = 1;PA3OD = 0;PA3AEN = 0;}break;
			case SPI_SSN_PA2:{SPSTA_TEMP |= (1 << 2);TRISA2 = 1;PA2PU = 1;PA2PD = 1;PA2OD = 0;PA2AEN = 0;}break;

			default:{SPSTA_TEMP |= (0 << 2);TRISA3 = 1;PA3PU = 1;PA3PD = 1;PA3OD = 0;PA3AEN = 0;}break;
		}
    }

	switch(SPI_GPIO_SEL)
	{
		case SPI_BUS_GPIO_MODE0:
		{
			SPSTA_TEMP |= (0 << 0);
		    if(SPI_MODE_t == SPI_Master_MODE)
			{
                TRISA2 = 0;PA2PU = 1;PA2PD = 1;PA2OD = 0;PA2AEN = 0;//SPI_SCK
				TRISA1 = 0;PA1PU = 1;PA1PD = 1;PA1OD = 0;PA1AEN = 0;//SPI_MOSI
				TRISA0 = 1;PA0PU = 1;PA0PD = 1;PA0OD = 0;PA0AEN = 0;//SPI_MISO

			}else if(SPI_MODE_t == SPI_Slave_MODE)
			{
                TRISA2 = 1;PA2PU = 1;PA2PD = 1;PA2OD = 0;PA2AEN = 0;//SPI_SCK
				TRISA1 = 1;PA1PU = 1;PA1PD = 1;PA1OD = 0;PA1AEN = 0;//SPI_MOSI
				TRISA0 = 1;PA0PU = 1;PA0PD = 1;PA0OD = 0;PA0AEN = 0;//SPI_MISO
			}
		}break;
        case SPI_BUS_GPIO_MODE1:
		{
			SPSTA_TEMP |= (1 << 0);
		    if(SPI_MODE_t == SPI_Master_MODE)
			{
                TRISD5 = 0;PD5PU = 1;PD5PD = 1;PD5OD = 0;PD5AEN = 0;//SPI_SCK
				TRISD6 = 0;PD6PU = 1;PD6PD = 1;PD6OD = 0;PD6AEN = 0;//SPI_MOSI
				TRISD7 = 1;PD7PU = 1;PD7PD = 1;PD7OD = 0;PD7AEN = 0;//SPI_MISO
			}else if(SPI_MODE_t == SPI_Slave_MODE)
			{
                TRISD5 = 1;PD5PU = 1;PD5PD = 1;PD5OD = 0;PD5AEN = 0;//SPI_SCK
				TRISD6 = 1;PD6PU = 1;PD6PD = 1;PD6OD = 0;PD6AEN = 0;//SPI_MOSI
				TRISD7 = 1;PD7PU = 1;PD7PD = 1;PD7OD = 0;PD7AEN = 0;//SPI_MISO
			}
		}break;
		default:
		{
			SPSTA_TEMP |= (0 << 0);
		    if(SPI_MODE_t == SPI_Master_MODE)
			{
                TRISA2 = 0;PA2PU = 1;PA2PD = 1;PA2OD = 0;PA2AEN = 0;//SPI_SCK
				TRISA1 = 0;PA1PU = 1;PA1PD = 1;PA1OD = 0;PA1AEN = 0;//SPI_MOSI
				TRISA0 = 1;PA0PU = 1;PA0PD = 1;PA0OD = 0;PA0AEN = 0;//SPI_MISO

			}else if(SPI_MODE_t == SPI_Slave_MODE)
			{
                TRISA2 = 1;PA2PU = 1;PA2PD = 1;PA2OD = 0;PA2AEN = 0;//SPI_SCK
				TRISA1 = 1;PA1PU = 1;PA1PD = 1;PA1OD = 0;PA1AEN = 0;//SPI_MOSI
				TRISA0 = 1;PA0PU = 1;PA0PD = 1;PA0OD = 0;PA0AEN = 0;//SPI_MISO
			}
		}break;
	}

    SPCON_TEMP |= (1 << 6);

	SPICLKEN = 1;
    SPSTA = SPSTA_TEMP;
    SPCON = SPCON_TEMP;

	printf("SPCON_TEMP = 0x%02X;SPCON = 0x%02X;\r\n",SPCON_TEMP,SPCON);
	printf("SPSTA_TEMP = 0x%02X;SPSTA = 0x%02X;\r\n",SPSTA_TEMP,SPSTA);
}

void main(void)
{
    unsigned char Send_Dat  = 0;
    unsigned char Send_Flag = 1;

    TRISA3 = 0;
	TRISA4 = 0;

    SPI_Master_CS1 = 1;
	SPI_Master_CS2 = 1;
	//TX_PB0_RX_PB1@9600
	UART0CLKEN = 1;
    TX0_MAP    = MAP_PB0;
    RX0_MAP    = MAP_PB1;
	TRISB0     = 0;
	TRISB1     = 1;
	UARTCON    = 0x00;
	SPBRG      = 0x1A;
	RCSTA      = 0x90;
	TXSTA      = 0x20;

	printf("***********************************\r\n");
	printf("*             XAD_TEST            *\r\n");
	printf("***********************************\r\n");
	printf("                                   \r\n");

    SPI_INIT(SS_Dis_SEL,SPI_SSN_PD0,SPI_BUS_GPIO_MODE1,SPI_Master_MODE,SPI_MODE1,SPI_Speed_Fsys_2);

    T0CLKEN = 1;
	TMR0    = 0x00;
	PR0     = 150;
	T0CON0  = 0x26;
	T0CON1  = 0x00;

    while(1)
    {
  	    if(TMR0IF == 1)
		{
		    TMR0IF = 0;
			T0CLKEN = 0;
            if(Send_Flag == 1)SPI_Master_CS1 = 0;else  if(Send_Flag == 2)SPI_Master_CS2 = 0;

            __delay_us(10);

			SPDAT = Send_Dat;
			while(!SPIIF)continue;
			SPIIF = 0;

            if(Send_Flag == 1){SPI_Master_CS1 = 1;Send_Flag = 1;}else if(Send_Flag == 2){SPI_Master_CS2 = 1;Send_Flag = 1;}

            printf("Master%d.Send_Data = 0x%02X,Recive_Data = 0x%02X,SPCON = 0x%02X,SPSTA = 0x%02X;\r\n",Send_Flag,Send_Dat,SPDAT,SPCON,SPSTA);

		    Send_Dat = Send_Dat + 1;
            if(Send_Dat >= 255)
			{
				Send_Dat = 0;
			}
			T0CLKEN = 1;
		}
	}
	printf("END!!\r\n");
}



void interrupt ISR_h (void)
{



}

void interrupt low_priority ISR_l (void)
{

}
