/******************** (C) COPYRIGHT 2020 SONiX *******************************
* COMPANY:		SONiX
* DATE:				2020/11
* AUTHOR:			SA1
* IC:					SN32F780/790
*____________________________________________________________________________
*	REVISION	Date				User		Description
*	1.0				2020/06/24	SA1			1. First version released
*																2. Compatible to CMSIS DFP Architecture in Keil MDK v5.X (http://www.keil.com/dd2/pack/)
*																3. Run HexConvert to generate bin file and show checksum after building.
*	1.1				2020/11/20	SA1			1. Update system_SN32F780.c
*
*____________________________________________________________________________
* 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 <SN32F780.h>
#include <SN32F700_Def.h>
#include ".\Interface\SPI\SPI.h"
#include ".\Interface\EBI\EBI.h"
#include ".\Interface\EBI8080LCD\EBI8080_LCD.h"
#include "Utility\Utility.h"

/*_____ D E C L A R A T I O N S ____________________________________________*/
void PFPA_Init(void);
void NotPinOut_GPIO_init(void);
void Show_Picture(uint32_t wFlashAdd, uint32_t wSize);

void INCFILE01(void);
void INCFILE02(void);

uint32_t wREG_Add;
uint32_t wRAM_Add;	 

uint8_t bARDYTO_Flag = 0;
uint8_t bRWERR_Flag = 0;
uint8_t bSMRST_Flag = 0;
uint8_t bACCDIS_Flag = 0;
uint8_t bDMATC_Flag = 0;
uint8_t bDMAHT_Flag = 0;

/*_____ D E F I N I T I O N S ______________________________________________*/
#ifndef	SN32F788					//Do NOT Remove or Modify!!!
	#error Please install SONiX.SN32F7_DFP.1.5.0.pack or version >= 1.5.0
#endif
#define	PKG						SN32F788				//User SHALL modify the package on demand (SN32F788)

#define	SHOW_PIC_HORIZONTAL			1  	 //show horizontal picture
#define	SHOW_PIC_VERTICAL				0		 //show vertical picture

#define INTERRUPT  1
#define POLLING    0

#define SPIn    1    //0:SPI0,1:SPI1

#define BANK_SEL   BANK0		//BANK0/BANK1/BANK2/BANK3
	
#define PIC_SIZE	 0x257FF					 //Number of data bytes (1 picture)
	
/*_____ M A C R O S ________________________________________________________*/

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

/*****************************************************************************
* Function		: main
* Description	: Add description of this project Here. 
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
int	main(void)
{
	uint32_t wFlashAdd;

	//User can configure System Clock with Configuration Wizard in system_SN32F780.c
	SystemInit();
	SystemCoreClockUpdate();				//Must call for SN32F780, Please do NOT remove!!!

	//Note: User can refer to ClockSwitch sample code to switch various HCLK if needed.

	PFPA_Init();										//User shall set PFPA if used, do NOT remove!!!
	//Use SPI1 PA3

	//1. User SHALL define PKG on demand.
	//2. User SHALL set the status of the GPIO which are NOT pin-out to input pull-up.
	NotPinOut_GPIO_init();

	//--------------------------------------------------------------------------
	//User Code starts HERE!!!

	#if SPIn
		SPI1_Init();  	
		EBI_8080DMA(EBI_DMA_SPI1, BANK_SEL, INTERRUPT);			//setting 8080 DMA to show picture		
	#else
		SPI0_Init();
		EBI_8080DMA(EBI_DMA_SPI0, BANK_SEL, INTERRUPT);			//setting 8080 DMA to show picture		
	#endif

	EBI_8080Init(BANK_SEL, EBI_BK8080MODE_A1D8);

	LCD_Init(SHOW_PIC_HORIZONTAL, SHOW_PIC_VERTICAL, BANK_SEL);		//setting 8080 to init LCD

	while(1)
	{
		#if INTERRUPT
				//**PIC1**//
					wFlashAdd = (uint32_t)INCFILE01;
					Show_Picture(wFlashAdd, PIC_SIZE);
					UT_DelayNms(8000);
				
				//**PIC2**//
					wFlashAdd = (uint32_t)INCFILE02;
					Show_Picture(wFlashAdd, PIC_SIZE);
					UT_DelayNms(8000);		
		#endif
		
		#if POLLING
				//**PIC1**//
					wFlashAdd = (uint32_t)INCFILE01;		
					Show_Picture(wFlashAdd, PIC_SIZE);		
					UT_DelayNms(8000);
				
				//**PIC2**//
					wFlashAdd = (uint32_t)INCFILE02;
					Show_Picture(wFlashAdd, PIC_SIZE);		
					UT_DelayNms(8000);				
		#endif
	}
}

