📄 lll_msr.c
字号:
/****************************************************************************
* *
* File: LLL_MSR.C (Source) *
* *
* Description: 89C52-Firmware for mifare (R) Serial Reader (MSR + SerMMM) *
* Low-Level-Library for access to MIFSLS1x2 *
* *
*****************************************************************************
* *
* Version | Date | Description *
* --------+----------+----------------------------------------------------- *
* | | TIMEOUT = 10 (1.0 ms) *
* | | MEDIUM_TIMEOUT = 16 (1.5 ms) *
* | | LONG_TIMEOUT = 95 (9.0 ms) *
* | | mifs_write, mifs_transfer: SHORT_TIMEOUT -> MEDIUM_T.*
* | | authentication_2, decrement_transfer *
* | | delay after reset 200us *
* *
****************************************************************************/
#include <reg52.h>
#include <string.h>
#include <intrins.h>
#include "msr.h"
#define __SRC
#include "lll_msr.h"
#undef __SRC
#define NO_TIMER2
// Local Defines
#define TIMEOUT 10 // 1.0 ms (0.944 ms)
#define MEDIUM_TIMEOUT 16 // 1.5 ms
#define LONG_TIMEOUT 95 // 9.0 ms
// Local Variables
static uchar __Mode = 0xC6;
static uchar __Divider = 0x0E;
static uchar mif_control = 0x0C;
/****************************************************************************
* *
* Function: mifs_request *
* *
* Input: Mode *
* Output: TagType *
* *
****************************************************************************/
uchar mifs_request(uchar _Mode, uchar idata *_TagType)
{
uchar _Status;
uchar _RetCode;
do
{
TOGGLE_WD();
MCM_CS=0;
MCM_STACON = _SOR | mif_control;
MCM_BAUDRATE = __Divider;
MCM_ENABLE = _PEN | _PRE;
MCM_MODE = __Mode;
MCM_STACON = mif_control;
MCM_RCODE = 3;
MCM_BCNTS = 7;
MCM_BCNTR = 16;
if (_Mode & ALL)
{
MCM_DATA = _REQUEST_ALL;
}
else
{
MCM_DATA = _REQUEST;
}
MCM_TOC = TIMEOUT;
while (!((_Status = MCM_STACON) & _DV));
MCM_TOC = 0;
if (_Status & (_TE | _BE))
{
_TagType[0] = 0;
_TagType[1] = 0;
if (_Status & _TE)
{
_RetCode = W_NOTAG;
}
else
{
_RetCode = W_BITCNT;
delay_50us(10);
}
}
else
{
_TagType[0] = MCM_DATA;
_TagType[1] = MCM_DATA;
_RetCode = OK;
break;
}
} while (!CmdReceived && (_Mode & EVER));
MCM_CS=1;
return (_RetCode);
}
/****************************************************************************
* *
* Function: mifs_anticoll *
* *
* Input: Bcnt, SNR *
* Output: SNR *
* *
****************************************************************************/
uchar mifs_anticoll(uchar _Bcnt, uchar idata *_SNR)
{
uchar i;
uchar _SnrChk, _SnrChkTmp;
uchar _Status;
uchar _Retcode;
TOGGLE_WD();
MCM_CS=0;
MCM_BCNTS = _Bcnt + 16;
MCM_STACON = _AC | mif_control;
delay_50us(1); // Bug Delay: min. 35 ms
MCM_DATA = 0x93;
MCM_DATA = ((2 + (_Bcnt >> 3)) << 4) | (_Bcnt & 0x07);
for (i = SERNR_in - SERNR; i < ((_Bcnt + 7)/8 + SERNR_in - SERNR); i++)
{
MCM_DATA = _SNR[i];
}
MCM_TOC = TIMEOUT;
while (!((_Status = MCM_STACON) & _DV));
MCM_TOC = 0;
if (_Status & _TE)
{
_Retcode = W_NOTAG;
}
else
{
if (_Status & _BE)
{
_Retcode = W_BITCNT;
delay_50us(10);
}
else
{
for (i = 0, _SnrChkTmp = 0; i < 4; i++)
{
_SNR[i] = MCM_DATA;
_SnrChkTmp ^= _SNR[i];
}
_SnrChk = MCM_DATA;
if (_SnrChk != _SnrChkTmp)
{
_Retcode = W_SERNR;
}
else
{
_Retcode = OK;
}
}
}
MCM_CS=1;
return (_Retcode);
}
/****************************************************************************
* *
* Function: mifs_select *
* *
* Input: SNR *
* Output: Size *
* *
****************************************************************************/
uchar mifs_select(uchar idata *_SNR, uchar idata *_Size)
{
uchar _Status;
uchar _Retcode;
TOGGLE_WD();
MCM_CS=0;
MCM_ENABLE = _PEN | _CEN | _CRE | _PRE;
MCM_BCNTS = 56;
MCM_BCNTR = 8;
MCM_DATA = 0x93;
MCM_DATA = 0x70;
MCM_DATA = _SNR[0];
MCM_DATA = _SNR[1];
MCM_DATA = _SNR[2];
MCM_DATA = _SNR[3];
MCM_DATA = _SNR[0] ^ _SNR[1] ^ _SNR[2] ^ _SNR[3];
MCM_TOC = TIMEOUT;
while (!((_Status = MCM_STACON) & _DV));
MCM_TOC = 0;
if (_Status & _TE)
{
_Retcode = W_NOTAG;
}
else
{
if (_Status & (_PE | _BE | _CE))
{
if (_Status & _BE)
{
_Retcode = W_BITCNT;
delay_50us(10);
}
else
{
if (_Status & _PE)
{
_Retcode = W_PARITY;
}
else
{
_Retcode = W_CRC;
}
}
}
else
{
*_Size = MCM_DATA;
_Retcode = OK;
}
}
MCM_CS=1;
return (_Retcode);
}
/****************************************************************************
* *
* Function: mifs_authentication *
* *
* Input: Mode, SecNr *
* Output: - *
* *
****************************************************************************/
uchar mifs_authentication(uchar _Mode, uchar _SecNr)
{
uchar _Fkt;
uchar _Status;
uchar _Retcode;
TOGGLE_WD();
MCM_CS=0;
if (_Mode & KEYB)
{
_Fkt = 0x61;
_SecNr |= _KEYB;
}
else
{
_Fkt = 0x60;
_SecNr |= _KEYA;
}
MCM_TOC = TIMEOUT;
MCM_BCNTS = 16;
MCM_KEYSTACON = _AUTH | (_Mode & 0x03);
MCM_KEYADR = _AUTH | _SecNr;
MCM_DATA = _Fkt;
MCM_DATA = _SecNr << 2;
while (!((_Status = MCM_STACON) & _DV));
MCM_TOC = 0;
if (_Status & (_TE | _AE | _PE | _BE))
{
if (_Status & _TE)
{
_Retcode = W_NOTAG;
}
else
{
if (_Status & _AE)
{
_Retcode = W_AUTH;
}
else
{
if (_Status & _PE)
{
_Retcode = W_PARITY;
}
else
{
delay_50us(10);
if ((MCM_DATA & 0x0A) == 0x00)
{
_Retcode = W_AUTH;
}
else
{
_Retcode = W_BITCNT;
}
}
}
}
}
else
{
_Retcode = OK;
}
MCM_CS=1;
return (_Retcode);
}
/****************************************************************************
* *
* Function: mifs_authentication_2 *
* *
* Input: Mode, KeyNr, Address *
* Output: - *
* *
****************************************************************************/
uchar mifs_authentication_2(uchar _Mode, uchar _KeyNr, uchar _Address)
{
uchar _Fkt;
uchar _Status;
uchar _Retcode;
TOGGLE_WD();
MCM_CS=0;
if (_Mode & KEYB)
{
_Fkt = 0x61;
_KeyNr |= _KEYB;
}
else
{
_Fkt = 0x60;
_KeyNr |= _KEYA;
}
MCM_TOC = TIMEOUT;
MCM_BCNTS = 16;
MCM_KEYSTACON = _AUTH | (_Mode & 0x03);
MCM_KEYADR = _AUTH | _KeyNr;
MCM_DATA = _Fkt;
MCM_DATA = _Address;
while (!((_Status = MCM_STACON) & _DV));
MCM_TOC = 0;
if (_Status & (_TE | _AE | _PE | _BE))
{
if (_Status & _TE)
{
_Retcode = W_NOTAG;
}
else
{
if (_Status & _AE)
{
_Retcode = W_AUTH;
}
else
{
if (_Status & _PE)
{
_Retcode = W_PARITY;
}
else
{
delay_50us(10);
if ((MCM_DATA & 0x0A) == 0x00)
{
_Retcode = W_AUTH;
}
else
{
_Retcode = W_BITCNT;
}
}
}
}
}
else
{
_Retcode = OK;
}
MCM_CS=1;
return (_Retcode);
}
/****************************************************************************
* *
* Function: mifs_halt *
* *
* Input: - *
* Output: - *
* *
****************************************************************************/
uchar mifs_halt(void)
{
uchar _Status;
uchar _Retcode;
TOGGLE_WD();
MCM_CS=0;
MCM_BCNTS = 16;
MCM_BCNTR = 4;
MCM_DATA = 0x50;
MCM_DATA = 0;
MCM_TOC = TIMEOUT;
while (!((_Status = MCM_STACON) & _DV));
MCM_TOC = 0;
if (_Status & _TE)
{
_Retcode = OK;
}
else
{
_Retcode = W_CODE;
}
MCM_CS=1;
return (_Retcode);
}
/****************************************************************************
* *
* Function: mifs_read *
* *
* Input: Adr *
* Output: Data *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -