#include <stdarg.h>
#include "yc11xx.h"
#include "yc11xx_uart.h"
#include "yc11xx_gpio.h"
#include "SCI7816.h"
#define LOW  0
#define HIGH 1
#define SCI7816_DETECT_GPIO	23
#define PULLUP 0
#define PULLDOWN 1


void LED_Run(void)
{
	static uint32_t times = 0;
	times++;

	if(times>0x50000)
	{
		times = 0;
		GPIO_CONFIG(2) = (GPIO_CONFIG(2)==GPCFG_OUTPUT_HIGH)? GPCFG_OUTPUT_LOW: GPCFG_OUTPUT_HIGH;
	} 
}
#define IO_TXA  8
#define IO_RXA  9
#define IO_TXB  6
#define IO_RXB  14
#define UARTBUFSIZE 128

void UartxInit(USART_TypeDef UARTx)
{
	USART_InitTypeDef USART_InitStruct ;
	
	USART_InitStruct.USART_BaudRate = UARTE_BAUDRATE_BAUDRATE_Baud115200;
	USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStruct.USART_WordLength = USART_WordLength_8b;	
	USART_InitStruct.USART_StopBits = USART_StopBits_1;
	USART_InitStruct.USART_Mode =USART_Mode_duplex;
	USART_InitStruct.USART_Parity = USART_Parity_Even ;
	USART_InitStruct.USART_RXLen = UARTBUFSIZE;
	USART_InitStruct.USART_TXLen = UARTBUFSIZE;

	if(UARTA == UARTx){
	GPIO_SetGpioMultFunction(IO_TXA,GPCFG_UART_TXD);
	GPIO_SetGpioMultFunction(IO_RXA,GPCFG_UART_RXD);
	}else if (UARTB == UARTx){
	GPIO_SetGpioMultFunction(IO_TXB,GPCFG_UARTB_TXD);
	GPIO_SetGpioMultFunction(IO_RXB,GPCFG_UARTB_RXD);
	}

	USART_Init(UARTx,&USART_InitStruct);
}
void debug_print(uint32_t data)
{
	 USART_SendData(UARTB,(data>>24) & 0xff);
	 USART_SendData(UARTB,(data>>16) & 0xff);
	 USART_SendData(UARTB,(data>>8 ) & 0xff);
	 USART_SendData(UARTB,(data>>0 ) & 0xff);
}

void print_buf(uint8_t *data,int len)
{
	 int i;
	for(i=0;i<len;i++)
	{
		printf("%02x ",data[i]);
	}
	printf("\n");
}

uint8_t sci7816_gpio_detection(uint8_t Gpio)
{
	if(GPIO_GetInputStatus(Gpio) == LOW)
	{
		while(GPIO_GetInputStatus(Gpio) == LOW)
			return LOW;
	}
	else
		return HIGH;
}

void delay_ms(unsigned int time)
{
	unsigned int num,j;
	for (num = 0; num <time; num++)
	{
		for (j = 0; j < 4000; j++);
	}
}

uint8_t card_remove=0;
int main(void)
{
	int i = 0x100000;
	uint8_t UrataReData=0,UratbReData=0;
	uint8_t buf[50]={0};
	uint8_t Rece_Data[50]={0};
	uint8_t pcmd[5]={0x00,0x84,0x00,0x00,0x08};
	GPIO_SetGpioMultFunction(26, GPCFG_OUTPUT_LOW);	
	UartxInit(UARTB); 	//print log 
	//UartxInit(UARTA);
	uint16_t recv_len = 0;
	while(i--);
	printf("This is SCI7816 demo test\r\n");
//	for(int i = 0; i< 256;i++)
//	{
//		USART_SendData(UARTB,i);
//		USART_SendData(UARTA,i);
//	}
	
	GPIO_SetInput(SCI7816_DETECT_GPIO,PULLUP);
	printf("wait card insert\n");
	while(1)
	{
		 if(sci7816_gpio_detection(SCI7816_DETECT_GPIO) == LOW && card_remove==0)
		 {
				card_remove=1;
				printf("card insert\n");
				if(SCI7816_Reset(buf,50,&recv_len)==0)
				{
					printf("SCI7816_Reset ok\n");
					delay_ms(10);
					if(SCI7816_CosOperation(pcmd,5,1,Rece_Data,50,&recv_len)==OK)
					{
						printf("SCI7816_CosOperation ok\n");
					}
					else
					{
						printf("SCI7816_CosOperation error\n");
					}
					printf("recv_len=%d",recv_len);
					printf("receive data:\n");
					print_buf(Rece_Data,recv_len);
				}
				else
				{
					printf("SCI7816_Reset fail\n");
				}
				printf("recv_len=%d\n",recv_len);
				printf("reset data:\n");
				print_buf(buf,recv_len);
				
		 }
		 else
		 {
			 if(card_remove==1 && sci7816_gpio_detection(SCI7816_DETECT_GPIO) == HIGH)
			 {
				 card_remove=0;
				 printf("card remove\n");
				 printf("wait card insert\n");
			 }
		 }
	}
	
}