/******************** (C) COPYRIGHT 2020 SONiX *******************************
* COMPANY:			SONiX
* DATE:					2020/06
* AUTHOR:				SA1
* IC:						SN32F780/790
* DESCRIPTION:	EBI related functions.
*____________________________________________________________________________
*	REVISION	Date				User	Description
*	1.0				2020/06/24	SA1		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 <SN32F780.h>
#include "EBI.h"
#include "..\..\Utility\Utility.h"

/*_____ D E C L A R A T I O N S ____________________________________________*/
uint8_t ARDYTO_Flag = 0;
uint8_t RWERR_Flag = 0;
uint8_t SMRST_Flag = 0;
uint8_t	ACCDIS_Flag = 0;

/*_____ 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		: EBI_Init
* Description	: None
* Input				: None
* Output			: None
* Return			: None
* Note				: None
*****************************************************************************/
void EBI_Init(uint8_t bank)
{
	SN_SYS1->AHBCLKEN = EBI_CLK_EN;	 //enable EBI_PCLK 
	
	EBI_Clear_Intflag();
	EBI_Interrupt_Enable();
	
	switch (bank)  //select BANK 
	{
		case BANK0:
					SN_EBI->CTRL |= (mskEBI_BANK0_EN |
													 mskEBI_BANK0_AND16 |
													 mskEBI_ARDY0_EN |
													 mskEBI_IDLE15T);
		
					SN_EBI->ALCTRL_b.AL0 = EBI_ADD25PIN;				
					SN_EBI->PR0 |= mskEBI_ARDY_ACTIVEHIGH;
		
					wBank_add = EBI_BANK0_ADD;
		break;		
		
		case BANK1:
					SN_EBI->CTRL |= (mskEBI_BANK1_EN |
													 mskEBI_BANK1_AND16 |
													 mskEBI_ARDY1_EN |
													 mskEBI_IDLE15T);
		
					SN_EBI->ALCTRL_b.AL1 = EBI_ADD25PIN;	
					SN_EBI->PR1 |= mskEBI_ARDY_ACTIVEHIGH;
		
					wBank_add = EBI_BANK1_ADD;
		break;
		
		case BANK2:
					SN_EBI->CTRL |= (mskEBI_BANK2_EN |
													 mskEBI_BANK2_AND16 |
													 mskEBI_ARDY2_EN |
													 mskEBI_IDLE15T);
		
					SN_EBI->ALCTRL_b.AL2 = EBI_ADD25PIN;			
					SN_EBI->PR2 |= mskEBI_ARDY_ACTIVEHIGH;
		
					wBank_add = EBI_BANK2_ADD;
		break;
		
		case BANK3:
					SN_EBI->CTRL |= (mskEBI_BANK3_EN |
													 mskEBI_BANK3_AND16 |
													 mskEBI_ARDY3_EN |
													 mskEBI_IDLE15T);
		
					SN_EBI->ALCTRL_b.AL3 = EBI_ADD25PIN;			
					SN_EBI->PR3 |= mskEBI_ARDY_ACTIVEHIGH;
		
					wBank_add = EBI_BANK3_ADD;	
		break;					
		
		default:
		break;
	}			
}

/*****************************************************************************
* Function		: EBI_Clear_Intflag
* Input				: None
* Output			: None
* Return			: None
* Note				: None
*****************************************************************************/
void EBI_Clear_Intflag(void)
{
	SN_EBI->IC |= (mskEBI_RIS_ARDYTOIC |
								 mskEBI_RIS_ACCDISIC |
								 mskEBI_RIS_SMRSTIC |
								 mskEBI_RIS_RWERRIC |
								 mskEBI_RIS_DMAHTIC |
								 mskEBI_RIS_DMATCIC);
}

/*****************************************************************************
* Function		: EBI_Interrupt_Enable
* Input				: None
* Output			: None
* Return			: None
* Note				: None
*****************************************************************************/
void EBI_Interrupt_Enable(void)
{
	NVIC_ClearPendingIRQ(EBI_IRQn);
	NVIC_EnableIRQ(EBI_IRQn);
	
	SN_EBI->IE |= (mskEBI_RWERREN_EN |
								 mskEBI_SMRSTEN_EN |
								 mskEBI_ACCDISEN_EN |
								 mskEBI_ARDYTOEN_EN);
}

/*****************************************************************************
* Function		: EBI_Interrupt_Disable
* Input				: None
* Output			: None
* Return			: None
* Note				: None
*****************************************************************************/
void EBI_Interrupt_Disable(void)
{
	NVIC_ClearPendingIRQ(EBI_IRQn);
	NVIC_DisableIRQ(EBI_IRQn);
	
	SN_EBI->IE |= (mskEBI_RWERREN_DIS |
								 mskEBI_SMRSTEN_DIS |
								 mskEBI_ACCDISEN_DIS |
								 mskEBI_ARDYTOEN_DIS);
}

/*****************************************************************************
* Function		: EBI_Write_16bDATA
* Input				: None
* Output			: None
* Return			: None
* Note				: None
*****************************************************************************/
void EBI_Write_16bDATA(uint32_t add, uint16_t data)
{
	*(uint16_t *)(add)= data; 
}

/*****************************************************************************
* Function		: EBI_Read_16bDATA
* Input				: None
* Output			: None
* Return			: (*(uint16_t *)(add))
* Note				: None
*****************************************************************************/
uint16_t EBI_Read_16bDATA(uint32_t add)
{
	return (*(uint16_t *)(add));
}

/*********************************************************************************************************
* Function Name: EBI_IRQHandler
* Description: EBI interrupt service routine
* Input: None
* Output: None
* Return: None
*********************************************************************************************************/
__irq	void EBI_IRQHandler (void)
{
	uint16_t hwRIS_buf;
	hwRIS_buf = SN_EBI->RIS;
	if ((hwRIS_buf & mskEBI_RIS_RWERRIF) == EBI_RIS_RWERRIF) 					//RWERRIF
	{
		SN_EBI->IC_b.RWERRIC = 1;
		RWERR_Flag = 1;
	}
	else if ((hwRIS_buf & mskEBI_RIS_ARDYTOIF) == EBI_RIS_ARDYTOIF) 	//ARDYTOIF
	{
		SN_EBI->IC_b.ARDYTOIC = 1;
		ARDYTO_Flag = 1;
	}
	else if ((hwRIS_buf & mskEBI_RIS_SMRSTIF) == EBI_RIS_SMRSTIF) 		//SMRSTIF
	{
		SN_EBI->IC_b.SMRSTIC = 1;
		SMRST_Flag = 1;
	}
	else if ((hwRIS_buf & mskEBI_RIS_ACCDISIF) == EBI_RIS_ACCDISIF) 	//ACCDISIF
	{
		SN_EBI->IC_b.ACCDISIC = 1;
		ACCDIS_Flag = 1;
	}
}
