Sunday, October 20, 2013

Pull request sent

Today I finally finished my rewrite of the MFRC522 library and sent a pull request to the upstream repository: https://github.com/miguelbalboa/rfid/pull/9. Now I look forward to hear Miguel's response - will he welcome my changes or reject them as "too invasive"?
Since last time I also added functions to use the MIFARE Classic Decrement/Increment/Restore/Transfer commands and an example of how to use them (it requires changing the sector trailer).

The next step in the project is building the electronics for driving a Ruko/Abloy EL582 solenoid lock I have bought for use in this project. I expect that to be a lot simpler than rewriting the MFRC522 library ;-)

Wednesday, October 16, 2013

MFRC522 library cleanup

Almost 8 weeks ago I wrote: "I will translate the comments to English, clean up, and implement proper anticollision and support for 7 and 10 byte UIDs."

I have now finished the following subjects:

  • Anticollision
  • 4, 7 and 10 byte UIDs (ie cascade levels 1, 2 and 3)
  • Cleanup
  • Verbose comments in English
  • Tested Authentication and reading from MIFARE Classic cards.
I still need a little work on:
  • Testing writes to MIFARE Classic cards (should not be a problem).
  • Support for reading and writing MIFARE Ultralight cards.
  • Updated example scripts
  • Updated keywords.txt 

So now I actually believe I will finish this job - and hopefully soon so I can get on to building some electronics for the electric lock.

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!

Friday, July 26, 2013

Using a matrix keypad is easy!

Today I wanted to try out the keypad I have bought for my project. It is a cheap ($2) 4x3 key membrane switch matrix keypad.

The keypad connected to my Iteaduino 2.2.
Using the Keypad library from http://playground.arduino.cc/Code/Keypad I was up and running in a few minutes. If I can spare the 7 IO-pins required this is almost too easy ;-)

I will have to find another way to connect it, as this connection uses pin 8 (ICP1), that I intend to use for the manchester decoding of EM4100 RFID tags. However this is no problem, as I think I will end up using a barebone Arduino for the finished product.

I am still waiting for the MIFARE reader kit, so next I will look at my $3 Nokia 5110 84x48 pixel display. It has a Philips PCD8544 controller using SPI for communication. It is well documented, and Adafruit has both a tutorial and a library for it.

Wednesday, July 24, 2013

Decoding/reading 125kHz RFID tags - live at GitHub

Initially I would like to create a system that could use any existing RFID-tag. That way we could use existing tags we already had from our jobs, transportation etc. So I ordered a 125kHZ RFID kit and a 13.56MHz Mifare kit. Both kits included both a reader circuit and a couple of sample cards.

When the 125kHz kit arrived I did not have the courage to connect it to my Arduino, as I was afraid of toasting the inputs with RS232 level signals. I waited for my DSO Quad pocket oscilloscope to arrive (more about that in a later post) and hooked it up. It revealed that the claim about onboard level converters for RS232 was bogus. Pin 3 was only an inverted signal of pin 2. The good thing was that I only needed the TTL level signals and that it worked great as soon as I connected pin 2 (TX) to the Arduino pin 10 and ran this sketch:
/*
 Receives from software serial, sends to hardware serial. 
 RX is digital pin 10 (connect to TX of other device)
 */
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX

void setup() {
  Serial.begin(9600);
  Serial.println("Scan card...");
  mySerial.begin(9600); // set the data rate for the SoftwareSerial port
}

void loop() { // run over and over
  if (mySerial.available()) {
    Serial.write(mySerial.read());
  }
}

Using the serial console in the Arduino IDE I received a steady stream of lines:
4E000194380003
4E000194380003
4E000194380003
4E000194380003
Somehow I had started suspecting the cards to be EM4100 compatible, and that is a 64 bit chip.
But each line is only 14 digits - and I expected 16 hexadecimal digits (for 64 bits).
The RFID receiver circuit has a pin labelled "Manchester code output", so I decided to try and decode that signal myself. Not yet comfortable with the Arduino platform I used the tool I knew best: PHP.
My PC could not directly interface with the manchester output pin, so I used the oscilloscope's ability to dump the captured trace to a CSV-file, and then parsed the CSV using PHP.
While working on my parser I found out about two things:

  1. The 64 bits on the EM4100 chip includes 11 header bits, a stop bit and 14 parity bits. This leaves 8 bits for a facility code and 32 bits for the UID.
  2. Another eBay dealer sells the same kit - and he includes a description of the serial output: 2 hex digits facility code, 10 decimal digits UID and 2 digits parity data.  

Not every trace was parsable, but I ended up with parsable CSV-files for each of the cards supplied with the kit. The Awid chip used for access control at my old work place however could not be parsed - it seems not to be EM4100 compatible.
Using "php dump.php DATA-Thick-card-500us.csv 3 70 12 18" I can get this output:
4E 0001943800
- which almost looks like the serial output from the receiver circuit. However I have given up on the 2 parity digits - I cannot fit 14 bits in 2 hex digits ;-) But who cares - the parity data is redundant.

The dump.php program tries both manchester and biphase decoding.

Printed on the card is "0001943800 029,43256". Initially I did not get it - but it is simply the 32 bit UID as a 10 digit decimal number followed by the high and low 16 bits as decimal:  29 * 2^16 + 43256 = 0001943800.

The next step was to implement almost the same functionality on the Arduino. I was inspired by the Arduino Cookbook to use Input Capture on the 16 bit Timer/Counter1. The result was an Arduino library (EM4100) that can capture EM4100 data from a manchester signal. It is available on https://github.com/Sthing/AccessThing/tree/master/Arduino/libraries/EM4100.

About the "any tag" idea: Well - I have become (a little) wiser. There are a lot of RFID standards and de facto standards - and a lot of custom implementations. I will have to settle for less - and EM4100 and MIFARE seems like good candidates.

Now it is getting really late and in the morning my brother and I are going canoing. I should get some sleep ;-)

Thursday, July 11, 2013

Welcome to my journey!

Today I have created the blog and written about the background for the project.

Even though it is not my native language I have decided to write in English. I hope someone outside Denmark can benefit from my work that way.

And thus it begins...