⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ds8007.h

📁 8051和DS8007怎样操作使用SMARTCARD的源代码
💻 H
字号:
/*---------------------------------------------------------------------------
 *  Copyright (C) 2004 Dallas Semiconductor Corporation, All Rights Reserved.
 * 
 *  Permission is hereby granted, free of charge, to any person obtaining a
 *  copy of this software and associated documentation files (the "Software"),
 *  to deal in the Software without restriction, including without limitation
 *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
 *  and/or sell copies of the Software, and to permit persons to whom the
 *  Software is furnished to do so, subject to the following conditions:
 * 
 *  The above copyright notice and this permission notice shall be included
 *  in all copies or substantial portions of the Software.
 * 
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 *  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 *  MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 *  IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES
 *  OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 *  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 *  OTHER DEALINGS IN THE SOFTWARE.
 * 
 *  Except as contained in this notice, the name of Dallas Semiconductor
 *  shall not be used except as stated in the Dallas Semiconductor
 *  Branding Policy.
 * ---------------------------------------------------------------------------
 */

/** 
 * \file DS8007.h
 * \brief Interface library for DS8007 smartcard interface chips
 *
 * This library contains routines required to communicate with smartcards
 * via an DS8007 while conforming to:
 * ISO 7816
 * EMV 4.1
 */

// Data types
typedef unsigned char uint8_t;
typedef signed short int16_t;
typedef unsigned short uint16_t;
typedef unsigned long uint32_t;

/**
 * Structure for holding ATR information.
 */
struct ATR
{
  uint8_t TS;
  uint8_t T0;
  int16_t TA[8];
  int16_t TB[8];
  int16_t TC[8];
  int16_t TD[8];
  uint8_t HistoricalLength;
  uint8_t Historical[15];
  int16_t TCK;
};

/**
 * Structure for holding interface information.
 */
struct interfaceStatus
{
  uint8_t presence;
  uint8_t selected;
  uint8_t version;
  uint8_t clockdiv;
};

// DS8007 Crystal Frequency
#define CRYSTAL_FREQUENCY_8007 12000000L

// Memory mapped DS8007 address
#define BASE_ADDRESS FARRAY (char,0x000000)  // This is for DS5250 mapped device
//#define BASE_ADDRESS FARRAY (char,0x300000)  // This is for TINIm390 mapped device
//#define BASE_ADDRESS FARRAY (char,0x000000)  // This is for DS5250 mapped emulated device

// Number of cards supported (used for sizing data structures)
#define CARD_SLOTS  2

// DS8007 Register definitions
#define CSR     0x00
#define CCR     0x01
#define PDR     0x02
#define UCR2    0x03
#define GTR     0x05
#define UCR1    0x06
#define PCR     0x07
#define TOC     0x08
#define TOR1    0x09
#define TOR2    0x0A
#define TOR3    0x0B
#define MSR     0x0C
#define FCR     0x0C
#define UTR     0x0D
#define URR     0x0D
#define USR     0x0E
#define HSR     0x0F

// Bit definitions within registers
#define CSR_ID_MASK         0xF0
#define CSR_nRIU_MASK       0x08
#define CSR_SC3_MASK        0x04
#define CSR_SC2_MASK        0x02
#define CSR_SC1_MASK        0x01

#define HSR_PRTL2_MASK      0x40
#define HSR_PRTL1_MASK      0x20
#define HSR_SUPL_MASK       0x10
#define HSR_PRL2_MASK       0x08
#define HSR_PRL1_MASK       0x04
#define HSR_INTAUXL_MASK    0x02
#define HSR_PTL_MASK        0x01

#define MSR_CLKSW_MASK      0x80
#define MSR_FE_MASK         0x40
#define MSR_BGT_MASK        0x20
#define MSR_CRED_MASK       0x10
#define MSR_PR2_MASK        0x08
#define MSR_PR1_MASK        0x04
#define MSR_INTAUX_MASK     0x02
#define MSR_TBE_RBF_MASK    0x01

#define FCR_PEC_MASK        0x70
#define FCR_FL_MASK         0x07

#define USR_TOL3_MASK       0x80
#define USR_TOL2_MASK       0x40
#define USR_TOL1_MASK       0x20
#define USR_EA_MASK         0x10
#define USR_PE_MASK         0x08
#define USR_OVR_MASK        0x04
#define USR_FER_MASK        0x02
#define USR_TBE_RBF_MASK    0x01

#define UCR1_FIP_MASK       0x40
#define UCR1_PROT_MASK      0x10
#define UCR1_T_R_MASK       0x08
#define UCR1_LCT_MASK       0x04
#define UCR1_SS_MASK        0x02
#define UCR1_CONV_MASK      0x01

#define UCR2_DISTBE_RBF_MASK 0x40
#define UCR2_DISAUX_MASK    0x20
#define UCR2_PDWN_MASK      0x10
#define UCR2_SAN_MASK       0x08
#define UCR2_nAUTOCONV_MASK 0x04
#define UCR2_CKU_MASK       0x02
#define UCR2_PSC_MASK       0x01

#define CCR_SHL_MASK        0x20
#define CCR_CST_MASK        0x10
#define CCR_SC_MASK         0x08
#define CCR_AC_MASK         0x07

#define PCR_C8_MASK         0x20
#define PCR_C4_MASK         0x10
#define PCR_1V8_MASK        0x08
#define PCR_RSTIN_MASK      0x04
#define PCR_3V_5V_MASK      0x02
#define PCR_START_MASK      0x01

