MFRC522 - Mifare 1k - PCD_Authenticate Failed

17 Sep 2018

Hi, I' m taking this error in my code:

"PCD_Authenticate() failed: Timeout in communication " I need some help :)

Sorry for my english!

//Test of cheap 13.56 Mhz RFID-RC522 module from eBay
//This code is based on Martin Olejar's MFRC522 library. Minimal changes
//Adapted for Nucleo STM32 F401RE. Should work on other Nucleos too

//Connect as follows:
//RFID pins        ->  Nucleo header CN5 (Arduino-compatible header)
//----------------------------------------
//RFID IRQ=pin5    ->   Not used. Leave open
//RFID MISO=pin4   ->   Nucleo SPI_MISO=PA_6=D12
//RFID MOSI=pin3   ->   Nucleo SPI_MOSI=PA_7=D11
//RFID SCK=pin2    ->   Nucleo SPI_SCK =PA_5=D13
//RFID SDA=pin1    ->   Nucleo SPI_CS  =PB_6=D10
//RFID RST=pin7    ->   Nucleo         =PA_9=D8
//3.3V and Gnd to the respective pins                              
                              
#include "mbed.h"
#include "MFRC522.h"

// Nucleo Pin for MFRC522 reset (pick another D pin if you need D8)
#define MF_RESET    D9

DigitalIn sw0(D2);
DigitalIn sw1(D3);

//Serial connection to PC for output
Serial pc(SERIAL_TX, SERIAL_RX);

MFRC522    RfChip   (SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS, MF_RESET);

uint8_t valor, size;

MFRC522::MIFARE_Key key;

void read_mode()
{   
    pc.printf("Read mode\n\r");
    while ( ! RfChip.PICC_IsNewCardPresent()) 
    {
        wait_ms(100);
    }
    
    if ( ! RfChip.PICC_ReadCardSerial())    
    {
        return;
    }
    
    pc.printf("Card UID: ");
    for (uint8_t i = 0; i < RfChip.uid.size; i++)
    {
      pc.printf(" %X02", RfChip.uid.uidByte[i]);
    }
    pc.printf("\n\r");
    
    uint8_t sector         = 1;
    uint8_t blockAddr      = 4;
    uint8_t trailerBlock   = 7;
    uint8_t status;
    uint8_t buffer[18];
    uint8_t size = sizeof(buffer);
    
    status = RfChip.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(RfChip.uid));                              
    if (status != MFRC522::STATUS_OK)   
    {    
        pc.printf("PCD_Authenticate() failed: \n\r");
        pc.printf(RfChip.GetStatusCodeName(status));
        pc.printf("\n\r");
        return;
    }
    
    status = RfChip.MIFARE_Read(blockAddr, buffer, &size);
    if (status != MFRC522::STATUS_OK) 
    {
        pc.printf("MIFARE_Read() failed: \n\r");
        pc.printf(RfChip.GetStatusCodeName(status));
        pc.printf("\n\r");
    }
    //Mostra os dados do nome no Serial Monitor 

    for (uint8_t i = 1; i < 16; i++)
    {
        pc.printf("%c",buffer[i]);
    }
    pc.printf("\n\r");
    
    //Obtem os dados do setor 0, bloco 1 = Sobrenome
    sector         = 0;
    blockAddr      = 1;
    trailerBlock   = 3;
 
    //Autenticacao usando chave A
    status=RfChip.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(RfChip.uid));
    if (status != MFRC522::STATUS_OK)
    {
        pc.printf("PCD_Authenticate() failed: \n\r");
        pc.printf(RfChip.GetStatusCodeName(status));
        pc.printf("\n\r");
        return;
    }
    status = RfChip.MIFARE_Read(blockAddr, buffer, &size);
    if (status != MFRC522::STATUS_OK)
    {
        pc.printf("MIFARE_Read() failed: \n\r");
        pc.printf(RfChip.GetStatusCodeName(status));
    }
    //Mostra os dados do sobrenome no Serial Monitor
    for (uint8_t i = 0; i < 16; i++)
    {
        pc.printf("%c",buffer[i]);
    }
    pc.printf("\n\r");

    // Halt PICC
    RfChip.PICC_HaltA();
    // Stop encryption on PCD
    RfChip.PCD_StopCrypto1();
    wait_ms(3000);
} 

void write_mode()
{
    pc.printf("Write mode\n\r");
    //Aguarda cartao
    while ( ! RfChip.PICC_IsNewCardPresent()) 
    {
        wait_ms(100);
    }
  
    if ( ! RfChip.PICC_ReadCardSerial())    
    {
        return;
    }
    
    pc.printf("Card UID: ");
    for (uint8_t i = 0; i < RfChip.uid.size; i++)
    {
      pc.printf(" %X02", RfChip.uid.uidByte[i]);
    }
    pc.printf("\n\r");
    
    //Mostra o tipo do cartao
    pc.printf("Tipo do PICC: ");
    uint8_t piccType = RfChip.PICC_GetType(RfChip.uid.sak);
    pc.printf(RfChip.PICC_GetTypeName(piccType));
    pc.printf("\n\r");
 
    uint8_t buffer[34] = {'o','l','a',' ','4','0','3'};
    uint8_t block;
    uint8_t status = 1, len;
  
    block = 1;
    //pc.println(F("Autenticacao usando chave A..."));
    status = RfChip.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(RfChip.uid));
    
    if (status != MFRC522::STATUS_OK) 
    {
        pc.printf("PCD_Authenticate() failed: \n\r");
        pc.printf(RfChip.GetStatusCodeName(status));
        pc.printf("\n\r");
        return;
    }
 
    //Grava no bloco 1
    status = RfChip.MIFARE_Write(block, buffer, 16);
    if (status != MFRC522::STATUS_OK) 
    {
        pc.printf("MIFARE_Write() failed: \n\r");
        pc.printf(RfChip.GetStatusCodeName(status));
        pc.printf("\n\r");
        return;
    }
 
    block = 2;
    //pc.println(F("Autenticacao usando chave A..."));
    
    status = RfChip.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(RfChip.uid));
    if (status != MFRC522::STATUS_OK) 
    {
        pc.printf("PCD_Authenticate() failed: \n\r");
        pc.printf(RfChip.GetStatusCodeName(status));
        pc.printf("\n\r");
        return;
    }
 
    //Grava no bloco 2
    status = RfChip.MIFARE_Write(block, &buffer[16], 16);
    if (status != MFRC522::STATUS_OK) 
    {
        pc.printf("MIFARE_Write() failed: \n\r");
        pc.printf(RfChip.GetStatusCodeName(status));
        pc.printf("\n\r");
        return;
    }
 
    block = 4;
    //pc.println(F("Autenticacao usando chave A..."));
    status = RfChip.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(RfChip.uid));
    if (status != MFRC522::STATUS_OK) 
    {
        pc.printf("PCD_Authenticate() failed: \n\r");
        pc.printf(RfChip.GetStatusCodeName(status));
        pc.printf("\n\r");
        return;
    }
 
    //Grava no bloco 4
    status = RfChip.MIFARE_Write(block, buffer, 16);
    if (status != MFRC522::STATUS_OK) 
    {
        pc.printf("MIFARE_Write() failed: \n\r");
        pc.printf(RfChip.GetStatusCodeName(status));
        pc.printf("\n\r");
        return;
    }
 
    block = 5;
    //pc.println(F("Authenticating using key A..."));
    status = RfChip.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(RfChip.uid));
    if (status != MFRC522::STATUS_OK) 
    {
        pc.printf("PCD_Authenticate() failed: \n\r");
        pc.printf(RfChip.GetStatusCodeName(status));
        return;
    }
 
    //Grava no bloco 5
    status = RfChip.MIFARE_Write(block, &buffer[16], 16);
    if (status != MFRC522::STATUS_OK) 
    {
        pc.printf("MIFARE_Write() failed: \n\r");
        pc.printf(RfChip.GetStatusCodeName(status));
        //return;
    }
    else
    {
        pc.printf("Dados gravados com sucesso!\n\r");
    }
 
    RfChip.PICC_HaltA(); // Halt PICC
    RfChip.PCD_StopCrypto1();  // Stop encryption on PCD
    wait_ms(5000);
}

int main(void) 
{
    pc.printf("starting...\n\r");
    RfChip.PCD_Init();

    while (true) 
    {       
    if(sw0 == 0)
    {
        wait_ms(500);
        if(sw0 == 0)
        {
            read_mode();
        }
    }
     
    if(sw1 == 0)
    {
        wait_ms(500);
        if(sw1 == 0)
        {
            write_mode();
        }
    }        
    }
}