/*---------------------------------------------------------------------------------------------*/
/* (c) Copyright Tokheim, unpublished work, created 2009. */
/* This computer program includes Confidential, Proprietary Information and is a Trade */
/* Secret of Tokheim. */
/* All use, disclosure, and / or reproduction is prohibited unless authorized in writing. */
/* All Rights Reserved. */
/*---------------------------------------------------------------------------------------------*/
/*
* $Header: //TNG/Software/Periph/UI/Src/ui_carddriver.c
*
* File : ui_carddriver.c
* Purpose : card driver function
* Status :
*
* Record of changes :
*
* Version Date Author Comment
* 00.01 08.08.09 Tang creation
*---------------------------------------------------------------------------------------------*/
/*-------------------------------includes------------------------------------*/
/* System includes */
#include <string.h> /* ARM library header files "memcpy" */
#include <stdio.h> /* printf library */
/* common includes */
#include <Tokheim.h>
#include <TqcDeviceInterface.h>
#include <TqcPeriphMsgs.h>
#include <can_lib.h> /* CAN library header files */
#include <crc.h>
#include <TqcNames.h>
#include <lpc23xx.h> /* LPC23XX processor declarations */
#include <can_obj.h> /* Not yet common ==> to be done ?? */
/* local includes */
#include <ui_main.h>
#include <ui_can.h>
#include <ui_general.h>
#include <ui_keyboard.h>
#include <ui_operation.h>
#include <ui_carddriver.h>
void set_tda_direct(BYTE cmd);
static RC_TYPE readByte(void);
static void writeByte(BYTE onebyte);
void tda_writeregister(BYTE address, BYTE value);
void tda_init(void);
BYTE tda_readregister(BYTE address);
BYTE tda_read_driver(BYTE address);
void tda_write_driver(BYTE address,BYTE val);
BYTE lockcard(BYTE rc);
void clearATRstruct(struct ATR*);
int tda_ATRsequence(BYTE mode, BYTE *ucbuff) ;
extern void confOutputPin(BYTE PortName, DWORD PinName);
extern void confInputPin(BYTE PortName, DWORD PinName);
extern void delay_us(UINT usnum);
extern void Delay_us(UINT usnum);
extern void __irq irq_uart0_handle(void);
BYTE workingBuffer[512];
BYTE ATRLength[CARD_SLOTS];
struct ATR lastATR[CARD_SLOTS];
BYTE TMode[CARD_SLOTS]; // T=0 or T=1
DWORD WWT[CARD_SLOTS]; // Work wait time
DWORD CWT[CARD_SLOTS]; // Character wait time
DWORD BWT[CARD_SLOTS]; // Block wait time
DWORD EDCtype[CARD_SLOTS];
BYTE NAD[CARD_SLOTS]; // Node address byte to use when communicating in T=1
BYTE IFSC[CARD_SLOTS]; // Maximum segment length when sending to card in T=1
BYTE currentSlot = 0;
/*----------------------------------------------------------------------------
* Function : clearATRStruct(struct ATR *myatr)
*
* Purpose : clear ATR
*
* Inputs : ---
* Outputs : ---
* Returns : ---
*
* Version Date Author Comment
* 00.01 03.08.2009 tang initiale template
*
*--------------------------------------------------------------------------*/
void clearATRStruct(struct ATR *myatr)
{
memset(myatr, 0xFF, sizeof(struct ATR));
myatr->HistoricalLength = 0;
}
/*----------------------------------------------------------------------------
* Function : update_crc(BYTE value, UINT crc)
*
* Purpose : calculate value
*
* Inputs : ---
* Outputs : ---
* Returns : crc val
*
* Version Date Author Comment
* 00.01 03.08.2009 tang initiale template
*
*--------------------------------------------------------------------------*/
UINT update_crc(BYTE value, UINT crc)
{
int i;
UINT newval = value << 8;
for (i = 0;i < 8;i++)
{
if ((crc ^ newval) & 0x8000)
{
crc <<= 1;
crc ^= 0x1021;
}
else
{
crc <<= 1;
}
newval <<= 1;
}
return crc;
}
/*----------------------------------------------------------------------------
* Function : readByte()
*
* Purpose : Read a byte from the UART or timeout after BWT (T=1), CWT (T=1) or WWT (T=0).
*
* Inputs : ---
* Outputs : ---
* Returns : ---
*
* Version Date Author Comment
* 00.01 03.08.2009 tang initiale template
*
*--------------------------------------------------------------------------*/
static RC_TYPE readByte(void)
{
BYTE val;
WORD timeout;
while(!(tda_readregister(MSR) & MSR_TBE_RBF_MASK))
{
val = tda_readregister(USR);
if (val &USR_PE_MASK)
{
return ERR_RECEIVE_PARITY;
}
if (val & (USR_TOL3_MASK|USR_TOL2_MASK|USR_TOL1_MASK))
{
return ERR_RECEIVE_TIMEOUT;
}
}
// Read and store byte
val = tda_readregister(URR);
if (TMode[currentSlot] == 0)
timeout = WWT[currentSlot]; // Use WWT for T=0
else
timeout = CWT[currentSlot]; // Use CWT for T=1
// Set up timer for 24 bit timer, start immediately
tda_writeregister(TOC, 0x00);
tda_writeregister(TOR3, timeout >> 16);
tda_writeregister(TOR2, timeout >> 8);
tda_writeregister(TOR1, timeout);
tda_writeregister(TOC, 0x68);
return val;
}
/*----------------------------------------------------------------------------
* Function : writeByte(BYTE onebyte)
*
* Purpose : Write a byte and leave the TDA8007 in transmit mode
*
* Inputs : ---
* Outputs : ---
* Returns : ---
*
* Version Date Author Comment
* 00.01 03.08.2009 tang initiale template
*
*--------------------------------------------------------------------------*/
static void writeByte(BYTE onebyte)
{
BYTE val;
int ncount = 3;
val = tda_readregister(UCR1);
// set T bit
tda_writeregister(UCR1,val | UCR1_T_R_MASK);
tda_writeregister(UTR,onebyte);
// wait for byte to go out
while ((!(tda_readregister(USR) & USR_TBE_RBF_MASK))&ncount);
}
/*----------------------------------------------------------------------------
* Function : writeByte(BYTE onebyte)
*
* Purpose :Write a byte and put the TDA8007 in receive mode
*
* Inputs : ---
* Outputs : ---
* Returns : ---
*
* Version Date Author Comment
* 00.01 03.08.2009 tang initiale template
*
*--------------------------------------------------------------------------*/
void writeLastByte(BYTE onebyte)
{
BYTE val;
WORD timeout;
int ncount = 5;
if (TMode[currentSlot] == 0)
timeout = WWT[currentSlot]; // Use WWT for T=0
else
timeout = BWT[currentSlot]; // Use BWT for T=1
// Set up timer for 24 bit timer, edge trigger start
tda_writeregister(TOC,0x00);
tda_writeregister(TOR3,timeout >> 16);
tda_writeregister(TOR2,timeout >> 8);
tda_writeregister(TOR1,timeout);
tda_writeregister(TOC,0x7C);
val = tda_readregister(UCR1);
// set LCT and T bit
tda_writeregister(UCR1,val | (UCR1_T_R_MASK|UCR1_LCT_MASK));
tda_writeregister(UTR,onebyte);
// wait for byte to go out
while ((!(tda_readregister(USR) & USR_TBE_RBF_MASK))& ncount);
}
/*----------------------------------------------------------------------------
* Function : generateEDC(BYTE type, BYTE onebyte, UINT value)
*
* Purpose :Generate Error Data Check value depending on EDC type
*
* Inputs : ---
* Outputs : ---
* Returns : ---
*
* Version Date Author Comment
* 00.01 03.08.2009 tang initiale template
*
*--------------------------------------------------------------------------*/
UINT generateEDC(BYTE type, BYTE onebyte, UINT value)
{
if (type == ED