Introduction

Smart Card Library
Contents | Index | Reference
Next
Introduction

Smart Card ISO-7816 Library

For

8,16 & 32 bit PIC Microcontrollers

The Smart Card library for PIC microcontrollers implements the ISO 7816-3 and ISO 7816-4 standard protocols. It allows the PIC microcontroller to communicate with smart cards compatible with these protocols. The library supports T=0 and T=1 smart card protocols.

The library comprises of PIC18/PIC24/dsPIC33F/PIC32 UART driver and T0/T1 protocol source code meeting ISO 7816-3 standard. An example high level code is also provided to help the user port the smart card library to different hardware boards and different microcontrollers of PIC family.

This document assumes that the reader has understanding of ISO 7816-3 standards and T=0/T=1 protocols.

Introduction
Microchip Smart Card Library 1.02.2 - [July 14, 2011]
Copyright © 2011 Microchip Technology, Inc. All rights reserved.


SW License Agreement

Contents | Index | Reference | Home
Previous | Next
SW License Agreement

MICROCHIP IS WILLING TO LICENSE THE ACCOMPANYING SOFTWARE AND DOCUMENTATION TO YOU ONLY ON THE CONDITION THAT YOU ACCEPT ALL OF THE FOLLOWING TERMS. TO ACCEPT THE TERMS OF THIS LICENSE, CLICK "I ACCEPT" AND PROCEED WITH THE DOWNLOAD OR INSTALL. IF YOU DO NOT ACCEPT THESE LICENSE TERMS, CLICK "I DO NOT ACCEPT," AND DO NOT DOWNLOAD OR INSTALL THIS SOFTWARE. BY DOWNLOADING AND INSTALLING THE SOFTWARE, LICENSEE AGREES TO BE BOUND BY THE TERMS OF THIS AGREEMENT.

NON-EXCLUSIVE SOFTWARE LICENSE AGREEMENT FOR ACCOMPANYING MICROCHIP SOFTWARE AND DOCUMENTATION

This Nonexclusive Software License Agreement ("Agreement") is a contract between you, your heirs, successors and assigns ("Licensee") and Microchip Technology Incorporated, a Delaware corporation, with a principal place of business at 2355 W. Chandler Blvd., Chandler, AZ 85224-6199, and its subsidiary, Microchip Technology (Barbados) II Incorporated (collectively, "Microchip") for the accompanying Microchip software including, but not limited to, Graphics Library Software, IrDA Stack Software, MCHPFSUSB Stack Software, Memory Disk Drive File System Software, mTouch(TM) Capacitive Library Software, Smart Card Library Software, TCP/IP Stack Software, MiWi(TM) DE Software, and/or any PC programs and any updates thereto (collectively, the "Software"), and accompanying documentation, including images and any other graphic resources provided by Microchip ("Documentation").

The Software and Documentation are licensed under this Agreement and not sold. U.S. copyright laws, international copyright treaties, and other intellectual property laws and treaties protect the Software and Documentation. Microchip reserves all rights not expressly granted to Licensee in this Agreement.

(1) Definitions. As used in this Agreement, the following capitalized terms will have the meanings defined below:

a. "Microchip Products" means Microchip microcontrollers and Microchip digital signal controllers.

b. "Licensee Products" means Licensee products that use or incorporate Microchip Products.

c. "Object Code" means the Software computer programming code that is in binary form (including related documentation, if any), and error corrections, improvements, modifications, and updates.

d. "Source Code" means the Software computer programming code that may be printed out or displayed in human readable form (including related programmer comments and documentation, if any), and error corrections, improvements, modifications, and updates.

e. "Third Party" means Licensee’s agents, representatives, consultants, clients, customers, or contract manufacturers.

f. "Third Party Products" means Third Party products that use or incorporate Microchip Products.

(2) Software License Grant. Microchip grants strictly to Licensee a non-exclusive, non-transferable, worldwide license to:

a. use the Software in connection with Licensee Products and/or Third Party Products;

b. if Source Code is provided, modify the Software, provided that no Open Source Components (defined in Section 5 below) are incorporated into such Software in such a way that would affect Microchip’s right to distribute the Software with the limitations set forth herein and provided that Licensee clearly notifies Third Parties regarding such modifications;

c. distribute the Software to Third Parties for use in Third Party Products, so long as such Third Party agrees to be bound by this Agreement (in writing or by "click to accept") and this Agreement accompanies such distribution;

d. sublicense to a Third Party to use the Software, so long as such Third Party agrees to be bound by this Agreement (in writing or by "click to accept");

e. with respect to the TCP/IP Stack Software, Licensee may port the ENC28J60.c, ENC28J60.h, ENCX24J600.c, and ENCX24J600.h driver source files to a non-Microchip Product used in conjunction with a Microchip ethernet controller;

f. with respect to the MiWi (TM) DE Software, Licensee may only exercise its rights when the Software is embedded on a Microchip Product and used with a Microchip radio frequency transceiver or UBEC UZ2400 radio frequency transceiver which are integrated into Licensee Products or Third Party Products.

For purposes of clarity, Licensee may NOT embed the Software on a non-Microchip Product, except as described in this Section.

(3) Documentation License Grant. Microchip grants strictly to Licensee a non-exclusive, non-transferable, worldwide license to use the Documentation in support of Licensee's authorized use of the Software

(4) Third Party Requirements. Licensee acknowledges that it is Licensee’s responsibility to comply with any third party license terms or requirements applicable to the use of such third party software, specifications, systems, or tools. Microchip is not responsible and will not be held responsible in any manner for Licensee’s failure to comply with such applicable terms or requirements.

(5) Open Source Components. Notwithstanding the license grant in Section 1 above, Licensee further acknowledges that certain components of the Software may be covered by so-called "open source" software licenses ("Open Source Components"). Open Source Components means any software licenses approved as open source licenses by the Open Source Initiative or any substantially similar licenses, including without limitation any license that, as a condition of distribution of the software licensed under such license, requires that the distributor make the software available in source code format. To the extent required by the licenses covering Open Source Components, the terms of such license will apply in lieu of the terms of this Agreement, and Microchip hereby represents and warrants that the licenses granted to such Open Source Components will be no less broad than the license granted in Section 1. To the extent the terms of the licenses applicable to Open Source Components prohibit any of the restrictions in this Agreement with respect to such Open Source Components, such restrictions will not apply to such Open Source Component.

(6) Licensee Obligations.

a. Licensee will ensure Third Party compliance with the terms of this Agreement, and will be liable for any breach of this Agreement committed by such Third Party.

b. Licensee will not: (i) engage in unauthorized use, modification, disclosure or distribution of Software or Documentation, or its derivatives; (ii) use all or any portion of the Software, Documentation, or its derivatives except in conjunction with Microchip Products or Third Party Products; or (iii) reverse engineer (by disassembly, decompilation or otherwise) Software or any portion thereof.

c. Licensee may not remove or alter any Microchip copyright or other proprietary rights notice posted in any portion of the Software or Documentation.

