Processing flow of CPU card programming[Copy link]
When the single-chip microcomputer control circuit detects that a CPU card has been inserted, the processing flow is as follows: 1), Power on and reset the card 2), Select the main file on the card to be operated 3), Power on and reset the ESAM module 4) Select the main file of the ESAM module to be operated 5) If the read and write permissions of the file are free, you can now read and write the file. If you have read and write permissions, you need to go through internal and external authentication before reading and writing the file. 6), Internal authentication between card and ESAM 7), External authentication between card and ESAM 8), Read (write) permissioned files. 2.1 Port Configuration Here are the microcontroller port definitions related to the CPU card and ESAM module used in the program. 2.1.1.端口定义
2.2.Variable and macro definitions 2.2.1. Card processing result variables unsigned int CardError; This variable defines various abnormal states that occur during the card processing process to indicate at which step the problem occurred, making it easier to find and solve the problem. The following is the definition of each bit of the variable. #define ErrorRSTBIT BIT0 //Card reset error #define ErrorRWBIT BIT1 //Card read and write error #define ErrorPasswBIT BIT2 //Card and Esam internal authentication error #define ErrorCardExtAuthBIT BIT3 //Card external authentication error #define ErrorEsamExtAuthBIT BIT4 //ESAM module external authentication error #define ErrorKeyUpdateBIT BIT5 //Key update error #define ErrorKeyBIT BIT6 //Key comparison error 2.2.2. CPU card communication byte length unsigned char TxAndRxBuff[124]; The data in the card or ESAM reading and writing process is transmitted through this variable. During the processing of the CPU card, a maximum of 124 bytes can be received and sent each time 2.2.3. Card and ESAM file main directory macro definition //Card main directory 3f 01, Esam main directory 3f 00 #define MainDir 0x3f #define CardMainDir 0x01 #define EsamMainDir 0x00 Before operating files on the card or ESAM, you must first select the main directory where the file is located. 2.2.4.Response status word //Response status word SW1, SW2, //The response data of the command executed correctly is SW1SW2=90 00 or SW1SW2=61 00 #define RespondStateByte1 0x90 #define RespondStateByte2 0x00 #define RespondStateByte3 0x61 2.2.5. Data reception result variable //cTxAndRxStatus CPU card receives data correctly #define ReceINSRightBIT BIT0 This variable is used to determine whether the data received from the card or ESAM is correct. 2.2.6. Card (ESAM operation flag variable) This variable is used to indicate whether the operation is performed on the card or the ESAM, and also to indicate whether the verification result of the data from the card or ESAM is correct or not //Variable CardWorkFlag #define CardORESAM BIT0 //0--SAM operation 1--Card operation #define CARDPRO_ERR BIT1 //0--Card read/write operation is normal 1-Card read/write error #define cCheckOrRe BIT2 #define ICXOR BIT6 //Checksum sent by card/ESAM #define MXOR BIT7 //Checksum calculated by CPU #define CheckflagBit BIT5
//命令正确执行的响应数据为SW1SW2=90 00或SW1SW2=61 00
#define RespondStateByte1 0x90
#define RespondStateByte2 0x00
#define RespondStateByte3 0x61
2.2.5. 数据接收结果变量
//cTxAndRxStatus CPU卡接收数据正确标志
#define ReceINSRightBIT BIT0
该变量用以判定从卡或者ESAM接受的数据是否正确。
2.2.6. 卡(ESAM操作标志变量)
此变量用以标明是对卡还是对ESAM进行操作,并且还标明从卡或者ESAM来的数据的校验结果正确与否
//变量CardWorkFlag
#define CardORESAM BIT0 //0--对SAM操作 1--对卡操作
#define CARDPRO_ERR BIT1 //0--读写卡操作正常 1--读写卡错误
#define cCheckOrRe BIT2
#define ICXOR BIT6 //卡/ESAM传来的校验和
#define MXOR BIT7 //CPU计算的校验和
#define CheckflagBit BIT5
//命令正确执行的响应数据为SW1SW2=90 00或SW1SW2=61 00
#define RespondStateByte1 0x90
#define RespondStateByte2 0x00
#define RespondStateByte3 0x61
2.2.5. 数据接收结果变量
//cTxAndRxStatus CPU卡接收数据正确标志
#define ReceINSRightBIT BIT0
该变量用以判定从卡或者ESAM接受的数据是否正确。
2.2.6. 卡(ESAM操作标志变量)
此变量用以标明是对卡还是对ESAM进行操作,并且还标明从卡或者ESAM来的数据的校验结果正确与否
//变量CardWorkFlag
#define CardORESAM BIT0 //0--对SAM操作 1--对卡操作
#define CARDPRO_ERR BIT1 //0--读写卡操作正常 1--读写卡错误
#define cCheckOrRe BIT2
#define ICXOR BIT6 //卡/ESAM传来的校验和
#define MXOR BIT7 //CPU计算的校验和
#define CheckflagBit BIT5Data reception result variable //cTxAndRxStatus CPU card receives data correctly flag #define ReceINSRightBIT BIT0 This variable is used to determine whether the data received from the card or ESAM is correct. 2.2.6. Card (ESAM operation flag variable) This variable is used to indicate whether the operation is performed on the card or the ESAM, and also to indicate whether the verification result of the data from the card or ESAM is correct or not //Variable CardWorkFlag #define CardORESAM BIT0 //0--SAM operation 1--Card operation #define CARDPRO_ERR BIT1 //0--Card read/write operation is normal 1-Card read/write error #define cCheckOrRe BIT2 #define ICXOR BIT6 //Checksum sent by card/ESAM #define MXOR BIT7 //Checksum calculated by CPU #define CheckflagBit BIT5Data reception result variable //cTxAndRxStatus CPU card receives data correctly flag #define ReceINSRightBIT BIT0 This variable is used to determine whether the data received from the card or ESAM is correct. 2.2.6. Card (ESAM operation flag variable) This variable is used to indicate whether the operation is performed on the card or the ESAM, and also to indicate whether the verification result of the data from the card or ESAM is correct or not //Variable CardWorkFlag #define CardORESAM BIT0 //0--SAM operation 1--Card operation #define CARDPRO_ERR BIT1 //0--Card read/write operation is normal 1-Card read/write error #define cCheckOrRe BIT2 #define ICXOR BIT6 //Checksum sent by card/ESAM #define MXOR BIT7 //Checksum calculated by CPU #define CheckflagBit BIT5