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

📄 main.c

📁 8051和DS8007怎样操作使用SMARTCARD的源代码
💻 C
字号:
/*---------------------------------------------------------------------------
 *  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.
 * ---------------------------------------------------------------------------
 */

#include <stdio.h>
#include <reg5000.h>
#include <absacc.h>
#include <stdlib.h>
#include <string.h>
#include "ds8007.h"
#include "LCD_Funct.h"

#define CPU_CRYSTAL 11059000//18432000*2
#define BAUD_RATE 57600//115200
#define BAUDDOUBLE 1


void serialSetup()
{
	TMOD=0x21;		// Timer 1: 8-bit auto-reload from TH1, Timer 0: 16-bit
	TH1 = 0xFE;

#if (BAUDDOUBLE == 1)
  	PCON|=0x80;		// Turn on baud rate doubler SMOD_0
#else
	PCON&=0x7F;		// Turn off baud rate doubler SMOD_0
#endif

	TCON=0x50;		// Enable timers 0 and 1
	SCON=0x50;		// 10-bit async, enabled

  TI=1;
}

uint8_t startsessionAPDU[] = { 0x80,0x84,0,0,8 };  // according to ACOS1
//uint8_t startsessionAPDU[] = { 0xC0,0x84,0,0,6 };  // according to Smart Cards book pp.116
//uint8_t startsessionAPDU[] = { 0x00,0x84,0,0,8 };  // according to ISO 7816
//uint8_t startsessionAPDU[] = { 0x80,0x84,0,0,8,0,0,0,0,0,0,0,0,8 };

 void doTest(int slot)
{
	uint8_t buff[512];
	uint8_t rbuff[512];
	int length;
	int16_t rlength;
	int i;

	dssc_selectcard(slot);
	i = dssc_powerup(POWERUP_ISO,POWERUP_5V);
	//i = dssc_powerup(POWERUP_EMV,POWERUP_5V);
 
	printf("Power up return: %d\n",i);

	if (i >= 0)
	{
		length = dssc_getATRbuffer(buff,512);

		printf("ATR:");
		for (i = 0;i < length;i++)
		{
			printf(" %02bx",buff[i]);
		}
		puts("\n");
		dssc_setNAD(0x00);
		rlength = dssc_sendAPDU(startsessionAPDU,sizeof(startsessionAPDU),rbuff);
		//rlength = dssc_sendAPDUT0(startsessionAPDU,sizeof(startsessionAPDU),rbuff);
		//dssc_sendsblockIFSD(254);
		//rlength = dssc_sendAPDUT1(startsessionAPDU,sizeof(startsessionAPDU),rbuff);

		printf("APDU Response: %d\n",rlength);
  		for (i = 0;i < rlength;i++)
			printf("%bX ",rbuff[i]);
  	}

	printf("\r\n");
	dssc_powerdown();
}

void main(void)
{
	int slot, tmp = 0;
  	uint8_t	val, temp;
	uint8_t LCD_Str[42];

	EA=0;
	serialSetup();
	puts("\n\nDS8007 Basic Test\n");
	val = dssc_readregister(CSR);
	printf("DS8007 Version: %bx\n",val>>4);
	dssc_init();

	printf("Compiled Crystal Frequency: %ld\n",CRYSTAL_FREQUENCY_8007);

	// Initialize LCD Module, and display 2-line message.
	LCD_Init();
	strcpy(LCD_Str, "Dual Smart Card Int");		// Create 2nd line of LCD text
	tmp = LCD_Curpos(2,1);						// Position cursor @ 1 line 2
	if (tmp == 0) LCD_WRStr(LCD_Str);			// Write string to LCD
	else printf("\n!! LCD cursor address out of range !!\n");
	strcpy(LCD_Str, "DS8007 MultiProtocol");	// Create first line of LCD text
	tmp = LCD_Curpos(1,1);						// Position cursor @ 1 line 1
	if (tmp == 0) LCD_WRStr(LCD_Str);			// Write string to LCD
	else printf("\n!! LCD cursor address out of range !!\n");
	tmp = LCD_Curpos(2,20);						// Return cussor @ 20 line 2


	while (1)
	{
		printf("\n\nWaiting for smartcard presence\n");
 		//val = dssc_readregister(MSR);
		//printf("MSR: %bx\n",val);
  		while (1)
  		{
    		val = dssc_readregister(MSR);
			if(temp != val){
	  			//printf("MSR: %bx\n",val);
	 			//printf("Status check - 1\n"); 
	  			temp = val;
			}
			if (val & MSR_PR1_MASK)
            {
				slot = 1;
				//printf("Status check - 3\n");
      			break;
    		}
    		if (val & MSR_PR2_MASK)
    		{
	  			slot = 2;
	  			printf("Status check - 4\n");
      			break;
		    }
		}
  
		printf("Found smartcard in slot %d\n",slot);

  		doTest(slot);

		printf("\r\n\r\nWaiting for smartcard removal from slot %d\n",slot);
  		do
  		{
    		val = dssc_readregister(MSR);
  		}
  		while(( (val&MSR_PR1_MASK) != 0) || ( (val&MSR_PR2_MASK) != 0) );
  		printf("Smartcard removed\n");
	}
  	printf("Exiting\n");
  	while (1);
}

⌨️ 快捷键说明

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