#include "main.h" #include "pn532.h" int writeToken=0; char string_to_write[20]; int stringLength = 0; //========================================================= // >>>>>>>>>>>> MAIN <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< //========================================================= int main(void) { uint32_t versiondata; uint8_t success; uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type) uint8_t data[16]; int i; HAL_Init(); SystemClock_Config(); uart2_Init(); i2c1_Init(); HAL_Delay(1000); pn532_reset(); HAL_Delay(100); versiondata = pn532_getFirmwareVersion(); HAL_Delay(200); if (! versiondata) { term_printf("Didn't find PN53x board\n\r"); while (1); // halt } term_printf("Found chip PN5%x\n\r",(versiondata>>24) & 0xFF); term_printf("Firmware version %d.%d\n\r",(versiondata>>16) & 0xFF, (versiondata>>8) & 0xFF); pn532_SAMConfig(); // configure board to read RFID tags HAL_Delay(200); term_printf("Write ? (esc for no ) \n\r"); // wait for string writeToken = term_scanf(string_to_write, &stringLength); term_printf("Waiting for an ISO14443A Card ..."); while(1) { // Wait for an ISO14443A type cards (Mifare, etc.). When one is found // 'uid' will be populated with the UID, and uidLength will indicate // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight) success = pn532_readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength, 1000); if (success) { term_printf("Found an ISO14443A card \n\r"); term_printf("UID Length: %d bytes \n\r", uidLength); if (uidLength == 4) { // We probably have a Mifare Classic card ... term_printf("Seems to be a Mifare Classic card (4 byte UID) \n\r"); term_printf("UID Value: 0x%x 0x%x 0x%x 0x%x\n\r", uid[0],uid[1],uid[2],uid[3] ); // Now we need to try to authenticate it for read/write access // Try with the factory default KeyA: 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF term_printf("Trying to authenticate block 4 with default KEYA value \n\r"); uint8_t keya[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; // Start with block 4 (the first block of sector 1) since sector 0 // contains the manufacturer data and it's probably better just // to leave it alone unless you know what you're doing success = pn532_mifareclassic_AuthenticateBlock(uid, uidLength, 4, 0, keya); HAL_Delay(200); if (success) { term_printf("Sector 1 (Blocks 4..7) has been authenticated \n\r"); // If you want to write something to block 4 to test with, uncomment // the following line and this text should be read back in a minute if(writeToken == 1) { term_printf("string to write : %s \n\r", string_to_write); //memcpy(data, (const uint8_t[]){ 'K', 'E', 'R', 'H', 'O', 'A', 'S', ' ', 'V','i','n','c','e','n','t',0}, sizeof data); memcpy(data, string_to_write, stringLength); success = pn532_mifareclassic_WriteDataBlock (4, data); if(success) { term_printf("Writing Block Finished"); writeToken = 0; } } // Try to read the contents of block 4 success = pn532_mifareclassic_ReadDataBlock(4, data); if (success) { // Data seems to have been read ... spit it out term_printf("Reading Block 4 (HEX): \n\r"); for(i=0 ; i<16 ; i++) { term_printf("%x ",data[i]); } term_printf("| %s",data); term_printf("\n\r"); // Wait a bit before reading the card again HAL_Delay(1000); } else { term_printf("Ooops ... unable to read the requested block. Try another key? \n\r"); } } else { term_printf("Ooops ... authentication failed: Try another key? \n\r"); } } } HAL_Delay(500); } // END WHILE return 0; }