原文出自 http://hilbert-space.de/?p=135
While laying out a PCB for my SWP reader project I realized that I haven’t ever tested the ISO-7816 (aka contact) interface yet. I probably forgot that because it’s not all that difficult and not that interesting, but I’d rather see it working before I order PCBs.
So I spend an hour or two in the internet looking for inspiration how other people did it. There are lots of specialized chips for this purpose out there, but sourcing is always a problem and since it’s “just” a simple serial interface I was more interested in a simple hack that will work.
Turns out there are a lot of simple SIM/Smart-card readers out there that just do this, and they pretty much all look like this:
Here Q1 is running as a open collector driver with R1 as a pull-up resistor. This transistor will invert the signal, that’s why there is an additional inverter A1 in front of the base. R2 and C1 are the usual base-resistor and speed-up capacitor.
You’ll find variations of this basic circuit all over the net. Sometimes they omit the speed-up capacitor, sometimes you find buffers in the RX-UART path, but that’s it basically.
I heated up my soldering iron and gave this circuit a test drive, and lo and behold: It works as expected (aka good enough in practice).
So problem solved, move on.
Not so. In the middle of the night it came to me, that almost the entire circuit is unnecessary. What does it really do? On the Q1 collector we see a replica of the TX-UART signal. The SIM card IO pin (which is just an open collector IO-pin) is able to pull the signal down at will without causing a short to TX-UART.
RX-UART picks up this signal and echoes back either what comes from TX-UART or from IO. Neither pin is pulling up the signal, that’s what R1 is doing.
So how about this:
It’s working just the same, just faster and with less parts.
In case that TX-UART is transmitting, and IO is listening the singal will just pass R1. If TX-UART stops transmitting the UART will go into idle-state (logic high). This effectively ties R1 to VCC and we have exactly the same behaviour as with the more “complex” circuit.
If the SIM transmit something SIM-IO just pulls the line down to ground. A bit of current will flow out of TX-UART, but that’s fine. Compared to drive a LED from a GPIO pin that’s nothing.
If you feel inspired to try this out, here is a short how-to:
- Configure the UART on the micro-controller side for 9600 baud, 8 data bits, two stop-bits and even parity.
- Power the SIM VCC pin, have reset low.
- Apply a clock signal 372 times the UART baud-rate: 3.57 Mhz.
- Wait a little for the SIM/SmartCard to stabilize.
- Raise the reset line to taking the SIM-card out of reset.
And then watch the Answer to Rest (ATR) signature arriving at your micro-controller UART-RX pin. You’re now ready to implement the ISO7816-3 T=0 or T=1 protocol and do some real data-exchange. With practically any micro-controller and just a simple resistor.
Oh, by the way. You’re allowed to let the IO pin of the SIM to pull down up to 500µA, so if you get problems with stray capacitance just lower R1. Minimum values are:
- 3.6k for 1.8V supply
- 6.6k for 3.3V supply
- 10k for 5V supply.