羽夜霜降 发表于 2018-10-18 11:28:36

使用STM32控制NXP的NFC读卡器芯片CLRC663读写ST公司的NFCM24...

现在项目上要求把NXP公司的 CLRC663芯片用STM32来控制读写操作

有没有大佬做过这方面的移植啊,
本人对于14443A的协议看得一脸蒙蔽,求大佬们帮帮忙啊,
能读出标签的UID,
我把main函数贴出来了,这是抄的网上的代码,用这个读写NXP 的MIFARE卡没有问题(SAK=0X08);

但是只能读st公司的m24sr(14443A标准)的UID,读写数据就不行,每次执行到
phKeyStore_SetKey成功(这个函数里面只有支持PH_KEYSTORE_KEY_TYPE_MIFARE的stwch case,其他类型的case都是空 ),但是不知道与me24sr是否符合,
之后
phalMfc_Authenticate函数执行失败,返回0x201或其他非0数值而跳出循环。
这个历程用的官方的库,实在没有办法了,又折腾了一个月了。
跪求大佬拉一波,指个路

/* 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 = {0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU};

// Don't change the following line
static /* const */ uint8_t Original_Key = {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;
uint8_t bBufferReader;
uint8_t Win={'W','h','a','t','a','f','u','c','k','e','r'};
uint8_t Rout;
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;
      uint8_t bUid;
      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));//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, 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, 6);
      //                  debug_printf_msg("\nThe used key for authentication is:");
      //                  debug_printf_hex_msg(&Key, 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));
      //                        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));
      
                              /*
                                 * 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, 4);
      
      //                        debug_printf_msg("\nThe Check Byte for the UID is:");
      //                        debug_printf_hex_msg(&bBufferReader, 1);
                              }
                              else if(bLength == 0x07)
                              {
      //                        debug_printf_hex_msg(&bBufferReader, 7);
      //
      //                        debug_printf_msg("\nThe Check Byte for the UID is:");
      //                        debug_printf_hex_msg(&bBufferReader, 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, 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");
                }
      }
}

羽夜霜降 发表于 2018-10-18 11:29:09

HELLO DaLao,



May I ask you a question about how can I shoud use clrc663 to read 14443a tag (m24sr64) by a wireless way?



Now,I can read and write MIFARE tag succesful, but can't do with 14443A tag?



This do sucessful:

status = phpalI14443p3a_ActivateCard(&I14443p3a, NULL, 0x00, bUid, &bLength, bSak, &bMoreCardsAvailable);



This failed:



PH_CHECK_SUCCESS_FCT(status, phKeyStore_SetKey(&Rc663keyStore, 0, 0,PH_KEYSTORE_KEY_TYPE_AES128, &Key, 0));this function return 0x201.



And I find that PH_KEYSTORE_KEY_TYPE_AES128 is not supported by this software lib, in that function the case of "PH_KEYSTORE_KEY_TYPE_AES128" is EMPTY!



Finally, may I ask if the clrc663 can read/write with 14443a tag produced by other company?

And, wether the software lib is complete to support to read/write with 14443a tag?



Tanks a lot,

have a nice day!

toofree 发表于 2018-10-18 12:26:19

别的我不懂。
刚看了手册,m24sr的默认密码是全0x00;而M1卡的默认密码不是0x00,A密码好像是全0xff,B密码不记得是不是全0xff了。
m24sr你改过密码没?如果没改的话,应该就是全0x00了。
那么换成“Key = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; ”试试。


羽夜霜降 发表于 2018-10-20 16:46:23

toofree 发表于 2018-10-18 12:26
别的我不懂。
刚看了手册,m24sr的默认密码是全0x00;而M1卡的默认密码不是0x00,A密码好像是全0xff,B密码 ...

你好,toofree:
十分感谢你帮忙查看,看了你的回复我又看了一下,MIFARE卡的密码是0xff,14443A是0x00,我改了密码仍然不行,MIFARE卡的密码是6字节的,M24SR数据手册上写的16字节,我都试过,还是报错,但是报错0x720了,还在各种瞎折腾中。
对了工程中有个setkey的函数,之前是MIFARE,我现在尝试了把这个参数改成14443A,仍然不行。。。

他的软件的逻辑大致是这样的:
1.初始化HAL
2.初始化BAL
3.初始化PAL
4.初始化KEY
5.初始化AL
在我看来,不同的也就是PAL和KEY的类型不同,所以只改了这两个地方,但是不行啊。

奔溃,大哥能否帮忙想想还是哪儿出了问题。

huangxuejia-292 发表于 2018-10-21 17:48:20

不懂NFC,但是用RC663做过金融POS驱动。
驱动一般只完成协议交互,不关心应用,例如密码密钥。
通常上电寻卡,寻到卡之后就防冲突,之后就进入APDU交互了,具体交互数据,都是应用管的。

羽夜霜降 发表于 2018-10-24 09:38:58

huangxuejia-292 发表于 2018-10-21 17:48
不懂NFC,但是用RC663做过金融POS驱动。
驱动一般只完成协议交互,不关心应用,例如密码密钥。
通常上电寻 ...

你好,huangxuejia-292
感谢回复,我现在就是读出来TAG的UID,但是无法匹配密码密钥。不知道RC633对支持的NFC芯片厂家有没有限制。现在读写NXP公司的MIFARE卡没有问题,但是我现在想用的TAG是ST公司的M24SR,读出UID之后始终不能读写。
想问问是不是就算TAG没有密码,也需要进行密码密钥匹配才能进入读写流程啊。我试了不用密码密钥匹配函数,读写函数也执行不了啊。

感谢帮助!

butterflyspring 发表于 2018-10-24 11:43:08

你为什么不用ST 的reader呢?M24SR是标准的1444a卡,不是mifair卡,你把ID读出来了,后面就是NDEF的流程了..

羽夜霜降 发表于 2018-10-25 16:41:54

butterflyspring 发表于 2018-10-24 11:43
你为什么不用ST 的reader呢?M24SR是标准的1444a卡,不是mifair卡,你把ID读出来了,后面就是NDEF的流程了.. ...

意思是用NXP的reader读不了m24sr吗

羽夜霜降 发表于 2018-10-25 17:25:41

大佬么能帮忙看看

羽夜霜降 发表于 2018-10-25 17:26:36

http://m.qpic.cn/psb?/V14TtgHU1924Ws/a9FwMayDP7RxLlY57l*lh0BXIxwXuPAiJR0a3JTt3VU!/b/dFMBAAAAAAAA&bo=4AfoBeAH6AUROQ4!&rf=viewer_4
页: [1] 2
查看完整版本: 使用STM32控制NXP的NFC读卡器芯片CLRC663读写ST公司的NFCM24...