📄 iccard.c
字号:
/***********************************************************************************
* iccard.c
*
* Copyright (c) 2002,深圳xx有限公司税控产品事业部
* All rights reserved.
*
* 文件名称: iccard.c
* 文件标识:
* 摘要:
*
* 当前版本: 1.00
* 作者:
* 完成日期: 2003.2
* 说明:
*
//Note, for the IC card Read/Write, only need modify the IC_read_byte() and IC_Write_Byte function
// Is OK for the sample machine.
// E6-ZXC 03-2-2 15:58
***********************************************************************************/
#include "ecrsys.h"
#include "data.h"
#include "ftype.h"
#include "sysdata.h"
#include "error.h"
//#include "iccard.h"
#include "pc_comm.h"
#include <string.h>
#define TIME_LIMIT 7000L
#define TCTIME_LIMIT 100000L
//#define REGISTLEN sizeof(RegisterDef)
//#define ITEMLEN sizeof(ItemDef)
//#define LICENSELEN sizeof(LicenseDef)
//#define TAXRECDLEN sizeof(TaxRecordDef)
//#define REGISTBEG 0x000000L
//#define ICINVOICECNT 0x003000L
//#define INVBEGADDR 0x003020L
//#define LICENSEBEG 0x000200L
//#define KEYCODEBEG (REGISTBEG+57)
//#define ITEMBEG 0x000100L
//#define PAYTAXFLG 0x000210L
//#define TAXRECDBEG 0x000300L
//#define ICTESTBEG 0x0007E0L
//#define ICGATEBEG 0x0007F0L
/*---------------------------------------------------------------*
IC Card Menory Map
page003H~page010H //(264*16 byte = 2M+ 64byte)sysflag store
//reserve 2M + 64bytebyte
page020H~page050H //Dept(264*48byte = 6M + 192byte)// Dept
page060H~page090H //sindex
page100H~page800H //PLU
*---------------------------------------------------------------*/
#define IC_START_ADDR 0x000000L
#define IC_SYSFLAG_ADDR 0x000318L
#define IC_DEPT_ADDR 0x002100L
#define IC_PLUSINDEX_ADDR 0x006300L
#define IC_PLU_ADDR 0x010800L
#define IC_MAX_ADDR 0x084000L
#define ROW_SIZE_IC 264
#define IC_NO_CARD 2
#define IC_BAD 3
#define IC_ADDR_OVER 4
#define IC_WRITE_FALID 6
/*******************************************************************
IC卡插入 ---- P8.3,外部中断1,低电平有效,输入
IC卡电源 ---- P14.0,输出,高电平有效
IC卡接口 ---- P14.1-P14.6, 定义与税控器最早版本一样
sbit Flash_RST = P1^0; p14_6
sbit Flash_RB = P1^1; p14_5
sbit Flash_SO = P1^4; p14_4
sbit Flash_CS = P1^5; p14_3
sbit Flash_SCK = P1^6; p14_2
sbit Flash_SI = P1^7; p14_1
********************************************************************/
//#define CARD_EXST p8_3 //IC卡插入
//#define IC_VCC p14_0 //IC卡电源
//#define Flash_RST p14_6 // c2
//#define Flash_RB p14_5 // c3
//#define Flash_SO p14_4 // c4
//#define Flash_CS p14_3 // c6
//#define Flash_SCK p14_2 // c7
//#define Flash_SI p14_1 // c8
//#define Flash_RST p14_6 //reset
//#define Flash_CS p14_5 //Chip select
//#define Flash_RB p14_4 //ready or busy
//#define Flash_SCK p14_3 //Serial Clock
//#define Flash_SO p14_2 //Serial out
//#define Flash_SI p14_1 //serial in
//analy 2005-11-20
#define CARD_EXST p14_4 //IC卡插入
#define IC_VCC p12_7 //IC卡电源
#define Flash_RST p12_1 //reset
#define Flash_CS p12_2 //Chip select
#define Flash_RB p12_3 //ready or busy
#define Flash_SCK p12_4 //Serial Clock
#define Flash_SO p12_5 //Serial out
#define Flash_SI p12_6 //serial in
#define ReadIccard IC_ReadString
extern void disint(void);
extern void enint(void);
void GetLicense(void);
/********************************************************************************
* 函数说明: Initial the IC card port.
* 输入参数: void
* 输出参数: void
* 返回值: void
* 说明: 关掉片选,禁止复位信号, IC设为Busy状态, 时钟信号置高,关掉电源
********************************************************************************/
void InitIccardPort(void)
{
pd14_4 = 0; // input
// pd14 = 0x4f; // ic vcc
//pd14 = 0x6b; // ic vcc
pd12 = 0xd6; //ic vcc
IC_VCC = 1; // disable ic vcc
}
/********************************************************************************
* 函数说明: open ic card power and led
* 输入参数: void
* 输出参数: void
* 返回值: void
********************************************************************************/
void EnCardVcc(void)
{
IC_VCC = 0;
}
/********************************************************************************
* 函数说明: close ic card power and led
* 输入参数: void
* 输出参数: void
* 返回值: void
********************************************************************************/
void DisCardVcc(void)
{
IC_VCC = 1;
}
/********************************************************************************
* 函数说明: Reset the IC card.
* 输入参数: void
* 输出参数: void
* 返回值: 0 is OK.
********************************************************************************/
byte IcCardReset(void)
{
byte i;
if (CARD_EXST != 0)
{
Flash_CS = 0;
for (i=0; i<2; i++);
Flash_RST = 1;
for (i=0; i<10; i++);
Flash_RST = 0;
for (i=0; i<100; i++); // Wait at least 10us
Flash_RST = 1;
for (i=0; i<2; i++);
Flash_CS = 1;
for (i=0; i<5; i++);
return 0;
}
else
return (IC_NO_CARD);
}
/********************************************************************************
* 函数说明: Initial the IC card.
* 输入参数: void
* 输出参数: void
* 返回值: 0 is OK.
********************************************************************************/
byte InitIccard(void)
{
if (CARD_EXST == 0)//如果有IC Card插入,P8_3检测为高电平
{
return (IC_NO_CARD);
}
EnCardVcc();//给IC卡提供电源
Flash_SCK = 0;//将SCK置为低电平
Wait(100); // Wait ic card power stable
IcCardReset();//对IC卡复位
return 0;
}
/***************************************************************
* Routine Area *
***************************************************************/
char IC_CardInsert(void)
{
return InitIccard();
}
/********************************************************************************
* 函数说明: Send one byte to AT45DB041
* 输入参数: send byte
* 输出参数: void
* 返回值: void
* 说明: 在SCK的电平下降沿检测信号
********************************************************************************/
void SendByte(byte sendByte)
{
byte i;
// Flash_SCK=0; // del e_fm
// Flash_CS=0;
for (i=0; i<8; i++)
{
// sendByte <<= 1;
// Flash_SI = CY;
if ((sendByte & 0x80) == 0x80)
{
Flash_SI = 1;
}
else
{
Flash_SI = 0;
}
Flash_SCK = 1;
Flash_SCK = 0;
sendByte <<= 1;
}
}
/********************************************************************************
* 函数说明: get one byte from AT45DB041
* 输入参数: void
* 输出参数: void
* 返回值: the getted byte
* 说明: 在SCK的电平下降沿检测信号
********************************************************************************/
byte GetByte(void)
{
byte i;
byte tmp_read_data = 0;
for(i=8; i>0; i--)
{
Flash_SCK = 1;
Flash_SCK = 0;
tmp_read_data = tmp_read_data * 2 +(byte)Flash_SO;
}
return ((byte)tmp_read_data);
}
/********************************************************************************
* 函数说明: write buff data to main flash
* 输入参数: buff No. and main flash start page
* 输出参数: void
* 返回值: the getted byte
********************************************************************************/
void WriteBuffToMain(WORD startPage, byte buffNo)
{
dword tmp;
byte i;
WORD timeOut = 0;
Flash_CS = 0;
// disint();
if (buffNo == 1)
{
SendByte(0x83);
}
else
{
SendByte(0x86);
}
tmp = startPage;
tmp <<= 9;
SendByte((byte)(tmp >>16));
SendByte((byte)(tmp >>8));
SendByte((byte)tmp );
// enint();
for (i=0; i<10; i++);
Flash_CS = 1;
for (i=0; i<3; i++);
while (!Flash_RB)
{
timeOut++;
if (timeOut >= 0xfff0)
{
break;
}
}
}
/********************************************************************************
* 函数说明: write data to buff(flash)
* 输入参数: buff No., buff addr, len and source string pointer
* 输出参数: void
* 返回值: void
********************************************************************************/
void WriteToBuff(byte buffNo, WORD addr, WORD len, byte *src)
{
Flash_CS = 0;
// disint();
if (buffNo == 1)
{
SendByte(0x84);
}
else
{
SendByte(0x87);
}
SendByte(0x00);
SendByte((byte)(addr>>8));
SendByte((byte)addr);
for (; len>0; len--)
{
SendByte(*src++);
}
// enint();
Flash_CS = 1;
}
/********************************************************************************
* 函数说明: write data to main(flash)
* 输入参数: buff No., buff addr, len and source string pointer
* 输出参数: void
* 返回值: void
********************************************************************************/
void FlashPageWrite(WORD startPage, WORD startAddr, WORD len, byte *dest)
{
WriteToBuff(1, startAddr, len, dest);
WriteBuffToMain(startPage, 1);
}
/********************************************************************************
* 函数说明: read data from main(flash)
* 输入参数: buff No., buff addr, len and source string pointer
* 输出参数: void
* 返回值: void
********************************************************************************/
void FlashPageRead(WORD startPage, WORD startAddr, WORD len, byte *dest)
{
WORD i;
dword tmp = 0;
Flash_CS = 0;
tmp = startPage;
// disint();
SendByte(0x52);
tmp <<= 9;
tmp = tmp + startAddr;
SendByte((byte)(tmp >> 16));
SendByte((byte)(tmp >> 8));
SendByte((byte)tmp);
SendByte(0x00);
SendByte(0x00);
SendByte(0x00);
SendByte(0x00);
for (i=0; i<10; i++);
for (i=0; i<len; i++)
{
*dest++ = GetByte();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -