/****************************************************************************
 *   $Id:: i2c_example.c                                                   $
 *   Project: NXP QN9020 I2C example
 *
 *   Description:
 *     This file contains I2C driver usage.
 *
****************************************************************************/

#include "i2c.h"
#include "system.h"

#include "gpio.h"
#include "Hal_Reg.h"
#include "MFRC522.h"
#include "typea.h"
#include "Mifare.h"
#include "RC522_API.h"
#include "uart.h"
#define NFC_ID_STING_LENGTH 13

uint8_t test1=0;


/****************************************************************************
*               I2C driver code example
*I2C driver has the master and slave mode.
*The master mode function is as below:
*@I2C_BYTE_WRITE: register address is 8 bits
*@I2C_BYTE_WRITE2: register address is 16 bits
*@I2C_nBYTE_WRITE: write n bytes data
*the read function is similar
*
*The slave mode function is as below:
*The read/write is done in the interrupt handler:I2C_IRQHandler
*
*Note: the Device Address is 7-bit without read/write bit.
*
****************************************************************************/


uint8_t i2cbuffer[104];

char status=0;

/* Main Program */
#if I2C_MODE == I2C_MASTER
int main (void)
{
	uint32_t i;
    uint32_t j;
    uint8_t buffer[100];
    uint8_t rxbuffer[100];
	
  uint32_t led_ct = 0;
	uint8_t snr[150];
	char read_id[NFC_ID_STING_LENGTH]={0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0};
	//char status=0;
	char Green_Status=0,Red_Status=0;

	unsigned char atqa[2] = {0x0,0x0};
	unsigned char sak = 0;	
	unsigned char din[30];
	unsigned char dout[30];

	unsigned char key[6] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
    
  SystemInit();
    
  //Initialize I2C master
  //  i2c_init(I2C_SCL_RATIO(100000), i2cbuffer, 104);
	
  i2c_init(I2C_SCL_RATIO(400000), i2cbuffer, 104); // wenxue I2C baud 400kbps

  // init MCU control pins: RST and IRQ,LED1
	mcu_pin_init();
	
	
  uart_init(QN_UART0, __USART_CLK, UART_115200);
	uart_tx_enable(QN_UART0, MASK_ENABLE);
  uart_rx_enable(QN_UART0, MASK_ENABLE);
	
	uart_printf(QN_UART0, (uint8_t *)"Hello NXP!\n");
		
 while(1)	 
 {
	 
   // *********** Config **********************
   i2c_init(I2C_SCL_RATIO(400000), i2cbuffer, 104);
	 // reset the chip
	 PcdReset();
	
	 // config chip for TypeA@106kbps
	 PcdConfig();		
	 
	// ********** Mifare operation *************
	// activate card
	if(!status)	
		status = PiccActiveCard(atqa, snr, &sak);
	
  if(status)	 // status!=0
		uart_printf(QN_UART0, (uint8_t *)"Status!=0\n");	
	
		// authenticate with Sector0 
	if(!status)	
			status = PiccAuthKey(0x60,snr, key, 0x0);


		// read block 0
		if(!status)	
			status = PiccRead(0x1, dout); 

		// write 0xAA t0 block 1
		if(!status)	
			memset(din, 0xAA, 16);
		
		if(!status)	
			status = PiccWrite(0x01, din);
		// read block 1
		if(!status)
			status = PiccRead(0x01, dout);

			if(!status)
		  status = read_NFC_Card(read_id);	
		
		
	  if(!status)
		{
		
			gpio_write_pin(GPIO_P05, GPIO_LOW);
		}
		else
			gpio_write_pin(GPIO_P05, GPIO_HIGH);
       			
	 status=0;	
   uart_printf(QN_UART0, (uint8_t *)"Come here!\n");
	 for( i=0;i<5000;i++);
		 for( i=0;i<5000;i++);		
	}
	
}
#else // I2C_SLAVE
int main (void)
{

    SystemInit();

    /*
     * Initialize I2C slave.
     * This i2c slave address is QN9020_I2C_ADDR.
     * I2c master can read n byte data from this slave, and write n byte data to this slave.
     */
    i2c_init(i2cbuffer, 104);

    while (1)                                /* Loop forever */
    {
    
    }
}
#endif /* I2C_MODE == I2C_MASTER */