d. Licensee will defend, indemnify and hold Microchip and its subsidiaries harmless from and against any and all claims, costs, damages, expenses (including reasonable attorney's fees), liabilities, and losses, including without limitation product liability claims, directly or indirectly arising from or related to: (i) the use, modification, disclosure or distribution of the Software, Documentation, or any intellectual property rights related thereto; (ii) the use, sale and distribution of Licensee Products or Third Party Products; and (iii) breach of this Agreement. THIS SECTION 3(d) STATES THE SOLE AND EXCLUSIVE LIABILITY OF THE PARTIES FOR INTELLECTUAL PROPERTY INFRINGEMENT.

(7) Confidentiality. Licensee agrees that the Software (including but not limited to the Source Code, Object Code and library files) and its derivatives, Documentation and underlying inventions, algorithms, know-how and ideas relating to the Software and the Documentation are proprietary information belonging to Microchip and its licensors ("Proprietary Information"). Except as expressly and unambiguously allowed herein, Licensee will hold in confidence and not use or disclose any Proprietary Information and will similarly bind its employees and Third Party(ies) in writing. Proprietary Information will not include information that: (i) is in or enters the public domain without breach of this Agreement and through no fault of the receiving party; (ii) the receiving party was legally in possession of prior to receiving it; (iii) the receiving party can demonstrate was developed by the receiving party independently and without use of or reference to the disclosing party's Proprietary Information; or (iv) the receiving party receives from a third party without restriction on disclosure. If Licensee is required to disclose Proprietary Information by law, court order, or government agency, License will give Microchip prompt notice of such requirement in order to allow Microchip to object or limit such disclosure. Licensee agrees that the provisions of this Agreement regarding unauthorized use and nondisclosure of the Software, Documentation and related Proprietary Rights are necessary to protect the legitimate business interests of Microchip and its licensors and that monetary damage alone cannot adequately compensate Microchip or its licensors if such provisions are violated. Licensee, therefore, agrees that if Microchip alleges that Licensee or Third Party has breached or violated such provision then Microchip will have the right to petition for injunctive relief, without the requirement for the posting of a bond, in addition to all other remedies at law or in equity.

(8) Ownership of Proprietary Rights. Microchip and its licensors retain all right, title and interest in and to the Software and Documentation ("Proprietary Rights") including, but not limited to all patent, copyright, trade secret and other intellectual property rights in the Software, Documentation, and underlying technology and all copies and derivative works thereof (by whomever produced). Further, copies and derivative works will be considered works made for hire with ownership vesting in Microchip on creation. To the extent such modifications and derivatives do not qualify as a "work for hire," Licensee hereby irrevocably transfers, assigns and conveys the exclusive copyright thereof to Microchip, free and clear of any and all liens, claims or other encumbrances, to the fullest extent permitted by law. Licensee and Third Party use of such modifications and derivatives is limited to the license rights described in Sections this Agreement.

(9) Termination of Agreement. Without prejudice to any other rights, this Agreement terminates immediately, without notice by Microchip, upon a failure by Licensee or Third Party to comply with any provision of this Agreement. Upon termination, Licensee and Third Party will immediately stop using the Software, Documentation, and derivatives thereof, and immediately destroy all such copies.

(10) Warranty Disclaimers. THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. MICROCHIP AND ITS LICENSORS ASSUME NO RESPONSIBILITY FOR THE ACCURACY, RELIABILITY OR APPLICATION OF THE SOFTWARE OR DOCUMENTATION. MICROCHIP AND ITS LICENSORS DO NOT WARRANT THAT THE SOFTWARE WILL MEET REQUIREMENTS OF LICENSEE OR THIRD PARTY, BE UNINTERRUPTED OR ERROR-FREE. MICROCHIP AND ITS LICENSORS HAVE NO OBLIGATION TO CORRECT ANY DEFECTS IN THE SOFTWARE. LICENSEE AND THIRD PARTY ASSUME THE ENTIRE RISK ARISING OUT OF USE OR PERFORMANCE OF THE SOFTWARE AND DOCUMENTATION PROVIDED UNDER THIS AGREEMENT.

(11) Limited Liability. IN NO EVENT WILL MICROCHIP OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER LEGAL OR EQUITABLE THEORY FOR ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES INCLUDING BUT NOT LIMITED TO INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS. The aggregate and cumulative liability of Microchip and its licensors for damages hereunder will in no event exceed $1000 or the amount Licensee paid Microchip for the Software and Documentation, whichever is greater. Licensee acknowledges that the foregoing limitations are reasonable and an essential part of this Agreement.

LICENSEE ACKNOWLEDGES AND AGREES THAT IT IS SOLELY RESPONSIBLE FOR THE LICENSEE PRODUCTS AND THIRD PARTY PRODUCTS, INCLUDING DETERMINING WHETHER SUCH PRODUCTS INFRINGE A PATENT, COPYRIGHT OR OTHER PROPRIETARY RIGHT OF ANY THIRD PARTY. LICENSEE AGREES THAT MICROCHIP HAS NO OBLIGATION TO INDEMNIFY OR DEFEND LICENSEE IN THE EVENT THAT A THIRD PARTY MAKES A CLAIM REGARDING LICENSEE PRODUCTS OR THIRD PARTY PRODUCTS.

(12) General. THIS AGREEMENT WILL BE GOVERNED BY AND CONSTRUED UNDER THE LAWS OF THE STATE OF ARIZONA AND THE UNITED STATES WITHOUT REGARD TO CONFLICTS OF LAWS PROVISIONS. Licensee agrees that any disputes arising out of or related to this Agreement, Software or Documentation will be brought in the courts of the State of Arizona. The parties agree to waive their rights to a jury trial in actions relating to this Agreement. If either the Microchip or Licensee employs attorneys to enforce any rights arising out of or relating to this Agreement, the prevailing party will be entitled to recover its reasonable attorneys' fees, costs and other expenses. This Agreement will constitute the entire agreement between the parties with respect to the subject matter hereof. It will not be modified except by a written agreement signed by an authorized representative of the Microchip. Microchip’s authorized representatives will have the right to reasonably inspect Licensee's premises and to audit Licensee's records and inventory of Licensee Products in order to ensure Licensee's adherence to the terms of this Agreement. If any provision of this Agreement will be held by a court of competent jurisdiction to be illegal, invalid or unenforceable, that provision will be limited or eliminated to the minimum extent necessary so that this Agreement will otherwise remain in full force and effect and enforceable. No waiver of any breach of any provision of this Agreement will constitute a waiver of any prior, concurrent or subsequent breach of the same or any other provisions hereof, and no waiver will be effective unless made in writing and signed by an authorized representative of the waiving party. Licensee agrees to comply with all export laws and restrictions and regulations of the Department of Commerce or other United States or foreign agency or authority. The indemnities, obligations of confidentiality, and limitations on liability described herein, and any right of action for breach of this Agreement prior to termination, will survive any termination of this Agreement. Neither this Agreement nor any rights, licenses or obligations hereunder, may be assigned by Licensee without the prior written approval of Microchip except pursuant to a merger, sale of all assets of Licensee or other corporate reorganization, provided that assignee agrees in writing to be bound by the Agreement. Any prohibited assignment will be null and void. Use, duplication or disclosure by the United States Government is subject to restrictions set forth in subparagraphs (a) through (d) of the Commercial Computer-Restricted Rights clause of FAR 52.227-19 when applicable, or in subparagraph (c)(1)(ii) of the Rights in Technical Data and Computer Software clause at DFARS 252.227-7013, and in similar clauses in the NASA FAR Supplement. Contractor/manufacturer is Microchip Technology Inc., 2355 W. Chandler Blvd., Chandler, AZ 85224-6199.

If Licensee has any questions about this Agreement, please write to Microchip Technology Inc., 2355 W. Chandler Blvd., Chandler, AZ 85224-6199 USA. ATTN: Marketing.

Copyright (c) 2011 Microchip Technology Inc. All rights reserved.

License Rev. No. 03-060111

SW License Agreement


Release Notes

Previous | Next
Release Notes
Resource Usage (v1.02.2)
  • Resource Usage for PIC18
  • Resource Usage for PIC24F
  • Resource Usage - PIC24H
  • Resource Usage - dsPIC33F
  • Resource Usage - PIC32

Peripherals

Type/Use | Specific/Configurable | Polled/Interrupt | Limitations
UART | Select via Programming, Tx and Rx Signals | Polled | None
Card Power Output | Select via #define in SCpic18.h or SCpic24.h or SCpic32.h or SCdspic33f | Polled | Be able to source sufficient current to power the Smartcard
Card Reset Output | Select via #define in SCpic18.h or SCpic24.h or SCpic32.h or SCdspic33f | Polled | Totempole or Open Drain with pullup
Card Present Input | Select via #define in SCpic18.h or SCpic24.h or SCpic32.h or SCdspic33f | Polled | Input with Pullup
Clock Output | REFO Output | n/a | Clock Output to Card should be close to 4MHz (3.57MHz for exact Baud Rate, but not required)
Topics
Name | Description
Resource Usage - PIC18 | These tables specify the program memory, execution speed, RAM usage, and build requirements for PIC18 devices.
Resource Usage - PIC24F | These tables specify the program memory, execution speed, RAM usage, and build requirements PIC24F devices.
Resource Usage - PIC24H | These tables specify the program memory, execution speed, RAM usage, and build requirements PIC24H devices.
Resource Usage - dsPIC33F | These tables specify the program memory, execution speed, RAM usage, and build requirements dsPIC33F devices.
Resource Usage - PIC32 | These tables specify the program memory, execution speed, RAM usage, and build requirements PIC32 devices.
Release Notes


Resource Usage - PIC18

Previous | Up | Next
Resource Usage - PIC18

These tables specify the program memory, execution speed, RAM usage, and build requirements for PIC18 devices.

Program Memory (bytes)
Module | Optimization Level | Size
Smartcard Library (T=0) | None | 4K
Smartcard Library (T=0) | Enable All | 2.8K
Smartcard Library (T=0 & T=1) | None | 6.8K
Smartcard Library (T=0 & T=1) | Enable All | 4.9K
RAM Usage (bytes)
Module/Layer | Global | Stack | Heap
Smartcard Library(T=0) | 300 | Not available | None
Smartcard Library(T=0 & T=1) | 330 | Not available | None

Build Requirements

None

Release Notes > Resource Usage - PIC18


Resource Usage - PIC24F

Resource Usage - PIC24F

These tables specify the program memory, execution speed, RAM usage, and build requirements PIC24F devices.

Program Memory
Module |  |
Smartcard Library (T=0) | None | 2.7K
Smartcard Library (T=0) | -O1 | 1.9K
Smartcard Library (T=0) | -Os | 1.7K
Smartcard Library (T=0 & T=1) | None | 4.7K
Smartcard Library (T=0 & T=1) | -O1 | 3.2K
Smartcard Library (T=0 & T=1) | -Os | 2.9K
RAM Usage (bytes)
Module/Layer | Global | Stack | Heap
Smartcard Library (T=0) | 300 | Not available | None
Smartcard Library (T=0 & T=1) | 330 | Not available | None
Build Requirements

None

Release Notes > Resource Usage - PIC24F


Resource Usage - PIC24H

Resource Usage - PIC24H

These tables specify the program memory, execution speed, RAM usage, and build requirements PIC24H devices.

Program Memory
Module |  |
Smartcard Library (T=0) | None | 2.7K
Smartcard Library (T=0) | -O1 | 2K
Smartcard Library (T=0) | -Os | 1.7K
Smartcard Library (T=0 & T=1) | None | 4.7K
Smartcard Library (T=0 & T=1) | -O1 | 3.2K
Smartcard Library (T=0 & T=1) | -Os | 2.9K
RAM Usage (bytes)
Module/Layer | Global | Stack | Heap
Smartcard Library (T=0) | 300 | Not available | None
Smartcard Library (T=0 & T=1) | 330 | Not available | None
Build Requirements

None

Release Notes > Resource Usage - PIC24H


Resource Usage - dsPIC33F

Resource Usage - dsPIC33F

These tables specify the program memory, execution speed, RAM usage, and build requirements dsPIC33F devices.

Program Memory
Module |  |
Smartcard Library (T=0) | None | 2.7K
Smartcard Library (T=0) | -O1 | 2K
Smartcard Library (T=0) | -Os | 1.7K
Smartcard Library (T=0 & T=1) | None | 4.7K
Smartcard Library (T=0 & T=1) | -O1 | 3.2K
Smartcard Library (T=0 & T=1) | -Os | 2.9K
RAM Usage (bytes)
Module/Layer | Global | Stack | Heap
Smartcard Library (T=0) | 300 | Not available | None
Smartcard Library (T=0 & T=1) | 330 | Not available | None
Build Requirements

None

Release Notes > Resource Usage - dsPIC33F


Resource Usage - PIC32

Resource Usage - PIC32

These tables specify the program memory, execution speed, RAM usage, and build requirements PIC32 devices.

Program Memory
Module |  |
Smartcard Library (T=0) | None | 6.2K
Smartcard Library (T=0) | -O1 | 4.6K
Smartcard Library (T=0) | -Os | 4.1K
Smartcard Library (T=0 & T=1) | None | 10.3K
Smartcard Library (T=0 & T=1) | -O1 | 7.2K
Smartcard Library (T=0 & T=1) | -Os | 6.4K
RAM Usage (bytes)
Module/Layer | Global | Stack | Heap
Smartcard Library (T=0) | 300 | Not available | None
Smartcard Library (T=0 & T=1) | 330 | Not available | None
Build Requirements

None

Release Notes > Resource Usage - PIC32


Smartcard Library Overview

Previous | Next
Smartcard Library Overview

At the current time there are two communication protocols that are in general use :

  • T = 0 asynchronous half duplex character transmission
  • T = 1 asynchronous half duplex block transmission

The data transfers between the card and the terminal happens on the single wire I/O line.

Following the initial reset of the card after insertion, the card responds with a series of characters called the Answer to Reset, or ATR. This series of characters establishes the initial communication details, including the specific protocol, bit timing, and data transfer details for all subsequent communications. While subsequent data transfers can change certain communications parameters, the ATR establishes initial communications conditions.

The Clock Signal for Baud rate generation is provided to the card by the reader (terminal). The Smartcard default baudrate divider is 372, which produce 9600 bps when a clock signal of 3.57MHz is supplied to the card. Most Smartcards allow higher clock rates, so a simple 4MHz clock can be easily used. Using a 4MHz clock, the default baudrate comes out to be 10752 bps. The PICs UART is appropriately configured by the library, so the communication can be setup using the higher baudrate settings.

The Smartcard 7816-3 communications requires a 0.5 stop bit. This is important for the Receiver, as it must pull the I/O line low before the middle of the stop bit (10.5 bit time from start edge) in order to indicate error condition to the Transmitter. The receiver pulls the line low for 1 to 2 bit time (etu). The transmitter checks the I/O line at the end of stop bit, or 11 etu. If the transmitter detects the line low, it retransmit the previous data byte after at least 2 etu.

The uart peripheral in PIC micros sets Rx Ready and Transmitter Empty flags to true at 0.5 stop bit, which allows the implementation of the 7816-3 error detection and retransmission protocol possible.

For more details about smart card communication using PIC microcontrollers, please refer [image] AN1370 : Smart Card Communication Using PIC® MCUs

Topics
Name | Description
Library Architecture | The Smartcard Library has a modular design with separate files for the high level library code and the low level driver for UART for implementing the ISO7816-3/4 protocol.
How the Library Works | The current release of smart card library supports PIC18, PIC24F, dsPIC33F, PIC24H & PIC32MX microcontrollers.The smart card library provides the API necessary to communicate with the ISO7816-3/4 compliant Smartcard. The sequence of the API calls is as given below. SClib.h contains all the API's that are required by the main application to communicate with the smart card. The current release of smart card library supports both T=0 and T=1 protocol. ... //Initialize smart card stack SC_Initialize(); ... // Wait untill the card is inserted in the slot while( !SC_CardPresent() ) ... //After detecting the card, turn on... more
Smartcard Library Overview


Library Architecture

Library Architecture

The Smartcard Library has a modular design with separate files for the high level library code and the low level driver for UART for implementing the ISO7816-3/4 protocol.

Smartcard Library Overview > Library Architecture


How the Library Works

How the Library Works

The current release of smart card library supports PIC18, PIC24F, dsPIC33F, PIC24H & PIC32MX microcontrollers.The smart card library provides the API necessary to communicate with the ISO7816-3/4 compliant Smartcard. The sequence of the API calls is as given below. SClib.h contains all the API's that are required by the main application to communicate with the smart card. The current release of smart card library supports both T=0 and T=1 protocol.

...

//Initialize smart card stack

SC_Initialize();

...

// Wait untill the card is inserted in the slot

while( !SC_CardPresent() )

...

//After detecting the card, turn on the power to the card and process Answer-to-Reset

if( !SC_PowerOnATR() )

...

//Do protocol & parameter selection.Configure the desired baud rate

if( !SC_DoPPS(ppsString) )

...

//Execute Card Commands

//If T=1 card is inserted in the slot, execute T=1 commands

if(SC_T1ProtocolType())

{

if( !SC_TransactT1( &prologueField,apduData,&cardResponse ) )

}

//If T=0 card is inserted in the slot, execute T=0 commands

else if(SC_T0ProtocolType())

{

if( !SC_TransactT0( &cardCommand, &cardResponse, apduData ) )

}

...

...

// Shut Down the Card when there is nothing to do with it

SC_Shutdown();

...

Note :

1)For T=1 protocol "prologueField" buffer should contain the prologue field(NAD,PCB,LENGTH) that needs to be sent to Smart Card.Once the transaction is completed between the card & the micro, response from the card is stored in "cardResponse" buffer."apduData" points to the data buffer of the command as well as data response from the card.

