TsplReport.c //
/******************************************************************************
* *
* M O D U L E D E F I N E *
* *
******************************************************************************/
#define TSPLREPORT_C
/******************************************************************************
* *
* C O M P I L E R D E F I N E D I N C L U D E F I L E S *
* *
******************************************************************************/
/******************************************************************************
* *
* U S E R D E F I N E D I N C L U D E F I L E S *
* *
******************************************************************************/
#include "Common.h"
#include "XCore.h"
#include "TsplReport.h"
/******************************************************************************
* *
* L O C A L D E F I N E S *
* *
******************************************************************************/
#if defined(TSPL)
/******************************************************************************
* *
* L O C A L T Y P E D E F S *
* *
******************************************************************************/
/******************************************************************************
* *
* L O C A L F U N C T I O N P R O T O T Y P E S *
* *
******************************************************************************/
/******************************************************************************
* *
* L O C A L I N I T I A L I Z E D D A T A D E F I N I T I O N S *
* *
******************************************************************************/
/******************************************************************************
* *
* L O C A L U N I T I A L I Z E D D A T A D E F I N I T I O N S *
* *
******************************************************************************/
/******************************************************************************
*
* Function:
* OutLineToRS232
*
* Description:
*
*
* Input:
* None
*
* Output:
* None.
*
******************************************************************************/
VOID InitialTsplReport(VOID)
{
}
#endif
TsplReport.h /
#ifndef TSPLREPORT_H
/******************************************************************************
* *
* M O D U L E D E F I N E *
* *
******************************************************************************/
#define TSPLREPORT_H
/******************************************************************************
* *
* C O M P I L E R D E F I N E D I N C L U D E F I L E S *
* *
******************************************************************************/
/* None */
/******************************************************************************
* *
* U S E R D E F I N E D I N C L U D E F I L E S *
* *
******************************************************************************/
#ifdef __cplusplus
extern "C" { /* Assume C declarations for C++ */
#endif /* __cplusplus */
/******************************************************************************
* *
* G L O B A L D E F I N E S *
* *
******************************************************************************/
/* None */
/******************************************************************************
* *
* S T R U C T U R E D E F I N I T I O N S *
* *
******************************************************************************/
/* None */
/******************************************************************************
* *
* G L O B A L V A R I A B L E S - N O I N I T I A L I Z E R S *
* *
******************************************************************************/
/* None */
/******************************************************************************
* *
* G L O B A L V A R I A B L E S - I N I T I A L I Z E R S *
* *
******************************************************************************/
/* None */
/******************************************************************************
* *
* F U N C T I O N P R O T O T Y P E S *
* *
******************************************************************************/
VOID InitialTsplReport(VOID);
#ifdef __cplusplus
} /* End of extern "C" { */
#endif /* __cplusplus */
#endif
TsplRfidFunc.c /
/******************************************************************************
* *
* M O D U L E D E F I N E *
* *
******************************************************************************/
#define TSPLRFIDFUNC_C
/******************************************************************************
* *
* C O M P I L E R D E F I N E D I N C L U D E F I L E S *
* *
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
/******************************************************************************
* *
* U S E R D E F I N E D I N C L U D E F I L E S *
* *
******************************************************************************/
#include "Common.h"
#include "XCore.h"
#include "XTask.h"
#include "XComm.h"
#include "XKey.h"
#include "XPrtEng.h"
#include "XTimer.h"
#include "XParser.h"
#include "XLED.h"
#include "XProFile.h"
#include "XFuseFlash.h"
#include "XVarBank.h"
#include "XBuzzer.h"
#include "..\ParserUtil.h"
#include "TsplUtil.h"
#include "TsplCnt.h"
#if defined(DURALABEL)
/******************************************************************************
* *
* L O C A L D E F I N E S *
* *
******************************************************************************/
//#define DEBUG_RF
//#define PROGRAM_TAG
//#define READER_TAG
#define LOCK_TAG
#define TAG_WITH_SS
#define UID_LENGTH 8
#define COMMAND_START 0x02
#define COMMAND_END 0x03
#define COMMAND_INVENTORY 0x41
#define COMMAND_SELECT 0x42
#define COMMAND_READ_BLOCK 0x43
#define COMMAND_WRITE_BLOCK 0x46
#define COMMAND_SUCCESS 0xE0
#define LABEL_BLOCK 7
#define CHKSUM_BLOCK 8
#define MILAGE_BLOCK 9
#define NEW_IDENTIFY_BLOCK 1
#define NEW_LABEL_BLOCK 2
#define NEW_CHKSUM_BLOCK 3
#define NEW_MILAGE_BLOCK 4
#define RFID_RETRY_TIME 3
// RESPONSE CODE
#define SELECT_TAG_PASS 0x14
#define READ_TAG_PASS 0x24
#define WRITE_TAG_PASS 0x44
#define EXPR_TOTAL 8
#define FUNC_MAX 5
#define OPERATOR_MAX 8 // tid num 0 ~ 7
#define OPERAND_KIND 6 // kind + - * / ^ %
#if defined(BIG_ENDIAN)
#define IDENTIFY_DATA_0 0x5AA50000
#define IDENTIFY_DATA_1 0x5AA50001
#else
#define IDENTIFY_DATA_0 0x0000A55A
#define IDENTIFY_DATA_1 0x0100A55A
#endif
#define DEFAULT_RETRY_TIMES 3
#define DEFAULT_DETECT_SCALE (INT)(TPH_DPI / 2)
#define DEFAULT_LIMIT_MISS_LENGTH (INT)(TPH_DPI * 15)
// error code
#define DONT_FIND_MODULE 0
#define MISS_ERROR 1
#define NAME_ERROR 2
#define MILAGE_ERROR 3
/******************************************************************************
* *
* L O C A L T Y P E D E F S *
* *
******************************************************************************/
typedef enum
{
FORMAT_OLD,
FORMAT_1,
}_eTAG_FORMAT;
typedef enum
{
CHECK_NAME,
CHECK_CHECKSUM,
}_eCHECK_FORMAT;
typedef struct
{
WORD Operator1;
WORD Operator2;
BYTE Operand;
}_Expression;
typedef struct
{
_Expression sExpr[EXPR_TOTAL];
BOOL Enable;
}_Function;
typedef struct
{
BYTE Data[EXPR_TOTAL];
BOOL Enable;
}_TIDEncode;
/******************************************************************************
* *
* L O C A L F U N C T I O N P R O T O T Y P E S *
* *
******************************************************************************/
STATIC VOID CheckDuraLabel(VOID);
VOID InitailDuraLabel(VOID);
/******************************************************************************
* *
* L O C A L I N I T I A L I Z E D D A T A D E F I N I T I O N S *
* *
******************************************************************************/
// Commands
BYTE CMD_GetTID[5] = {0x02, 0x05, 0x20, 0x14, '\0'};
BYTE CMD_PutTID2SS[5] = {0x02, 0x0d, 0x68, 0x14, '\0'};
STATIC BYTE MSCodeRadom[256] =
{
0x9F, 0x98, 0xC7, 0x2F, 0x62, 0xC9, 0x63, 0x2E, 0x9A, 0xF9, 0xBD, 0x4B, 0xC6, 0x4A, 0x99, 0x77,
0x44, 0xA5, 0xF4, 0x3B, 0xA0, 0x35, 0x37, 0xE0, 0x18, 0xFD, 0xE5, 0x55, 0x36, 0xD5, 0x47, 0xBC,
0x97, 0xFF, 0xC5, 0x48, 0x2D, 0xE6, 0xD4, 0xD7, 0x9E, 0xDA, 0x11, 0x30, 0x78, 0x0F, 0xF2, 0xA4,
0xFA, 0x49, 0x61, 0x0E, 0xF1, 0x84, 0x1F, 0x8A, 0xF3, 0x3A, 0x2B, 0xE8, 0xD3, 0x85, 0x25, 0x01,
0x54, 0xD2, 0x16, 0xE4, 0x83, 0x53, 0x75, 0x7D, 0x00, 0x7A, 0xDD, 0x74, 0x89, 0x1E, 0xBF, 0x2C,
0xDB, 0x20, 0x1B, 0xD9, 0x15, 0xB8, 0x79, 0xE1, 0x73, 0xA9, 0x57, 0x02, 0x10, 0x76, 0x22, 0x03,
0x52, 0x09, 0xCF, 0x17, 0xBB, 0x5C, 0x5D, 0x21, 0xC4, 0x5E, 0xFB, 0x58, 0x0D, 0xEC, 0x4F, 0x3D,
0xA2, 0x3E, 0x81, 0x9D, 0x24, 0x67, 0xC8, 0x43, 0x60, 0x12, 0x50, 0x7E, 0x23, 0x72, 0x06, 0x2A,
0x9C, 0xF8, 0x51, 0x8E, 0x8F, 0x66, 0x56, 0x88, 0xA7, 0x80, 0x9B, 0xA8, 0xC0, 0x5A, 0x32, 0x04,
0xCE, 0x4E, 0x8D, 0x1D, 0x59, 0xE7, 0x39, 0xAD, 0xB0, 0xE2, 0x19, 0x6F, 0xCB, 0xD6, 0xCC, 0xB3,
0xBA, 0xAA, 0x7F, 0x71, 0xC3, 0x65, 0x07, 0xB1, 0x64, 0xEA, 0x70, 0x69, 0xAB, 0x05, 0xCD, 0x13,
0x31, 0xED, 0xA6, 0xB2, 0x90, 0x08, 0xDF, 0xA3, 0x3F, 0x68, 0xD8, 0x6A, 0xE9, 0xEB, 0xAE, 0x96,
0x26, 0x38, 0x5B, 0x0C, 0xD0, 0x29, 0xE3, 0x34, 0x46, 0xDE, 0x7C, 0x1A, 0x0A, 0x8C, 0xCA, 0x1C,
0x82, 0xF7, 0xA1, 0xB9, 0x0B, 0x87, 0x5F, 0x7B, 0x3C, 0x8B, 0xF5, 0xF6, 0xAF, 0x27, 0xAC, 0xFE,
0x33, 0xBE, 0xB4, 0xFC, 0x4C, 0x6B, 0x28, 0x42, 0xC1, 0x6C, 0x91, 0x14, 0x95, 0x93, 0xEF, 0x94,
0x86, 0xB6, 0x45, 0x40, 0xD1, 0x92, 0x41, 0xEE, 0xF0, 0x4D, 0x6D, 0xC2, 0xB7, 0x6E, 0xB5, 0xDC,
};
STATIC CONST BYTE SecretArray[256] =
{
0xD7, 0xB1, 0x7D, 0x75, 0x89, 0x62, 0x5B, 0xB8, 0x9B, 0x64, 0x3D, 0x4D, 0x6E, 0x69, 0xAC, 0xF3,
0x94, 0x11, 0x1A, 0x1C, 0x5D, 0xF5, 0x7B, 0xC3, 0xCE, 0xE1, 0x09, 0x3A, 0x51, 0x86, 0xE3, 0x96,
0x61, 0x2D, 0xA9, 0x43, 0x0E, 0x57, 0x49, 0x17, 0x56, 0x76, 0xDB, 0xD6, 0xD5, 0x46, 0xBB, 0x7C,
0xE9, 0x28, 0x35, 0xAD, 0xD1, 0x81, 0xB5, 0xA7, 0x92, 0xEC, 0xEF, 0x20, 0x41, 0x72, 0xA6, 0xDF,
0xC6, 0xE7, 0xA3, 0x5C, 0x14, 0x42, 0x90, 0x5F, 0x23, 0xC8, 0xAB, 0xBC, 0x5A, 0xBD, 0x6A, 0x7E,
0xCC, 0x85, 0xEE, 0xEB, 0xED, 0x74, 0x0B, 0x73, 0x82, 0x63, 0xE0, 0x50, 0xFA, 0x45, 0xF8, 0x32,
0xC0, 0xB2, 0xD8, 0x80, 0xF4, 0xAE, 0x8B, 0xA0, 0x33, 0x44, 0x79, 0x4F, 0xFB, 0xD0, 0x06, 0x05,
0x6D, 0x36, 0x3F, 0xE2, 0x1E, 0xCB, 0xDA, 0x4C, 0x16, 0x25, 0xD9, 0x93, 0x04, 0x9E, 0x6C, 0x8C,
0xE4, 0xA8, 0x12, 0x9D, 0x83, 0x8E, 0x0A, 0x6B, 0x08, 0x9A, 0x8F, 0x54, 0x53, 0x2E, 0xDD, 0x19,
0xB4, 0xBA, 0x71, 0x24, 0x6F, 0x02, 0x66, 0x7A, 0x26, 0xF7, 0xF9, 0xFE, 0xE6, 0x60, 0x59, 0xC5,
0x38, 0xDE, 0xF6, 0xA5, 0x78, 0x4B, 0x1B, 0x4A, 0x77, 0x98, 0x67, 0x37, 0x9F, 0x99, 0x34, 0x55,
0x10, 0x31, 0xB9, 0xDC, 0xFD, 0xBE, 0x3E, 0x1F, 0x27, 0xB3, 0x5E, 0xC2, 0xE8, 0x2A, 0x0D, 0xB0,
0x01, 0x9C, 0xA1, 0xA4, 0x2B, 0x40, 0x84, 0x87, 0xF1, 0xCA, 0xA2, 0xC1, 0xFF, 0x07, 0x1D, 0xB7,
0x58, 0x21, 0x0C, 0xC9, 0xF0, 0x7F, 0xD2, 0x68, 0xAF, 0x91, 0x03, 0x2F, 0xE5, 0xFC, 0x4E, 0x15,
0xCD, 0xD3, 0x65, 0x29, 0x88, 0x48, 0x30, 0x3C, 0xBF, 0xF2, 0xCF, 0xB6, 0x18, 0x70, 0xEA, 0x13,
0xAA, 0xC7, 0x95, 0x22, 0x97, 0x2C, 0x8D, 0xC4, 0x8A, 0x00, 0x47, 0x52, 0x0F, 0x39, 0x3B, 0xD4,
};
/******************************************************************************
* *
* L O C A L U N I T I A L I Z E D D A T A D E F I N I T I O N S *
* *
******************************************************************************/
STATIC BYTE UID[UID_LENGTH];
STATIC BYTE UIDRecord[UID_LENGTH];
STATIC INT UnRegMilage;
STATIC INT RfidMilage;
STATIC INT StepNumber;
STATIC BYTE aResponeTID[32];
STATIC BYTE aResponseLabel[4];
STATIC BYTE aResponseCheck[4];
STATIC BYTE aResponseMilage[4];
STATIC BYTE TagType;
STATIC BOOL RFIDCommand;
STATIC INT iDebugResponse;
STATIC BYTE MilageHi, MilageLo, CodeIndex, CodeCheck;
STATIC INT RetryTimes;
STATIC INT DetectScale;
STATIC INT LimitMissLength;
STATIC _Function sOldDecodeFunc;
STATIC _Function sDecodeFunc[FUNC_MAX];
STATIC _Function sEncodeFunc;
STATIC _TIDEncode OldDecodeData;
STATIC BOOL OldEncode;
STATIC _TIDEncode NewDecodeData[FUNC_MAX];
STATIC _TIDEncode NewEncodeData;
STATIC _eTAG_FORMAT eTagFormat;
STATIC WORD CheckCRC(BYTE *data, INT len)
{
WORD crc;
INT i, j;
crc = 0x0000;
for (i = 0; i < len; i++)
{
crc ^= *data++;
for (j = 0; j < 8; j++)
{
if (crc & 0x0001)
{
crc >>= 1;
crc ^= 0x8408;
}
else
crc >>= 1;
}
}
*data++ = crc / 256;
*data++ = crc % 256;
return (crc);
}
STATIC VOID EncodeData(INT milage, BYTE *data)
{
MilageHi = milage / 256;
MilageLo = milage % 256;
CodeCheck = (MilageHi * MilageLo) ^ CodeIndex;
CodeIndex = MSCodeRadom[CodeCheck];
*(data + 0) = (BYTE)CodeIndex;
if ((CodeIndex % 3) == 0)
{
*(data + 1) = (BYTE)MilageHi;
*(data + 2) = (BYTE)MilageLo;
*(data + 3) = (BYTE)CodeCheck;
}
else if ((CodeIndex % 3) == 1)
{
*(data + 1) = (BYTE)CodeCheck;
*(data + 2) = (BYTE)MilageHi;
*(data + 3) = (BYTE)MilageLo;
}
else
{
*(data + 1) = (BYTE)MilageLo;
*(data + 2) = (BYTE)CodeCheck;
*(data + 3) = (BYTE)MilageHi;
}
}
STATIC VOID DecodeData(BYTE *data)
{
CodeIndex = *(data + 0);
if ((CodeIndex % 3) == 0)
{
MilageHi = *(data + 1);
MilageLo = *(data + 2);
CodeCheck = *(data + 3);
}
else if ((CodeIndex % 3) == 1)
{
CodeCheck = *(data + 1);
MilageHi = *(data + 2);
MilageLo = *(data + 3);
}
else
{
MilageLo = *(data + 1);
CodeCheck = *(data + 2);
MilageHi = *(data + 3);
}
}
STATIC VOID MessageSend(BYTE *msg, INT len)
{
CheckCRC(msg + 1, len - 1);
Uart2Buffer(msg, len + 2);
}
STATIC INT MessageReceive(BYTE *msg)
{
BYTE *target;
BYTE data;
BYTE len;
INT crc, check;
INT delayNum;
INT i;
target = msg;
#ifdef READER_TAG
delayNum = SetDelayTime(500);
#else
delayNum = SetDelayTime(80);
#endif
while (1)
{
// get start character
while (!Uart2GetByte(&data))
{
if (!CheckDelayTimer(delayNum))
{
ClrDelayTime(delayNum);
return 0;
}
}
if (data != COMMAND_START)
continue;
*target++ = data;
// get length
while (!Uart2GetByte(&data))
{
if (!CheckDelayTimer(delayNum))
{
ClrDelayTime(delayNum);
return 0;
}
}
*target++ = data;
len = data;
// get content
for (i = 0; i < len; i++)
{
while (!Uart2GetByte(&data))
{
if (!CheckDelayTimer(delayNum))
{
ClrDelayTime(delayNum);
return 0;
}
}
*target++ = data;
}
break;
}
ClrDelayTime(delayNum);
crc = CheckCRC(msg + 1, len - 1);
check = *(msg + len) * 256 + *(msg + len + 1);
if (crc != check)
return 0;
return (len + 1);
}
STATIC INT ReadBlock(BYTE block, BYTE *data)
{
BYTE request[32];
BYTE command = 0x28;
INT length = 5;
#ifndef TAG_WITH_SS
command = 0x60;
if (TagType == 0x03)
{
memcpy(request + length, UID, 4);
length += 4;
}
else
{
memcpy(request + length, UID, 8);
length += 8;
}
#endif
request[length++] = block; // start of block
request[length++] = 1; // number of block
request[0] = COMMAND_START;
request[1] = length;
request[2] = command;
request[3] = READ_TAG_PASS;
request[4] = TagType;
MessageSend(request, length);
length = MessageReceive(data);
delay_msec(40);
if (length == 0)
return _ERROR;
if (*(data + 2) != READ_TAG_PASS)
return _ERROR;
switch (block)
{
case LABEL_BLOCK:
case NEW_LABEL_BLOCK:
memcpy(aResponseLabel, data + 3, 4);
break;
case CHKSUM_BLOCK:
case NEW_CHKSUM_BLOCK:
memcpy(aResponseCheck, data + 3, 4);
break;
case MILAGE_BLOCK:
case NEW_MILAGE_BLOCK:
memcpy(aResponseMilage, data + 3, 4);
break;
}
return _SUCCESS;
}
STATIC INT WriteBlock(BYTE block, BYTE *data)
{
BYTE request[32];
BYTE response[32];
BYTE command = 0x28;
INT length = 5;
#ifndef TAG_WITH_SS
command = 0x60;
if (TagType == 0x03)
{
memcpy(request + length, UID, 4);
length += 4;
}
else
{
memcpy(request + length, UID, 8);
length += 8;
}
#endif
request[length++] = block; // start of block
request[length++] = 1; // number of block
memcpy(request + length, data, 4);
length += 4;
request[0] = COMMAND_START;
request[1] = length;
request[2] = command;
request[3] = WRITE_TAG_PASS;
request[4] = TagType;
MessageSend(request, length);
length = MessageReceive(response);
delay_msec(100);
if (length == 0)
return _ERROR;
if (response[2] != WRITE_TAG_PASS)
return _ERROR;
return _SUCCESS;
}
STATIC INT LockBlock(BYTE block)
{
BYTE request[32];
BYTE response[32];
BYTE command = 0x2c;
INT length = 5;
#ifndef TAG_WITH_SS
command = 0x64;
if (TagType == 0x03)
{
memcpy(request + length, UID, 4);
length += 4;
}
else
{
memcpy(request + length, UID, 8);
length += 8;
}
#endif
request[length++] = block; // start of block
request[length++] = 1; // number of block
request[0] = COMMAND_START;
request[1] = length;
request[2] = command;
request[3] = WRITE_TAG_PASS;
request[4] = TagType;
MessageSend(request, length);
length = MessageReceive(response);
delay_msec(100);
if (length == 0)
return _ERROR;
if (response[2] != WRITE_TAG_PASS)
return _ERROR;
return _SUCCESS;
}
STATIC BOOL DecodeFunctionExpress(_Function *pFunction, CHAR *pData)
{
INT i;
for (i = 0; i < EXPR_TOTAL; i++)
{
pFunction->sExpr[i].Operator1 = (*pData++ & 0x0f) - 1;
pFunction->sExpr[i].Operand = (*pData++ & 0x0f) - 1;
pFunction->sExpr[i].Operator2 = (*pData++ & 0x0f) - 1;
if ((pFunction->sExpr[i].Operator1 >= OPERATOR_MAX) ||
(pFunction->sExpr[i].Operator2 >= OPERATOR_MAX) ||
(pFunction->sExpr[i].Operand >= OPERAND_KIND))
{
pFunction->Enable = FALSE;
return FALSE;
}
}
pFunction->Enable = TRUE;
return TRUE;
}
STATIC BYTE CaculateFunction(BYTE Operate1, BYTE Operate2, BYTE Operand)
{
if (Operand == 0)
return Operate1 + Operate2;
if (Operand == 1)
return Operate1 - Operate2;
if (Operand == 2)
return Operate1 * Operate2;
if (Operand == 3)
return Operate2 ? (Operate1 / Operate2) : 0xff;
if (Operand == 4)
return Operate1 ^ Operate2;
if (Operand == 5)
return Operate2 ? (Operate1 % Operate2) : 0;
return _NULL;
}
STATIC BOOL CaculateEncodeData(BYTE *tid)
{
INT i, j;
// caculate old data
if (sOldDecodeFunc.Enable || OldEncode)
{
//Encode TID for Label Block
OldDecodeData.Data[0] = (BYTE)(*(tid + 7) + *(tid + 1));
OldDecodeData.Data[1] = (BYTE)(*(tid + 6) - *(tid + 2));
OldDecodeData.Data[2] = (BYTE)(*(tid + 5) * *(tid + 0));
OldDecodeData.Data[3] = (BYTE)(*(tid + 4) ^ *(tid + 3));
//Encode TID for CheckSum Block
OldDecodeData.Data[4] = (BYTE)((*(tid + 7) + *(tid + 3))) ^ OldDecodeData.Data[0];
OldDecodeData.Data[5] = (BYTE)((*(tid + 6) + *(tid + 0))) ^ OldDecodeData.Data[1];
OldDecodeData.Data[6] = (BYTE)((*(tid + 5) + *(tid + 2))) ^ OldDecodeData.Data[2];
OldDecodeData.Data[7] = (BYTE)((*(tid + 4) + *(tid + 1))) ^ OldDecodeData.Data[3];
OldDecodeData.Enable = TRUE;
}
// caculate new data
for (i = 0; i < FUNC_MAX; i++)
{
if (sDecodeFunc[i].Enable)
{
for (j = 0; j < EXPR_TOTAL; j++)
{
NewDecodeData[i].Data[j] =
CaculateFunction(*(tid + sDecodeFunc[i].sExpr[j].Operator1),
*(tid + sDecodeFunc[i].sExpr[j].Operator2),
sDecodeFunc[i].sExpr[j].Operand);
}
NewDecodeData[i].Enable = TRUE;
}
else
NewDecodeData[i].Enable = FALSE;
}
// caculate encode data
if (sEncodeFunc.Enable)
{
for (j = 0; j < EXPR_TOTAL; j++)
{
NewEncodeData.Data[j] =
CaculateFunction(*(tid + sEncodeFunc.sExpr[j].Operator1),
*(tid + sEncodeFunc.sExpr[j].Operator2),
sEncodeFunc.sExpr[j].Operand);
}
NewEncodeData.Enable = TRUE;
}
else
NewEncodeData.Enable = FALSE;
return TRUE;
}
STATIC BOOL CheckFormat(CHAR *pData, _eCHECK_FORMAT eFormat, INT *pType)
{
INT StartOffset;
INT i;
if (eFormat == CHECK_NAME)
StartOffset = 0;
else if (eFormat == CHECK_CHECKSUM)
StartOffset = 4;
*pType = 0;
if (eTagFormat == FORMAT_OLD)
{
if (OldDecodeData.Enable == TRUE)
{
if (memcmp(pData, OldDecodeData.Data + StartOffset, 4) == 0)
{
*pType = 0;
return TRUE;
}
}
}
else if (eTagFormat == FORMAT_1)
{
for (i = 0; i < FUNC_MAX; i++)
{
if (NewDecodeData[i].Enable == TRUE)
{
if (memcmp(pData, NewDecodeData[i].Data + StartOffset, 4) == 0)
{
*pType = i + 1;
return TRUE;
}
}
}
}
return FALSE;
}
STATIC VOID ClrStepNumber(VOID)
{
INT imask = DisableInterrupt(MOTION_IMASK);
StepNumber = 0;
EnableInterrupt(imask);
}
STATIC VOID SetStepNumber(INT Data)
{
INT imask = DisableInterrupt(MOTION_IMASK);
StepNumber += Data;
EnableInterrupt(imask);
}
STATIC VOID MotionStepNumber(VOID)
{
_MotionJob *MJob = GetActiveJob();
if (MJob->WorkJob.Type == PRINT_JOB)
{
INT Status = GetMotorStatus();
if (Status == MOTOR_FORWARD)
{
StepNumber += 1;
}
if (Status == MOTOR_BACKWARD)
{
StepNumber -= 1;
if (StepNumber < -1024)
StepNumber = -1024;
}
}
}
STATIC VOID ClearRfidVar(VOID)
{
memset(UID, 0, sizeof(UID));
memset(UIDRecord, 0, sizeof(UIDRecord));
UnRegMilage = 0;
RfidMilage = 1;
TagType = 0;
RFIDCommand = FALSE;
sOldDecodeFunc.Enable = FALSE;
OldDecodeData.Enable = FALSE;
eTagFormat = FORMAT_OLD;
RetryTimes = DEFAULT_RETRY_TIMES;
DetectScale = DEFAULT_DETECT_SCALE;
LimitMissLength = DEFAULT_LIMIT_MISS_LENGTH;
ClrStepNumber();
}
STATIC INT ErrorProcesser_SS(VOID)
{
BYTE NullSpace[4] = {0xFF, 0xFF, 0xFF, 0xFF};
INT MissTime;
INT BlockNum;
INT i;
#ifdef LOCK_TAG
// Erase Label Block
if (eTagFormat == FORMAT_OLD)
BlockNum = LABEL_BLOCK;
else if (eTagFormat == FORMAT_1)
BlockNum = NEW_LABEL_BLOCK;
else
BlockNum = 0;
MissTime = 0;
while (WriteBlock(BlockNum, NullSpace) == _ERROR)
{
#ifdef DEBUG_RF
SendPrintf("\r\n ReTry... %d", MissTime);
#endif
if (MissTime > RetryTimes)
{
#ifdef DEBUG_RF
SendString("\r\n SET LABEL (SS) Error");
#endif
return _ERROR;
}
else
MissTime += 1;
}
#ifdef DEBUG_RF
SendString("\r\n Erase LABEL Success");
#endif
//Erase CheckSum Block
if (eTagFormat == FORMAT_OLD)
BlockNum = CHKSUM_BLOCK;
else if (eTagFormat == FORMAT_1)
BlockNum = NEW_CHKSUM_BLOCK;
else
BlockNum = 0;
MissTime = 0;
while (WriteBlock(BlockNum, NullSpace) == _ERROR)
{
#ifdef DEBUG_RF
SendPrintf("\r\n ReTry... %d", MissTime);
#endif
if (MissTime > RetryTimes)
{
#ifdef DEBUG_RF
SendString("\r\n SET ChkSum (SS) Error");
#endif
return _ERROR;
}
else
MissTime += 1;
}
#ifdef DEBUG_RF
SendString("\r\n Erase ChkSum Success");
#endif
//Lock Milage Block
if (eTagFormat == FORMAT_OLD)
BlockNum = MILAGE_BLOCK;
else if (eTagFormat == FORMAT_1)
BlockNum = NEW_MILAGE_BLOCK;
else
BlockNum = 0;
MissTime = 0;
while (LockBlock(BlockNum) != _ERROR)
{
if (MissTime > RetryTimes)
{
#ifdef DEBUG_RF
SendString("\r\n Read Block (SS) Error");
#endif
return _ERROR;
}
else
MissTime += 1;
}
#endif
#ifdef DEBUG_RF
SendString("\r\nLock Block OK!!");
#endif
return _SUCCESS;
}
STATIC VOID DuraLabelError(BYTE error_code)
{
#ifdef DEBUG_PRNT
sysprintf("Enter DuraLabelError() : error_code = %d\n", error_code); // ch_20211210
#endif
#ifdef READER_TAG
if (error_code == NAME_ERROR)
{
#if defined(MONO_LED)
ShowLED(HID_LED_IDLE, HID_LED_OFF, HID_LED_ON);
#else
ShowLED(HID_LED_RED);
#endif
SendPrintf("{S%04d}", error_code);
EnableBuzzer(8, 100);
while (CheckBuzzerStatus()) {}
delay_msec(20);
EnableBuzzer(8, 100);
while (CheckBuzzerStatus()) {}
delay_msec(50);
EnableBuzzer(8, 100);
while (CheckBuzzerStatus()) {}
delay_msec(50);
EnableBuzzer(8, 200);
while (CheckBuzzerStatus()) {}
}
else
#endif
{
BYTE status = 0;
WORD job_error;
if (iDebugResponse == 1)
SendString("\r\n RFID Error -- ( Milage Null ) or LableID Empty !!");
StopKeyFunc();
StopJobManager();
ClrStepNumber();
SendPrintf("{S%04d}", error_code);
while (1)
{
if (status != _SUCCESS && error_code == MILAGE_ERROR)
status = ErrorProcesser_SS();
job_error = GetJobErrorState();
if (job_error & ERROR_TPH_OPEN)
{
InitailDuraLabel();
break;
}
#if defined(MONO_LED)
ShowLED(HID_LED_IDLE, HID_LED_ON, HID_LED_OFF);
delay_msec(500);
ShowLED(HID_LED_IDLE, HID_LED_OFF, HID_LED_ON);
delay_msec(500);
#elif defined(POLY_LED)
ShowLED(HID_LED_ORANGE);
delay_msec(500);
ShowLED(HID_LED_RED);
delay_msec(500);
#endif
}
}
}
STATIC INT FN_GetTID(VOID)
{
BYTE aComRequest[32];
BYTE aRespone[32];
BYTE *pText, temp, bMessageLen;
INT i, iComCnt;
INT Length = 0;
BYTE *pResponse;
BYTE *pTID;
for (i = 0 ; i < sizeof(aRespone) ; i++)
aRespone[i] = 0;
pText = CMD_GetTID;
iComCnt = sizeof(CMD_GetTID);
bMessageLen = CMD_GetTID[1];
for (i = 0 ; i < sizeof(CMD_GetTID) ; i++)
aComRequest[i] = *(pText + i);
aComRequest[iComCnt - 1] = 0x00; // 0: AUTO DETECT
MessageSend(aComRequest, bMessageLen);
Length = MessageReceive(aRespone);
if (aRespone[0] == 0)
{
if (iDebugResponse == 1)
SendPrintf("\r\nCan't Find TAG !!");
return _NULL;
}
if (!Length)
{
if (iDebugResponse == 1)
SendPrintf("\r\nGetTID Error !!");
return _ERROR;
}
pResponse = &aRespone[0];
aResponeTID[0] = *pResponse;
pResponse += 1;
aResponeTID[1] = *pResponse;
pResponse += 1; // Length
aResponeTID[2] = *pResponse;
pResponse += 1; // Response Code
if (aResponeTID[2] == SELECT_TAG_PASS)
{
for (i = 0 ; i < aResponeTID[1] - 1 ; i++)
{
aResponeTID[3 + i] = *pResponse;
pResponse += 1;
}
}
else
return _ERROR;
TagType = aResponeTID[3];
pTID = &aResponeTID[4];
memcpy((CHAR *)UID, (CHAR *)pTID, UID_LENGTH);
CaculateEncodeData(pTID);
if (iDebugResponse == 1)
SendPrintf("\r\nGetTID OK !!");
delay_msec(30);
return _SUCCESS;
}
STATIC INT FN_TAG2SS(VOID)
{
BYTE aComRequest[32];
BYTE aRespone[32];
BYTE BlockData_S[32];
BYTE *pText, temp, bMessageLen;
INT i, iComCnt;
INT Length = 0;
BYTE bTagType;
INT MissTime;
DWORD IdentifyData;
#if defined(TAG_WITH_SS)
bTagType = TagType;
pText = CMD_PutTID2SS;
iComCnt = sizeof(CMD_PutTID2SS);
bMessageLen = CMD_PutTID2SS[1];
for (i = 0 ; i < sizeof(CMD_PutTID2SS) ; i++)
aComRequest[i] = *(pText + i);
aComRequest[iComCnt - 1] = bTagType;
if (bTagType == 0x03) // Tagit HF
{
for (i = 0 ; i < 4 ; i++)
aComRequest[iComCnt++] = aResponeTID[4 + i];
}
else
{
if (aResponeTID[1] == 0x0C)
{
for (i = 0 ; i < 8 ; i++)
{
aComRequest[iComCnt++] = aResponeTID[4 + i];
}
}
else
{
for (i = 0 ; i < 8 ; i++)
{
aComRequest[iComCnt++] = aResponeTID[3 + i];
}
}
}
MessageSend(aComRequest, bMessageLen);
Length = MessageReceive(aRespone);
if (!Length)
{
if (iDebugResponse == 1)
SendPrintf("\r\nPut TAG to SS CRC Error !!");
return _ERROR;
}
if (aRespone[2] != SELECT_TAG_PASS)
{
if (iDebugResponse == 1)
SendPrintf("\r\nPut TAG to SS Response Error : #%02X ", aRespone[2]);
return _ERROR;
}
delay_msec(20);
#endif
MissTime = 0;
while (ReadBlock(NEW_IDENTIFY_BLOCK, BlockData_S) == _ERROR)
{
if (MissTime >= RetryTimes)
{
if (iDebugResponse == 1)
SendString("\r\n Read Identify Block (SS) Error");
return _SUCCESS;
}
else
MissTime += 1;
}
IdentifyData = IDENTIFY_DATA_1;
// Verify Name String
if (memcmp(BlockData_S + 3, (CHAR *)&IdentifyData, 4))
eTagFormat = FORMAT_OLD;
else
eTagFormat = FORMAT_1;
if (iDebugResponse == 1)
SendPrintf("\r\nTAG2SS OK !!(%d)", eTagFormat);
return _SUCCESS;
}
STATIC INT FN_READ_BLOCK_SS(VOID)
{
BYTE BlockData_S[32], BlockData_T[32];
DOUBLE dNum;
INT i;
INT MissTime;
if (!GetExpNumber(LINE_END, &dNum))
return _ERROR;
MissTime = 0;
while (ReadBlock(dNum, BlockData_S) == _ERROR)
{
if (MissTime >= RetryTimes)
{
if (iDebugResponse == 1)
SendString("\r\n Read Block (SS) Error");
return _SUCCESS;
}
else
MissTime += 1;
}
for (i = 0 ; i < 4 ; i++)
SendPrintf("%02X ", BlockData_S[3 + i]);
return _SUCCESS;
}
STATIC INT FN_WRITE_BLOCK_SS(VOID)
{
BYTE BlockData[32];
DOUBLE dData;
INT i, j;
CHAR Data;
INT MissTime;
RemoveSpace();
if (!GetExpNumber(COMMA_END, &dData))
return _ERROR;
for (i = 0 ; i < 4 ; i++)
{
BlockData[i] = 0;
for (j = 0 ;j < 2 ; j++)
{
BlockData[i] <<= 4;
Data = NextByte();
if (Data >= '0' && Data <= '9')
BlockData[i] += (Data & 0x0f);
else if (Data >= 'A' && Data <= 'F')
BlockData[i] += ((Data & 0x0f) + 9);
else if (Data >= 'a' && Data <= 'f')
BlockData[i] += ((Data & 0x0f) + 9);
else
return _ERROR;
}
}
MissTime = 0;
while (WriteBlock(dData, BlockData) == _ERROR)
{
if (MissTime >= RetryTimes)
{
if (iDebugResponse == 1)
SendString("\r\n Write Block (SS) Error");
return _ERROR;
}
else
MissTime += 1;
}
if (iDebugResponse == 1)
SendString("\r\n Write Block (SS) OK");
return _SUCCESS;
}
STATIC INT FN_READ_LABEL_SS(VOID)
{
BYTE Token[10];
INT i;
INT MissTime;
MissTime = 0;
while (ReadBlock(LABEL_BLOCK, Token) == _ERROR)
{
if (MissTime >= RetryTimes)
{
if (iDebugResponse == 1)
SendString("\r\n Read Label (SS) Error");
return _ERROR;
}
else
MissTime += 1;
}
MissTime = 0;
while (ReadBlock(CHKSUM_BLOCK, Token) == _ERROR)
{
if (MissTime >= RetryTimes)
{
if (iDebugResponse == 1)
SendString("\r\n Read CheckSum (SS) Error");
return _ERROR;
}
else
MissTime += 1;
}
if (iDebugResponse == 1)
{
SendString("\r\n Label : ");
for (i = 0 ;i < 4 ; i++)
SendPrintf("%02X ", aResponseLabel[i]);
SendString("\r\n Label <Encode>: ");
for (i = 0 ;i < 4 ; i++)
SendPrintf("%02X ", OldDecodeData.Data[i]);
SendString("\r\n Check Sum : ");
for (i = 0 ;i < 4 ; i++)
SendPrintf("%02X ", aResponseCheck[i]);
SendString("\r\n Check <Encode>: ");
for (i = 0 ;i < 4 ; i++)
SendPrintf("%02X ", OldDecodeData.Data[i+4]);
}
return _SUCCESS;
}
STATIC INT FN_READ_MILAGE_SS(VOID)
{
BYTE Token[10];
INT lData;
INT i, inch_milage, inch_milage1;
INT MissTime;
INT BlockNum;
if (eTagFormat == FORMAT_OLD)
BlockNum = MILAGE_BLOCK;
else if (eTagFormat == FORMAT_1)
BlockNum = NEW_MILAGE_BLOCK;
else
BlockNum = 0;
MissTime = 0;
while (ReadBlock(BlockNum, Token) == _ERROR)
{
if (MissTime >= RetryTimes)
{
if (iDebugResponse == 1)
SendString("\r\n Read Milage (SS) Error");
return _ERROR;
}
else
MissTime += 1;
}
DecodeData(Token + 3);
RfidMilage = MilageHi * 256 + MilageLo;
if (iDebugResponse == 1)
{
SendPrintf("\r\nBlock Num : %d ", BlockNum);
inch_milage = (INT)((FLOAT)(RfidMilage * DetectScale) / TPH_DPI);
inch_milage1 = (RfidMilage * DetectScale) % (INT)TPH_DPI;
SendPrintf("\r\nTag Milage : %d.%d Inch", inch_milage, inch_milage1);
SendChar('[');
for (i = 0 ;i < 4 ; i++)
SendPrintf(" %02X ", *(Token + 3 + i));
SendChar(']');
}
return _SUCCESS;
}
STATIC INT FN_READ_MILAGE_VAR(VOID)
{
SendPrintf("{M%d}", RfidMilage * DetectScale / MM_DOT);
return _SUCCESS;
}
STATIC INT FN_SEL_TAG_TYPE(VOID)
{
DOUBLE dData;
if (!GetExpNumber(LINE_END, &dData))
return _ERROR;
TagType = (BYTE)dData;
if (iDebugResponse == 1)
SendPrintf("\r\n TAG : %02X", TagType);
return _SUCCESS;
}
STATIC INT FN_ReadTID(VOID)
{
BYTE aComRequest[32];
BYTE aRespone[32];
BYTE *pText, temp, bMessageLen;
INT i, iComCnt;
INT Length = 0;
BYTE *pResponse;
BYTE *pTID;
for (i = 0 ; i < sizeof(aRespone) ; i++)
aRespone[i] = 0;
pText = CMD_GetTID;
iComCnt = sizeof(CMD_GetTID);
bMessageLen = CMD_GetTID[1];
for (i = 0 ; i < sizeof(CMD_GetTID) ; i++)
aComRequest[i] = *(pText + i);
aComRequest[iComCnt - 1] = 0x00; // 0: AUTO DETECT
MessageSend(aComRequest, bMessageLen);
Length = MessageReceive(aRespone);
if (aRespone[0] == 0)
{
if (iDebugResponse == 1)
SendPrintf("\r\nCan't Find TAG !!");
return _NULL;
}
if (!Length)
{
if (iDebugResponse == 1)
SendPrintf("\r\nGetTID Error !!");
return _ERROR;
}
pResponse = &aRespone[0];
aResponeTID[0] = *pResponse;
pResponse += 1;
aResponeTID[1] = *pResponse;
pResponse += 1; // Length
aResponeTID[2] = *pResponse;
pResponse += 1; // Response Code
if (aResponeTID[2] == SELECT_TAG_PASS)
{
for (i = 0; i < aResponeTID[1] - 1; i++)
{
aResponeTID[3+i] = *pResponse;
pResponse += 1;
}
}
else
return _ERROR;
TagType = aResponeTID[3];
pTID = &aResponeTID[4];
if (iDebugResponse == 1)
{
SendString("\r\n TID Data :");
for (i = 0; i < 8; i++)
SendPrintf(" %02X", *(pTID + i));
}
return _SUCCESS;
}
STATIC INT FN_Identify(VOID)
{
BYTE BlockData_S[32], BlockData_T[32];
DOUBLE dNum;
INT i;
INT MissTime;
DWORD IdentifyData;
if (ReadBlock(NEW_IDENTIFY_BLOCK, BlockData_S) == _ERROR)
{
if (iDebugResponse == 1)
SendString("\r\n Read Identify Block (SS) Error");
return _ERROR;
}
IdentifyData = IDENTIFY_DATA_1;
// Verify Name String
if (memcmp(BlockData_S + 3, (CHAR *)&IdentifyData, 4))
eTagFormat = FORMAT_OLD;
else
eTagFormat = FORMAT_1;
if (iDebugResponse == 1)
{
SendString("\r\n Block Identify Data : ");
for (i = 0; i < 4; i++)
SendPrintf("%02X ", BlockData_S[3 + i]);
}
return _SUCCESS;
}
STATIC INT FN_FuseTCF(VOID)
{
CHAR *SrcPtr, *Ptr, *SavePrt;
CHAR *pCurrent;
ULONG SaveLen;
DOUBLE Num;
INT i;
if (!GetExpNumber(COMMA_END, &Num))
return _ERROR;
SaveLen = (ULONG)Num;
if (SaveLen > PROFILE_MAX_LEN)
return _ERROR;
SrcPtr = malloc(PROFILE_MAX_LEN);
/* receive upside TCF */
Ptr = SrcPtr;
for (i = 0; i < SaveLen; i++)
*Ptr++ = NextByte() ^ SecretArray[i % 256];
/* chack TCF end */
for (Ptr = SrcPtr; Ptr < SrcPtr + SaveLen; Ptr++)
{
if (*(Ptr - 1) == LF_CHAR || *(Ptr - 1) == CR_CHAR)
if (memcmp(Ptr, "END", 3) == 0)
break;
}
if (Ptr == SrcPtr + SaveLen)
{
free(SrcPtr);
return _ERROR;
}
/* check save length */
if ((ULONG)Ptr - (ULONG)SrcPtr + 8 > PROFILE_MAX_LEN)
{
free(SrcPtr);
return _ERROR;
}
/* insert [RFID] */
memcpy(Ptr, "[RFID]\r\n", 8);
Ptr += 8;
/* udate save length */
SaveLen = (ULONG)Ptr - (ULONG)SrcPtr;
/* load downside TCF */
for (pCurrent = (CHAR *)PROFILE_ADDR; pCurrent < (CHAR *)PROFILE_ADDR + PROFILE_MAX_LEN; pCurrent++)
{
if (*(UCHAR *)pCurrent == 0xff || *(UCHAR *)pCurrent == 0x0)
{
Ptr = SrcPtr + SaveLen;
break;
}
if (*(pCurrent - 1) == LF_CHAR || *(pCurrent - 1) == CR_CHAR)
{
if (memcmp(pCurrent, "END", 3) == 0)
break;
if (memcmp(pCurrent, "[RFID]", 6) == 0)
Ptr = SrcPtr + SaveLen - 8;
}
/* check save length */
if ((ULONG)Ptr - (ULONG)SrcPtr >= PROFILE_MAX_LEN)
{
free(SrcPtr);
return _ERROR;
}
*Ptr++ = *pCurrent;
}
if (pCurrent == (CHAR *)PROFILE_ADDR + PROFILE_MAX_LEN)
Ptr = SrcPtr + SaveLen;
/* check save length */
if ((ULONG)Ptr - (ULONG)SrcPtr + 3 > PROFILE_MAX_LEN)
{
free(SrcPtr);
return _ERROR;
}
/* insert END */
memcpy(Ptr, "END", 3);
Ptr += 3;
/* udate save length */
SaveLen = (ULONG)Ptr - (ULONG)SrcPtr;
/* close interrupt */
WaitTransmitEnd();
DisableInterrupt(0xF);
CLOSE_WATCH_DOG;
/* write flash */
FuseFlashProgram((BYTE *)PROFILE_ADDR, (BYTE *)SrcPtr, SaveLen, TRUE, TRUE);
free(SrcPtr);
return _SUCCESS;
}
STATIC INT FN_SAVETCF(VOID)
{
CHAR *SrcPtr, *Ptr;
CHAR *pCurrent;
ULONG SaveLen;
DOUBLE Num;
INT i;
if (!GetExpNumber(COMMA_END, &Num))
return _ERROR;
SaveLen = (ULONG)Num;
if (SaveLen > PROFILE_MAX_LEN)
return _ERROR;
SrcPtr = malloc(PROFILE_MAX_LEN);
/* load upside TCF */
Ptr = SrcPtr;
for (pCurrent = (CHAR *)PROFILE_ADDR; pCurrent < (CHAR *)PROFILE_ADDR + PROFILE_MAX_LEN; pCurrent++)
{
if (*(UCHAR *)pCurrent == 0xff || *(UCHAR *)pCurrent == 0x0)
{
Ptr = SrcPtr;
break;
}
if (*(pCurrent - 1) == LF_CHAR || *(pCurrent - 1) == CR_CHAR)
{
if (memcmp(pCurrent, "END", 3) == 0)
break;
if (memcmp(pCurrent, "[RFID]", 6) == 0)
break;
}
*Ptr++ = *pCurrent;
}
if (pCurrent == (CHAR *)PROFILE_ADDR + PROFILE_MAX_LEN)
Ptr = SrcPtr;
/* insert [RFID] */
if (memcmp(Ptr - 2, "\r\n", 2) == 0)
Ptr -= 2;
memcpy(Ptr, "\r\n[RFID]\r\n", 10);
Ptr += 10;
/* check save length */
if ((ULONG)Ptr - (ULONG)SrcPtr + SaveLen > PROFILE_MAX_LEN)
{
free(SrcPtr);
return _ERROR;
}
/* receive downside TCF */
for (i = 0; i < SaveLen; i++)
*Ptr++ = NextByte() ^ SecretArray[i % 256];
/* udate save length */
SaveLen = (ULONG)Ptr - (ULONG)SrcPtr;
/* chack TCF end */
for (Ptr = SrcPtr; Ptr < SrcPtr + SaveLen; Ptr++)
{
if (*(Ptr - 1) == LF_CHAR || *(Ptr - 1) == CR_CHAR)
if (memcmp(Ptr, "END", 3) == 0)
break;
}
if (Ptr == SrcPtr + SaveLen)
{
free(SrcPtr);
return _ERROR;
}
/* close interrupt */
WaitTransmitEnd();
DisableInterrupt(0xF);
CLOSE_WATCH_DOG;
/* write flash */
FuseFlashProgram((BYTE *)PROFILE_ADDR, (BYTE *)SrcPtr, SaveLen, TRUE, TRUE);
free(SrcPtr);
return _SUCCESS;
}
STATIC INT FN_CheckTagType(VOID)
{
INT BlockNum;
INT MissTime;
INT NameTagType;
INT ChecksumTagType;
CHAR Token[20];
if (FN_GetTID() != _SUCCESS)
{
SendString("\r\nAsk Tag Type Error(TID)");
return _SUCCESS;
}
delay_msec(200);
if (FN_TAG2SS() != _SUCCESS)
{
SendString("\r\nAsk Tag Type Error(2SS)");
return _SUCCESS;
}
if (FN_Identify() != _SUCCESS)
{
SendString("\r\nAsk Tag Type Error(Identify)");
return _SUCCESS;
}
if (eTagFormat == FORMAT_OLD)
BlockNum = LABEL_BLOCK;
else if (eTagFormat == FORMAT_1)
BlockNum = NEW_LABEL_BLOCK;
else
BlockNum = 0;
// Read Name String
MissTime = 0;
while (ReadBlock(BlockNum, (BYTE *)Token) == _ERROR)
{
if (MissTime >= RetryTimes)
{
SendString("\r\nAsk Tag Type Error(Read LabelName)");
return _SUCCESS;
}
else
MissTime += 1;
}
if (CheckFormat(Token + 3, CHECK_NAME, &NameTagType) == FALSE)
{
SendString("\r\nAsk Tag Type Error(LabelName)");
return _SUCCESS;
}
// Read Name String
if (eTagFormat == FORMAT_OLD)
BlockNum = CHKSUM_BLOCK;
else if (eTagFormat == FORMAT_1)
BlockNum = NEW_CHKSUM_BLOCK;
else
BlockNum = 0;
MissTime = 0;
while (ReadBlock(BlockNum, (BYTE *)Token) == _ERROR)
{
if (MissTime >= RetryTimes)
{
SendString("\r\nAsk Tag Type Error(Read CheckSum)");
return _SUCCESS;
}
else
MissTime += 1;
}
if (CheckFormat(Token + 3, CHECK_CHECKSUM, &ChecksumTagType) == FALSE)
{
SendString("\r\nAsk Tag Type Error(CheckSum)");
return _SUCCESS;
}
if (NameTagType != ChecksumTagType)
{
SendString("\r\nAsk Tag Type Error(CheckSum different)");
return _SUCCESS;
}
if (NameTagType == 0)
SendPrintf("\r\nTag support old function");
else
SendPrintf("\r\nTag support function %d", NameTagType);
return _SUCCESS;
}
INT FN_RFID(VOID)
{
CHAR bTemp;
_CmdEntry *CmdEntry;
CONST STATIC _CmdEntry RFIDEntry[] =
{
{"READ_ID", FN_GetTID},
{"TAG2SS", FN_TAG2SS},
{"TAG_READ_BLOCK_SS", FN_READ_BLOCK_SS},
{"TAG_WRITE_BLOCK_SS", FN_WRITE_BLOCK_SS},
{"READ_LABEL_SS", FN_READ_LABEL_SS},
{"READ_MILAGE_SS", FN_READ_MILAGE_SS},
{"READ_MILAGE_VAR", FN_READ_MILAGE_VAR},
{"READ_TID", FN_ReadTID},
{"SEL_TAG", FN_SEL_TAG_TYPE},
{"IDENTIFY", FN_Identify},
{"FuseTCF", FN_FuseTCF},
{"SAVETCF", FN_SAVETCF},
{"CHECK_TAG_TYPE", FN_CheckTagType},
{(CHAR *)_NULL, _NULL}
};
CHAR Token[ STRING_LEN ];
INT CmdID;
RemoveSpace();
if (GetToken(Token, &bTemp, "\r\n\t ", sizeof(Token)) >= (sizeof(Token) - 1))
return _ERROR;
// BackByte(bTemp);
// check TSPL normal command
if ((CmdEntry = CheckStringCommand(Token, RFIDEntry)) && RFIDCommand)
{
if (CmdEntry->func() == _SUCCESS)
{
delay_msec(100);
return _SUCCESS;
}
return _ERROR;
}
return _ERROR;
}
STATIC INT FN_SET_LABEL_SS(VOID)
{
CHAR Token[100];
INT MissTime;
DWORD IdentifyData;
if (OldDecodeData.Enable == FALSE || OldEncode == FALSE)
{
SendString("\r\n SET LABEL Error");
return _ERROR;
}
IdentifyData = IDENTIFY_DATA_0;
MissTime = 0;
while (WriteBlock(NEW_IDENTIFY_BLOCK, (BYTE *) &IdentifyData) == _ERROR)
{
if (MissTime >= RetryTimes)
{
SendString("\r\n SET LABEL Error");
return _ERROR;
}
else
MissTime += 1;
}
MissTime = 0;
while (WriteBlock(LABEL_BLOCK, OldDecodeData.Data) == _ERROR)
{
if (MissTime >= RetryTimes)
{
SendString("\r\n SET LABEL Error");
return _ERROR;
}
else
MissTime += 1;
}
MissTime = 0;
while (WriteBlock(CHKSUM_BLOCK, OldDecodeData.Data + 4) == _ERROR)
{
if (MissTime >= RetryTimes)
{
SendString("\r\n SET LABEL Error");
return _ERROR;
}
else
MissTime += 1;
}
SendString("\r\n SET LABEL Success");
return _SUCCESS;
}
STATIC INT FN_SET_MILAGE_SS(VOID)
{
INT lData;
CHAR Token[100];
BYTE aEncodeData[4];
INT MissTime;
INT Feet;
if (!GetExpString(LINE_END, Token, 100))
return _ERROR;
if (OldDecodeData.Enable == FALSE || OldEncode == FALSE)
{
SendString("\r\n SET MILEAGE Error");
return _ERROR;
}
lData = atol(Token);
Feet = lData;
lData = lData * 12 * TPH_DPI / DetectScale;
lData = lData & 0x0000ffff;
Feet = lData * DetectScale / TPH_DPI / 12;
EncodeData(lData, aEncodeData);
MissTime = 0;
while (WriteBlock(MILAGE_BLOCK, aEncodeData) == _ERROR)
{
if (MissTime >= RetryTimes)
{
SendString("\r\n SET MILEAGE Error");
return _ERROR;
}
else
MissTime += 1;
}
SendPrintf("\r\n SET MILEAGE: %d feet", Feet);
return _SUCCESS;
}
STATIC INT FN_LOCK_BLOCK_SS(VOID)
{
int i;
DOUBLE dData;
INT MissTime;
if (!GetExpNumber(LINE_END, &dData))
return _ERROR;
MissTime = 0;
while (LockBlock(dData) == _ERROR)
{
if (MissTime >= RetryTimes)
{
//SendString("\r\n Lock Block (SS) Error");
return _ERROR;
}
else
MissTime += 1;
}
//SendString("\r\nLock Block OK!!");
return _SUCCESS;
}
STATIC INT FN_SET_NDATA_SS(VOID)
{
CHAR Token[100];
INT MissTime;
DWORD IdentifyData;
if (sEncodeFunc.Enable == FALSE || NewEncodeData.Enable == FALSE)
{
SendString("\r\n SET LABEL Error(1)");
return _ERROR;
}
// write identify data
IdentifyData = IDENTIFY_DATA_1;
MissTime = 0;
while (WriteBlock(NEW_IDENTIFY_BLOCK, (BYTE *) &IdentifyData) == _ERROR)
{
if (MissTime >= RetryTimes)
{
SendString("\r\n SET LABEL Error(2)");
return _ERROR;
}
else
MissTime += 1;
}
// write label block
MissTime = 0;
while (WriteBlock(NEW_LABEL_BLOCK, NewEncodeData.Data) == _ERROR)
{
if (MissTime >= RetryTimes)
{
SendString("\r\n SET LABEL Error(3)");
return _ERROR;
}
else
MissTime += 1;
}
// write label block
MissTime = 0;
while (WriteBlock(NEW_CHKSUM_BLOCK, NewEncodeData.Data + 4) == _ERROR)
{
if (MissTime >= RetryTimes)
{
SendString("\r\n SET LABEL Error(4)");
return _ERROR;
}
else
MissTime += 1;
}
SendString("\r\n SET LABEL Success");
return _SUCCESS;
}
STATIC INT FN_SET_MDATA_SS(VOID)
{
INT lData;
CHAR Token[100];
BYTE aEncodeData[4];
INT MissTime;
INT i;
BYTE Temp1, Temp2;
INT Feet;
// if ( !GetExpString(LINE_END, Token, 100) )
// return _ERROR;
for (i = 0 ; i < 4 ;i++)
{
Temp1 = NextByte() ^ SecretArray[i*2];
Temp2 = NextByte() ^ SecretArray[i*2 + 1];
*(Token + i) = (Temp1 << 4) + (Temp2 & 0x0f);
}
*(Token + i) = '\0';
if (sEncodeFunc.Enable == FALSE || NewEncodeData.Enable == FALSE)
{
SendString("\r\n SET MILAGE Error");
return _ERROR;
}
lData = atol(Token);
Feet = lData;
lData = lData * 12 * 203 / 100;
lData = lData & 0x0000ffff;
Feet = lData * 100 / 203 / 12;
EncodeData(lData, aEncodeData);
MissTime = 0;
while (WriteBlock(NEW_MILAGE_BLOCK, aEncodeData) == _ERROR)
{
if (MissTime >= RetryTimes)
{
SendString("\r\n SET MILAGE Error");
return _ERROR;
}
else
MissTime += 1;
}
SendPrintf("\r\n SET MILEAGE: %d feet", Feet);
return _SUCCESS;
}
INT FN_SET_RF_COMMAND(VOID)
{
CHAR token[STRING_LEN];
DOUBLE number;
if (!GetExpToken(COMMA_END, token, STRING_LEN))
return _ERROR;
if (!GetExpNumber(LINE_END, &number))
return _ERROR;
if (number >= 0 && number <= 2)
iDebugResponse = (INT)number;
if (strcmp(token, "ON") == 0)
{
RFIDCommand = TRUE;
if (iDebugResponse == 1)
SendString("\r\nRF Commands On 1 !!");
else if (iDebugResponse == 2)
SendString("\r\nRF Commands On 2 !!");
}
else if (strcmp(token, "OFF") == 0)
{
RFIDCommand = FALSE;
if (iDebugResponse == 0)
SendString("\r\nRF Commands Off!!");
}
else
return _ERROR;
WaitWorkJobEnd();
delay_msec(100);
return _SUCCESS;
}
STATIC INT FN_LIMIT_RETRY(VOID)
{
DOUBLE number;
if (!GetExpNumber(LINE_END, &number))
return _ERROR;
RetryTimes = (INT)number;
return _SUCCESS;
}
STATIC INT FN_LIMIT_DETECT(VOID)
{
_PrintCfg *cfg = GrabPrintConfig();
DOUBLE number1, number2;
if (!GetExpNumber(COMMA_END, &number1))
return _ERROR;
if (!GetExpNumber(LINE_END, &number2))
return _ERROR;
if (number2 == 0)
number2 = 1;
if (number1 == 0)
LimitMissLength = (INT)(number2 * TPH_DPI);
else
LimitMissLength = (INT)(number2 * cfg->fPaperSize);
ClrStepNumber();
return _SUCCESS;
}
INT FN_RFSET(VOID)
{
CONST STATIC _CmdEntry RFSetEntry[] =
{
{"LOCK_BLOCK_SS", FN_LOCK_BLOCK_SS},
{"SET_LABEL_SS", FN_SET_LABEL_SS},
{"SET_MILAGE_SS", FN_SET_MILAGE_SS},
{"SET_NDATA_SS", FN_SET_NDATA_SS},
{"SET_MDATA_SS", FN_SET_MDATA_SS},
{"LIMIT_RETRY", FN_LIMIT_RETRY},
{"LIMIT_DETECT", FN_LIMIT_DETECT},
{(CHAR *)_NULL, _NULL}
};
_CmdEntry *entry;
CHAR Token[STRING_LEN];
CHAR data;
RemoveSpace();
if (GetToken(Token, &data, "\r\n\t ", sizeof(Token)) >= (sizeof(Token) - 1))
return _ERROR;
// check RFID command
if (RFIDCommand && (entry = CheckStringCommand(Token, RFSetEntry)))
{
if (entry->func() == _SUCCESS)
{
delay_msec(100);
return _SUCCESS;
}
return _ERROR;
}
return _ERROR;
}
STATIC INT Flow_CheckTID(VOID)
{
INT MissTime = 0;
INT Data;
INT i;
#ifdef DEBUG_RF
SendString("\r\nRead ID -> ");
#endif
MissTime = 0;
while (FN_GetTID() != _SUCCESS)
{
#ifdef DEBUG_RF
SendPrintf("\r\n ReTry... <%d>", MissTime);
#endif
if (MissTime > RetryTimes)
return _NULL;
MissTime += 1;
}
#ifdef DEBUG_RF
SendString("Tag2SS -> ");
#endif
MissTime = 0;
while (FN_TAG2SS() == _ERROR)
{
#ifdef DEBUG_RF
SendPrintf("\r\n ReTry... <%d>", MissTime);
#endif
if (MissTime > RetryTimes)
return _NULL;
MissTime += 1;
}
Data = 0;
for (i = 0; i < UID_LENGTH; i++)
Data += UIDRecord[i];
memcpy((CHAR *)pPrintRec->UidName, (CHAR *)UIDRecord, UID_LENGTH);
// get UID first time
if (Data == 0)
memcpy((CHAR *)UIDRecord, (CHAR *)UID, UID_LENGTH);
else
{
if (memcmp((CHAR *)UIDRecord, (CHAR *)UID, UID_LENGTH))
{
DuraLabelError(NAME_ERROR);
return _NULL;
}
}
return _SUCCESS;
}
STATIC INT Flow_Identify(VOID)
{
INT MissTime = 0;
INT Data;
INT i;
// Read Identify block
MissTime = 0;
while (FN_Identify() == _ERROR)
{
#ifdef DEBUG_RF
SendPrintf("\r\n ReTry... <%d>", MissTime);
#endif
if (MissTime > RetryTimes)
return _NULL;
MissTime += 1;
}
return _SUCCESS;
}
STATIC INT Flow_IDVerify(VOID)
{
CHAR Token[20];
INT MissTime = 0;
INT Data;
INT BlockNum;
INT i;
#ifdef DEBUG_RF
SendString("Read Label -> ");
#endif
if (eTagFormat == FORMAT_OLD)
BlockNum = LABEL_BLOCK;
else if (eTagFormat == FORMAT_1)
BlockNum = NEW_LABEL_BLOCK;
else
BlockNum = 0;
// Read Name String
MissTime = 0;
while (ReadBlock(BlockNum, (BYTE *)Token) == _ERROR)
{
#ifdef DEBUG_RF
SendPrintf("\r\n ReTry... <%d>", MissTime);
#endif
if (MissTime > RetryTimes)
return _NULL;
MissTime += 1;
}
// Verify Name String
if (!CheckFormat(Token + 3, CHECK_NAME, &i))
{
#ifdef DEBUG_RF
SendString("\r\n Compare LableID Error !!");
#endif
DuraLabelError(NAME_ERROR);
return _NULL;
}
#ifdef DEBUG_RF
SendString("Compare LableID OK -> ");
#endif
#ifdef DEBUG_RF
SendString("Read CHECK SUM ID -> ");
#endif
return _SUCCESS;
}
STATIC INT Flow_CheckSum(VOID)
{
CHAR Token[20];
INT MissTime = 0;
INT Data;
INT BlockNum;
INT i;
#ifdef DEBUG_RF
SendString("Read CheckSum -> ");
#endif
// Read Name String
if (eTagFormat == FORMAT_OLD)
BlockNum = CHKSUM_BLOCK;
else if (eTagFormat == FORMAT_1)
BlockNum = NEW_CHKSUM_BLOCK;
else
BlockNum = 0;
MissTime = 0;
while (ReadBlock(BlockNum, (BYTE *)Token) == _ERROR)
{
#ifdef DEBUG_RF
SendPrintf("\r\n ReTry... <%d>", MissTime);
#endif
if (MissTime > RetryTimes)
return _NULL;
MissTime += 1;
}
// Verify Name String
if (!CheckFormat(Token + 3, CHECK_CHECKSUM, &i))
{
#ifdef DEBUG_RF
SendString("\r\n Compare CHKSUM ID Error !!");
#endif
DuraLabelError(NAME_ERROR);
return _NULL;
}
#ifdef DEBUG_RF
SendString("Compare CHKSUM ID OK -> ");
#endif
return _SUCCESS;
}
STATIC INT Flow_ReadMilage(VOID)
{
CHAR Token[20];
INT MissTime = 0;
INT Data;
INT BlockNum;
INT i;
if (iDebugResponse == 1)
SendString("\r\nRead Mileage -> ");
// Read Milage
// Read Name String
if (eTagFormat == FORMAT_OLD)
BlockNum = MILAGE_BLOCK;
else if (eTagFormat == FORMAT_1)
BlockNum = NEW_MILAGE_BLOCK;
else
BlockNum = 0;
MissTime = 0;
while (ReadBlock(BlockNum, (BYTE *)Token) == _ERROR)
{
#ifdef DEBUG_RF
SendPrintf("\r\n ReTry... <%d>", MissTime);
#endif
if (MissTime > RetryTimes)
return _NULL;
MissTime += 1;
}
DecodeData(aResponseMilage);
if (RfidMilage == 1) // for unknown reason, the read mileage is sometimes a little bit short
RfidMilage = MilageHi * 256 + MilageLo;
if (iDebugResponse)
SendPrintf("[MR:%d, %d]", RfidMilage, MilageHi * 256 + MilageLo);
return _SUCCESS;
}
STATIC INT Flow_WriteMilage(VOID)
{
BYTE aEncodeData[4];
INT MissTime = 0;
INT Data;
INT BlockNum;
INT i, inch_milage, inch_milage1;
INT UnRegMilageBackup;
UnRegMilageBackup = UnRegMilage;
RfidMilage -= UnRegMilage / DetectScale;
// check Milage
if (RfidMilage < 0)
DuraLabelError(MILAGE_ERROR);
EncodeData(RfidMilage, aEncodeData);
if (iDebugResponse == 1)
SendString("\r\nWrite Mileage -> ");
if (eTagFormat == FORMAT_OLD)
BlockNum = MILAGE_BLOCK;
else if (eTagFormat == FORMAT_1)
BlockNum = NEW_MILAGE_BLOCK;
else
BlockNum = 0;
MissTime = 0;
while (WriteBlock(BlockNum, aEncodeData) == _ERROR)
{
if (MissTime > RetryTimes)
{
UnRegMilage = UnRegMilageBackup;
RfidMilage += UnRegMilage / DetectScale;
return _NULL;
}
MissTime += 1;
}
UnRegMilageBackup = UnRegMilage % DetectScale;
UnRegMilage = UnRegMilageBackup;
inch_milage = RfidMilage * DetectScale / TPH_DPI;
UnRegMilageBackup = (RfidMilage * DetectScale) % (INT)TPH_DPI;
if (iDebugResponse)
SendPrintf("\r\nTag Milage : %d.%d Inch", inch_milage, UnRegMilageBackup);
return _SUCCESS;
}
STATIC INT Flow_WaitRemoveTag(VOID)
{
INT MissTime = 0;
INT Data = 0;
INT i;
while (FN_GetTID() != _SUCCESS)
{
#ifdef DEBUG_RF
SendPrintf("\r\n ReTry... <%d>", MissTime);
#endif
if (MissTime > RetryTimes)
return _ERROR;
MissTime += 1;
}
#ifdef DEBUG_RF
SendString("Tag2SS -> ");
#endif
MissTime = 0;
while (FN_TAG2SS() == _ERROR)
{
#ifdef DEBUG_RF
SendPrintf("\r\n ReTry... <%d>", MissTime);
#endif
if (MissTime > RetryTimes)
return _ERROR;
MissTime += 1;
}
for (i = 0 ; i < UID_LENGTH ; i++)
Data += UIDRecord[i];
if (memcmp((CHAR *)UIDRecord, (CHAR *)UID, UID_LENGTH))
return _ERROR;
return _SUCCESS;
}
STATIC VOID ScanProgramedTag(VOID)
{
STATIC INT sequence = 0;
switch (sequence)
{
case 0:
if (Flow_CheckTID() != _SUCCESS)
{
#if defined(MONO_LED)
ShowLED(HID_LED_IDLE, HID_LED_OFF, HID_LED_ON);
#else
ShowLED(HID_LED_ORANGE);
#endif
break;
}
sequence++;
case 1:
if (Flow_Identify() != _SUCCESS)
{
sequence = 0;
break;
}
sequence++;
case 2:
if (Flow_IDVerify() != _SUCCESS)
{
sequence = 0;
break;
}
sequence++;
case 3:
if (Flow_CheckSum() != _SUCCESS)
{
sequence = 0;
break;
}
sequence++;
case 4:
if (Flow_ReadMilage() != _SUCCESS)
{
sequence = 0;
break;
}
EnableBuzzer(7, 150);
sequence++;
case 5:
if (CheckBuzzerStatus())
break;
#if defined(MONO_LED)
ShowLED(HID_LED_IDLE, HID_LED_OFF, HID_LED_ON);
#else
ShowLED(HID_LED_GREEN);
#endif
sequence++;
case 6:
if (Flow_WaitRemoveTag() == _SUCCESS)
break;
#if defined(MONO_LED)
ShowLED(HID_LED_OFF, HID_LED_OFF, HID_LED_OFF);
#else
ShowLED(HID_LED_DARK);
#endif
ClearRfidVar();
sequence++;
default:
sequence = 0;
break;
}
return ;
}
STATIC VOID DuraLabelProcess(VOID)
{
STATIC INT sequence = 0;
STATIC INT previous = 0;
STATIC INT missing = 0;
INT status;
pPrintRec->UnRegMilage = StepNumber;
if (pPrintRec->UnRegMilage >= LimitMissLength && missing >= RFID_RETRY_TIME)
DuraLabelError(MISS_ERROR);
if (pPrintRec->UnRegMilage != previous && StepNumber != 0)
{
previous = UnRegMilage = pPrintRec->UnRegMilage;
ClrStepNumber();
switch (sequence)
{
case 0:
if ((status = Flow_CheckTID()) != _SUCCESS)
{
missing++;
break;
}
missing = 0;
sequence++;
case 1:
if ((status = Flow_Identify()) != _SUCCESS)
break;
sequence++;
case 2:
if ((status = Flow_IDVerify()) != _SUCCESS)
break;
sequence++;
case 3:
if ((status = Flow_CheckSum()) != _SUCCESS)
break;
sequence++;
case 4:
if ((status = Flow_ReadMilage()) != _SUCCESS)
break;
sequence++;
case 5:
if ((status = Flow_WriteMilage()) != _SUCCESS)
break;
sequence++;
default:
sequence = 0;
break;
}
if (status != _SUCCESS)
{
SetStepNumber(pPrintRec->UnRegMilage);
pPrintRec->UnRegMilage = StepNumber;
if (iDebugResponse == 2)
SendPrintf("\r\n Remain %d dots not yet storage to TAG.", pPrintRec->UnRegMilage);
sequence = 0;
}
}
return;
}
STATIC TASK(DuraLabelTask, arg)
{
#ifdef READER_TAG
ScanProgramedTag();
#else
DuraLabelProcess();
#endif
StartPeriodFunc(CheckDuraLabel);
TaskExit();
}
STATIC VOID CheckDuraLabel(VOID)
{
STATIC BYTE cnt = 0;
#ifndef READER_TAG
if (cnt++ > (100 / PERIOD_TIMES))
#endif
{
CancelPeriodFunc(CheckDuraLabel);
TaskCreate(DuraLabelTask, 0, TASK_DURALABEL_STACK);
cnt = 0;
}
}
VOID InitailDuraLabel(VOID)
{
CHAR buffer[256];
CHAR key[32];
FLOAT data;
INT miss, i;
ClearRfidVar();
/* get decode function */
for (i = 0; i < FUNC_MAX; i++)
{
sDecodeFunc[i].Enable = FALSE;
sprintf(key, "DECODE FUNCTION %d", i);
if (GetProfileString(key, buffer))
DecodeFunctionExpress(&sDecodeFunc[i], buffer);
}
if (GetProfileInt("OLDDECODE FUN ENABLE", &data))
sOldDecodeFunc.Enable = (SHORT)data;
/* get encode function */
sEncodeFunc.Enable = FALSE;
OldEncode = FALSE;
#ifdef PROGRAM_TAG
if (GetProfileString("OLD DECODE FUNCTION", buffer))
DecodeFunctionExpress(&sEncodeFunc, buffer);
if (GetProfileInt("OLDENCODE FUNCTION", &data))
OldEncode = (SHORT)data;
#endif
if (GetProfileInt("RFIDCOMMAND", &data))
RFIDCommand = (BOOL)data;
miss = 0;
while (FN_GetTID() == _NULL)
{
#ifdef DEBUG_RF
SendPrintf("\r\n Check M1 Module......%d times", miss);
#endif
if (miss >= 10)
DuraLabelError(DONT_FIND_MODULE);
else
miss += 1;
}
/* check have change TAG ? */
/* if (memcmp((CHAR *)pPrintRec->UidName, (CHAR *)UID, UID_LENGTH))
* pPrintRec->UnRegMilage = 0;
* else
* SetStepNumber(pPrintRec->UnRegMilage);
* GP say disable this function 2008-08-05 */
#ifndef PROGRAM_TAG
StartPeriodFunc(CheckDuraLabel);
SetMotionCallback(MotionStepNumber);
#endif
}
#endif
TsplUart2Buffer.c //
#include "Common.h"
#if defined(UART2_BUFFER)
/******************************************************************************
* *
* M O D U L E D E F I N E *
* *
******************************************************************************/
#define TSPLUART2BUFFER_C
/******************************************************************************
* *
* C O M P I L E R D E F I N E D I N C L U D E F I L E S *
* *
******************************************************************************/
/* None */
/******************************************************************************
* *
* U S E R D E F I N E D I N C L U D E F I L E S *
* *
******************************************************************************/
#include "XCore.h"
#include "XComm.h"
#include "XParser.h"
#include "XBt.h"
VOID (*Uart2ButterChar)(BYTE);
/******************************************************************************
* *
* L O C A L D E F I N E S *
* *
******************************************************************************/
#if defined(AT91SAM9260)
#if defined(T045_PCB) ||defined(T40_PCB)
#define UART2_BUTTER_RTS_READY AT91F_PIO_ClearOutput(AT91C_BASE_PIOB, AT91C_PIO_PB14)
#define UART2_BUTTER_RTS_BUSY AT91F_PIO_SetOutput(AT91C_BASE_PIOB, AT91C_PIO_PB14)
#define UART2_BUTTER_CTS_READY ( AT91F_PIO_IsInputSet(AT91C_BASE_PIOB, AT91C_PIO_PB15) != AT91C_PIO_PB15 )
#define UART2_BUTTER_CTS_BUSY ( AT91F_PIO_IsInputSet(AT91C_BASE_PIOB, AT91C_PIO_PB15) == AT91C_PIO_PB15 )
#elif defined(TTP245P_PCB)
#define UART2_BUTTER_RTS_READY AT91F_PIO_ClearOutput(AT91C_BASE_PIOB, AT91C_PIO_PB28)
#define UART2_BUTTER_RTS_BUSY AT91F_PIO_SetOutput(AT91C_BASE_PIOB, AT91C_PIO_PB28)
#define UART2_BUTTER_CTS_READY ( AT91F_PIO_IsInputSet(AT91C_BASE_PIOB, AT91C_PIO_PB29) != AT91C_PIO_PB29 )
#define UART2_BUTTER_CTS_BUSY ( AT91F_PIO_IsInputSet(AT91C_BASE_PIOB, AT91C_PIO_PB29) == AT91C_PIO_PB29 )
#endif
#elif defined(SH7040)
#define UART_RTS_READY ( PA.DR.BIT.B3 = LOW )
#define UART_RTS_BUSY ( PA.DR.BIT.B3 = HIGH )
#define UART_CTS_READY ( PF.DR.BIT.B3 == LOW )
#define UART_CTS_BUSY ( PF.DR.BIT.B3 == HIGH )
#define UART_CLOCK SCI0.SMR.BIT.CKS
#define UART_STOP SCI0.SMR.BIT.STOP
#define UART_DATABIT SCI0.SMR.BIT.CHR
#define UART_PARITY SCI0.SMR.BIT._PE
#define UART_ODD_ENVE SCI0.SMR.BIT.OE
#define UART_BRR SCI0.BRR
#define UART_SMR SCI0.SMR.BYTE
#elif defined(N3290)
#define UART2_BUTTER_RTS_READY gpio_setportval(GPIO_PORTB,BIT5,0)
#define UART2_BUTTER_RTS_BUSY gpio_setportval(GPIO_PORTB,BIT5,BIT5)
#define UART2_BUTTER_CTS_READY ((inpw(REG_GPIOB_PIN)&BIT6) != BIT6)
#define UART2_BUTTER_CTS_BUSY ((inpw(REG_GPIOB_PIN)&BIT6) == BIT6)
#endif
#define UART2_BUTTER_RECEIVE_BUF_SIZE 65536 // 64K
#define UART2_BUTTER_RECEIVE_BUF_FULL 63488 // 62K
#define UART2_BUTTER_RECEIVE_BUF_EMPTY 61440 // 60K
#define UART2_BUTTER_TRANSMIT_BUF_SIZE 4096 // 4K
/******************************************************************************
* *
* L O C A L T Y P E D E F S *
* *
******************************************************************************/
/* None */
/******************************************************************************
* *
* L O C A L F U N C T I O N P R O T O T Y P E S *
* *
******************************************************************************/
/* None */
/******************************************************************************
* *
* L O C A L I N I T I A L I Z E D D A T A D E F I N I T I O N S *
* *
******************************************************************************/
/* None */
/******************************************************************************
* *
* L O C A L U N I T I A L I Z E D D A T A D E F I N I T I O N S *
* *
******************************************************************************/
#if defined(__HITACHI__)
#pragma section Buffer
#endif
STATIC BYTE Uart2ButterRxBuffer[UART2_BUTTER_RECEIVE_BUF_SIZE];
#if defined(__HITACHI__)
#pragma section
#endif
STATIC BYTE *pUart2ButterRxBegin;
STATIC BYTE *pUart2ButterRxEnd;
STATIC BYTE *pUart2ButterRxInput;
STATIC BYTE *pUart2ButterRxOutput;
STATIC INT Uart2ButterRxLength;
STATIC BOOL Uart2ButterRxFull;
STATIC BYTE Uart2ButterTxBuffer[UART2_BUTTER_TRANSMIT_BUF_SIZE];
STATIC BYTE *pUart2ButterTxBegin;
STATIC BYTE *pUart2ButterTxEnd;
STATIC BYTE *pUart2ButterTxInput;
STATIC BYTE *pUart2ButterTxOutput;
STATIC INT Uart2ButterTxLength;
STATIC INT Uart2ButterTxInsertChar;
#if defined(__HITACHI__)
#pragma section ISR
#endif
/******************************************************************************
*
* Function:
*
*
* Description:
*
*
* Input:
* None
*
* Output:
* None.
*
******************************************************************************/
STATIC VOID Uart2ButterOutTxDataReg(BYTE data)
{
Uart2OutTxDataReg(data);
}
STATIC VOID CheckUart2ButterTransmit(VOID)
{
BYTE data;
INT imask;
imask = DisableInterrupt(COMM_IMASK);
if ( !CheckUart2Transmit() )
{
if ( Uart2ButterTxDataOut(&data) )
Uart2ButterOutTxDataReg(data);
}
EnableInterrupt(imask);
}
VOID BufferUart2Butter(BYTE data)
{
Uart2ButterTxDataIn(data);
CheckUart2ButterTransmit();
}
VOID DirectUart2Butter(BYTE data)
{
Uart2Char(data);
}
VOID Uart2ButterWaitTransmitEnd(VOID)
{
Uart2WaitTransmitEnd();
}
VOID SetDirectUart2Butter(VOID)
{
Uart2ButterWaitTransmitEnd();
Uart2ButterChar = DirectUart2Butter;
}
VOID SetBufferUart2Butter(VOID)
{
Uart2ButterChar = BufferUart2Butter;
}
/******************************************************************************
*
* Function:
* UartSetting
*
* Description:
* This global function Uart receive data.
*
* Input:
* None
*
* Output:
* None.
*
******************************************************************************/
VOID Uart2ButterSetting(_UartSetting UartSet)
{
Uart2Setting(UartSet);
}
/******************************************************************************
*
* Function:
* SetUartHandShake
*
* Description:
* This global function Uart receive data.
*
* Input:
* None
*
* Output:
* None.
*
******************************************************************************/
VOID SetUart2ButterHandShake(_eUartStatus UartStatus)
{
if ( UartStatus == UART_STATUS_BUSY )
{
UART2_BUTTER_RTS_BUSY;
Uart2ButterTxInsert(XOFF);
}
else
{
UART2_BUTTER_RTS_READY;
Uart2ButterTxInsert(XON);
}
CheckUart2ButterTransmit();
}
/******************************************************************************
*
* Function:
* InitialUartDrv
*
* Description:
* This global function initializes the Uart Driver.
*
* Input:
* None
*
* Output:
* None.
*
******************************************************************************/
VOID InitialUart2ButterDrv(VOID)
{
pUart2ButterRxBegin = Uart2ButterRxBuffer;
pUart2ButterRxEnd = Uart2ButterRxBuffer + UART2_BUTTER_RECEIVE_BUF_SIZE - 1;
pUart2ButterRxInput = Uart2ButterRxBuffer;
pUart2ButterRxOutput = Uart2ButterRxBuffer;
Uart2ButterRxLength = 0;
Uart2ButterRxFull = FALSE;
pUart2ButterTxBegin = Uart2ButterTxBuffer;
pUart2ButterTxEnd = Uart2ButterTxBuffer + UART2_BUTTER_TRANSMIT_BUF_SIZE - 1;
pUart2ButterTxInput = Uart2ButterTxBuffer;
pUart2ButterTxOutput = Uart2ButterTxBuffer;
Uart2ButterTxLength = 0;
Uart2ButterTxInsertChar = -1;
}
/******************************************************************************
*
* Function:
* RxDataIn
*
* Description:
* This global function to save data to buffer
*
* Input:
* None
*
* Output:
* None.
*
******************************************************************************/
VOID Uart2ButterRxDataIn(BYTE data)
{
INT imask;
imask = DisableInterrupt(COMM_IMASK);
*pUart2ButterRxInput++ = data;
if ( pUart2ButterRxInput > pUart2ButterRxEnd )
pUart2ButterRxInput = pUart2ButterRxBegin;
Uart2ButterRxLength += 1;
if ( Uart2ButterRxFull == FALSE )
{
if ( Uart2ButterRxLength >= UART2_BUTTER_RECEIVE_BUF_FULL )
{
Uart2ButterRxFull = TRUE;
SetUart2ButterHandShake(UART_STATUS_BUSY);
}
}
EnableInterrupt(imask);
}
/******************************************************************************
*
* Function:
* RxDataOut
*
* Description:
* This global function to Load data to buffer
*
* Input:
* None
*
* Output:
* None.
*
******************************************************************************/
BOOL Uart2ButterRxDataOut(BYTE *data)
{
INT imask;
if ( Uart2ButterRxLength == 0 )
return FALSE;
imask = DisableInterrupt(COMM_IMASK);
*data = *pUart2ButterRxOutput++;
if ( pUart2ButterRxOutput > pUart2ButterRxEnd )
pUart2ButterRxOutput = pUart2ButterRxBegin;
Uart2ButterRxLength -= 1;
if ( Uart2ButterRxFull == TRUE )
{
if ( Uart2ButterRxLength < UART2_BUTTER_RECEIVE_BUF_EMPTY )
{
Uart2ButterRxFull = FALSE;
SetUart2ButterHandShake(UART_STATUS_READY);
}
}
EnableInterrupt(imask);
return TRUE;
}
/******************************************************************************
*
* Function:
* RxDataLength
*
* Description:
* This global function to check have data in buffer
*
* Input:
* None
*
* Output:
* None.
*
******************************************************************************/
INT Uart2ButterRxDataLength(VOID)
{
return Uart2ButterRxLength;
}
/******************************************************************************
*
* Function:
* RxBufFull
*
* Description:
*
*
* Input:
* None
*
* Output:
* None.
*
******************************************************************************/
BOOL Uart2ButterRxBufferFull(VOID)
{
return Uart2ButterRxFull;
}
/******************************************************************************
*
* Function:
* TxInsert
*
* Description:
*
*
* Input:
* None
*
* Output:
* None.
*
******************************************************************************/
VOID Uart2ButterTxInsert(BYTE data)
{
INT imask;
imask = DisableInterrupt(COMM_IMASK);
Uart2ButterTxInsertChar = (INT)data;
EnableInterrupt(imask);
}
/******************************************************************************
*
* Function:
* TxDataIn
*
* Description:
* This global function to save data to buffer
*
* Input:
* None
*
* Output:
* None.
*
******************************************************************************/
VOID Uart2ButterTxDataIn(BYTE data)
{
INT imask;
while ( Uart2ButterTxLength >= UART2_BUTTER_TRANSMIT_BUF_SIZE ){}
imask = DisableInterrupt(COMM_IMASK);
*pUart2ButterTxInput++ = data;
if ( pUart2ButterTxInput > pUart2ButterTxEnd )
pUart2ButterTxInput = pUart2ButterTxBegin;
Uart2ButterTxLength += 1;
EnableInterrupt(imask);
}
/******************************************************************************
*
* Function:
* TxDataOut
*
* Description:
* This global function to save data to buffer
*
* Input:
* None
*
* Output:
* None.
*
******************************************************************************/
BOOL Uart2ButterTxDataOut(BYTE *data)
{
INT imask;
if ( Uart2ButterTxLength == 0 && Uart2ButterTxInsertChar == -1 )
return FALSE;
imask = DisableInterrupt(COMM_IMASK);
if ( Uart2ButterTxInsertChar != -1 )
{
*data = (BYTE)Uart2ButterTxInsertChar;
Uart2ButterTxInsertChar = -1;
}
else
{
*data = *pUart2ButterTxOutput++;
if ( pUart2ButterTxOutput > pUart2ButterTxEnd )
pUart2ButterTxOutput = pUart2ButterTxBegin;
Uart2ButterTxLength -= 1;
}
EnableInterrupt(imask);
return TRUE;
}
INT Uart2ButterWrite(INT port,CONST VOID *buf, INT size)
{
INT rc;
for (rc = 0; rc < size; rc++)
Uart2ButterChar(*((BYTE *)buf + rc));
return rc;
}
/******************************************************************************
*
* Function:
* UartReceiveISR
*
* Description:
* This global function Uart receive data.
*
* Input:
* None
*
* Output:
* None.
*
******************************************************************************/
VOID Uart2ButterReceiveISR(BYTE data)
{
INT Length;
CHAR Buf[5];
INT i;
INT Port;
CLOSE_WATCH_DOG;
#if defined(UART2_BT)
Port = BT_PORT;
#elif defined(UART2_WIFI)
Port = WIFI_PORT;
#endif
Length = CheckImmedCmd(data, Buf, Port);
#if defined(EPL2)
if ( Length == 1 )
Length = EplCheckImmedCmd(data, Buf, Port);
#endif
#if defined(DPL)
if ( Length == 1 )
Length = DplCheckImmedCmd(data, Buf, Port);
#endif
i = 0;
while ( i < Length )
{
Uart2ButterRxDataIn(Buf[i]);
i += 1;
}
}
/******************************************************************************
*
* Function:
* UartTransmitISR
*
* Description:
* This global function Uart transmit data.
*
* Input:
* None
*
* Output:
* None.
*
******************************************************************************/
VOID Uart2ButterTransmitISR(VOID)
{
BYTE data;
StopUart2Transmit();
if ( Uart2ButterTxDataOut(&data) )
Uart2ButterOutTxDataReg(data);
}
BOOL Uart2BtRxDataOut(BYTE *data)
{
return Uart2ButterRxDataOut(data);
}
BOOL Uart2WifiRxDataOut(BYTE *data)
{
return Uart2ButterRxDataOut(data);
}
#endif
TsplUart2Buffer.h ///
/******************************************************************************
版权所有 (C), 2005-2015, 极速电子有限公司
******************************************************************************
文 件 名 : TsplUart2Buffer.h
版 本 号 : 初稿
作 者 : xiangzhongyong
生成日期 : 2016年1月12日
最近修改 :
功能描述 : TsplUart2Buffer.c 的头文件
函数列表 :
修改历史 :
1.日 期 : 2016年1月12日
作 者 : xiangzhongyong
修改内容 : 创建文件
******************************************************************************/
#ifndef __TSPLUART2BUFFER_H__
#define __TSPLUART2BUFFER_H__
/*----------------------------------------------*
* 包含头文件 *
*----------------------------------------------*/
#include "XUart.h"
/*----------------------------------------------*
* 外部变量说明 *
*----------------------------------------------*/
/*----------------------------------------------*
* 外部函数原型说明 *
*----------------------------------------------*/
/*----------------------------------------------*
* 内部函数原型说明 *
*----------------------------------------------*/
/*----------------------------------------------*
* 全局变量 *
*----------------------------------------------*/
/*----------------------------------------------*
* 模块级变量 *
*----------------------------------------------*/
/*----------------------------------------------*
* 常量定义 *
*----------------------------------------------*/
/*----------------------------------------------*
* 宏定义 *
*----------------------------------------------*/
#ifdef __cplusplus
#if __cplusplus
extern "C"{
#endif
#endif /* __cplusplus */
extern VOID BufferUart2Butter(BYTE data);
extern VOID DirectUart2Butter(BYTE data);
extern VOID InitialUart2ButterDrv(VOID);
extern VOID SetBufferUart2Butter(VOID);
extern VOID SetDirectUart2Butter(VOID);
extern VOID SetUart2ButterHandShake(_eUartStatus UartStatus);
extern BOOL Uart2BtRxDataOut(BYTE *data);
extern VOID Uart2ButterReceiveISR(BYTE);
extern BOOL Uart2ButterRxBufferFull(VOID);
extern VOID Uart2ButterRxDataIn(BYTE data);
extern INT Uart2ButterRxDataLength(VOID);
extern BOOL Uart2ButterRxDataOut(BYTE *data);
extern VOID Uart2ButterSetting(_UartSetting UartSet);
extern VOID Uart2ButterTransmitISR(VOID);
extern VOID Uart2ButterTxDataIn(BYTE data);
extern BOOL Uart2ButterTxDataOut(BYTE *data);
extern VOID Uart2ButterTxInsert(BYTE data);
extern VOID Uart2ButterWaitTransmitEnd(VOID);
extern INT Uart2ButterWrite(INT port,CONST VOID *buf, INT size);
extern BOOL Uart2WifiRxDataOut(BYTE *data);
#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /* __cplusplus */
#endif /* __TSPLUART2BUFFER_H__ */