faiz180490
Posts: 8
Joined: Sun Dec 11, 2016 11:51 am

Interfacing a ISO 7816 compliant smart card with the RPi3

Fri May 05, 2017 3:15 am

Hi All,
I am working on a project that requires me to power up and communicate an ISO 7816 smart card with the Raspberry Pi 3 using the GPIO pins. I read about using the UART to do this but I have another device using the UART port at the moment.

So Does anyone know of a way I can interface the Smart card using the SPI or the I2C ports? Any solution, H/W or S/W will be good.

gregeric
Posts: 1509
Joined: Mon Nov 28, 2011 10:08 am

Re: Interfacing a ISO 7816 compliant smart card with the RPi

Fri May 05, 2017 6:32 am

You can purchase USB smartcard readers - search for "smartmouse" or "phoenix", commonly available from satellite dish/stb retailers.

If you're OK with hardware, you can build one youself from an FTDI FT232 breakout board, smartcard socket, a schottky diode and one resistor. That will clock the card at a fixed 6MHz. The retail items usually offer a range of programmable clock rates.

faiz180490
Posts: 8
Joined: Sun Dec 11, 2016 11:51 am

Re: Interfacing a ISO 7816 compliant smart card with the RPi

Fri May 05, 2017 6:44 am

Hi Gregeric,

Thank you for the help. I can not use the USB ports, it has to be interfaced by the GPIO headers on the Pi. Hence, I cant use the USB Card reader. When I say smart card, I actually mean I have the chip without the card. Sorry about not mentioning that. I am looking at building something like a HAT that sits on the header pins with the smart card chip on it.

Do you know of any solutions that involve the use of ICs that can convert the 7816 protocol to SPI or I2C so that I can make use of the respective GPIO pins on the Pi?

gregeric
Posts: 1509
Joined: Mon Nov 28, 2011 10:08 am

Re: Interfacing a ISO 7816 compliant smart card with the RPi

Fri May 05, 2017 7:05 am

You could use Joan's pigpio library to bit-bang the serial i/o, generate the clock, & do the card detect. Your hardware will then be reduced to level shifting (if needed to 5V) & the Schottky diode to combine the Tx/Rx lines.

Edit: looks like pigpio bitbanging serial only does read, not write? http://abyz.co.uk/rpi/pigpio/cif.html#g ... alReadOpen

A google search quickly finds i2c/spi to UART bridges.

faiz180490
Posts: 8
Joined: Sun Dec 11, 2016 11:51 am

Re: Interfacing a ISO 7816 compliant smart card with the RPi

Fri May 05, 2017 8:03 am

Ah Yes! I was looking into the pigpio library but I am little skeptical of the "bit banging". I haven't tried it before but it seems that people think that its unreliable. But looks like I might end up trying this after all.

This might be a stupid question, but you mentioned using a schottky diode to combine the Rx/Tx lines. May I know what exactly happens when I use the diode? I also saw in some other document where the Rx/Tx lines were combined using a 4.7Kohm resistor. How is this different from the diode? This is more of an electrical/electronics question. But I would really appreciate it if you could answer. As far as I understand, it has to do with pulling one of the pins high and the other low with respect to what is being done, reception or transmission. I am right?

gregeric
Posts: 1509
Joined: Mon Nov 28, 2011 10:08 am

Re: Interfacing a ISO 7816 compliant smart card with the RPi

Fri May 05, 2017 8:16 am

As I said in my edit, it seems that pigpio is missing a bitbang Tx capability.

Serial comms in ISO7816 is half-duplex on one i/o line with an open collector Tx line wired to the Rx line. The Pi doesn't have open collector outputs, so the diode is used to simulate that: cathode to the Pi Tx line, anode to the Rx. You'll need a pullup resistor on the Rx pin too, either the gpio internal one or external resistor.

Without the diode, while the Pi's Tx line is idling high, any time the card pulled the line low you'd have a high connected to a low - not good.

Ah yes, a resistor would be OK too, serving as the pullup when idling high and pulling the line down when transmitting.

faiz180490
Posts: 8
Joined: Sun Dec 11, 2016 11:51 am

Re: Interfacing a ISO 7816 compliant smart card with the RPi

Sun May 07, 2017 2:43 am

Thanks for the help. I'll probably try the bit banging technique and see where that leads me.

Return to “Interfacing (DSI, CSI, I2C, etc.)”