2)For T=0 protocol "cardCommand" buffer should contain the command that needs to be sent to the Smart Card. Once the transaction is completed between the card & the micro, response from the card is stored in "cardResponse" buffer. "apduData" points to the data buffer of command as well as data response from the card.

Smartcard Library Overview > How the Library Works


Running the Demos

Previous | Next
Running the Demos

The smart card library has been tested successfully with the below hardware platforms:

  • HPC Explorer + PIC18F46J50 PIM + SC (Smart/Sim Card) PICTail Board
  • HPC Explorer + PIC18F87J50 PIM + SC (Smart/Sim Card) PICTail Board
  • PICDEM FS USB Board (PIC18F4550) + SC (Smart/Sim Card) PICTail Board
  • LPC USB Board + PIC18F14K50 + SC (Smart/Sim Card) PICTail Board
  • Exp 16 Board + PIC24FJ256GB110 + SC (Smart/Sim Card) PICTail Board
  • Exp 16 Board + PIC24HJ256GP610 + SC (Smart/Sim Card) PICTail Board
  • Exp 16 Board + PIC32MX795F512L + SC (Smart/Sim Card) PICTail Board
  • Exp 16 Board + dsPIC33FJ128MC710 + SC (Smart/Sim Card) PICTail Board

Note: Please refer the getting started document of smart card demo for further details of the hardware configurations.The getting started document is available in the “<Install Directory\Smart Card Demo” directory.

Communication between the interfacing device and smart card is done in the following order:-

  1. Insertion of the smart card in the slot.
  2. Detection of the smart card insertion by the microcontroller (interfacing device).
  3. Microcontroller does the cold reset of the smart card.
  4. Answer to Reset (ATR) response by the card.
  5. PPS exchange (if smart card supports it).
  6. Execution of the transaction(s) between the card & the interfacing device.
  7. Removal of the smart card from the slot.
  8. Detection of the smart card removal by the microcontroller.
  9. Deactivation of the contacts.

Smart card demo uses the selected hardware platform as a smart card reader.The demo has to be run in the debug mode of MPLAB IDE. The demo waits in the while(1) loop until the smart card is inserted in the smart card connector slot. Once the card is inserted in the slot, ATR (Answer to Reset) and PPS (Protocol & Parameter Selection) operations are performed.The commands are sent to the smart card as per the manual provided by the manufacturer. The smart card executes the sent command and replies back with the appropriate data to the microcontroller via the I/O line.The port pins connection b/w the micro & smart card is defined in "sc_config.h" file.

Example code for T=0 card :-

The demo executes the card commands namely SUBMIT CODE, SELECT FILE, READ RECORD & WRITE RECORD. The command list can be extended further as per the project requirement.

Example code for T=1 card :-

The demo executes the "Get CPLC(Card Production Life Cycle) data" command for T=1 java card. The command list can be extended further as per the smart card manual and the project requirement.

Running the Demos


Configuring the Library

Previous | Next
Configuring the Library

The current smartcard software library supports 8,16 & 32 bit PIC microcontrollers.The port pins connection b/w the micro & smart card is defined in "sc_config.h" file.If the user wants to connect the smart card signals to different port pins of the micro, then he or she needs to modify the pin mapping in “sc_config.h” file.

The demo uses the signal connections between the smart card & PIC micro port pins as per the below tables:-

S.No | Signal Name | PIC18F46J50 | PIC18F87J50 | PIC18F4550 | PIC18F14K50
1 | SIM_CARD_DET | RB1 | RB1 | RB1 | RB4
2 | SMART_CLK | RB2 | RC2 | RC2 | RC2
3 | SMART_I/O | RC6,RC7 | RC6,RC7 | RC6,RC7 | RB7,RB5
4 | SMART_RST | RB4 | RB4 | RB4 | RC1
5 | SMART_CARD_DET | RB3 | RB3 | RB3 | RB6
6 | SMART_VCC | RB0 | RB0 | RB0 | RC0
S.No | Signal Name | PIC24FJ256GB110 | PIC32MX795F512L | PIC24HJ256GP610 | dsPIC33FJ128MC710
1 | SIM_CARD_DET | RB1 [RP1] | RB1 | RB1 | RB1
2 | SMART_CLK | RB15 [RP29] | RD1 | RD1 | RD1
3 | SMART_I/O | RC4 [RPI41],RF2 [RP30] | RF2 , RF8 | RF2, RF3 | RF2, RF3
4 | SMART_RST | RE8 [RPI33] | RE8 | RA12 | RE8
5 | SMART_CARD_DET | RB0 | RB0 | RB0 | RB0
6 | SMART_VCC | RB9 [RP9] | RB9 | RB9 | RB9

SMART_CARD_DET/SIM_CARD_DET signals indicate the presence of Smart Card/Sim Card respectively to the microcontroller. Either of one between Smart Card & Sim Card has to be inserted in the Smart Card PICTail board. If both the cards are inserted at a time in the PICTail card, then the demo won’t work successfully.

Enabling the macro "SC_PROTO_T1" in “sc_config.h” file, will enable the smart card library to support both T=0 & T=1 cards.Disabling the macro "SC_PROTO_T1" in “sc_config.h” file, will enable the smart card library to support only T=0 cards.

Configuring the Library


Library API

Previous | Next
Library API
Files | Functions | Macros | Types | Variables

This section lists the API provided by Microchip Smartcard Library

