/*
*************************************************************
 Copyright (C), 2016-2020 smartlinkMicro. Co., Ltd.  All rights reserved.
 Filename:
 Complier:    ARM-MDK 5.xx
 Target OS:   none
 Target HW:
 Author:        htyi
 Version :      V1.0.0
 Date:
 Description:
 History:
     <author>     <time>     <version >   <desc>

*************************************************************
*/
#include "emv_if.h"
#include "sl2x23.h"
#include "Rfid_interface.h"
#include "lpcd.h"

extern uint8_t  card_A;
extern uint8_t  card_B;
extern uint8_t  card_F;
extern uint8_t  card_V;
extern uint8_t  sak;
extern uint16_t atqa;
int8_t nfc_demo(void) {
    volatile uint8_t card_det = 0, g_ret, demo_step = 0;
    while(1) {
        switch(demo_step) {
            case 0://询卡
                card_A = 0;
                card_B = 0;
                card_F = 0;
                card_V = 0;
                g_ask_block_flag = 0;
                #ifdef SPD_MODE
                pcd_sleep_exit(); //Soft power on
                #endif
                pcd_set_rate(1);
                g_ret = pcd_polling(g_tag_info.serial_num);
                if(g_ret == 0) demo_step = 1;
                break;
            case 1:
                if(card_A) {
                    if((sak == 0x08) || (sak == 0x18)) { //MIFARE
                        NFC_A_Mifare_Test(g_tag_info.serial_num);
                        demo_step = 0;
                        card_det  = 1;
                        break;
                    }
                    if(atqa == 0x000c) {
                        NFC_A_Tag1_Test(g_tag_info.serial_num);
                        demo_step = 0;
                        card_det  = 1;
                        break;
                    }
                    if(atqa == 0x4400) {
                        NFC_A_Tag2_Test(g_tag_info.serial_num);
                        demo_step = 0;
                        card_det  = 1;
                        break;
                    }
                }
                g_ret = pcd_active(1);
                if(g_ret == 0) {
                    demo_step = 2;
                } else {
                    demo_step = 0;
                }
                break;
            case 2:
                if(card_A) {
                    Iblock_getrand(1);
                }
                if(card_B) {
                    g_ask_block_flag = 1;
                    g_ret = get_idcard_num(g_tag_info.serial_num);
                    if(g_ret == 0) {
                        g_tag_info.uid_length = 10;
                        PRINT_BUF("ID_NUM: ", g_tag_info.serial_num, g_tag_info.uid_length);
                        printf("## test end##\r\n\r\n");
                    } else {
                        Iblock_getrand(1);
                    }
                }
                if(card_F) {
                    NFC_F_Tag3_Test(g_tag_info.serial_num);
                }
                if(card_V) {
                    NFC_V_Tag5_Test(g_tag_info.serial_num);
                }
                demo_step = 0;
                card_det  = 1;
                break;
            default:
                demo_step = 0;
                card_det  = 0;
                break;
        }
        if(demo_step == 0) {
            pcd_antenna_off();
            delay_1ms(1);
            #ifdef SPD_MODE
            pcd_sleep();      //Soft power off
            #endif
            break;
        }
    }
    return card_det;
}

void lpcd_demo(void) {
    uint8_t adc_value;
    uint32_t wait_cnt = 0;
    irq_flag_io = 0;
    lpcd_cfg.t1 = LPCD_DETECT_PERIOD;      //0x12 300ms; //0x20 500ms ; //0x38 800ms
    lpcd_cfg.sense = LPCD_DETECT_SENS;
    printf("******* start lpcd_demo() *******\r\n");
    osc_calib();
    lpcd_init(1, 0x40); //fst_en
    lpcd_entry();
    while(1) {
        #ifdef NOT_IRQ
        if(!(INT_PIN == 0))
        #else
        if(irq_flag_io)
        #endif
        {
            printf("...................................\r\n");
            printf("\r\n******* l、pcd_exit() *********\r\n");
            irq_flag_io = 0;
            adc_value = lpcd_exit();
            Rfid_Init();
            printf("******* 2、nfc_demo() *********\r\n");
            lpcd_init(nfc_demo(), adc_value);
            pcd_antenna_off();
            printf("******* 3、lpcd_entry() *******\r\n");
            lpcd_entry();
        } else {
            wait_cnt++;
            if(wait_cnt == 500000) {
                wait_cnt = 0;
                printf(".");
            }
        }
    }
}


void sl2823_test(void) {
    printf("\r\nsl2x23_complete_example_V1.0\r\n");
    delay_1ms(2);
    spi_wr_test();
    Rfid_Init();
    pcd_default_info();
    irq_flag_io = 0;
    #ifdef LPCD_MODE
    lpcd_demo();
    #else
    while(1) {
        nfc_demo();
        delay_1ms(500);
        printf(".");
    }
    #endif
}

#ifndef USE_STDPERIPH_DRIVER
int main() {
    sl2823_test();
}
#endif
