Martin Olejar
/
FRDM_MFRC522
MFRC522 example project for FRDM
Diff: main.cpp
- Revision:
- 1:8e41a7b03f45
- Parent:
- 0:1d9c7c0b5015
diff -r 1d9c7c0b5015 -r 8e41a7b03f45 main.cpp --- a/main.cpp Sat Dec 14 21:41:08 2013 +0000 +++ b/main.cpp Wed Dec 18 00:46:40 2013 +0000 @@ -1,46 +1,49 @@ #include "mbed.h" #include "MFRC522.h" -#define BOARD_KL25Z 1 -//#define BOARD_LPC11U24 1 - - -#if defined(BOARD_KL25Z) +#if defined(TARGET_KL25Z) /* KL25Z Pins for MFRC522 SPI interface */ -#define SPI_MOSI PTA16 -#define SPI_MISO PTA17 -#define SPI_SCLK PTD1 -#define SPI_CS PTD3 +#define SPI_MOSI PTC6 +#define SPI_MISO PTC7 +#define SPI_SCLK PTC5 +#define SPI_CS PTC4 /* KL25Z Pin for MFRC522 reset */ -#define MF_RESET PTD2 +#define MF_RESET PTC3 /* KL25Z Pins for UART Debug port */ #define UART_RX PTA1 #define UART_TX PTA2 -#elif defined(BOARD_LPC11U24) +#elif defined(TARGET_LPC11U24) /* LPC11U24 Pins for MFRC522 SPI interface */ -#define SPI_MOSI p16 -#define SPI_MISO p15 -#define SPI_SCLK p13 -#define SPI_CS p34 +#define SPI_MOSI P0_9 +#define SPI_MISO P0_8 +#define SPI_SCLK P1_29 +#define SPI_CS P0_2 /* LPC11U24 Pin for MFRC522 reset */ -#define MF_RESET p17 +#define MF_RESET P1_13 /* LPC11U24 Pins for UART Debug port */ -#define UART_RX p20 -#define UART_TX p19 +#define UART_RX P0_18 +#define UART_TX P0_19 + +/* LED Pins */ +#define LED_RED P0_7 +#define LED_GREEN P1_22 #endif -Serial DebugUART (UART_TX, UART_RX); -MFRC522 RfChip (SPI_MOSI, SPI_MISO, SPI_SCLK, SPI_CS, MF_RESET); +DigitalOut LedRed (LED_RED); +DigitalOut LedGreen (LED_GREEN); +Serial DebugUART(UART_TX, UART_RX); +MFRC522 RfChip (SPI_MOSI, SPI_MISO, SPI_SCLK, SPI_CS, MF_RESET); +/* Local functions */ void DumpMifareClassicToSerial (MFRC522::Uid *uid, uint8_t piccType, MFRC522::MIFARE_Key *key); void DumpMifareClassicSectorToSerial(MFRC522::Uid *uid, MFRC522::MIFARE_Key *key, uint8_t sector); void DumpMifareUltralightToSerial (void); @@ -55,7 +58,7 @@ MFRC522::MIFARE_Key key; // UID - printf("Card UID:"); + printf("Card UID: "); for (uint8_t i = 0; i < uid->size; i++) { printf(" %X02", uid->uidByte[i]); @@ -64,7 +67,7 @@ // PICC type uint8_t piccType = RfChip.PICC_GetType(uid->sak); - printf("PICC type: %s \n\r", RfChip.PICC_GetTypeName(piccType).c_str()); + printf("PICC Type: %s \n\r", RfChip.PICC_GetTypeName(piccType)); // Dump contents @@ -74,10 +77,7 @@ case MFRC522::PICC_TYPE_MIFARE_1K: case MFRC522::PICC_TYPE_MIFARE_4K: // All keys are set to FFFFFFFFFFFFh at chip delivery from the factory. - for (uint8_t i = 0; i < 6; i++) - { - key.keyByte[i] = 0xFF; - } + for (uint8_t i = 0; i < 6; i++) { key.keyByte[i] = 0xFF; } DumpMifareClassicToSerial(uid, piccType, &key); break; @@ -127,15 +127,17 @@ no_of_sectors = 40; break; - default: // Should not happen. Ignore. + default: + // Should not happen. Ignore. break; } // Dump sectors, highest address first. if (no_of_sectors) { - printf("Sector Block 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 AccessBits \n\r"); - for (uint8_t i = no_of_sectors - 1; i >= 0; i--) + printf("Sector Block 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 AccessBits \n\r"); + printf("----------------------------------------------------------------------------------------- \n\r"); + for (uint8_t i = no_of_sectors - 1; i > 0; i--) { DumpMifareClassicSectorToSerial(uid, key, i); } @@ -167,7 +169,7 @@ // The four CX bits are stored together in a nible cx and an inverted nible cx_. uint8_t c1, c2, c3; // Nibbles uint8_t c1_, c2_, c3_; // Inverted nibbles - bool invertedError; // True if one of the inverted nibbles did not match + bool invertedError = false; // True if one of the inverted nibbles did not match uint8_t g[4]; // Access bits for each of the four groups. uint8_t group; // 0-3 - active group for access bits bool firstInGroup; // True for the first block dumped in the group @@ -193,9 +195,10 @@ uint8_t buffer[18]; uint8_t blockAddr; isSectorTrailer = true; - for (char blockOffset = no_of_blocks - 1; blockOffset >= 0; blockOffset--) + for (uint8_t blockOffset = no_of_blocks - 1; blockOffset > 0; blockOffset--) { blockAddr = firstBlock + blockOffset; + // Sector number - only on first line if (isSectorTrailer) { @@ -205,35 +208,40 @@ { printf(" "); } + // Block number - printf(" %3d ", blockAddr); + printf(" %3d ", blockAddr); + // Establish encrypted communications before reading the first block if (isSectorTrailer) { status = RfChip.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, firstBlock, key, uid); if (status != MFRC522::STATUS_OK) { - printf("PCD_Authenticate() failed: %s \r\n", RfChip.GetStatusCodeName(status).c_str()); + printf("PCD_Authenticate() failed: %s \r\n", RfChip.GetStatusCodeName(status)); return; } } + // Read block byteCount = sizeof(buffer); status = RfChip.MIFARE_Read(blockAddr, buffer, &byteCount); if (status != MFRC522::STATUS_OK) { - printf("MIFARE_Read() failed: %s \r\n", RfChip.GetStatusCodeName(status).c_str()); + printf("MIFARE_Read() failed: %s \r\n", RfChip.GetStatusCodeName(status)); continue; } + // Dump data for (uint8_t index = 0; index < 16; index++) { - printf(" %2d", buffer[index]); - if ((index % 4) == 3) - { - printf(" "); - } + printf(" %3d", buffer[index]); +// if ((index % 4) == 3) +// { +// printf(" "); +// } } + // Parse sector trailer data if (isSectorTrailer) { @@ -244,7 +252,7 @@ c2_ = buffer[6] >> 4; c3_ = buffer[7] & 0xF; invertedError = (c1 != (~c1_ & 0xF)) || (c2 != (~c2_ & 0xF)) || (c3 != (~c3_ & 0xF)); - + g[0] = ((c1 & 1) << 2) | ((c2 & 1) << 1) | ((c3 & 1) << 0); g[1] = ((c1 & 2) << 1) | ((c2 & 2) << 0) | ((c3 & 2) >> 1); g[2] = ((c1 & 4) << 0) | ((c2 & 4) >> 1) | ((c3 & 4) >> 2); @@ -267,7 +275,7 @@ if (firstInGroup) { // Print access bits - printf(" [ %d %d %d ] ", (g[group] >> 2) & 1, (g[group] >> 1) & 1, (g[group] >> 0) & 1); + printf(" [ %d %d %d ] ", (g[group] >> 2) & 1, (g[group] >> 1) & 1, (g[group] >> 0) & 1); if (invertedError) { printf(" Inverted access bits did not match! "); @@ -299,7 +307,7 @@ uint8_t buffer[18]; uint8_t i; - printf("Page 0 1 2 3"); + printf("Page 0 1 2 3"); // Try the mpages of the original Ultralight. Ultralight C has more pages. for (uint8_t page = 0; page < 16; page +=4) { @@ -308,7 +316,7 @@ status = RfChip.MIFARE_Read(page, buffer, &byteCount); if (status != MFRC522::STATUS_OK) { - printf("MIFARE_Read() failed: %s \n\r", RfChip.GetStatusCodeName(status).c_str()); + printf("MIFARE_Read() failed: %s \n\r", RfChip.GetStatusCodeName(status)); break; } @@ -316,7 +324,7 @@ for (uint8_t offset = 0; offset < 4; offset++) { i = page + offset; - printf(" %2d ", i); // Pad with spaces + printf(" %2d ", i); // Pad with spaces for (uint8_t index = 0; index < 4; index++) { i = 4 * offset + index; @@ -330,25 +338,45 @@ int main() { + /* Set debug UART speed */ DebugUART.baud(115200); + printf("< mbed RFID demo >\n\r"); + printf("\n\r"); + /* Init. RC522 Chip */ RfChip.PCD_Init(); + /* Read RC522 version */ + uint8_t temp = RfChip.PCD_ReadRegister(MFRC522::VersionReg); + printf("MFRC522 version: %d\n\r", temp & 0x07); + printf("\n\r"); + while(1) { + LedRed = 1; + LedGreen = 1; + // Look for new cards if ( ! RfChip.PICC_IsNewCardPresent()) { + wait_ms(500); continue; } + LedRed = 0; + // Select one of the cards if ( ! RfChip.PICC_ReadCardSerial()) { + wait_ms(500); continue; } + LedRed = 1; + LedGreen = 0; + // Dump debug info about the card. PICC_HaltA() is automatically called. DumpToSerial(&(RfChip.uid)); + wait_ms(200); } }