Topics
Name | Description
Functions | The following table lists functions in this documentation.
Types | The following table lists types in this documentation.
Macros | The following table lists macros in this documentation.
Files | The following table lists files in this documentation.
Variables | The following table lists variables in this documentation.
Files
Name | Description
SClib.h | FileName: SClib.h Dependencies: See INCLUDES section Processor: PIC18, PIC24 Microcontrollers Hardware: This demo is natively intended to be used on Exp 16, LPC & HPC Exp board. This demo can be modified for use on other hardware platforms. Complier: Microchip C18 (for PIC18), C30 (for PIC24) Company: Microchip Technology, Inc. Software License Agreement: The software supplied herewith by Microchip Technology Incorporated (the “Company”) for its PIC® Microcontroller is intended and supplied to you, the Company’s customer, for use solely and exclusively on Microchip PIC Microcontroller products. The software is owned by the Company and/or its supplier, and is protected under... more
Functions
Name | Description
SC_CardPresent | This macro checks if card is inserted in the socket
SC_DoPPS | This function does the PPS exchange with the smart card & configures the baud rate of the PIC UART module as per the PPS response from the smart card.
SC_GetCardState | This function returns the current state of SmartCard
SC_Initialize | This function initializes the smart card library
SC_PowerOnATR | This function performs the power on sequence of the SmartCard and interprets the Answer-to-Reset data received from the card.
SC_Shutdown | This function Performs the Power Down sequence of the SmartCard
SC_TransactT0 | This function Sends/recieves the ISO 7816-4 compaliant APDU commands to the card.
SC_TransactT1 | This function Sends/recieves the ISO 7816-4 compaliant T = 1 commands to the card.
Macros
Name | Description
__SC_MCP_LIB__ | Smart Card Library
SC_ABORT_RESPONSE | PCB byte for Abort Response of T1 Protocol
SC_AUTHENTICATE | Authenticate Command code to the Smart Card
SC_BWI | DEFAULT Value of BWI Indicator used in calculation of BWT for T=1 protocol
SC_CHANGE_PIN | Change Pin Command code to the Smart Card
SC_CLEAR_CARD | Clear Card Command code to the Smart Card
SC_CRC_TYPE_EDC | Cyclic Redundancy Check(CRC) type is used for EDC in Epilogue Field
SC_CREDIT | Credit Command code to the Smart Card
SC_CWI | DEFAULT Value of CWI Indicator used in calculation of CWT for T=1 protocol
SC_DEBIT | Debit Command code to the Smart Card
SC_GET_RESPONSE | Get Response Command code to the Smart Card
SC_IFS_RESPONSE | PCB byte for IFS Response of T1 Protocol
SC_INQUIRE_ACCT | Inquire Account Command code to the Smart Card
SC_LRC_TYPE_EDC | Longitudnal Redundancy Check(LRC) type is used for EDC in Epilogue Field
SC_READ_RECORD | Read Record Command code to the Smart Card
SC_RESYNC_REQ | PCB byte for Resync Request of T1 Protocol
SC_REVOKE | Revoke Command code to the Smart Card
SC_SELECT_FILE | Select File Command code to the Smart Card
SC_START_SESSION | Start Session Command code to the Smart Card
SC_STATE_CARD_ACTIVE | Card is powered and ATR received
SC_STATE_CARD_INACTIVE | Card present but not powered
SC_STATE_CARD_NOT_PRESENT | No Card Detected
SC_SUBMIT_CODE | Submit Code Command to the Smart Card
SC_T0ProtocolType | Returns '1' if T=0 protocol is supported & Returns 0 otherwise
SC_T1ProtocolType | Returns '1' if T=1 protocol is supported & Returns 0 otherwise
SC_TA1Present | Returns '1' if TA1 present & Returns 0 otherwise
SC_TA2Present | Returns '1' if TA2 present & Returns 0 otherwise
SC_TB1Present | Returns '1' if TB1 present & Returns 0 otherwise
SC_TB2Present | Returns '1' if TB2 present & Returns 0 otherwise
SC_TC1Present | Returns '1' if TC1 present & Returns 0 otherwise
SC_TC2Present | Returns '1' if TC2 present & Returns 0 otherwise
SC_TD1Present | Returns '1' if TD1 present & Returns 0 otherwise
SC_TD2Present | Returns '1' if TD2 present & Returns 0 otherwise
SC_WAIT_TIME_EXT_RESPONSE | PCB byte for Wait Time Extension Response of T1 Protocol
SC_WI | DEFAULT Value of WI Indicator used in calculation of WWT for T=0 protocol
SC_WRITE_RECORD | Write Record Command code to the Smart Card
Types
Name | Description
SC_APDU_COMMAND | SmartCard APDU Command 7816-4
SC_APDU_RESPONSE | SmartCard APDU Response structure 7816-4
SC_ERROR | Smart Card error types
SC_T1_PROLOGUE_FIELD | Prologue Field for T=1 Protocol
T1BLOCK_TYPE | Block types in T=1 protocol
Variables
Name | Description
scATR_HistoryBuffer | Historical bytes sent by Smart Card
scATR_HistoryLength | Number of Historical bytes present
scATRLength | length of ATR data sent by smart card
scCardATR | ATR data sent by smartcard.
scLastError | Smart Card Error type is stored in this variable
scPPSresponse | PPS Response Bytes
scPPSresponseLength | Length of PPS Response
scTA1 | TA1 determines the clock-rate conversion factor F & bit-rate-adjustment factor D
scTA2 | TA2 determines whether the smart card will operate in specific mode or negotiable mode following the ATR
scTA3 | TA3 conveys the Information Field Size Integer (IFSI) for the smart card.
scTB1 | TB1 conveys information on the smart card's programming voltage requirements.
scTB2 | TB2 conveys PI2, which determines the value of programming voltage required by the smart card. The value of PI1 in TB1 is superceded when TB2 is present
scTB3 | TB3 indicates the value of the Character Waiting Time Integer (CWI) and Block Waiting Time Integer (BWI) used to compute the Character Waiting Time (CWT) and Block Waiting Time (BWT).
scTC1 | TC1 determines the extra guard time to be added between consecutive characters sent to the smart card from the terminal.
scTC2 | TC2 is specific to protocol type T=0. TC2 conveys work waiting-time integer (WI) that determines the maximum interval between the leading edge of the start bit of any character sent by the smart card and the leading edge of the start bit of the previous character sent either by the card or the reader
scTC3 | When TC3 is present, it indicates the type of block-error detection to be used. When TC3 is not present, the default longitudinal redundancy check (LRC) is used.
scTD1 | TD1 indicates if any further interface bytes are to be transmitted, and if so, which protocol will be used.
scTD2 | The TD2 character has the same function as the TD1 character.
scTD3 | TD3 indicates interface bytes similar to that of TD1 & TD2
Library API


Functions

Functions
Functions
Name | Description
SC_CardPresent | This macro checks if card is inserted in the socket
SC_DoPPS | This function does the PPS exchange with the smart card & configures the baud rate of the PIC UART module as per the PPS response from the smart card.
SC_GetCardState | This function returns the current state of SmartCard
SC_Initialize | This function initializes the smart card library
SC_PowerOnATR | This function performs the power on sequence of the SmartCard and interprets the Answer-to-Reset data received from the card.
SC_Shutdown | This function Performs the Power Down sequence of the SmartCard
SC_TransactT0 | This function Sends/recieves the ISO 7816-4 compaliant APDU commands to the card.
SC_TransactT1 | This function Sends/recieves the ISO 7816-4 compaliant T = 1 commands to the card.
Library API > Functions


SC_CardPresent Function

SC_CardPresent Function
C
BOOL SC_CardPresent();
Description

This macro checks if card is inserted in the socket

Preconditions

SC_Initialize() is called

Remarks

None

Library API > Functions > SC_CardPresent Function


SC_DoPPS Function

SC_DoPPS Function
C
BOOL SC_DoPPS(
    BYTE * ppsPtr
);
Description

This function does the PPS exchange with the smart card & configures the baud rate of the PIC UART module as per the PPS response from the smart card.

Preconditions

SC_PowerOnATR was success

Remarks

This function is called when SC_PowerOnATR() returns TRUE.

Library API > Functions > SC_DoPPS Function


SC_GetCardState Function

SC_GetCardState Function
C
int SC_GetCardState();
Description

This function returns the current state of SmartCard

Preconditions

SC_Initialize is called.

Return Values
Return Values | Description
SC_STATE_CARD_NOT_PRESENT | No Card Detected
SC_STATE_CARD_ACTIVE | Card is powered and ATR received
SC_STATE_CARD_INACTIVE | Card present but not powered
Remarks

None

Library API > Functions > SC_GetCardState Function


SC_Initialize Function

SC_Initialize Function
C
void SC_Initialize();
Description

This function initializes the smart card library

Preconditions

None

Remarks

None

Library API > Functions > SC_Initialize Function


SC_PowerOnATR Function

SC_PowerOnATR Function
C
BOOL SC_PowerOnATR();
Description

This function performs the power on sequence of the SmartCard and interprets the Answer-to-Reset data received from the card.

Preconditions

SC_Initialize() is called, and card is present

Remarks

None

Library API > Functions > SC_PowerOnATR Function


SC_Shutdown Function

SC_Shutdown Function
C
void SC_Shutdown();
Description

This function Performs the Power Down sequence of the SmartCard

Preconditions

SC_Initialize is called.

Remarks

None

Library API > Functions > SC_Shutdown Function


SC_TransactT0 Function

SC_TransactT0 Function
C
BOOL SC_TransactT0(
    SC_APDU_COMMAND* apduCommand,
    SC_APDU_RESPONSE* apduResponse,
    BYTE* apduDataBuffer
);
Description

This function Sends/recieves the ISO 7816-4 compaliant APDU commands to the card.

Preconditions

SC_DoPPS was success or SC_DoPPS functionality not called

Parameters
Parameters | Description
SC_APDU_COMMAND* apduCommand | Pointer to APDU Command Structure
SC_APDU_RESPONSE* pResp | Pointer to APDU Response structure
BYTE* pResp | Pointer to the Command/Response Data buffer
Remarks

In the APDU command structure, the LC field defines the number of data bytes to be transmitted to the card. This array can hold max of 256 bytes, which can be redefined by the user. The LE field in APDU command defines the number of bytes expected to be received from the card. This array can hold max 256 bytes, which can be redefined by the user.

Library API > Functions > SC_TransactT0 Function


SC_TransactT1 Function

SC_TransactT1 Function
C
BOOL SC_TransactT1(
    SC_T1_PROLOGUE_FIELD* pfield,
    BYTE* iField,
    SC_APDU_RESPONSE* apduResponse
);
Description

This function Sends/recieves the ISO 7816-4 compaliant T = 1 commands to the card.

Preconditions

SC_DoPPS was success

Parameters
Parameters | Description
SC_T1_PROLOGUE_FIELD* pfield | Pointer to Prologue Field
BYTE* iField | Pointer to the Information Field of Tx/Rx Data
SC_APDU_RESPONSE* apduResponse | Pointer to APDU Response structure
Library API > Functions > SC_TransactT1 Function


Types

Types
Enumerations
Name | Description
SC_ERROR | Smart Card error types
T1BLOCK_TYPE | Block types in T=1 protocol
Structures
Name | Description
SC_APDU_COMMAND | SmartCard APDU Command 7816-4
SC_APDU_RESPONSE | SmartCard APDU Response structure 7816-4
SC_T1_PROLOGUE_FIELD | Prologue Field for T=1 Protocol
Library API > Types


SC_APDU_COMMAND Structure

SC_APDU_COMMAND Structure
C
typedef struct {
  BYTE CLA;
  BYTE INS;
  BYTE P1;
  BYTE P2;
  BYTE LC;
  BYTE LE;
} SC_APDU_COMMAND;
Description

SmartCard APDU Command 7816-4

Members
Members | Description
BYTE CLA; | Command class
BYTE INS; | Operation code
BYTE P1; | Selection Mode
BYTE P2; | Selection Option
BYTE LC; | Data length
BYTE LE; | Expected length of data to be returned
Library API > Types > SC_APDU_COMMAND Structure


SC_APDU_RESPONSE Structure

SC_APDU_RESPONSE Structure
C
typedef struct {
  WORD RXDATALEN;
  BYTE SW1;
  BYTE SW2;
} SC_APDU_RESPONSE;
Description

SmartCard APDU Response structure 7816-4

Members
Members | Description
WORD RXDATALEN; | Recieved Data length from smart card(excluding SW1 and SW2 bytes)
BYTE SW1; | Status byte 1
BYTE SW2; | Status byte 2
Library API > Types > SC_APDU_RESPONSE Structure


SC_ERROR Enumeration

SC_ERROR Enumeration
C
typedef enum {
  SC_ERR_NONE,
  SC_ERR_CARD_NOT_SUPPORTED,
  SC_ERR_BAR_OR_NO_ATR_RESPONSE,
  SC_ERR_CARD_NOT_PRESENT,
  SC_ERR_CARD_NO_RESPONSE,
  SC_ERR_RECEIVE_LRC,
  SC_ERR_RECEIVE_CRC,
  SC_CARD_VPP_ERR,
  SC_ERR_ATR_DATA,
  SC_ERR_RSV1
} SC_ERROR;
Description

Smart Card error types

Members
Members | Description
SC_ERR_NONE | No Error
SC_ERR_CARD_NOT_SUPPORTED | Card Not Supported
SC_ERR_BAR_OR_NO_ATR_RESPONSE | No ATR Response from the card
SC_ERR_CARD_NOT_PRESENT | Card Not present in the slot
SC_ERR_CARD_NO_RESPONSE | No response from the card
SC_ERR_RECEIVE_LRC | LRC Error in the block recieved from the card
SC_ERR_RECEIVE_CRC | CRC Error in the block recieved from the card
SC_CARD_VPP_ERR | VPP Error recieved from the card
SC_ERR_ATR_DATA | ERROR in ATR data recieved from the card
SC_ERR_RSV1 | Smart Card Error 1 (Reserved) - can be used based upon the Application
Library API > Types > SC_ERROR Enumeration


SC_T1_PROLOGUE_FIELD Structure

