Thursday, August 22, 2013

MIFARE: Initialization and anticollision

Oh my - where did those four weeks go? Well, vacation and getting back to work took most of my time - but I did do a little work on the project.

The Nokia 5110 84x48 pixel display I mentioned last was tested briefly. The code from Adafruit worked just fine - and so did the backlight when I stopped connecting the pin to Vcc and used GND instead. Doh!

The MIFARE reader kit has arrived. It is based on the NXP MFRC522 Contactless Reader IC, and uses SPI for communication with the micro controller. The data sheet for the MFRC522 IC clearly documents how to operate the reader chip, and (I quote) "The digital module manages the complete ISO/IEC 14443 A framing and error detection (parity and CRC) functionality." So far so good. But not a word about the protocol for communicating with the MIFARE chips.
Some time with google.com reveals that the first level of missing information is contained in "ISO/IEC 14443-3 Identification cards -- Contactless integrated circuit cards -- Proximity cards -- Part 3: Initialization and anticollision". It is available for CHF 162 (about USD 175)! Hrmm - more than I like. Luckily the final draft from the WG8 working group is freely available here! Using this protocol I can get the UID of the RFID card. Accessing the card contents is another matter - but never mind, I only need the UID.

I looked around for existing Arduino code to access the reader and cards, and mostly I found variations of the same code:
  • The "original" from Dr. Leong at B2CQSHOP with Chinese comments.
  • A modified version from Grant Gibson.
  • A library version from Miguel Balboa with Spanish comments.
None of these variants support cards with 7 and 10 byte UIDs (the most common is 4 bytes - or at least used to be. MIFARE Ultralight has 7 byte UIDs). Also the code does not support the anticollision protocol, ie it only works if there is only one card in the RF field (I might be wrong on this one, I have not studied the code enough to be certain).

Actually there is another piece of software: NXP has a Visual C++ project available for download (look for "200312"). After a cursory look I have decided to forget about NXP's code, and concentrate on the Arduino code from Miguel Balboa. I will translate the comments to English, clean up, and implement proper anticollision and support for 7 and 10 byte UIDs. All changes will be offered as a GitHub pull request to Miguel, but I do not yet know if he is interested. Stay tuned!