/******************** (C) COPYRIGHT 2022 SONiX *******************************
* COMPANY:		SONiX
* DATE:				2022/11
* AUTHOR:			SA1
* IC:					SN32F700B
*____________________________________________________________________________
*	REVISION	Date				User		Description
*	1.0				2015/08/25	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.4				2017/07/14	SA1			1. Update system_SN32F700B.c to V1.0.3
*																2. Fix __FLASH_LPM_PLL_MODE in Flash.h
*	2.0				2018/01/26	SA1			1. Add NotPinOut_GPIO_init to set the status of the GPIO which are NOT pin-out to input pull-up.
*																2. Add CodeOption_SN32F700B.s to modify Code option, please modify with Configuration Wizard, 
*																	 and Strongly recommend to keep CS0 for debugging with SN-LINK.
*	2.2				2018/10/05	SA1			1. Remove CodeOption_SN32F700B.s, please contact SONiX FAE if needed.
*																2. Fix mskI2C_GCEN_DIS, mskI2C_GCEN_EN, mskI2C_ADD_MODE_7BIT, mskI2C_ADD_MODE_10BIT in I2C.h
*	2.7				2019/12/31	SA1			1. Update system_SN32F700B.c to V1.1.0
*	3.0				2022/11/11	SA1			1. Update system_SN32F700B.c to V2.0.0
*																2. Update Flash.h
*
*____________________________________________________________________________
* 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 <SN32F700B.h>
#include <SN32F700_Def.h>
#include "Interface\I2C\I2C.h"


/*_____ D E C L A R A T I O N S ____________________________________________*/
void	NotPinOut_GPIO_init(void);

void	I2C_TestFun(void); 

uint32_t wI2C_TestCase=0;
uint8_t bI2C_MasEndFlag = 0;
uint8_t bI2C_SlaEndFlag = 0;
uint8_t bI2C_MasCheckFIFO[30];
uint8_t bI2C_SlaCheckFIFO[30];


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


//I2C Master use
#define		I2C_SEND_4BYTE			4				//Set the Number of sending Data
#define		I2C_SEND_9BYTE			9				//Set the Number of sending Data

#define		I2C_NO_REPEAT				0				//No use repeat
#define		I2C_REPEAT_START		1				//Next transfer with a Repated Start condition
#define		I2C_NEXT_START			2				//Work next transfer

#define		I2C_MAS_ACK_1BYTE		0				//Send (wReTxNum + 1) Data when wRepeatTX is 1.
#define		I2C_MAS_ACK_2BYTE		1
#define		I2C_MAS_ACK_3BYTE		2
#define		I2C_MAS_ACK_4BYTE		3
#define		I2C_MAS_ACK_5BYTE		4

#define		I2C_MAS_TX_ADDR			0x07		//Master Tx addr

//I2C Slave use
#define		I2C_4BYTE_NACK			4			//Return NACK when getting the number of data


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


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

/*****************************************************************************
* Function		: main
* Description	: Demo code of I2C
* Input			: None
* Output		: None
* Return		: None
* Note			: Connect I2C0 and I2C1
*****************************************************************************/
int	main (void)
{
	//User can configure System Clock with Configuration Wizard in system_SN32F700B.c
	SystemInit();
	SystemCoreClockUpdate();				//Must call for SN32F700B, Please do NOT remove!!!

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

	//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!!!

	//System Interface Init
	I2C0_Init();

	//Set the address sent by Master
	hwI2C_Device_Addr_I2C0 = I2C_MAS_TX_ADDR;

	//I2C data
	bI2C_MasTxData[0] = 0x055;
	bI2C_MasTxData[1] = 0x0A5;
	bI2C_MasTxData[2] = 0x077;
	bI2C_MasTxData[3] = 0x0FF;
	bI2C_MasTxData[4] = 0x001;

	bI2C_MasTxData[5] = 0x099;
	bI2C_MasTxData[6] = 0x011;
	bI2C_MasTxData[7] = 0x0B0;
	bI2C_MasTxData[8] = 0x00F;
	bI2C_MasTxData[9] = 0x0E3;  

	I2C_TestFun();

	//All Pass
	while(1);												  
}