SC_T1_PROLOGUE_FIELD Structure
C
typedef struct {
  BYTE NAD;
  BYTE PCB;
  BYTE LENGTH;
} SC_T1_PROLOGUE_FIELD;
Description

Prologue Field for T=1 Protocol

Members
Members | Description
BYTE NAD; | Node Address
BYTE PCB; | Protocol Control Byte
BYTE LENGTH; | LENGTH of I-Field
Library API > Types > SC_T1_PROLOGUE_FIELD Structure


T1BLOCK_TYPE Enumeration

T1BLOCK_TYPE Enumeration
C
typedef enum {
  I_BLOCK,
  R_BLOCK,
  S_BLOCK,
  INVALID_BLOCK
} T1BLOCK_TYPE;
Description

Block types in T=1 protocol

Members
Members | Description
I_BLOCK | I Block
R_BLOCK | R Block
S_BLOCK | S Block
INVALID_BLOCK | INVALID BLOCK
Library API > Types > T1BLOCK_TYPE Enumeration


Macros

Macros
Macros
Name | Description
__SC_MCP_LIB__ | Smart Card Library
SC_ABORT_RESPONSE | PCB byte for Abort Response of T1 Protocol
SC_AUTHENTICATE | Authenticate Command code to the Smart Card
SC_BWI | DEFAULT Value of BWI Indicator used in calculation of BWT for T=1 protocol
SC_CHANGE_PIN | Change Pin Command code to the Smart Card
SC_CLEAR_CARD | Clear Card Command code to the Smart Card
SC_CRC_TYPE_EDC | Cyclic Redundancy Check(CRC) type is used for EDC in Epilogue Field
SC_CREDIT | Credit Command code to the Smart Card
SC_CWI | DEFAULT Value of CWI Indicator used in calculation of CWT for T=1 protocol
SC_DEBIT | Debit Command code to the Smart Card
SC_GET_RESPONSE | Get Response Command code to the Smart Card
SC_IFS_RESPONSE | PCB byte for IFS Response of T1 Protocol
SC_INQUIRE_ACCT | Inquire Account Command code to the Smart Card
SC_LRC_TYPE_EDC | Longitudnal Redundancy Check(LRC) type is used for EDC in Epilogue Field
SC_READ_RECORD | Read Record Command code to the Smart Card
SC_RESYNC_REQ | PCB byte for Resync Request of T1 Protocol
SC_REVOKE | Revoke Command code to the Smart Card
SC_SELECT_FILE | Select File Command code to the Smart Card
SC_START_SESSION | Start Session Command code to the Smart Card
SC_STATE_CARD_ACTIVE | Card is powered and ATR received
SC_STATE_CARD_INACTIVE | Card present but not powered
SC_STATE_CARD_NOT_PRESENT | No Card Detected
SC_SUBMIT_CODE | Submit Code Command to the Smart Card
SC_T0ProtocolType | Returns '1' if T=0 protocol is supported & Returns 0 otherwise
SC_T1ProtocolType | Returns '1' if T=1 protocol is supported & Returns 0 otherwise
SC_TA1Present | Returns '1' if TA1 present & Returns 0 otherwise
SC_TA2Present | Returns '1' if TA2 present & Returns 0 otherwise
SC_TB1Present | Returns '1' if TB1 present & Returns 0 otherwise
SC_TB2Present | Returns '1' if TB2 present & Returns 0 otherwise
SC_TC1Present | Returns '1' if TC1 present & Returns 0 otherwise
SC_TC2Present | Returns '1' if TC2 present & Returns 0 otherwise
SC_TD1Present | Returns '1' if TD1 present & Returns 0 otherwise
SC_TD2Present | Returns '1' if TD2 present & Returns 0 otherwise
SC_WAIT_TIME_EXT_RESPONSE | PCB byte for Wait Time Extension Response of T1 Protocol
SC_WI | DEFAULT Value of WI Indicator used in calculation of WWT for T=0 protocol
SC_WRITE_RECORD | Write Record Command code to the Smart Card
Library API > Macros


__SC_MCP_LIB__ Macro

__SC_MCP_LIB__ Macro
C
#define __SC_MCP_LIB__
Description

Smart Card Library

Library API > Macros > __SC_MCP_LIB__ Macro


SC_ABORT_RESPONSE Macro

SC_ABORT_RESPONSE Macro
C
#define SC_ABORT_RESPONSE (BYTE)0xE2
Description

PCB byte for Abort Response of T1 Protocol

Library API > Macros > SC_ABORT_RESPONSE Macro


SC_AUTHENTICATE Macro

SC_AUTHENTICATE Macro
C
#define SC_AUTHENTICATE 0x82
Description

Authenticate Command code to the Smart Card

Library API > Macros > SC_AUTHENTICATE Macro


SC_BWI Macro

SC_BWI Macro
C
#define SC_BWI (BYTE)0x04
Description

DEFAULT Value of BWI Indicator used in calculation of BWT for T=1 protocol

Library API > Macros > SC_BWI Macro


SC_CHANGE_PIN Macro

SC_CHANGE_PIN Macro
C
#define SC_CHANGE_PIN 0x24
Description

Change Pin Command code to the Smart Card

Library API > Macros > SC_CHANGE_PIN Macro


SC_CLEAR_CARD Macro

SC_CLEAR_CARD Macro
C
#define SC_CLEAR_CARD 0x30
Description

Clear Card Command code to the Smart Card

Library API > Macros > SC_CLEAR_CARD Macro


SC_CRC_TYPE_EDC Macro

SC_CRC_TYPE_EDC Macro
C
#define SC_CRC_TYPE_EDC (BYTE)1
Description

Cyclic Redundancy Check(CRC) type is used for EDC in Epilogue Field

Library API > Macros > SC_CRC_TYPE_EDC Macro


SC_CREDIT Macro

SC_CREDIT Macro
C
#define SC_CREDIT 0xE2
Description

Credit Command code to the Smart Card

Library API > Macros > SC_CREDIT Macro


SC_CWI Macro

SC_CWI Macro
C
#define SC_CWI (BYTE)13
Description

DEFAULT Value of CWI Indicator used in calculation of CWT for T=1 protocol

Library API > Macros > SC_CWI Macro


SC_DEBIT Macro

SC_DEBIT Macro
C
#define SC_DEBIT 0xE6
Description

Debit Command code to the Smart Card

Library API > Macros > SC_DEBIT Macro


SC_GET_RESPONSE Macro

SC_GET_RESPONSE Macro
C
#define SC_GET_RESPONSE 0xC0
Description

Get Response Command code to the Smart Card

Library API > Macros > SC_GET_RESPONSE Macro


SC_IFS_RESPONSE Macro

SC_IFS_RESPONSE Macro
C
#define SC_IFS_RESPONSE (BYTE)0xE1
Description

PCB byte for IFS Response of T1 Protocol

Library API > Macros > SC_IFS_RESPONSE Macro


SC_INQUIRE_ACCT Macro

SC_INQUIRE_ACCT Macro
C
#define SC_INQUIRE_ACCT 0xE4
Description

Inquire Account Command code to the Smart Card

Library API > Macros > SC_INQUIRE_ACCT Macro


SC_LRC_TYPE_EDC Macro

SC_LRC_TYPE_EDC Macro
C
#define SC_LRC_TYPE_EDC (BYTE)0
Description

Longitudnal Redundancy Check(LRC) type is used for EDC in Epilogue Field

Library API > Macros > SC_LRC_TYPE_EDC Macro


SC_READ_RECORD Macro

SC_READ_RECORD Macro
C
#define SC_READ_RECORD 0xB2
Description

Read Record Command code to the Smart Card

Library API > Macros > SC_READ_RECORD Macro


SC_RESYNC_REQ Macro

SC_RESYNC_REQ Macro
C
#define SC_RESYNC_REQ (BYTE)0xC0
Description

PCB byte for Resync Request of T1 Protocol

Library API > Macros > SC_RESYNC_REQ Macro


SC_REVOKE Macro

SC_REVOKE Macro
C
#define SC_REVOKE 0xE8
Description

Revoke Command code to the Smart Card

Library API > Macros > SC_REVOKE Macro


SC_SELECT_FILE Macro

SC_SELECT_FILE Macro
C
#define SC_SELECT_FILE 0xA4
Description

Select File Command code to the Smart Card

Library API > Macros > SC_SELECT_FILE Macro


SC_START_SESSION Macro

SC_START_SESSION Macro
C
#define SC_START_SESSION 0x84
Description

Start Session Command code to the Smart Card

Library API > Macros > SC_START_SESSION Macro


SC_STATE_CARD_ACTIVE Macro

SC_STATE_CARD_ACTIVE Macro
C
#define SC_STATE_CARD_ACTIVE 20    // Card is powered and ATR received
Description

Card is powered and ATR received

Library API > Macros > SC_STATE_CARD_ACTIVE Macro


SC_STATE_CARD_INACTIVE Macro

SC_STATE_CARD_INACTIVE Macro
C
#define SC_STATE_CARD_INACTIVE 30    // Card present but not powered
Description

Card present but not powered

Library API > Macros > SC_STATE_CARD_INACTIVE Macro


SC_STATE_CARD_NOT_PRESENT Macro

SC_STATE_CARD_NOT_PRESENT Macro
C
#define SC_STATE_CARD_NOT_PRESENT 10    // No Card Detected
Description

No Card Detected

Library API > Macros > SC_STATE_CARD_NOT_PRESENT Macro


SC_SUBMIT_CODE Macro

SC_SUBMIT_CODE Macro
C
#define SC_SUBMIT_CODE 0x20
Description

Submit Code Command to the Smart Card

Library API > Macros > SC_SUBMIT_CODE Macro


SC_T0ProtocolType Macro

SC_T0ProtocolType Macro
C
#define SC_T0ProtocolType (((scTD1 & 0x0F) == 0x00)?TRUE:FALSE)
Description

Returns '1' if T=0 protocol is supported & Returns 0 otherwise

Library API > Macros > SC_T0ProtocolType Macro


SC_T1ProtocolType Macro

SC_T1ProtocolType Macro
C
#define SC_T1ProtocolType (((scTD1 & 0x0F) == 0x01)?TRUE:FALSE)
Description

Returns '1' if T=1 protocol is supported & Returns 0 otherwise

Library API > Macros > SC_T1ProtocolType Macro


SC_TA1Present Macro

SC_TA1Present Macro
C
#define SC_TA1Present ((scCardATR[1] & 0x10)?TRUE:FALSE)
Description

Returns '1' if TA1 present & Returns 0 otherwise

Library API > Macros > SC_TA1Present Macro


SC_TA2Present Macro

SC_TA2Present Macro
C
#define SC_TA2Present ((scTD1 & 0x10)?TRUE:FALSE)
Description

Returns '1' if TA2 present & Returns 0 otherwise

Library API > Macros > SC_TA2Present Macro


SC_TB1Present Macro

SC_TB1Present Macro
C
#define SC_TB1Present ((scCardATR[1] & 0x20)?TRUE:FALSE)
Description

Returns '1' if TB1 present & Returns 0 otherwise

Library API > Macros > SC_TB1Present Macro


