/******************** (C) COPYRIGHT 2022 SONiX *******************************
* COMPANY:	SONiX
* DATE:		2022/01/06
* IC:			SN8F5762
*____________________________________________________________________________
* REVISION		Date				Author		Description
* 1.0					2022/01/06	SONiX			1. First release
*____________________________________________________________________________
*****************************************************************************/
/*_____ I N C L U D E S _________________________________*/

#include <SN8F5762.h>
#include "i2c.h"

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

/*_____ D E F I N I T I O N S ___________________________*/
#define I2C_PIN_SEL                 0

#define	I2C_SLAVE_ADDRESS					   	0x60
#define	I2C_MASTER_ADDRESS						0x20


/*_____ D E C L A R A T I O N S _________________________*/
uint8_t	bI2C_TxDataBuf[10]={0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9};
uint8_t	bI2C_RxDataBuf[10];
uint8_t bMN_DataBuf[10];

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


/**
 *	@Brief		main: ADC Test Demo Function
 *	@param		None
 *	@retval		None
 */

void main(void)
{
	/*	Initialize GPIO status	*/
	GPIO_Initialization();															
	
	/*	Initializes and configures the I2C function	*/
	I2C_ConfigureRegister(I2C_PIN_SEL, mskI2CCON_I2C_CLOCKDIV_80, I2C_SLAVE_ADDRESS, I2C_MASTER_ADDRESS);

	/*	Enable global interrupt	*/
	EAL = 1;
	
	/*	Initializes data buf and start to I2C transmittion	*/
  I2C_InitSlaveTX(I2C_TX_SEL, bI2C_TxDataBuf, sizeof(bI2C_TxDataBuf));
	
		/*	Initializes data buf and start to I2C transmittion	*/
	I2C_InitSlaveRX(I2C_RX_SEL, bI2C_RxDataBuf, sizeof(bI2C_RxDataBuf));
  
	while(1)
  {
    /*  Check status for new data transmission  and repeat data transmission  */
    if(I2C_CheckReciDataDone())
    {
      bMN_DataBuf[10] = bI2C_RxDataBuf[10];
      /*  get new data  */
      I2C_RepeatSlave();
    }
    else
    {
      /*  Check incomplete tranmission status */
      if(I2C_CheckReciDataStop())
      {
        I2C_RepeatSlave();
      }
    }
		
		if(I2C_CheckTransDataDone())
    {
      I2C_RepeatSlave();
    }
    else
    {
      /*  Check incomplete tranmission status */
      if(I2C_CheckTransDataStop())
      {
         I2C_RepeatSlave();
      }
		}
  }

}