// Power up modes
#define POWERUP_ISO     0
#define POWERUP_EMV     1

// Power up voltages
#define POWERUP_5V      0
#define POWERUP_3V      1
#define POWERUP_1p8V    2

// Types of ERC
#define EDC_TYPE_LRC    0
#define EDC_TYPE_CRC    1

// Error return values
#define ERR_POWERUP_VOLTAGE_INVALID     -1
#define ERR_POWERUP_INTERRUPTED         -2
#define ERR_POWERUP_ATR_TIMEOUT         -3
#define ERR_POWERUP_ATR_INVALID         -4
#define ERR_POWERUP_ATR_CRC_FAILURE     -5
#define ERR_PROTOCOL_UNSUPPORTED        -6
#define ERR_INVALID_SLOT                -7
#define ERR_RECEIVE_TIMEOUT             -8
#define ERR_RECEIVE_PARITY              -9
#define ERR_RECEIVE_LRC                 -10
#define ERR_RECEIVE_CRC                 -11
#define ERR_RECEIVE_SEQUENCENUM         -12
#define ERR_SETIFSD_FAILURE             -13


/**
 * \brief   Read a specified DS8007 register.
 *
 * \param   address Register address to read (e.g. MSR, UTR)
 * \return  Value of register
 */
uint8_t dssc_readregister(uint8_t address);

/**
 * \brief   Write a specified DS8007 register.
 * \param   address Register address to write (e.g. MSR, UTR)
 * \param   value Value to write
 */
void dssc_writeregister(uint8_t address,uint8_t value);

/**
 * \brief   Power up the currently selected smartcard.
 * \param   mode Powerup mode to follow (e.g. POWERUP_ISO or POWERUP_EMV)
 * \param   voltage Powerup voltage to use (POWERUP_5V, POWERUP3V or POWERUP_1p8V)
 * \return  0 for success or <0 for failure
 */
int16_t dssc_powerup(uint8_t mode,uint8_t voltage);

/**
 * \brief   Warm reset the currently selected smartcard.
 * \param   mode Powerup mode to follow (e.g. POWERUP_ISO or POWERUP_EMV)
 * \return  0 for success or <0 for failure
 */
int16_t dssc_warmreset(uint8_t mode);

/**
 * \brief   Power down the currently selected smartcard.
 * \return  0 for success or <0 for failure
 */
int16_t dssc_powerdown();

/**
 * \brief   Select a smartcard slot to use.
 * \param   slot Slot to use.  1, 2, or 3.
 * \return  0 for success or <0 if slot not in range
 */
int16_t dssc_selectcard(uint8_t slot);

/**
 * \brief   Check for smartcard presence in specified slot.
 * \param   slot Slot to use.  Either 1 or 2.
 * \return  0 for no presence, 1 for presence or <0 if slot not in range
 */
int16_t dssc_checkpresence(uint8_t slot);

/**
 * \brief   Retrieve the ATR captured during the last dssc_powerup() call.
 * \param   buff Buffer to store ATR.
 * \param   length Maximum length to copy into buffer.
 * \return  Length of ATR buffer returned
 */
int16_t dssc_getATRbuffer(uint8_t *buff,int16_t length);

/**
 * \brief   Retrieve the ATR structure of the currently selected card.
 * \param   userATR Struct in which to store ATR information
 */
void dssc_getATR(struct ATR *userATR);

/**
 * \brief   Send an APDU to currently selected smartcard.
 *
 * Will choose to call either dssc_sendAPDUT0 or T1 based on TD
 * protocol selection of ATR.
 * 
 * \param   buffer Holds the Command APDU to send
 * \param   length Length of Command APDU
 * \param   rbuffer Location to put Response APDU
 * \return  Length of APDU buffer returned or <0 for failure
 */
int16_t dssc_sendAPDU(uint8_t *buffer,int16_t length,uint8_t *rbuffer);

/**
 * \brief   Send an APDU using protocol T=0 to currently selected smartcard.
 * \param   buffer Holds the Command APDU to send
 * \param   length Length of Command APDU
 * \param   rbuffer Location to put Response APDU
 * \return  Length of APDU buffer returned or <0 for failure
 */
int16_t dssc_sendAPDUT0(uint8_t *buffer,int16_t length,uint8_t *rbuffer);

/**
 * \brief   Send an APDU using protocol T=1 to currently selected smartcard.
 * \param   buffer Holds the Command APDU to send
 * \param   length Length of Command APDU
 * \param   rbuffer Location to put Response APDU
 * \return  Length of APDU buffer returned or <0 for failure
 */
int16_t dssc_sendAPDUT1(uint8_t *buffer,int16_t length,uint8_t *rbuffer);

/**
 * \brief   Send an sblock with IFSD parameter.  (for T=1 communication only)
 * \param   IFSD value to use
 *
 * \return  0 for success or <0 for failure
 */
int16_t dssc_sendsblockIFSD(uint8_t IFSD);

/**
 * \brief   Set node address parameter.  (for T=1 communication only)
 * \param   value NAD value to use
 */
void dssc_setNAD(uint8_t value);

/**
 * \brief   Initialize the DS8007 library
 *
 * Must be called before using the library for the first time.  Resets
 * the UART and deselects all cards.
 *
 * \return  0 for success or <0 for failure
 */
int16_t dssc_init();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -