/******************************************************************************
 * @file     main.c
 * @brief    Main program body
 * @version  V1.8.0
 * @date     31-Dec-2021
 *
 * @note
 * Copyright (C) 2021 Spintrol Limited Corporation. All rights reserved.
 *
 * @attention
 * THIS SOFTWARE JUST PROVIDES CUSTOMERS WITH CODING INFORMATION REGARDING
 * THEIR PRODUCTS, WHICH AIMS AT SAVING TIME FOR THEM. SPINTROL SHALL NOT BE
 * LIABLE FOR THE USE OF THE SOFTWARE. SPINTROL DOES NOT GUARANTEE THE
 * CORRECTNESS OF THIS SOFTWARE AND RESERVES THE RIGHT TO MODIFY THE SOFTWARE
 * WITHOUT NOTIFICATION.
 *
 ******************************************************************************/

#include"spc1168.h"
#include<stdio.h>
#include<stdlib.h>

#define                 Rx_FIFO_INT_TH             0xb                    /* Rx thresgold as 12 entry */
#define                 T_BUFFER_SIZE              128
#define                 I2C_SPEED                  400000
#define                 I2C_Slave_ADDR             0x9                    /* IIC Slve ADDR */

uint32_t                gu32BuffSize                = T_BUFFER_SIZE;
uint8_t                 gau8TxBuf[T_BUFFER_SIZE];
uint8_t                 gau8RxBuf[T_BUFFER_SIZE]; 
ErrorStatus             estatus;
uint32_t                num;

void I2C_MasterSentReadCMD(I2C_REGS* I2Cx, I2C_AddrModeEnum eAddrMode, uint16_t u16TargetAddr, uint32_t u32Count)
{
  uint32_t i;

  /* Set Address Mode */
  I2C->I2CMASTERADDR.bit.MASTERADDR10B = eAddrMode;

  /* Set Target Slave Address*/
  I2C->I2CMASTERADDR.bit.TARADDR = u16TargetAddr;

  for(i = 0; i < u32Count; i++)
  {
		/* the delay time there denpend on the IIC clock, lower clock frequency means larger delay time */
		Delay_Us(110);	
		
    /* Write TxFIFO, Send Read Comamnd */
    I2C_MasterReadCmd(I2Cx);
  }
  
}
 
void Master_Tx_data(void)
{
  int i;
  
  /* Generate random datum to transmit */
  for(i=0; i < gu32BuffSize; i++)
    gau8TxBuf[i] = rand() & 0xFF;

  printf("Master Tx data...\n");
  I2C_MasterWrite(I2C, I2C_ADDR_7BIT, I2C_Slave_ADDR, gau8TxBuf, gu32BuffSize);
	
	I2C_MasterSentReadCMD(I2C, I2C_ADDR_7BIT, I2C_Slave_ADDR, gu32BuffSize);
}


/*************************************************************************************************************************
 *
 * @brief      In this case, the IIC controller is used as master and sent datum to the slave and read these datum back to 
 *             compara, if there is a item not equal with each other, the test FAIL.
 *
 *************************************************************************************************************************/


int main()
{
  FLASH_WALLOW();
  FLASH_SetTiming(200000000);
  /*Disable flash write access after flash operation had done*/
  FLASH_WDIS();

  CLOCK_InitWithRCO(CLOCK_HCLK_200MHZ);

  Delay_Init();

  /*
   * Init the UART
   *
   * 1.Set the GPIO34/35 as UART FUNC
   *
   * 2.Enable the UART CLK
   *
   * 3.Set the rest para
   */
  GPIO_SetPinChannel(GPIO_34,GPIO34_UART_TXD);
  GPIO_SetPinChannel(GPIO_35,GPIO35_UART_RXD);
  CLOCK_EnableModule(UART_MODULE);
  UART_Init(UART,38400);

  /* Config GPIO38~39 as I2C function */
  GPIO_SetPinChannel(GPIO_38, GPIO38_I2C_SDA);
  GPIO_SetPinChannel(GPIO_39, GPIO39_I2C_SCL);

  /*
   * Set Output Strength as 20mA, in case of many more
   * slaves are linking to the master.
   */
  GPIO_SetOutStrength(GPIO_38, GPIO_OUT_STRENGTH_20MA);
  GPIO_SetOutStrength(GPIO_39, GPIO_OUT_STRENGTH_20MA);

  /* Set Rx FIFO threshold as 12 entry */
  I2C_SetRxFIFOThreshold(I2C, Rx_FIFO_INT_TH);

  /* Disable All INT */
  I2C_DisableAllInt(I2C);

  /* Enable the INT of Rx reached threshold */
  I2C_EnableRxDataAvailableInt(I2C);

  /* Clear All INT */
  I2C_ClearInt(I2C, I2C_INT_ALL);

  /* Init I2C as Master */
  estatus = I2C_MasterInit(I2C, I2C_SPEED);
  if(estatus == ERROR)
  {
    printf("[IIC master init FAIL] I2C clock is not fast enough to support the speed\n");
    return 0;
  }

  /* Enable CM4 INT request */
  NVIC_EnableIRQ(I2C_IRQn);

  /* Master start to transmit and recieve data */
  Master_Tx_data();

  while(1)
  {

  }
}

void I2C_IRQHandler(void)
{
	while(!I2C_IsRxNotEmpty(I2C)) { }

	/* Read RxFIFO, Read Data */
	gau8RxBuf[num] = I2C_ReadByte(I2C);
	printf("data is %d\n",gau8RxBuf[num]);
	num++;
	
	/* Wait I2C Bus Idle */
	while(I2C_IsActivity(I2C)) { }
}