SC_TB2Present Macro

SC_TB2Present Macro
C
#define SC_TB2Present ((scTD1 & 0x20)?TRUE:FALSE)
Description

Returns '1' if TB2 present & Returns 0 otherwise

Library API > Macros > SC_TB2Present Macro


SC_TC1Present Macro

SC_TC1Present Macro
C
#define SC_TC1Present ((scCardATR[1] & 0x40)?TRUE:FALSE)
Description

Returns '1' if TC1 present & Returns 0 otherwise

Library API > Macros > SC_TC1Present Macro


SC_TC2Present Macro

SC_TC2Present Macro
C
#define SC_TC2Present ((scTD1 & 0x40)?TRUE:FALSE)
Description

Returns '1' if TC2 present & Returns 0 otherwise

Library API > Macros > SC_TC2Present Macro


SC_TD1Present Macro

SC_TD1Present Macro
C
#define SC_TD1Present ((scCardATR[1] & 0x80)?TRUE:FALSE)
Description

Returns '1' if TD1 present & Returns 0 otherwise

Library API > Macros > SC_TD1Present Macro


SC_TD2Present Macro

SC_TD2Present Macro
C
#define SC_TD2Present ((scTD1 & 0x80)?TRUE:FALSE)
Description

Returns '1' if TD2 present & Returns 0 otherwise

Library API > Macros > SC_TD2Present Macro


SC_WAIT_TIME_EXT_RESPONSE Macro

SC_WAIT_TIME_EXT_RESPONSE Macro
C
#define SC_WAIT_TIME_EXT_RESPONSE (BYTE)0xE3
Description

PCB byte for Wait Time Extension Response of T1 Protocol

Library API > Macros > SC_WAIT_TIME_EXT_RESPONSE Macro


SC_WI Macro

SC_WI Macro
C
#define SC_WI (BYTE)0x0A
Description

DEFAULT Value of WI Indicator used in calculation of WWT for T=0 protocol

Library API > Macros > SC_WI Macro


SC_WRITE_RECORD Macro

SC_WRITE_RECORD Macro
C
#define SC_WRITE_RECORD 0xD2
Description

Write Record Command code to the Smart Card

Library API > Macros > SC_WRITE_RECORD Macro


Files

Files
Files
Name | Description
SClib.h | FileName: SClib.h Dependencies: See INCLUDES section Processor: PIC18, PIC24 Microcontrollers Hardware: This demo is natively intended to be used on Exp 16, LPC & HPC Exp board. This demo can be modified for use on other hardware platforms. Complier: Microchip C18 (for PIC18), C30 (for PIC24) Company: Microchip Technology, Inc. Software License Agreement: The software supplied herewith by Microchip Technology Incorporated (the “Company”) for its PIC® Microcontroller is intended and supplied to you, the Company’s customer, for use solely and exclusively on Microchip PIC Microcontroller products. The software is owned by the Company and/or its supplier, and is protected under... more
Library API > Files


SClib.h

SClib.h

FileName: SClib.h Dependencies: See INCLUDES section Processor: PIC18, PIC24 Microcontrollers Hardware: This demo is natively intended to be used on Exp 16, LPC & HPC Exp board. This demo can be modified for use on other hardware platforms. Complier: Microchip C18 (for PIC18), C30 (for PIC24) Company: Microchip Technology, Inc.

Software License Agreement:

The software supplied herewith by Microchip Technology Incorporated (the “Company”) for its PIC® Microcontroller is intended and supplied to you, the Company’s customer, for use solely and exclusively on Microchip PIC Microcontroller products. The software is owned by the Company and/or its supplier, and is protected under applicable copyright laws. All rights are reserved. Any use in violation of the foregoing restrictions may subject the user to criminal sanctions under applicable laws, as well as to civil liability for the breach of the terms and conditions of this license.

THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.

******************************************************************

File Description:

Change History: Rev Description

--- ----------------------------------------

1.0 Initial release 1.01 Cleaned up unnecessary variables,supported T=1 protocol and improvments in T=0 functions following the coding standards 1.03 1) Modifed PPS functionality API. 2) Modified the code in more structured way.

Enumerations
Name | Description
SC_ERROR | Smart Card error types
T1BLOCK_TYPE | Block types in T=1 protocol
Functions
Name | Description
SC_CardPresent | This macro checks if card is inserted in the socket
SC_DoPPS | This function does the PPS exchange with the smart card & configures the baud rate of the PIC UART module as per the PPS response from the smart card.
SC_GetCardState | This function returns the current state of SmartCard
SC_Initialize | This function initializes the smart card library
SC_PowerOnATR | This function performs the power on sequence of the SmartCard and interprets the Answer-to-Reset data received from the card.
SC_Shutdown | This function Performs the Power Down sequence of the SmartCard
SC_TransactT0 | This function Sends/recieves the ISO 7816-4 compaliant APDU commands to the card.
SC_TransactT1 | This function Sends/recieves the ISO 7816-4 compaliant T = 1 commands to the card.
Macros
Name | Description
__SC_MCP_LIB__ | Smart Card Library
SC_ABORT_RESPONSE | PCB byte for Abort Response of T1 Protocol
SC_AUTHENTICATE | Authenticate Command code to the Smart Card
SC_BWI | DEFAULT Value of BWI Indicator used in calculation of BWT for T=1 protocol
SC_CHANGE_PIN | Change Pin Command code to the Smart Card
SC_CLEAR_CARD | Clear Card Command code to the Smart Card
SC_CRC_TYPE_EDC | Cyclic Redundancy Check(CRC) type is used for EDC in Epilogue Field
SC_CREDIT | Credit Command code to the Smart Card
SC_CWI | DEFAULT Value of CWI Indicator used in calculation of CWT for T=1 protocol
SC_DEBIT | Debit Command code to the Smart Card
SC_GET_RESPONSE | Get Response Command code to the Smart Card
SC_IFS_RESPONSE | PCB byte for IFS Response of T1 Protocol
SC_INQUIRE_ACCT | Inquire Account Command code to the Smart Card
SC_LRC_TYPE_EDC | Longitudnal Redundancy Check(LRC) type is used for EDC in Epilogue Field
SC_READ_RECORD | Read Record Command code to the Smart Card
SC_RESYNC_REQ | PCB byte for Resync Request of T1 Protocol
SC_REVOKE | Revoke Command code to the Smart Card
SC_SELECT_FILE | Select File Command code to the Smart Card
SC_START_SESSION | Start Session Command code to the Smart Card
SC_STATE_CARD_ACTIVE | Card is powered and ATR received
SC_STATE_CARD_INACTIVE | Card present but not powered
SC_STATE_CARD_NOT_PRESENT | No Card Detected
SC_SUBMIT_CODE | Submit Code Command to the Smart Card
SC_T0ProtocolType | Returns '1' if T=0 protocol is supported & Returns 0 otherwise
SC_T1ProtocolType | Returns '1' if T=1 protocol is supported & Returns 0 otherwise
SC_TA1Present | Returns '1' if TA1 present & Returns 0 otherwise
SC_TA2Present | Returns '1' if TA2 present & Returns 0 otherwise
SC_TB1Present | Returns '1' if TB1 present & Returns 0 otherwise
SC_TB2Present | Returns '1' if TB2 present & Returns 0 otherwise
SC_TC1Present | Returns '1' if TC1 present & Returns 0 otherwise
SC_TC2Present | Returns '1' if TC2 present & Returns 0 otherwise
SC_TD1Present | Returns '1' if TD1 present & Returns 0 otherwise
SC_TD2Present | Returns '1' if TD2 present & Returns 0 otherwise
SC_WAIT_TIME_EXT_RESPONSE | PCB byte for Wait Time Extension Response of T1 Protocol
SC_WI | DEFAULT Value of WI Indicator used in calculation of WWT for T=0 protocol
SC_WRITE_RECORD | Write Record Command code to the Smart Card
Structures
Name | Description
SC_APDU_COMMAND | SmartCard APDU Command 7816-4
SC_APDU_RESPONSE | SmartCard APDU Response structure 7816-4
SC_T1_PROLOGUE_FIELD | Prologue Field for T=1 Protocol
Variables
Name | Description
scATR_HistoryBuffer | Historical bytes sent by Smart Card
scATR_HistoryLength | Number of Historical bytes present
scATRLength | length of ATR data sent by smart card
scCardATR | ATR data sent by smartcard.
scLastError | Smart Card Error type is stored in this variable
scPPSresponse | PPS Response Bytes
scPPSresponseLength | Length of PPS Response
scTA1 | TA1 determines the clock-rate conversion factor F & bit-rate-adjustment factor D
scTA2 | TA2 determines whether the smart card will operate in specific mode or negotiable mode following the ATR
scTA3 | TA3 conveys the Information Field Size Integer (IFSI) for the smart card.
scTB1 | TB1 conveys information on the smart card's programming voltage requirements.
scTB2 | TB2 conveys PI2, which determines the value of programming voltage required by the smart card. The value of PI1 in TB1 is superceded when TB2 is present
scTB3 | TB3 indicates the value of the Character Waiting Time Integer (CWI) and Block Waiting Time Integer (BWI) used to compute the Character Waiting Time (CWT) and Block Waiting Time (BWT).
scTC1 | TC1 determines the extra guard time to be added between consecutive characters sent to the smart card from the terminal.
scTC2 | TC2 is specific to protocol type T=0. TC2 conveys work waiting-time integer (WI) that determines the maximum interval between the leading edge of the start bit of any character sent by the smart card and the leading edge of the start bit of the previous character sent either by the card or the reader
scTC3 | When TC3 is present, it indicates the type of block-error detection to be used. When TC3 is not present, the default longitudinal redundancy check (LRC) is used.
scTD1 | TD1 indicates if any further interface bytes are to be transmitted, and if so, which protocol will be used.
scTD2 | The TD2 character has the same function as the TD1 character.
scTD3 | TD3 indicates interface bytes similar to that of TD1 & TD2
Library API > Files > SClib.h


Variables

Variables
Variables
Name | Description
scATR_HistoryBuffer | Historical bytes sent by Smart Card
scATR_HistoryLength | Number of Historical bytes present
scATRLength | length of ATR data sent by smart card
scCardATR | ATR data sent by smartcard.
scLastError | Smart Card Error type is stored in this variable
scPPSresponse | PPS Response Bytes
scPPSresponseLength | Length of PPS Response
scTA1 | TA1 determines the clock-rate conversion factor F & bit-rate-adjustment factor D
scTA2 | TA2 determines whether the smart card will operate in specific mode or negotiable mode following the ATR
scTA3 | TA3 conveys the Information Field Size Integer (IFSI) for the smart card.
scTB1 | TB1 conveys information on the smart card's programming voltage requirements.
scTB2 | TB2 conveys PI2, which determines the value of programming voltage required by the smart card. The value of PI1 in TB1 is superceded when TB2 is present
scTB3 | TB3 indicates the value of the Character Waiting Time Integer (CWI) and Block Waiting Time Integer (BWI) used to compute the Character Waiting Time (CWT) and Block Waiting Time (BWT).
scTC1 | TC1 determines the extra guard time to be added between consecutive characters sent to the smart card from the terminal.
scTC2 | TC2 is specific to protocol type T=0. TC2 conveys work waiting-time integer (WI) that determines the maximum interval between the leading edge of the start bit of any character sent by the smart card and the leading edge of the start bit of the previous character sent either by the card or the reader
scTC3 | When TC3 is present, it indicates the type of block-error detection to be used. When TC3 is not present, the default longitudinal redundancy check (LRC) is used.
scTD1 | TD1 indicates if any further interface bytes are to be transmitted, and if so, which protocol will be used.
scTD2 | The TD2 character has the same function as the TD1 character.
scTD3 | TD3 indicates interface bytes similar to that of TD1 & TD2
Library API > Variables