/*****************************************************************************
* Function		: Show_Picture
* Description	: show the picture
* Input			: wFlashAdd:the address of data in SPI flash,  wSize:the size of data in SPI flash
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
void  Show_Picture(uint32_t wFlashAdd, uint32_t wSize)
{	
	EBI_Clear_Intflag();
	
	while(SN_EBI->RIS != 0);
	SN_EBI->DMACNT = wSize;	
	SN_EBI->DMAHTCNT = (wSize/2);

	#if SPIn	
		__SPI1_CLR_SEL0;
		//***read data from SPI Flash***//
		SPI1_TX_DATA(0x03);    //0x03 : read cmd
		SPI1_TX_DATA((wFlashAdd&0xFF0000)>>16);			//read add
		SPI1_TX_DATA((wFlashAdd&0xFF00)>>8);				//read add
		SPI1_TX_DATA((wFlashAdd&0xFF)>>0);					//read add	
	#else
		__SPI0_CLR_SEL0;
		//***read data from SPI Flash***//
		SPI0_TX_DATA(0x03);    //0x03 : read cmd
		SPI0_TX_DATA((wFlashAdd&0xFF0000)>>16);			//read add
		SPI0_TX_DATA((wFlashAdd&0xFF00)>>8);				//read add
		SPI0_TX_DATA((wFlashAdd&0xFF)>>0);					//read add		
	#endif
	
	SN_EBI->DMACTRL_b.DMAEN = 1;					

	#if POLLING
		while(SN_EBI->RIS_b.DMAHTIF == 0);
		while(SN_EBI->RIS_b.DMATCIF == 0);	
		EBI_Clear_Intflag();
	#endif
	
	#if INTERRUPT
		while(bDMATC_Flag == 0);
		bDMAHT_Flag = 0;
		bDMATC_Flag = 0;
	#endif

	#if SPIn	
		__SPI1_SET_SEL0;
	#else
		__SPI0_SET_SEL0;
	#endif
}

/*****************************************************************************
* Function		: NotPinOut_GPIO_init
* Description	: Set the status of the GPIO which are NOT pin-out to input pull-up. 
* Input				: None
* Output			: None
* Return			: None
* Note				: 1. User SHALL define PKG on demand.
*****************************************************************************/
void NotPinOut_GPIO_init(void)
{
#if (PKG == SN32F798) || (PKG == SN32F788)
	//set P0.8~P0.9, P0.16~P0.19 to input pull-up
	SN_GPIO0->CFG = 0xAAA0AAAA;
	SN_GPIO0->CFG1 = 0x0;
	//set P1.16~P1.19 to input pull-up
	SN_GPIO1->CFG1 = 0x0;
	//set P3.14~P3.19 to input pull-up
	SN_GPIO3->CFG = 0x0AAAAAAA;
	SN_GPIO3->CFG1 = 0x0;
#elif (PKG == SN32F797)
	//set P0.6~P0.9, P0.12~P0.19 to input pull-up
	SN_GPIO0->CFG = 0x00A00AAA;
	SN_GPIO0->CFG1 = 0x0;
	//set P1.6~P1.7, P1.10~P1.11, P1.16~P1.19 to input pull-up
	SN_GPIO1->CFG = 0xAA0A0AAA;
	SN_GPIO1->CFG1 = 0x0;
	//set P2.10~P2.15 to input pull-up
	SN_GPIO2->CFG = 0x000AAAAA;
	//set P3.14~P3.19 to input pull-up
	SN_GPIO3->CFG = 0x0AAAAAAA;
	SN_GPIO3->CFG1 = 0x0;
#elif (PKG == SN32F795)
	//set P0.0~0.P0.2, P0.4, P0.6~P0.9, P0.12~P0.19 to input pull-up
	SN_GPIO0->CFG = 0x00A00880;
	SN_GPIO0->CFG1 = 0x0;
	//set P1.4~P1.7, P1.9~P1.19 to input pull-up
	SN_GPIO1->CFG = 0x000200AA;
	SN_GPIO1->CFG1 = 0x0;
	//set P2.10~P2.15 to input pull-up
	SN_GPIO2->CFG = 0x000AAAAA;
	//set P3.8~P3.11, P3.14~P3.19 to input pull-up
	SN_GPIO3->CFG = 0x0A00AAAA;
	SN_GPIO3->CFG1 = 0x0;
#endif
}

/*****************************************************************************
* Function		: HardFault_Handler
* Description	: ISR of Hard fault interrupt
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
__irq void HardFault_Handler(void)
{
	NVIC_SystemReset();
}
