用stm32控制clrc663读写st公司的m24sr存在的问题

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

用stm32控制clrc663读写st公司的m24sr存在的问题

6,739 Views
768839134
Contributor I

您好,

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

用官方的例程可以对类型为  “MIFARE”  的卡进行读写操作。

但是对类型为“14443A”的卡进行操作则会失败,
用官方例程能读出14443A的卡的UID,我用的卡是st公司的m24sr64,

再后面的步骤是需要操作密钥,但是操作密钥的函数一直返回失败(返回0x201)。

我看官方给的函数里面在setkey的函数里面只写了mifare卡类型的密钥,请问和这个有关系吗?

我把密钥类型改成14443A和MIFARE卡来操作14443A的卡都不行,但是CLRC663的数据手册上明确写出了是支持14443A类型协议的,我想请问一下CLRC663到底支不支持其他公司生产的类型为14443A的卡啊,比如我正在使用的这款ST公司的M24SR?如果支持的话为什么不能读写呢?

万分感谢,下面贴出main.c的函数:

/* 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");
                }
        }
}

跪求大佬拉一波,指个路

Labels (1)
Tags (4)
0 Kudos
Reply
9 Replies

5,383 Views
276833507
Contributor I

你对14443卡的处理应该参考经典卡处理的流程。

0 Kudos
Reply

5,383 Views
768839134
Contributor I

经典卡处理流程只到读uid就没了,没有读写dnef流程。

发自我的iPhone

0 Kudos
Reply

5,382 Views
276833507
Contributor I

我最近也在研究,不过是MFRC630,可以加个微信共同研究:15680000266

你的这个实例 如果跟踪下去,你会发现只处理经典卡。

0 Kudos
Reply

5,382 Views
Kan_Li
NXP TechSupport
NXP TechSupport

It is supported, but you have to check with the ST supporter how to operate with their cards. You may refer to the following for more details.

Hope that helps,


Have a great day,
Kan

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos
Reply

5,382 Views
768839134
Contributor I

能读到14443A的UID,假设我也知道M24SR读应该发什么指令,但是怎么操作RC663发送指令我不知道。所以我想问的是怎么操作RC663发送和接受数据或者指令的问题。

 

谢谢

0 Kudos
Reply

5,382 Views
768839134
Contributor I

dear KanLi,

This is the software pack downloaded from your website, and I find it donnot have Ex3.6.8.10. May you send them to me?

Thanks a lot.May you have a nice day.

Dengbo

17345070507

发自我的iPhone

0 Kudos
Reply

5,382 Views
Kan_Li
NXP TechSupport
NXP TechSupport

These examples are included in the export control version of NFC reader library, which you may request from the docstore, please refer to the attachment for details.

pastedImage_1.png

Hope that helps,


Have a great day,
Kan

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos
Reply

5,382 Views
768839134
Contributor I

dear kan,

Icannot find sw375619 in your website, could you send the pack to me,thanks a lot.

may you have a nice day.

发自我的iPhone

0 Kudos
Reply

5,382 Views
768839134
Contributor I

hello Kan Li,

It belongs to 144443A, may i ask what should i do with the software to operate with 144443A tags? What differance have between 14443A and Mifare for me to use the sofeware?

thanks a lot,have a nice day

Dengbo

发自我的iPhone

0 Kudos
Reply