📄 fm1702.lst
字号:
1044 2 if((temp & 0x08) == 0x08) return(FM1702_CRCERR);
1045 2 temp = SPIRead(FIFO_Length);
1046 2 if(temp != 1) return(FM1702_BYTECOUNTERR);
1047 2 Read_FIFO(RevBuffer); /* 从FIFO中读取应答信息 */
C51 COMPILER V7.50 FM1702 10/16/2007 16:33:32 PAGE 18
1048 2 temp = *RevBuffer;
1049 2
1050 2 if((temp == 0x18) || (temp == 0x08) || (temp == 0x88) || (temp == 0x53)) /* 判断应答信号是否正确 */
1051 2 return(FM1702_OK);
1052 2 else
1053 2 return(FM1702_SELERR);
1054 2 }
1055 1 }
1056
1057 /****************************************************************/
1058 /*名称: Authentication */
1059 /*功能: 该函数实现密码认证的过程*/
1060 /*输入: UID: 卡片序列号地址*/
1061 /* SecNR: 扇区号*/
1062 /* mode: 模式*/
1063 /*输出: FM1702_NOTAGERR: 无卡*/
1064 /* FM1702_PARITYERR: 奇偶校验错*/
1065 /* FM1702_CRCERR: CRC校验错*/
1066 /* FM1702_OK: 应答正确*/
1067 /* FM1702_AUTHERR: 权威认证有错*/
1068 /****************************************************************/
1069 uchar Authentication(uchar idata *UID, uchar SecNR, uchar mode)
1070 {
1071 1
1072 1 uchar idata i;
1073 1 uchar idata temp, temp1;
1074 1
1075 1 uchar temp0;
1076 1 if(SecNR >= 0x20)
1077 1 {
1078 2 temp0 = SecNR -0x20;
1079 2 SecNR = 0x20 + temp0 * 4;
1080 2 }
1081 1
1082 1 SPIWrite(CRCPresetLSB,0x63);
1083 1 SPIWrite(CWConductance,0x3f);
1084 1 // SPIWrite(ModConductance,0X3f);
1085 1 // temp1 = SPIRead(Control);
1086 1 // temp1 = temp1 & 0xf7;
1087 1 // SPIWrite(Control,temp1);
1088 1 if(mode == RF_CMD_AUTH_LB)
1089 1 RevBuffer[0] = RF_CMD_AUTH_LB;
1090 1 else
1091 1 RevBuffer[0] = RF_CMD_AUTH_LA;
1092 1 RevBuffer[1] = SecNR * 4 + 3;
1093 1 for(i = 0; i < 4; i++)
1094 1 {
1095 2 RevBuffer[2 + i] = UID[i];
1096 2 }
1097 1
1098 1 SPIWrite(ChannelRedundancy,0x0f);
1099 1 temp = Command_Send(6, RevBuffer, Authent1);
1100 1 if(temp == FALSE)
1101 1 {
1102 2
1103 2 return 0x99;
1104 2 }
1105 1
1106 1 temp = SPIRead(ErrorFlag); //ErrorFlag address is 0x0A.
1107 1 if((temp & 0x02) == 0x02) return FM1702_PARITYERR;
1108 1 if((temp & 0x04) == 0x04) return FM1702_FRAMINGERR;
1109 1 if((temp & 0x08) == 0x08) return FM1702_CRCERR;
C51 COMPILER V7.50 FM1702 10/16/2007 16:33:32 PAGE 19
1110 1 temp = Command_Send(0, RevBuffer, Authent2);
1111 1 if(temp == FALSE)
1112 1 {
1113 2
1114 2 return 0x88;
1115 2 }
1116 1
1117 1 temp = SPIRead(ErrorFlag);
1118 1 if((temp & 0x02) == 0x02) return FM1702_PARITYERR;
1119 1 if((temp & 0x04) == 0x04) return FM1702_FRAMINGERR;
1120 1 if((temp & 0x08) == 0x08) return FM1702_CRCERR;
1121 1 temp1 = SPIRead(Control);
1122 1 temp1 = temp1 & 0x08;
1123 1 if(temp1 == 0x08)
1124 1 {
1125 2 return FM1702_OK;
1126 2 }
1127 1
1128 1 return FM1702_AUTHERR;
1129 1 }
1130
1131 /****************************************************************/
1132 /*名称: MIF_Read */
1133 /*功能: 该函数实现读MIFARE卡块的数值*/
1134 /*输入: buff: 缓冲区首地址*/
1135 /* Block_Adr: 块地址*/
1136 /*输出: FM1702_NOTAGERR: 无卡*/
1137 /* FM1702_PARITYERR: 奇偶校验错*/
1138 /* FM1702_CRCERR: CRC校验错*/
1139 /* FM1702_BYTECOUNTERR: 接收字节错误*/
1140 /* FM1702_OK: 应答正确*/
1141 /****************************************************************/
1142 uchar MIF_READ(uchar idata *buff, uchar Block_Adr)
1143 {
1144 1
1145 1 uchar idata temp;
1146 1
1147 1 SPIWrite(CRCPresetLSB,0x63);
1148 1 SPIWrite(CWConductance,0x3f);
1149 1 SPIWrite(ModConductance,0x3f);
1150 1 SPIWrite(ChannelRedundancy,0x0f);
1151 1
1152 1 buff[0] = RF_CMD_READ;
1153 1 buff[1] = Block_Adr;
1154 1 temp = Command_Send(2, buff, Transceive);
1155 1 if(temp == 0)
1156 1 {
1157 2 return FM1702_NOTAGERR;
1158 2 }
1159 1
1160 1 temp = SPIRead(ErrorFlag);
1161 1 if((temp & 0x02) == 0x02) return FM1702_PARITYERR;
1162 1 if((temp & 0x04) == 0x04) return FM1702_FRAMINGERR;
1163 1 if((temp & 0x08) == 0x08) return FM1702_CRCERR;
1164 1 temp = SPIRead(FIFO_Length);
1165 1 if(temp == 0x10)
1166 1 {
1167 2 Read_FIFO(buff);
1168 2 return FM1702_OK;
1169 2 }
1170 1 else if(temp == 0x04)
1171 1 {
C51 COMPILER V7.50 FM1702 10/16/2007 16:33:32 PAGE 20
1172 2 Read_FIFO(buff);
1173 2 return FM1702_OK;
1174 2 }
1175 1 else
1176 1 {
1177 2 return FM1702_BYTECOUNTERR;
1178 2 }
1179 1 }
1180
1181 /****************************************************************/
1182 /*名称: MIF_Write */
1183 /*功能: 该函数实现写MIFARE卡块的数值*/
1184 /*输入: buff: 缓冲区首地址*/
1185 /* Block_Adr: 块地址*/
1186 /*输出: FM1702_NOTAGERR: 无卡*/
1187 /* FM1702_BYTECOUNTERR: 接收字节错误*/
1188 /* FM1702_NOTAUTHERR: 未经权威认证*/
1189 /* FM1702_EMPTY: 数据溢出错误*/
1190 /* FM1702_CRCERR: CRC校验错*/
1191 /* FM1702_PARITYERR: 奇偶校验错*/
1192 /* FM1702_WRITEERR: 写卡块数据出错*/
1193 /* FM1702_OK: 应答正确*/
1194 /****************************************************************/
1195 uchar MIF_Write(uchar idata *buff, uchar Block_Adr)
1196 {
1197 1
1198 1 uchar idata temp;
1199 1 uchar idata *F_buff;
1200 1
1201 1 SPIWrite(CRCPresetLSB,0x63);
1202 1 SPIWrite(CWConductance,0x3f);
1203 1 F_buff = buff + 0x10;
1204 1 SPIWrite(ChannelRedundancy,0x07); /* Note: this line is for 1702, different from RC500*/
1205 1 *F_buff = RF_CMD_WRITE;
1206 1 *(F_buff + 1) = Block_Adr;
1207 1 temp = Command_Send(2, F_buff, Transceive);
1208 1 if(temp == FALSE)
1209 1 {
1210 2 return(FM1702_NOTAGERR);
1211 2 }
1212 1
1213 1 temp = SPIRead(FIFO_Length);
1214 1 if(temp == 0)
1215 1 {
1216 2 return(FM1702_BYTECOUNTERR);
1217 2 }
1218 1
1219 1 Read_FIFO(F_buff);
1220 1 temp = *F_buff;
1221 1 switch(temp)
1222 1 {
1223 2 case 0x00: return(FM1702_NOTAUTHERR);
1224 2 case 0x04: return(FM1702_EMPTY);
1225 2 case 0x0a: break;
1226 2 case 0x01: return(FM1702_CRCERR);
1227 2 case 0x05: return(FM1702_PARITYERR);
1228 2 default: return(FM1702_WRITEERR);
1229 2 }
1230 1
1231 1 temp = Command_Send(16, buff, Transceive);
1232 1 if(temp == TRUE)
1233 1 {
C51 COMPILER V7.50 FM1702 10/16/2007 16:33:32 PAGE 21
1234 2 return(FM1702_OK);
1235 2 }
1236 1 else
1237 1 {
1238 2 temp = SPIRead(ErrorFlag);
1239 2 if((temp & 0x02) == 0x02)
1240 2 return(FM1702_PARITYERR);
1241 2 else if((temp & 0x04) == 0x04)
1242 2 return(FM1702_FRAMINGERR);
1243 2 else if((temp & 0x08) == 0x08)
1244 2 return(FM1702_CRCERR);
1245 2 else
1246 2 return(FM1702_WRITEERR);
1247 2 }
1248 1 }
1249
1250 /****************************************************************/
1251 /*名称: MIF_Increment */
1252 /*功能: 该函数实现MIFARE卡自动增值操作*/
1253 /*输入: buff: 四个字节数值起始地址*/
1254 /* Block_Adr: 块地址*/
1255 /*输出: FM1702_NOTAGERR: 无卡*/
1256 /* FM1702_BYTECOUNTERR: 接收字节错误*/
1257 /* FM1702_NOTAUTHERR: 未经权威认证*/
1258 /* FM1702_EMPTY: 数据溢出错误*/
1259 /* FM1702_CRCERR: CRC校验错*/
1260 /* FM1702_PARITYERR: 奇偶校验错*/
1261 /* FM1702_INCRERR: 卡片增款操作失败*/
1262 /* FM1702_OK: 应答正确*/
1263 /****************************************************************/
1264 uchar MIF_Increment(uchar idata *buff, uchar Block_Adr)
1265 {
1266 1
1267 1 uchar temp;
1268 1 uchar idata *F_buff;
1269 1
1270 1 SPIWrite(CRCPresetLSB,0x63);
1271 1 SPIWrite(CWConductance,0x3f);
1272 1 F_buff = buff + 4;
1273 1 *F_buff = RF_CMD_INC;
1274 1 *(F_buff + 1) = Block_Adr;
1275 1 SPIWrite(ChannelRedundancy,0x07);
1276 1 temp = Command_Send(2, F_buff, Transceive);
1277 1 if(temp == FALSE)
1278 1 {
1279 2 return FM1702_NOTAGERR;
1280 2 }
1281 1
1282 1 temp = SPIRead(FIFO_Length);
1283 1 if(temp == 0)
1284 1 {
1285 2 return FM1702_BYTECOUNTERR;
1286 2 }
1287 1
1288 1 Read_FIFO(F_buff);
1289 1 temp = *F_buff;
1290 1 switch(temp)
1291 1 {
1292 2 case 0x00: /* break; */return(FM1702_NOTAUTHERR);
1293 2 case 0x04: return(FM1702_EMPTY);
1294 2 case 0x0a: break;
1295 2 case 0x01: return(FM1702_CRCERR);
C51 COMPILER V7.50 FM1702 10/16/2007 16:33:32 PAGE 22
1296 2 case 0x05: return(FM1702_PARITYERR);
1297 2 default: return(FM1702_INCRERR);
1298 2 }
1299 1
1300 1 temp = Command_Send(4, buff, Transmit);
1301 1 if(temp == FALSE)
1302 1 {
1303 2 return
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -