📄 haltcl.c
字号:
/* ============================================================================ Project Name : jayaCard Module Name : proto/hal/<template>/haltcl.c Version : $Id: haltcl.c,v 1.18 2004/01/11 09:56:33 dgil Exp $ Description: T=CL send/receive blocks The Original Code is jayaCard code. The Initial Developer of the Original Code is Gilles Dumortier. Portions created by the Initial Developer are Copyright (C) 2002-2004 the Initial Developer. All Rights Reserved. Contributor(s): This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; see http://www.gnu.org/licenses/gpl.html History Rev Description 011903 dgil wrote it from scratch ============================================================================*/#include "precomp.h"/* ========================================================================= halrom.c ========================================================================= */extern jbyte code tcl_sync[4];/* ========================================================================= __hal_tcl_setbaudrate() Change the T=CL baudrate ========================================================================= */void __hal_tcl_setbaudrate(jbyte baudrate){ /* change the baudrate now - update some register(s) */ /* update */ tcl_baudrate = baudrate;}/* ========================================================================= __hal_tcl_sendinit() If FIFO : init the FIFO for sending + store byte Otherwise : init the CRC + calculate CRC of byte + send some SYNC (based on the current baudrate) + send byte ========================================================================= */void __hal_tcl_sendinit(jbyte by){ LOCAL(jbyte,nsync); nsync = tcl_sync[tcl_baudrate&0x03]; /* init the CRC */ HAL_TCL_INIT_CRC(); /* calculate CRC of byte */ /* prepare to send SYNC */ /* send some SYNC */lsync: if (nsync>0) { nsync--; if (nsync!=0) { /* send the SYNC 0xFF and wait send finished */ goto lsync; } else { /* send the final SYNC 0x7F wait send finished */ } } /* prepare to send DATA */ /* send byte */}/* ========================================================================= __hal_tcl_send() If FIFO : store byte in the FIFO Otherwise : wait + send + CRC ========================================================================= */void __hal_tcl_send(jbyte by){ /* wait previous send finished */ /* send byte */ /* calculate CRC of byte */ HAL_TCL_CRC(by);}/* ========================================================================= __hal_tcl_sendbuf() send a buffer of bytes ========================================================================= */void __hal_tcl_sendbuf(jbyte xdata * buf, jbyte n){ // ToBeImplemented more efficient LOCAL(jbyte,i); for (i=0; i<n; i++) __hal_tcl_send(buf[i]);}/* ========================================================================= __hal_tcl_sendgo() If FIFO : start sending the FIFO content Otherwise : send the CRC managing the special framing at high speed If bWaitSent, wait for the send finished ========================================================================= */void __hal_tcl_sendgo(jbool bWaitSent){ #if fifo /* start FIFO sending */ /* wait end of send */ if (bWaitSent) HAL_WAIT(); #else /* wait previous send finished */ /* send CRC LO */ /* wait previous send finished */ if (tcl_baudrate!=BAUDRATE_106) { /* special framing for the last byte set some register(s) */ } /* send CRC HI */ /* wait send finished */ if (bWaitSent) { } #endif}/* ========================================================================= __hal_tcl_receive() ========================================================================= */jbyte __hal_tcl_receive(jbyte xdata * buf,jbyte n,jbool isRaw){ #if fifo /* enable wakeup on receive */ /* wait TX interrupt to signal the receive completion */ HAL_WAIT(); /* get number bytes of the received buffer */ tcl_cnt = 0x00 /* __y read some register */; /* check the message size. Too short or too big ? */ if (tcl_cnt<3) return 0; if (tcl_cnt>n) return 0; /* initialisation of CRC module for */ HAL_TCL_INIT_CRC(); if (!isRaw) { /* receive PCB */ tcl_pcb = 0x00 /* __y read byte from the fifo */; HAL_TCL_CRC(tcl_pcb); tcl_cnt--; /* receive CID ? */ if ((tcl_pcb&TCL_MASK_CID)==TCL_MASK_CID) { tcl_cid = 0x00 /* __y read byte from the fifo */; HAL_TCL_CRC(tcl_cid); tcl_cid--; } /* receive NAD ? */ if ((tcl_pcb&TCL_MASK_NAD)==TCL_MASK_NAD) { tcl_nad = 0x00 /* __y read byte from the fifo */; HAL_TCL_CRC(tcl_nad); tcl_cnt--; } } if (tcl_cnt<2) return 0; /* calculate and store each received byte */ for (n=0; n<tcl_cnt-2; n++) { /* read FIFO */ d = 0x00 /* __y read byte from FIFO */; /* calculate CRC */ HAL_TCL_CRC(d); /* store in buffer */ buf[n] = d; } /* check the CRC */ if (0/* __y read fifo */ != HAL_TCL_LOCRC()) return 0; if (0/* __y read fifo */ != HAL_TCL_HICRC()) return 0; return tcl_cnt; #else /* b1 and b2 are temporary. At the end of the reception, they store the two bytes of the received CRC */ register jbyte b1; register jbyte b2; /* be sure transmission ended */ /* prepare to receive */ /* init the CRC */ HAL_TCL_INIT_CRC(); /* switch register(s) */ /* Clear receive status */ if (!isRaw) { /* receive PCB */ while (!1/* __y wait our char*/); if (/* __y end of frame*/0) goto err; tcl_pcb = 0x00/* __y read byte from the register */; /* receive CID ? */ if ((tcl_pcb&TCL_MASK_CID)==TCL_MASK_CID) { while (!1/* __y wait our char*/) ; if (/*__y end of frame*/0) goto err; tcl_cid = 0x00/* __y read byte from the register */; } /* receive NAD ? */ if ((tcl_pcb&TCL_MASK_NAD)==TCL_MASK_NAD) { while (!1/* __y wait our char*/); if (/* __y end of frame*/0) goto err; tcl_nad = 0x00/* __y read byte from the register */; } } /* initialise number of bytes */ tcl_cnt = 0x0; /* receive until EOT */receive: /* wait a char */ while (!1 /* __y wait our char*/) ; if (0/* __y end of trame*/) goto last_byte; if (tcl_cnt==0) { b1 = 0x00/* __y get the char */; } else { if (tcl_cnt==1) { b2 = b1; b1 = 0x00 /* __y get the char*/; } else { buf[tcl_cnt-2] = b2; b2 = b1; b1 = 0x00 /* __y get the char */; } } /* one more char in the buffer */ tcl_cnt++; /* check we have enough space */ if (tcl_cnt>=n) goto err; /* receive next char */ goto receive;last_byte: /* check message size. Too short ? */ if (tcl_cnt<3) goto err; /* for somme component, check end of frame size */ if (0/* __y error */) goto err; /* for somme component, need to dummy read the end of frame */ b1 = 0x00 /* __y get char */; /* check the CRC */ if (0 != HAL_TCL_LOCRC()) goto err; if (0 != HAL_TCL_HICRC()) goto err; if (0/* __y overrun or parity error or other error*/) goto err; return tcl_cnt;err: /* dummy read / if needed by the component */ b1 = 0x00 /* __y get char */; /* wait until end of frame */ while (0 /* __y end of frame detected */); /* return 0 bytes received because of some error */ return 0; #endif}/* ========================================================================= That's all folks ! ========================================================================= */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -