ST AN2284 Application note

Introduction
The main purpose of this software and hardware package is to provide resources facilitating the development of an application using the Smart Card Peripheral.
The software interface is composed of library source files and some application template source files.
AN2284
APPLICATION NOT E
Smart Card Interface
with the STR71xx
Rev 1.0
AN2284/0106 1/40
www.st.com
40
AN2284
Contents
1 File Organization Of The Smart Card Library . . . . . . . . . . . . . . . . . . . . . . . 6
2 Smart Card Reader Using STR710: Overview . . . . . . . . . . . . . . . . . . . . . . . 7
3 Device Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3.1 STR710 Microcontroller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
4 SC Peripheral Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
4.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
4.2 External Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
4.3 Protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
4.4 Smart Card Clock Generator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11
5 SC Reader: Basic Hw For 5v Card . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
6 SC ISO7816: Protocol Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
6.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
6.2 ISO 7816-2 - Pins Location . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
6.2.1 Pin Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
7 ISO 7816-3 - Electronic Signal and Transmission Protocol . . . . . . . . . . . 15
7.0.1 Card Power-on and Reset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
7.0.2 Data Transfer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
7.0.3 Answer to Reset: ATR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
7.1 ISO 7816-4 - Smart Card Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
7.1.1 The T0 Protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
7.2 Application Level Protocols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
7.2.1 The ISO 7816-4 APDU . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
7.2.2 The File System API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
7.2.3 The ISO 7816-4 Funct ions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
7.2.4 The Security API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
8 SC Reader Lib: Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
8.1 SC parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
2/40
AN2284
8.1.1 SC_Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
8.1.2 SC_State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
8.1.3 CmdStatus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
8.2 How to Send ADPU Commands to SC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
8.2.1 SC_SELECT_FILE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
8.2.2 SC_GET_A2R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
8.2.3 SC_GET_RESPONCE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
8.2.4 SC_READ_BINARY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
8.2.5 SC_CREATE_FILE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
8.2.6 SC_UPDATE_BINARY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
8.2.7 SC_VERIFY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
9 SC Reader Lib: Parity Error Management . . . . . . . . . . . . . . . . . . . . . . . . . 34
9.1 Data Sent From Card to Reader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
9.2 Data Sent From Reader to Card . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
10 SC Reader Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
10.1 Firmware Package Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
10.2 Firmware Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
10.2.1 Smart Card Start Up: Answer to Reset (A2R) . . . . . . . . . . . . . . . . . . . . . . . . 36
10.2.2 Readi ng a File at aSpecified Path . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
10.2.3 Writing a File at a Specified Path . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
10.2.4 Creating a File at a Specified Path . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
10.2.5 Creating a File at a Specified Path With Pin Protection . . . . . . . . . . . . . . . . 37
11 Revision History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
3/40
AN2284
Figures
Figure 1. ISO 7816-3 Asynchronous Protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Figure 2. Basic Hardware For 5V Card Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Figure 3. Contact Definitions for Smart Cards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Figure 4. Reader and Card FS M . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Figure 5. Answer to Reset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Figure 6. Application Communication Architecture. . . . . . . . . . . . . . . . . . . . . . . . . . 21
Figure 7. The Comm and APDU Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Figure 8. The Response APDU Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Figure 9. The Smart Card File System Architecture. . . . . . . . . . . . . . . . . . . . . . . . . 23
Figure 10. State Machine for Smart Card Operation . . . . . . . . . . . . . . . . . . . . . . . . . 30
Figure 11. Firmware Package: Directory Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Figure 12. Smart Card Example: File System Description. . . . . . . . . . . . . . . . . . . . . 36
4/40
AN2284
Tables
Table 1. File Library Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Table 2. Smart Card Pins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Table 3. Pin Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Table 4. The Answer-to-Reset Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Table 5. CLA Instruction Set Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Table 6. ISO 7816-4 INS Codes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Tabl e 7 . Firmware P a ckage : D i r e c tories Descr iptio n . . . . . . . . . . . . . . . . . . . . . . . 35
5/40

1 File Organization Of The Smart Car d Libr ary AN2284

1 File Organization Of The Sm ar t Card Library
The following table presents the library modules:

Table 1. File Library Description

File Description Device_SCR.h Smart Card definiti ons, types definitions and function prototypes Device_SCR.c T0 protocol management sc.h,sc.c Physical layer
6/40

AN2284 2 Smart Card Reader Using STR710: Overv iew

2 Smart Car d R ea d er Usi ng STR710: Over view
A smart card reader was developed using the STR710 ARM7TDMI powered microcon troller and a basic HW to interface 5V powered smart card.
The Smart Card Library was developed in order to support ISO7816-3/4 specification.
7/40
3 Device Description AN2284

3 Device Description

3.1 STR710 Microcontroller

Here is a brief description of the STR710 microcontroller. Please refer to the STR710 datasheet for a detailed description.
Memories
- 272KB (256+16K) FLASH program memory (100,000 cycles endurance, data retention 20 years)
- 64KB RAM
- External Memory Interface (EMI) for up to 4 banks of SRAM, Flash, ROM
- Multi-boot capability
Clock, Reset and Supply Management
- 3.3V application supply and I/O interface
- Embedded 1.8V voltage regulator for core supply
- 0 to 16MHz external main oscillator
- 32kHz external backup oscillator
- Internal PLL for CPU clock
- Up to 50MHz CPU operating frequency when executing from flash
- Realtime Clock for clock-calendar function
- 4 power saving modes: SLOW, WAIT, STOP and STANDBY modes
Nested interrupt controller
- Fast interrupt handling with multiple vectors
- 32 vectors with 16 IRQ priority levels
- 2 maskable FIQ sources
Up to 48 I/O ports
- 30/32/48 multifunctional bidirectional I/O
- 14 ports with interrupt capability
5 Timers
- 16-bit watchdog timer Four 16-bit timers each with: 2 input captures, 2 output compares, PWM and pulse counter
10 Communications Interfaces
- 2 I2C interfaces (1 multiplexed with SPI)
- 4 UART asynchronous serial communications interfaces
- Smart Card ISO7816-3 interface on UART1
- 2 BSPI synchronous serial interfaces
- CAN interface (2.0B Active)
8/40
AN2284 3 Device Description
- USB v 2.0 Full Speed (12Mbit/s) Device Function with Suspend and Resume support
- HDLC synchronous communications interface
4-channel 12-bit A/D Converter
- Conversion time:
- 4 channels: up to 500Hz (2 ms)
- 1 channel: up to 1kHz (1 ms)
- Conversion range: 0 to 2.5V
Development Tools Support
- JTAG with debug mode trigger request
9/40

4 SC Peripheral Descript ion AN2284

4 SC Perip h er al Description

4.1 Introduction

The SmartCard Interface is an extension of UART1; for the description of the UART registers, please refer to STR710 datasheet. The SmartCard interface is designed to support asynchronous protocol SmartCards as defined in the ISO7816-3 standard.
With SmartCard mode enabled, UART1 is configured as:
- eight data bits plus parity
- 0.5 or 1.5 stop bits A 16 bit counter and the SmartCard clock generator provide the clock to the SmartCard. GPIO
bits in conjunction with software are used to provide the rest of the functions required to interface to the SmartCard.
The inverse signalling convention as defined in ISO7816-3, inverted data and MSB first, is handled in the software.

4.2 External Interface

Table 2. Smart Card Pins

Pin Number Pin Name Function
P0.12 SCCIk Smart card clock P0.10 I/O I/O serial data: open drain @ both ends Any GPIO RST Rese t to card
VCC Supply voltage Vpp Programming voltage
The ScRST, ScCmdVpp (command for Vpp), ScCmdVcc (command for Vcc), and ScDetect signals (signal for card detection) are provided by GPIO bits of the IO ports under software control. Programming the GPIO bits of the port for alternate function modes connects the UART TXD data signal to the ScDataOut pin with the correct driver type and the clock generator to the ScClk pin.

4.3 Protocol

The ISO standard defines the bit times for the asynchronous protocol in terms of a time unit called an ETU which is related to the clock frequency input to the card. One bit time is of length one ETU. The UART transmitter output and receiver input need to be connected together externally. F or the transmission of data from the STR71x to the SmartCard, the UART will need to be set up in SmartCard mode.
10/40
AN2284 4 SC Peripheral Descr iption

Figure 1. ISO 7816-3 Asynchronous Protocol

4.4 Smart Card Clock Generator

The SmartCard clock generator provides a clock signal to the connected SmartCard. The SmartCard uses this clock to derive the baud rate clock for the serial I/O between the SmartCard and another UART. The clock is also used for the CPU in the card, if present.
Operation of the Smart Card interface requires that the clock rate to the card is adjusted while the CPU in the card is running code so that the baud rate can be changed or the performance of the card can be increased. The protocols that govern the negotiation of these clock rates and the altering of the clock rate are detailed in ISO7816-3 standard.
The clock is used as the CPU clock for the Smart Card, therefore updates to the microcontroller clock rate must be synchronized to the SmartCard clock, i.e. the clock high or low pulse widths must not be shorter than either the old or new programmed value.
11/40

5 SC Reader: Basic Hw For 5v Card AN2284

5 SC Reader: Basic Hw For 5v Card
Some adjustments are needed when using a 5V powered smart card. Figure 2. shows how to interface a 5V smart card with the microcontroller (STR710):

Figure 2. Basic Hardware For 5V Card Interface

12/40
Loading...
+ 28 hidden pages