搜索
查看: 1774|回复: 6

[已解决] 关于移植NXP-CLRC663的这款NFC读卡器芯片驱动的提问

[复制链接]

该用户从未签到

2

主题

33

帖子

0

蝴蝶豆

初级会员

最后登录
2019-8-15
发表于 2018-9-25 10:32:27 | 显示全部楼层 |阅读模式
现在项目上要求把NXP公司的 CLRC663芯片用STM32来控制读写操作

有没有大佬做过这方面的移植啊,
本人对于14443A的协议看得一脸蒙蔽,求大佬们帮帮忙啊,
能读出标签的UID,但是不知道怎么操作SPI发指令读写标签,流程在14443A写了出来,但是看不懂

跪求大佬拉一波,指个路


回复

使用道具 举报

该用户从未签到

1

主题

14

帖子

0

蝴蝶豆

高级会员

最后登录
2021-2-21
发表于 2018-9-25 12:28:20 | 显示全部楼层
你把代码发出来,
回复 支持 反对

使用道具 举报

  • TA的每日心情
    奋斗
    2021-4-15 11:47
  • 签到天数: 537 天

    [LV.9]

    29

    主题

    2176

    帖子

    127

    蝴蝶豆

    论坛元老

    最后登录
    2023-8-27
    发表于 2018-9-25 14:11:31 | 显示全部楼层
    你可以参考一下PN532之类的代码,这些NFC的原理都是类似的

    评分

    参与人数 1蝴蝶豆 +2 收起 理由
    zero99 + 2

    查看全部评分

    回复 支持 反对

    使用道具 举报

    该用户从未签到

    0

    主题

    4

    帖子

    0

    蝴蝶豆

    新手上路

    最后登录
    2018-9-25
    发表于 2018-9-25 20:21:35 | 显示全部楼层
            赞一个!丰富STM32CubeMX的学习和推广!.

    评分

    参与人数 1ST金币 -3 收起 理由
    zero99 -3 求助帖不要灌水

    查看全部评分

    回复 支持 反对

    使用道具 举报

    该用户从未签到

    2

    主题

    33

    帖子

    0

    蝴蝶豆

    初级会员

    最后登录
    2019-8-15
     楼主| 发表于 2018-10-17 17:08:57 | 显示全部楼层
    /****************************************************************************
    * Copyright (C), 道尔智控
    *
    * 内容简述:        RC663操作底层函数
    *      
    *        原理图参考:RC663 Layout.pdf
    *   PCB为安富莱demo板
    * 文件历史:
    * 版本号  日期        作者    说明
    * v1.0.0  2014-03-13  林春志  创建工程文件
    *
    */
    /* Includes ------------------------------------------------------------------*/
    #include "public.h"

    // Forward declarations
    static void Fill_Block (uint8_t *pBlock, uint8_t MaxNr);

    /* Set the key for the Mifare (R) Classic cards. */
    static /* const */ uint8_t Key[6] = {0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU};

    // Don't change the following line
    static /* const */ uint8_t Original_Key[6] = {0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU};

    /*******************************************************************************
    **   Fill Block
    **   This function is just used to generate some example data for
    **   write operations on the card.
    *******************************************************************************/
    static void Fill_Block (uint8_t *pBlock, uint8_t MaxNr)
    {
            uint8_t i;

            for (i = 0; i <= MaxNr; i++)
            {
                    *pBlock++ = i;
            }
    }
    //PA8 蜂鸣、PB5绿灯
    extern int32_t BspGetSysTicks(void);
    extern uint8_t BspGetDlyTicks(int32_t TicksHome,int32_t Ticks);

    #define IndicateON() {GPIO_SetBits(GPIOA,GPIO_Pin_8); GPIO_ResetBits(GPIOB,GPIO_Pin_3);}
    #define IndicateOFF(){GPIO_ResetBits(GPIOA,GPIO_Pin_8);GPIO_SetBits(GPIOB,GPIO_Pin_3);}
    void GpioInit(void)
    {
            GPIO_InitTypeDef GPIO_InitStructure;

            RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB, ENABLE);

            GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
            GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
            GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
            GPIO_Init(GPIOA, &GPIO_InitStructure);

            GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
            GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
            GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
            GPIO_Init(GPIOB, &GPIO_InitStructure);
           
            IndicateOFF();
    }

    uint32_t BeepDevTicks;
    uint8_t BeepFlag;
    void BeepON(void)
    {
            IndicateON();
            BeepDevTicks = BspGetSysTicks();
            BeepFlag = 1;
    }
    void BeepRun(void)
    {
            if(BeepFlag)
            {
                    if(BspGetDlyTicks(BeepDevTicks,20))
                    {
                            BeepFlag = 0;
                            IndicateOFF();       
                    }
            }       
    }
    /*******************************************************************************
    **   Main Function
    *******************************************************************************/
    uint8_t bHalBufferReader[0x40];
    uint8_t bBufferReader[0x60];
    uint8_t Win[11]={'W','h','a','t','a','f','u','c','k','e','r'};
    uint8_t Rout[11];
    int main (void)
    {
            unsigned int volatile i;
           
            /*BFL(Basic Function Library) data parameter storage*/
            phbalReg_Stub_DataParams_t balReader;
            phhalHw_Rc663_DataParams_t halReader;
            phpalI14443p3a_Sw_DataParams_t I14443p3a;
            phpalI14443p4_Sw_DataParams_t I14443p4;
            phpalMifare_Sw_DataParams_t palMifare;
            phKeyStore_Rc663_DataParams_t Rc663keyStore;
            phalMfc_Sw_DataParams_t alMfc;
            phStatus_t status;
            void *pHal;
            uint8_t bSak[1];
            uint8_t bUid[10];
            uint8_t bMoreCardsAvailable;
            uint8_t bLength;
           
            /* Initialize GPIO (sets up clock) */
            SystemInit();                        //72M
            SysTick_Config(720000);        //10ms
            GpioInit();
            //SPI1_Init();
            RegCtl_SpiHwInit();
            /* Perform a hardware reset */
            Reset_RC663_device();
            /* Initialize the Reader BAL (Bus Abstraction Layer) component */
            phbalReg_Stub_Init(&balReader, sizeof(phbalReg_Stub_DataParams_t));
            /* Initialize the Reader HAL (Hardware Abstraction Layer) component */
        status = phhalHw_Rc663_Init(&halReader,sizeof(phhalHw_Rc663_DataParams_t),&balReader,0,bHalBufferReader,sizeof(bHalBufferReader),bHalBufferReader,sizeof(bHalBufferReader));
        /* Set the parameter to use the SPI interface */
        halReader.bBalConnectionType = PHHAL_HW_BAL_CONNECTION_SPI;
        /* Set the generic pointer */
        pHal = &halReader;
        /* Initializing specific objects for the communication with
             * Mifare (R) Classic cards.
             * The Mifare (R) Classic card is compliant of
             * ISO 14443-3 and ISO 14443-4
            */
              /* Initialize the 14443-3A PAL (Protocol Abstraction Layer) component */
            PH_CHECK_SUCCESS_FCT(status, phpalI14443p3a_Sw_Init(&I14443p3a,sizeof(phpalI14443p3a_Sw_DataParams_t), pHal));
              /* Initialize the 14443-4 PAL component */
            PH_CHECK_SUCCESS_FCT(status, phpalI14443p4_Sw_Init(&I14443p4,sizeof(phpalI14443p4_Sw_DataParams_t), pHal));
              /* Initialize the Mifare PAL component */
            PH_CHECK_SUCCESS_FCT(status, phpalMifare_Sw_Init(&palMifare,sizeof(phpalMifare_Sw_DataParams_t), pHal, &I14443p4));
              /* Initialize the keystore component */
            PH_CHECK_SUCCESS_FCT(status, phKeyStore_Rc663_Init(&Rc663keyStore,sizeof(phKeyStore_Rc663_DataParams_t), pHal));
              /* Initialize the Mifare (R) Classic AL component - set NULL because
               * the keys are loaded in E2 by the function */
            /* phKeyStore_SetKey */
            PH_CHECK_SUCCESS_FCT(status, phalMfc_Sw_Init(&alMfc,sizeof(phalMfc_Sw_DataParams_t), &palMifare,NULL));
            /* SoftReset the IC.The SoftReset only resets the RC663 to EEPROM configuration. */
            PH_CHECK_SUCCESS_FCT(status, phhalHw_Rc663_Cmd_SoftReset(pHal));
            /* Read the version of the reader IC */
            PH_CHECK_SUCCESS_FCT(status, phhalHw_ReadRegister(&halReader,PHHAL_HW_RC663_REG_VERSION, bBufferReader));
            /* Reset the Rf field */
            PH_CHECK_SUCCESS_FCT(status, phhalHw_FieldReset(pHal));
            /* Apply the type A protocol settings and activate the RF field. */
            PH_CHECK_SUCCESS_FCT(status, phhalHw_ApplyProtocolSettings(pHal,PHHAL_HW_CARDTYPE_ISO14443A));
            /* Activate the communication layer part 3 of the ISO 14443A standard. */
            //BeepON();
           
            while(1)
            {
                    BeepRun();
                    status = phpalI14443p3a_ActivateCard(&I14443p3a, NULL, 0x00, bUid, &bLength, bSak, &bMoreCardsAvailable);
           
                    /* Check if we have a card in the RF field.
                     * If so, check what card it is. */
                    if (PH_ERR_SUCCESS == status)
                    {
                            /* Check if there is an ISO-4 compliant in the RF field */
                            if (0x20 == (*bSak & 0x20))
                            {
                                    //<<<<<<<<<<<<<
                                    //BeepON();
                                    /* Mifare Classic card, set Key Store */
                                    PH_CHECK_SUCCESS_FCT(status, phKeyStore_SetKey(&Rc663keyStore, 0, 0,PH_KEYSTORE_KEY_TYPE_AES128, &Key[0], 0));//663的驱动写的只支持MIFARE类型的秘钥,硬件手册写的支持14443A
                                    /* Authenticate with the Key
                             * We can authenticate at any block of a sector and we will get the access to all blocks of the same sector
                             * For example authenticating at block 5, we will get the access to blocks 4, 5, 6 and 7.
                             */
                            /* Send authentication for block 6 */
                            status = phalMfc_Authenticate(&alMfc, 6, PHHAL_HW_MFC_KEYA, 1, 0, bUid, bLength);

                            /* Check for Status */
                            if ((status & PH_ERR_MASK) != PH_ERR_SUCCESS)
                            {
                                /* Print Error info */
    //                            DEBUG_PRINTF("\nAuthentication Failed!!!");
    //                            DEBUG_PRINTF("\nPlease correct the used key");
    //                            DEBUG_PRINTF("\nExecution aborted!!!\n");
                                break;
                            }

    //                        DEBUG_PRINTF("\nAuthentication Successful");

                            /* Empty the bDataBuffer */
    //                        memset(bDataBuffer, '\0', DATA_BUFFER_LEN);

    //                        DEBUG_PRINTF("\nRead data from Block 4");

                            /* Read data from block 4 */
                            status = phalMfc_Read(&alMfc, 4, Rout);

                            /* Check for Status */
                            if (status != PH_ERR_SUCCESS)
                            {
                                /* Print Error info */
    //                            DEBUG_PRINTF("\nRead operation failed!!!\n");
    //                            DEBUG_PRINTF("\nExecution aborted!!!\n\n");
                                break; /* Break from the loop*/
                            }

    //                        DEBUG_PRINTF("\nRead Success");
    //                        DEBUG_PRINTF("\nThe content of Block 4 is:\n");

    //                        phApp_Print_Buff(&bDataBuffer[0], MFC_BLOCK_DATA_SIZE);
    //                        DEBUG_PRINTF("\n\n --- End of Read Operation --- \n");

    //                        DEBUG_PRINTF("\nWrite data to Block 4");

                            /* Write data to block 4 */
                            status = phalMfc_Write(&alMfc, 4, Win);

                            /* Check for Status */
                            if (status != PH_ERR_SUCCESS)
                            {
                                /* Print Error info */
    //                            DEBUG_PRINTF("\nWrite operation failed!!!\n");
    //                            DEBUG_PRINTF("\nExecution aborted!!!\n");
                                break; /* Break from the loop*/
                            }
            //                  debug_printf_msg("ISO-4 compliant card detected");
                            }
                            /* Check if there is a Mifare Classic card in the RF field */
                            else if (0x08 == (*bSak & 0x08))
                            {
                              BeepON();
            //                  debug_printf_msg("Mifare Classic card detected");
            //                  debug_printf_msg("\nThe original key is:");
            //                  debug_printf_hex_msg(&Original_Key[0], 6);
            //                  debug_printf_msg("\nThe used key for authentication is:");
            //                  debug_printf_hex_msg(&Key[0], 6);
                                    /* First step for us is to authenticate with the Key at the Mifare
                                     * Classic in the field.
                                     * You need to authenticate at any block of a sector and you
                                     * may get access to all other blocks of the sector.
                                     * For example authenticating at block 5 you will get access to
                                     * the blocks 4, 5, 6, 7.
                                     */
                                    /* Mifare Classic card, set Key Store */
                                    PH_CHECK_SUCCESS_FCT(status, phKeyStore_SetKey(&Rc663keyStore, 0, 0,PH_KEYSTORE_KEY_TYPE_MIFARE, &Key[0], 0));
            //                        debug_printf_msg("\n**** Set Key Store successful");
           
                                    /* Mifare Classic card, send authentication for sector 0 */
                                    status = phalMfc_Authenticate(&alMfc, 0, PHHAL_HW_MFC_KEYA, 0, 0, bUid, bLength);
                                    if(status)
                                    {
            //                                debug_printf_msg("\n!!! Authentication was not successful.\n"
            //                                                                 "!!! Please correct the key at line 86.");
            //                                debug_printf_msg("\n/****** Abort of execution ******/");
                                            continue;
                                    }
            //                        debug_printf_msg("\n**** Authentication successful");
           
                                    /* Mifare Classic card, send authentication - alternative way */
              //                        PH_CHECK_SUCCESS_FCT(status, phpalMifare_MfcAuthenticate(&palMifare,
              //                                                     0, PHHAL_HW_MFC_KEYA, Key, bUid));
           
                                    /* Check the UID of the Classic card in the field */
                                    PH_CHECK_SUCCESS_FCT(status, phalMfc_Read(&alMfc, 0,&bBufferReader[0]));
           
                                    /*
                                     * We need this #ifdefs because we would get errors when compiling
                                     * for Release configuration.
                                     */
            //                        #ifdef DEBUG
            //                        debug_printf_msg("\nThe UID is:");
           
                                    if(bLength == 0x04)
                                    {
            //                          debug_printf_hex_msg(&bBufferReader[0], 4);
           
            //                          debug_printf_msg("\nThe Check Byte for the UID is:");
            //                          debug_printf_hex_msg(&bBufferReader[4], 1);
                                    }
                                    else if(bLength == 0x07)
                                    {
            //                          debug_printf_hex_msg(&bBufferReader[0], 7);
            //
            //                          debug_printf_msg("\nThe Check Byte for the UID is:");
            //                          debug_printf_hex_msg(&bBufferReader[7], 1);
                                    }
                                    else
                                    {
            //                          debug_printf("\nLength of the UID not supported.");
                                    }
           
            //                        #endif
                                    /* Mifare Classic card, send authentication for sector 1 */
                                    PH_CHECK_SUCCESS_FCT(status, phalMfc_Authenticate(&alMfc, 6,PHHAL_HW_MFC_KEYA, 0, 0, bUid, bLength));
           
                                    /* fill block with data */
                                    Fill_Block(bBufferReader, 15);
           
                                    /* Write data @ block 4 */
                                    PH_CHECK_SUCCESS_FCT(status, phalMfc_Write(&alMfc, 4, Win));//写
            //                        debug_printf_msg("\nWrite successful 16 bytes");
           
                                    /* Empty the bBufferReader */
                                    memset(bBufferReader, '\0', 0x60);
           
                                    /* Read the just written data.
                                     * In one reading action we always get the whole Block.
                                     */
            //                        debug_printf_msg("\nReading the just written 16 bytes");
                                    PH_CHECK_SUCCESS_FCT(status, phalMfc_Read(&alMfc, 4, Rout));//读
                                    while(1);
           
                                    #ifdef DEBUG
                                    int i;
            //                        debug_printf_msg("\nThe content of Block 4 is:");
                                    for (i=0; i < 4; i++)
                                    {
            //                          debug_printf_hex_msg(&bBufferReader[i*4], 4);
            //                          debug_printf_msg("-----Cut-----\n");
                                    }
                                    #endif
                            }
                            /* The last possibility is a Mifare UltraLight card */
                            else
                            {
            //                        debug_printf_msg("Mifare UltraLight card detected");
                            }
            //                GPIOSetValue( LED_PORT, LED_BIT, LED_OFF );
                    }
                    else
                    {
            //          debug_printf_msg("No card detected");
                    }
            }
    }

    /******************* (C) COPYRIGHT 道尔智控 *****END OF FILE****/
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    2

    主题

    33

    帖子

    0

    蝴蝶豆

    初级会员

    最后登录
    2019-8-15
     楼主| 发表于 2018-10-17 17:14:44 | 显示全部楼层
    ljman1101 发表于 2018-9-25 12:28
    你把代码发出来,

    大神你好,我把main函数贴出来了,这是抄的网上的代码,用这个读写NXP 的MIFARE卡没有问题(SAK=0X08);
    但是可以读st公司的m24sr(14443A标准)的UID,但是读写数据就不行,每次执行到
    if (0x20 == (*bSak & 0x20))
    的分支后,phKeyStore_SetKey成功(这个函数里面只有支持PH_KEYSTORE_KEY_TYPE_MIFARE的stwch case,其他类型的case都是空 ),但是不知道与me24sr是否符合;
    phalMfc_Authenticate函数执行失败,返回0x204或其他非0数值而跳出循环。
    这个历程用的官方的库,实在没有办法了,又折腾了一个月了。
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    2

    主题

    33

    帖子

    0

    蝴蝶豆

    初级会员

    最后登录
    2019-8-15
     楼主| 发表于 2018-10-17 17:16:54 | 显示全部楼层
    stm1024 发表于 2018-9-25 14:11
    你可以参考一下PN532之类的代码,这些NFC的原理都是类似的

    我在淘宝上买了个clrc663的板子,他们用spi手动写了个读写uid的程序,完成了放冲突环的流程,但是我看14443A的协议,不知道怎么获得ATS,14443A的协议里面也没有写获得方式,而流程中需要获得ATS才能进入读写流程。
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /3 下一条

    Archiver|手机版|小黑屋|论坛-意法半导体STM32/STM8技术社区

    GMT+8, 2024-4-19 18:28 , Processed in 1.171075 second(s), 36 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表