scATR_HistoryBuffer Variable

scATR_HistoryBuffer Variable
C
BYTE* scATR_HistoryBuffer;
Description

Historical bytes sent by Smart Card

Library API > Variables > scATR_HistoryBuffer Variable


scATR_HistoryLength Variable

scATR_HistoryLength Variable
C
BYTE scATR_HistoryLength;
Description

Number of Historical bytes present

Library API > Variables > scATR_HistoryLength Variable


scATRLength Variable

scATRLength Variable
C
BYTE scATRLength;
Description

length of ATR data sent by smart card

Library API > Variables > scATRLength Variable


scCardATR Variable

scCardATR Variable
C
BYTE scCardATR[];
Description

ATR data sent by smartcard.

Library API > Variables > scCardATR Variable


scLastError Variable

scLastError Variable
C
SC_ERROR scLastError;
Description

Smart Card Error type is stored in this variable

Library API > Variables > scLastError Variable


scPPSresponse Variable

scPPSresponse Variable
C
BYTE scPPSresponse[7];
Description

PPS Response Bytes

Library API > Variables > scPPSresponse Variable


scPPSresponseLength Variable

scPPSresponseLength Variable
C
BYTE scPPSresponseLength;
Description

Length of PPS Response

Library API > Variables > scPPSresponseLength Variable


scTA1 Variable

scTA1 Variable
C
BYTE scTA1;
Description

TA1 determines the clock-rate conversion factor F & bit-rate-adjustment factor D

Library API > Variables > scTA1 Variable


scTA2 Variable

scTA2 Variable
C
BYTE scTA2;
Description

TA2 determines whether the smart card will operate in specific mode or negotiable mode following the ATR

Library API > Variables > scTA2 Variable


scTA3 Variable

scTA3 Variable
C
BYTE scTA3;
Description

TA3 conveys the Information Field Size Integer (IFSI) for the smart card.

Library API > Variables > scTA3 Variable


scTB1 Variable

scTB1 Variable
C
BYTE scTB1;
Description

TB1 conveys information on the smart card's programming voltage requirements.

Library API > Variables > scTB1 Variable


scTB2 Variable

scTB2 Variable
C
BYTE scTB2;
Description

TB2 conveys PI2, which determines the value of programming voltage required by the smart card. The value of PI1 in TB1 is superceded when TB2 is present

Library API > Variables > scTB2 Variable


scTB3 Variable

scTB3 Variable
C
BYTE scTB3;
Description

TB3 indicates the value of the Character Waiting Time Integer (CWI) and Block Waiting Time Integer (BWI) used to compute the Character Waiting Time (CWT) and Block Waiting Time (BWT).

Library API > Variables > scTB3 Variable


scTC1 Variable

scTC1 Variable
C
BYTE scTC1;
Description

TC1 determines the extra guard time to be added between consecutive characters sent to the smart card from the terminal.

Library API > Variables > scTC1 Variable


scTC2 Variable

scTC2 Variable
C
BYTE scTC2;
Description

TC2 is specific to protocol type T=0. TC2 conveys work waiting-time integer (WI) that determines the maximum interval between the leading edge of the start bit of any character sent by the smart card and the leading edge of the start bit of the previous character sent either by the card or the reader

Library API > Variables > scTC2 Variable


scTC3 Variable

scTC3 Variable
C
BYTE scTC3;
Description

When TC3 is present, it indicates the type of block-error detection to be used. When TC3 is not present, the default longitudinal redundancy check (LRC) is used.

Library API > Variables > scTC3 Variable


scTD1 Variable

scTD1 Variable
C
BYTE scTD1;
Description

TD1 indicates if any further interface bytes are to be transmitted, and if so, which protocol will be used.

Library API > Variables > scTD1 Variable


scTD2 Variable

scTD2 Variable
C
BYTE scTD2;
Description

The TD2 character has the same function as the TD1 character.

Library API > Variables > scTD2 Variable


scTD3 Variable

scTD3 Variable
C
BYTE scTD3;
Description

TD3 indicates interface bytes similar to that of TD1 & TD2

Library API > Variables > scTD3 Variable


Integrating with an Existing Application

Previous | Next
Integrating with an Existing Application

It is easy to integrate the smart card library with the existing applications.The smart card library uses UART and 4 I/O port pins.

Smart Card consists of 8 pins namely:-

I/O: Input or Output for serial data to the integrated circuit inside the card.

VPP: Programming voltage input (optional use by the card).

GND: Ground (reference voltage).

CLK: Clocking or timing signal.

RST: Reset Signal to the Card.

VCC: Power supply input (optional use by the card).

The pins used for the communication b/w the smart card & PIC micro are given in Configuring the Library section.“sc_config.h” is the only file where the user has to modify to port the smart card stack to different PIC microcontrollers.

The API's that needs to be called by the main application are mentioned in SClib.h file.Please refer "How the Library Works" to know the usage of smart card library API's.

Integrating with an Existing Application


Migration Notes for Smart Card Library

Migration notes – Smartcard Library

1. v1.02.2

1.1. Variable names

1.2. Type Definitions

1.3. Function Names

2. v1.02

3. v1.01

3.1. Variable Names

3.2. Type Definitions

3.3. Function Names

Trademarks:

1. v1.02.2

Modified the PPS functionality as per ISO 7816 standard. Fixed BWT (Block Wait Time) and WT (Wait Time) calculation issues. Removed recursive function calls and modified the code to make it well structured and organized.

Modified "SCdrv_EnableDelayTimerIntr" and "SCdrv_SetDelayTimerCnt" macros to configure for

16 bit timers (this macro is used to provide delays).

“WaitMicroSec()" & "WaitMilliSec()" macros are removed from sc_config.h file.

Moved timer interrupts (used by smart card stack) to ISO 7816 hardware driver files.

Added "TIMER1_SINGLE_COUNT_MICRO_SECONDS" and "TIMER0_SINGLE_COUNT_MICRO_SECONDS" macros in sc_config.h file.

WaitMicroSec() and WaitMilliSec() delay functions have been rewritten in the ISO 7816 driver files to provide accurate delays.

1.1. Variable names

The following PPS response variables have been added as part of the global memory.

Names | Description
scPPSresponse[7] | PPS Response Bytes from smart card
scPPSresponseLength | Length of PPS Response

1.2. Type Definitions

None.

1.3. Function Names

The prototype definition of function “SC_DoPPS( )” has been changed to “SC_DoPPS( BYTE *ppsPtr )”. The input parameter for “SC_DoPPS” function is PPS request string. This feature enables the user to send the desired PPS request to the card.

2. v1.02

Supported smart card library stack to PIC32, PIC24H and dsPIC33F devices.

3. v1.01

3.1. Variable Names

The following list of variable names has been changed to follow a common coding standard across the smartcard library.

Changed From | Changed To
SC_CardATR | scCardATR
SC_ATRLen | scATRLength
SC_LastError | scLastError
SC_TA1 | scTA1
SC_TA2 | scTA2
SC_TA3 | scTA3
SC_TB1 | scTB1
SC_TB2 | scTB2
SC_TB3 | scTB3
SC_TC1 | scTC1
SC_TC2 | scTC2
SC_TC3 | scTC3
SC_TD1 | scTD1
SC_TD2 | scTD2
SC_TD3 | scTD3
SC_ATR_HistBfr | scATR_HistoryBuffer
SC_ATR_HistLen | scATR_HistoryLength

3.2. Type Definitions

The following list of type definitions has been changed to make them more understandable.

Changed From | Changed To
SC_APDU_Cmd | SC_APDU_COMMAND
SC_APDU_Resp | SC_APDU_RESPONSE

3.3. Function Names

The function name “SC_Transact” has been changed to “SC_TransactT0” to signify that this function handles only T=0 transactions with the smart card.

The function name “SC_TransactT1” has been added newly to signify that this function handles only T=1 transactions with the smart card. The application has to call “SC_TransactT0” or “SC_TransactT1” function depending upon the card inserted.

Trademarks:

The Microchip name and logo, the Microchip logo, MPLAB, and PIC are registered trademarks of Microchip Technology Incorporated in the U.S.A. and other countries.

PICDEM is a trademark of Microchip Technology Incorporated in the U.S.A. and other countries.

Microsoft, Windows, and Windows Vista are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries.

Borland, and C++ Builder are trademarks or registered trademarks of Borland Software Corporation in the United States and other countries.

Linux is the registered trademark of Linus Torvalds in the U.S. and other countries.


Library API Files

Library API Files
Files
Name | Description
SClib.h | FileName: SClib.h Dependencies: See INCLUDES section Processor: PIC18, PIC24 Microcontrollers Hardware: This demo is natively intended to be used on Exp 16, LPC & HPC Exp board. This demo can be modified for use on other hardware platforms. Complier: Microchip C18 (for PIC18), C30 (for PIC24) Company: Microchip Technology, Inc. Software License Agreement: The software supplied herewith by Microchip Technology Incorporated (the “Company”) for its PIC® Microcontroller is intended and supplied to you, the Company’s customer, for use solely and exclusively on Microchip PIC Microcontroller products. The software is owned by the Company and/or its supplier, and is protected under... more
Library API


Library API Functions

Library API Functions
Functions
Name | Description
SC_CardPresent | This macro checks if card is inserted in the socket
SC_DoPPS | This function does the PPS exchange with the smart card & configures the baud rate of the PIC UART module as per the PPS response from the smart card.
SC_GetCardState | This function returns the current state of SmartCard
SC_Initialize | This function initializes the smart card library
SC_PowerOnATR | This function performs the power on sequence of the SmartCard and interprets the Answer-to-Reset data received from the card.
SC_Shutdown | This function Performs the Power Down sequence of the SmartCard
SC_TransactT0 | This function Sends/recieves the ISO 7816-4 compaliant APDU commands to the card.
SC_TransactT1 | This function Sends/recieves the ISO 7816-4 compaliant T = 1 commands to the card.
Library API


Functions (AutoDoc Folder)

Legend
Legend
Method
Library API


Library API Macros