/*****************************************************************************
* Function		: I2C_TestFun
* Description	:
				  I2C0 is Master.
					 
				  Case 1(wI2C_TestCase = 0): Master sends 4 data
				  Case 2(wI2C_TestCase = 1): Master sends 4 data ,and then works Repeat Start to send 4 data
				  Case 3(wI2C_TestCase = 2): Master sends 4 data over ,and then sends another 4 data.
				  Case 4(wI2C_TestCase = 3): Master gets 9 data.		
				  Case 5(wI2C_TestCase = 4): Master gets 4 data ,and then works Repeat Start to get 5 data
				  Case 6(wI2C_TestCase = 5): Master gets 4 data over ,and then gets another 5 data.

				  When transmission is over , Check that the data of Master and Slave is right.

* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
void I2C_TestFun(void)
{
	uint32_t i=0;
	uint32_t wcheckDataPinter=0;
	uint32_t wcheckDataPinter1=0;

	for( wI2C_TestCase = 0; wI2C_TestCase< 6; wI2C_TestCase++)
	{
		SN_I2C0->CTRL |= (0x01 << 5);	//Start

		while (bI2C_MasEndFlag == 0);
		bI2C_MasEndFlag = 0;
		while (bI2C_SlaEndFlag == 0);
		bI2C_SlaEndFlag = 0;
		bI2C_SlaRxPointer = 0;
		bI2C_SlaTxPointer = 0;

		if(wI2C_TotalError != 0x00)
		{
			while(1);					//Happen Error
		}

		if(wI2C_TestCase >= 0x03)
		{
			for( i = 0; i< 9; i++)
			{
				bI2C_MasCheckFIFO[wcheckDataPinter] = bI2C_MasRxData[i];
				wcheckDataPinter=wcheckDataPinter+1;
			}
		}
		else if(wI2C_TestCase == 0x00)
		{
			for( i = 0; i< 4; i++)
			{
				bI2C_SlaCheckFIFO[wcheckDataPinter1] = bI2C_SlaRxData[i];
				wcheckDataPinter1=wcheckDataPinter1+1;
			}
		}
		else	
		{
			//wI2C_TestCase = 1 & 2
			for( i = 0; i< 8; i++)
			{
				bI2C_SlaCheckFIFO[wcheckDataPinter1] = bI2C_SlaRxData[i];
				wcheckDataPinter1=wcheckDataPinter1+1;
			}
		}

		//Dealy 	
		for( i = 0; i< 1000; i++);
	}

	//Check I2C0 Master Getting Data
	wI2C_TestCase = 0;
	for( i = 0; i< 27; i++)
	{
		if(bI2C_MasCheckFIFO[i] != bI2C_MasTxData[wI2C_TestCase++])
		{
			while(1);
		}

		if(wI2C_TestCase == 9)
		{
			wI2C_TestCase = 0;
		}
	}

	//check I2C1 Slave Getting Data
	wI2C_TestCase = 0;
	for( i = 0; i< 4; i++)
	{
		if(bI2C_SlaCheckFIFO[i] != bI2C_MasTxData[wI2C_TestCase++])
		{
			while(1);
		}
	}

	wI2C_TestCase = 0;
	for( i = 4; i< 20; i++)
	{
		if(bI2C_SlaCheckFIFO[i] != bI2C_MasTxData[wI2C_TestCase++])
		{
			while(1);
		}

		if(wI2C_TestCase == 8)
		{
			wI2C_TestCase = 0;
		}
	}
}

/*****************************************************************************
* Function		: I2C0_IRQHandler
* Description	: ISR of I2C0 interrupt
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
__irq void I2C0_IRQHandler(void)
{
	if(wI2C_TestCase == 0)
	{
		I2C0_Master_Tx(&bI2C_MasTxData[bI2C_MasTxPointer], &bI2C_MasTxPointer, &bI2C_MasEndFlag, hwI2C_Device_Addr_I2C0, I2C_SEND_4BYTE, I2C_NO_REPEAT, I2C_MAS_ACK_1BYTE);
	}
	else if(wI2C_TestCase == 1)
	{
	 	I2C0_Master_Tx(&bI2C_MasTxData[bI2C_MasTxPointer], &bI2C_MasTxPointer, &bI2C_MasEndFlag, hwI2C_Device_Addr_I2C0, I2C_SEND_4BYTE, I2C_REPEAT_START, I2C_MAS_ACK_4BYTE);
	}
	else if(wI2C_TestCase == 2)
	{
		I2C0_Master_Tx(&bI2C_MasTxData[bI2C_MasTxPointer], &bI2C_MasTxPointer, &bI2C_MasEndFlag, hwI2C_Device_Addr_I2C0, I2C_SEND_4BYTE, I2C_NEXT_START, I2C_MAS_ACK_4BYTE);
	}
	else if(wI2C_TestCase == 3)
	{
		I2C0_Master_Rx(&bI2C_MasRxData[bI2C_MasRxPointer], &bI2C_MasRxPointer, &bI2C_MasEndFlag, hwI2C_Device_Addr_I2C0, I2C_SEND_9BYTE, I2C_NO_REPEAT, I2C_MAS_ACK_1BYTE);		
	}
	else if(wI2C_TestCase == 4)
	{
		I2C0_Master_Rx(&bI2C_MasRxData[bI2C_MasRxPointer], &bI2C_MasRxPointer, &bI2C_MasEndFlag, hwI2C_Device_Addr_I2C0, I2C_SEND_4BYTE, I2C_REPEAT_START, I2C_MAS_ACK_5BYTE);
	}
	else if(wI2C_TestCase == 5)
	{
		I2C0_Master_Rx(&bI2C_MasRxData[bI2C_MasRxPointer], &bI2C_MasRxPointer, &bI2C_MasEndFlag, hwI2C_Device_Addr_I2C0, I2C_SEND_4BYTE, I2C_NEXT_START, I2C_MAS_ACK_5BYTE);
	}
}

/*****************************************************************************
* 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 == SN32F706B)
	//set P0.0~P0.1 to input pull-up
	SN_GPIO0->CFG = 0xAAAAA0;
#elif (PKG == SN32F705B)
	//set P0.0~P0.1 to input pull-up
	SN_GPIO0->CFG = 0x00AAAAA0;
	//set P1.5, P1.8~P1.11 to input pull-up
	SN_GPIO1->CFG = 0x0000A2AA;
	//set P2.5~P2.9 to input pull-up
	SN_GPIO2->CFG = 0x000002AA;
	//set P3.0~P3.3 to input pull-up
	SN_GPIO3->CFG = 0x000AAA00;
#elif (PKG == SN32F7051B)
	//set P0.6~P0.7 to input pull-up
	SN_GPIO0->CFG = 0x00AA0AAA;
	//set P1.5, P1.8~P1.11 to input pull-up
	SN_GPIO1->CFG = 0x0000A2AA;
	//set P2.8~P2.9 to input pull-up
	SN_GPIO2->CFG = 0x0000AAAA;
	//set P3.0~P3.5 to input pull-up
	SN_GPIO3->CFG = 0x000AA000;
#elif (PKG == SN32F704B)
	//set P0.0~P0.1, P0.3 to input pull-up
	SN_GPIO0->CFG = 0x00AAAA20;
	//set P1.5, P1.8~P1.11 to input pull-up
	SN_GPIO1->CFG = 0x0000A2AA;
	//set P2.2~P2.9 to input pull-up
	SN_GPIO2->CFG = 0x0000000A;
	//set P3.0~P3.3 to input pull-up
	SN_GPIO3->CFG = 0x000AAA00;
#elif (PKG == SN32F702B)
	//set P0.3~P0.9 to input pull-up
	SN_GPIO0->CFG = 0x00A0002A;
	//set P1.5, P1.8, P1.10~P1.11 to input pull-up
	SN_GPIO1->CFG = 0x0008A2AA;
	//set P2.3~P2.9 to input pull-up
	SN_GPIO2->CFG = 0x0000002A;
	//set P3.0~P3.5, P3.7~P3.9 to input pull-up
	SN_GPIO3->CFG = 0x00002000;
#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();
}

