/******************** (C) COPYRIGHT 2020 SONiX *******************************
* COMPANY:			SONiX
* DATE:					2020/06
* AUTHOR:				SA1
* IC:						SN32F780/790
* DESCRIPTION:	SPI0 related functions.
*____________________________________________________________________________
*	REVISION	Date				User		Description
*	1.0				2020/06/24	SA1			1. First release
*
*____________________________________________________________________________
* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS TIME TO MARKET.
* SONiX SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL 
* DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT OF SUCH SOFTWARE
* AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION CONTAINED HEREIN 
* IN CONNECTION WITH THEIR PRODUCTS.
*****************************************************************************/

/*_____ I N C L U D E S ____________________________________________________*/
#include <SN32F790.h>
#include "SPI.h"
#include "..\..\Utility\Utility.h"

/*_____ D E C L A R A T I O N S ____________________________________________*/

/*_____ D E F I N I T I O N S ______________________________________________*/

/*_____ M A C R O S ________________________________________________________*/

/*_____ F U N C T I O N S __________________________________________________*/

/*****************************************************************************
* Function		: SPI0_Init
* Description	: Initialization of SPI0
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
void SPI0_Init(void)
{
	__SPI0_SEL0_SETTING;
	__SPI0_SET_SEL0;	
	
	//Enable HCLK for SPI0
	SN_SYS1->AHBCLKEN_b.SPI0CLKEN = 1;							//Enable clock for SPI0.

	//SPI0 setting
	SN_SPI0->CTRL0_b.DL = SPI_DL_8;									//3 ~ 16 Data length
	SN_SPI0->CTRL0_b.MS = SPI_MS_MASTER_MODE;				//Master/Slave selection bit
	SN_SPI0->CTRL0_b.LOOPBACK = SPI_LOOPBACK_DIS; 	//Loop back mode
	SN_SPI0->CTRL0_b.SDODIS = SPI_SDODIS_EN; 				//Slave data output 
//	SN_SPI0->CTRL0_b.TXFIFOTH = SPI_TXFIFOTH_4;			//(ONLY used in slave mode)

	SN_SPI0->DFDLY_b.DFETCH_EN = 1;

	SN_SPI0->CLKDIV_b.DIV = 0;											//SPIn clock divider //0:SCK = SPIn_PCLK/2

	//SPI0 SPI mode
	SN_SPI0->CTRL1 = mskSPI_CPHA_FALLING_EDGE|			//Clock phase for edge sampling
									 mskSPI_CPOL_SCK_IDLE_LOW|			//Clock polarity selection bit
									 mskSPI_MLSB_MSB;								//MSB/LSB selection bit

	//SPI0 SEL0 setting
	SN_SPI0->CTRL0_b.SELDIS = SPI_SELDIS_DIS; 			//Auto-SEL disable bit

	//SPI0 Interrupt Enable
	SN_SPI0->IE_b.RXFIFOTHIE = 1;

	//SPI0 Fifo reset
	__SPI0_FIFO_RESET;

	//SPI0 enable	
	SN_SPI0->CTRL0_b.SPIEN  = SPI_SPIEN_EN;    			//SPI enable bit
}

/*****************************************************************************
* Function		: SPI0_TX_DATA
* Description	: Data
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
void SPI0_TX_DATA(uint8_t bData)
{
	uint8_t bRD;
	
	SN_SPI0->DATA = bData;
	while((SN_SPI0->STAT & 0x10));
	bRD = SN_SPI0->DATA;
}

/*****************************************************************************
* Function		: SPI0_Enable
* Description	: SPI0 enable setting
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
void SPI0_Enable(void)
{
	SN_SYS1->AHBCLKEN_b.SPI0CLKEN = 1;							//Enable clock for SPI0.
  SN_SPI0->CTRL0_b.SPIEN = SPI_SPIEN_EN;    			//SPI enable bit
	__SPI0_FIFO_RESET;
}

/*****************************************************************************
* Function		: SPI0_Disable
* Description	: SPI0 disable setting
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
void SPI0_Disable(void)
{
  SN_SPI0->CTRL0_b.SPIEN  = SPI_SPIEN_DIS;    		//SPI disable bit
	SN_SYS1->AHBCLKEN_b.SPI0CLKEN = 0;							//Disable clock for SPI0.
}

/*********************************************************************************************************
* Function Name: SPI0_IRQHandler
* Description:
* Input:
* Output:
* Return:
*********************************************************************************************************/
__irq	void SPI0_IRQHandler (void)
{
	uint16_t hwRIS_buf;

	hwRIS_buf = SN_SPI0->RIS;
	if ((hwRIS_buf & mskSPI_DMATCIF) ==  mskSPI_DMATCIF) 	//DMATCIF
	{
		SN_SPI0->IC_b.DMATCIC = 1;
		bSPI0_DMATC_Flag = 1;	
	}
	if ((hwRIS_buf & mskSPI_DMAHTIF) ==  mskSPI_DMAHTIF) 	//DMAHTIF
	{
		SN_SPI0->IC_b.DMAHTIC = 1;
		bSPI0_DMAHT_Flag = 1;
	}	
}