Library API Macros
Macros
Name | Description
__SC_MCP_LIB__ | Smart Card Library
SC_ABORT_RESPONSE | PCB byte for Abort Response of T1 Protocol
SC_AUTHENTICATE | Authenticate Command code to the Smart Card
SC_BWI | DEFAULT Value of BWI Indicator used in calculation of BWT for T=1 protocol
SC_CHANGE_PIN | Change Pin Command code to the Smart Card
SC_CLEAR_CARD | Clear Card Command code to the Smart Card
SC_CRC_TYPE_EDC | Cyclic Redundancy Check(CRC) type is used for EDC in Epilogue Field
SC_CREDIT | Credit Command code to the Smart Card
SC_CWI | DEFAULT Value of CWI Indicator used in calculation of CWT for T=1 protocol
SC_DEBIT | Debit Command code to the Smart Card
SC_GET_RESPONSE | Get Response Command code to the Smart Card
SC_IFS_RESPONSE | PCB byte for IFS Response of T1 Protocol
SC_INQUIRE_ACCT | Inquire Account Command code to the Smart Card
SC_LRC_TYPE_EDC | Longitudnal Redundancy Check(LRC) type is used for EDC in Epilogue Field
SC_READ_RECORD | Read Record Command code to the Smart Card
SC_RESYNC_REQ | PCB byte for Resync Request of T1 Protocol
SC_REVOKE | Revoke Command code to the Smart Card
SC_SELECT_FILE | Select File Command code to the Smart Card
SC_START_SESSION | Start Session Command code to the Smart Card
SC_STATE_CARD_ACTIVE | Card is powered and ATR received
SC_STATE_CARD_INACTIVE | Card present but not powered
SC_STATE_CARD_NOT_PRESENT | No Card Detected
SC_SUBMIT_CODE | Submit Code Command to the Smart Card
SC_T0ProtocolType | Returns '1' if T=0 protocol is supported & Returns 0 otherwise
SC_T1ProtocolType | Returns '1' if T=1 protocol is supported & Returns 0 otherwise
SC_TA1Present | Returns '1' if TA1 present & Returns 0 otherwise
SC_TA2Present | Returns '1' if TA2 present & Returns 0 otherwise
SC_TB1Present | Returns '1' if TB1 present & Returns 0 otherwise
SC_TB2Present | Returns '1' if TB2 present & Returns 0 otherwise
SC_TC1Present | Returns '1' if TC1 present & Returns 0 otherwise
SC_TC2Present | Returns '1' if TC2 present & Returns 0 otherwise
SC_TD1Present | Returns '1' if TD1 present & Returns 0 otherwise
SC_TD2Present | Returns '1' if TD2 present & Returns 0 otherwise
SC_WAIT_TIME_EXT_RESPONSE | PCB byte for Wait Time Extension Response of T1 Protocol
SC_WI | DEFAULT Value of WI Indicator used in calculation of WWT for T=0 protocol
SC_WRITE_RECORD | Write Record Command code to the Smart Card
Library API


Macros (AutoDoc Folder)

Legend
Legend
Macro
Library API


Library API Types

Library API Types
Types
Name | Description
SC_APDU_COMMAND | SmartCard APDU Command 7816-4
SC_APDU_RESPONSE | SmartCard APDU Response structure 7816-4
SC_ERROR | Smart Card error types
SC_T1_PROLOGUE_FIELD | Prologue Field for T=1 Protocol
T1BLOCK_TYPE | Block types in T=1 protocol
Library API


Types (AutoDoc Folder)

Legend
Legend
Type
Library API


Library API Variables

Library API Variables
Variables
Name | Description
scATR_HistoryBuffer | Historical bytes sent by Smart Card
scATR_HistoryLength | Number of Historical bytes present
scATRLength | length of ATR data sent by smart card
scCardATR | ATR data sent by smartcard.
scLastError | Smart Card Error type is stored in this variable
scPPSresponse | PPS Response Bytes
scPPSresponseLength | Length of PPS Response
scTA1 | TA1 determines the clock-rate conversion factor F & bit-rate-adjustment factor D
scTA2 | TA2 determines whether the smart card will operate in specific mode or negotiable mode following the ATR
scTA3 | TA3 conveys the Information Field Size Integer (IFSI) for the smart card.
scTB1 | TB1 conveys information on the smart card's programming voltage requirements.
scTB2 | TB2 conveys PI2, which determines the value of programming voltage required by the smart card. The value of PI1 in TB1 is superceded when TB2 is present
scTB3 | TB3 indicates the value of the Character Waiting Time Integer (CWI) and Block Waiting Time Integer (BWI) used to compute the Character Waiting Time (CWT) and Block Waiting Time (BWT).
scTC1 | TC1 determines the extra guard time to be added between consecutive characters sent to the smart card from the terminal.
scTC2 | TC2 is specific to protocol type T=0. TC2 conveys work waiting-time integer (WI) that determines the maximum interval between the leading edge of the start bit of any character sent by the smart card and the leading edge of the start bit of the previous character sent either by the card or the reader
scTC3 | When TC3 is present, it indicates the type of block-error detection to be used. When TC3 is not present, the default longitudinal redundancy check (LRC) is used.
scTD1 | TD1 indicates if any further interface bytes are to be transmitted, and if so, which protocol will be used.
scTD2 | The TD2 character has the same function as the TD1 character.
scTD3 | TD3 indicates interface bytes similar to that of TD1 & TD2
Library API


Variables (AutoDoc Folder)

Legend
Legend
Variable
Library API


Contents

Contents
Introduction
SW License Agreement
Release Notes
Resource Usage - PIC18
Resource Usage - PIC24F
Resource Usage - PIC24H
Resource Usage - dsPIC33F
Resource Usage - PIC32
Smartcard Library Overview
Library Architecture
How the Library Works
Running the Demos
Configuring the Library
Library API
Functions
SC_CardPresent Function
SC_DoPPS Function
SC_GetCardState Function
SC_Initialize Function
SC_PowerOnATR Function
SC_Shutdown Function
SC_TransactT0 Function
SC_TransactT1 Function
Types
SC_APDU_COMMAND Structure
SC_APDU_RESPONSE Structure
SC_ERROR Enumeration
SC_T1_PROLOGUE_FIELD Structure
T1BLOCK_TYPE Enumeration
Macros
__SC_MCP_LIB__ Macro
SC_ABORT_RESPONSE Macro
SC_AUTHENTICATE Macro
SC_BWI Macro
SC_CHANGE_PIN Macro
SC_CLEAR_CARD Macro
SC_CRC_TYPE_EDC Macro
SC_CREDIT Macro
SC_CWI Macro
SC_DEBIT Macro
SC_GET_RESPONSE Macro
SC_IFS_RESPONSE Macro
SC_INQUIRE_ACCT Macro
SC_LRC_TYPE_EDC Macro
SC_READ_RECORD Macro
SC_RESYNC_REQ Macro
SC_REVOKE Macro
SC_SELECT_FILE Macro
SC_START_SESSION Macro
SC_STATE_CARD_ACTIVE Macro
SC_STATE_CARD_INACTIVE Macro
SC_STATE_CARD_NOT_PRESENT Macro
SC_SUBMIT_CODE Macro
SC_T0ProtocolType Macro
SC_T1ProtocolType Macro
SC_TA1Present Macro
SC_TA2Present Macro
SC_TB1Present Macro
SC_TB2Present Macro
SC_TC1Present Macro
SC_TC2Present Macro
SC_TD1Present Macro
SC_TD2Present Macro
SC_WAIT_TIME_EXT_RESPONSE Macro
SC_WI Macro
SC_WRITE_RECORD Macro
Files
SClib.h
Variables
scATR_HistoryBuffer Variable
scATR_HistoryLength Variable
scATRLength Variable
scCardATR Variable
scLastError Variable
scPPSresponse Variable
scPPSresponseLength Variable
scTA1 Variable
scTA2 Variable
scTA3 Variable
scTB1 Variable
scTB2 Variable
scTB3 Variable
scTC1 Variable
scTC2 Variable
scTC3 Variable
scTD1 Variable
scTD2 Variable
scTD3 Variable
Integrating with an Existing Application
Library Migration


Index

Index
_ | C | F | H | I | L | M | R | S | T | V
_
__SC_MCP_LIB__ macro
C
Configuring the Library
F
Files
Functions
H
How the Library Works
I
Integrating with an Existing Application
Introduction
L
Library API
Library Architecture
Library Migration
M
Macros
R
Release Notes
Resource Usage - dsPIC33F
Resource Usage - PIC18
Resource Usage - PIC24F
Resource Usage - PIC24H
Resource Usage - PIC32
Running the Demos
S
SC_ABORT_RESPONSE macro
SC_APDU_COMMAND structure
SC_APDU_RESPONSE structure
SC_AUTHENTICATE macro
SC_BWI macro
SC_CardPresent function
SC_CHANGE_PIN macro
SC_CLEAR_CARD macro
SC_CRC_TYPE_EDC macro
SC_CREDIT macro
SC_CWI macro
SC_DEBIT macro
SC_DoPPS function
SC_ERROR enumeration
SC_GET_RESPONSE macro
SC_GetCardState function
SC_IFS_RESPONSE macro
SC_Initialize function
SC_INQUIRE_ACCT macro
SC_LRC_TYPE_EDC macro
SC_PowerOnATR function
SC_READ_RECORD macro
SC_RESYNC_REQ macro
SC_REVOKE macro
SC_SELECT_FILE macro
SC_Shutdown function
SC_START_SESSION macro
SC_STATE_CARD_ACTIVE macro
SC_STATE_CARD_INACTIVE macro
SC_STATE_CARD_NOT_PRESENT macro
SC_SUBMIT_CODE macro
SC_T0ProtocolType macro
SC_T1_PROLOGUE_FIELD structure
SC_T1ProtocolType macro
SC_TA1Present macro
SC_TA2Present macro
SC_TB1Present macro
SC_TB2Present macro
SC_TC1Present macro
SC_TC2Present macro
SC_TD1Present macro
SC_TD2Present macro
SC_TransactT0 function
SC_TransactT1 function
SC_WAIT_TIME_EXT_RESPONSE macro
SC_WI macro
SC_WRITE_RECORD macro
scATR_HistoryBuffer variable
scATR_HistoryLength variable
scATRLength variable
scCardATR variable
scLastError variable
SClib.h
scPPSresponse variable
scPPSresponseLength variable
scTA1 variable
scTA2 variable
scTA3 variable
scTB1 variable
scTB2 variable
scTB3 variable
scTC1 variable
scTC2 variable
scTC3 variable
scTD1 variable
scTD2 variable
scTD3 variable
Smartcard Library Overview
SW License Agreement
T
T1BLOCK_TYPE enumeration
Types
V
Variables


Introduction

Contents | Index | Reference
Next
Introduction

Smart Card ISO-7816 Library

For

8,16 & 32 bit PIC Microcontrollers

The Smart Card library for PIC microcontrollers implements the ISO 7816-3 and ISO 7816-4 standard protocols. It allows the PIC microcontroller to communicate with smart cards compatible with these protocols. The library supports T=0 and T=1 smart card protocols.

The library comprises of PIC18/PIC24/dsPIC33F/PIC32 UART driver and T0/T1 protocol source code meeting ISO 7816-3 standard. An example high level code is also provided to help the user port the smart card library to different hardware boards and different microcontrollers of PIC family.

This document assumes that the reader has understanding of ISO 7816-3 standards and T=0/T=1 protocols.

Introduction