Fixed indentation
This commit is contained in:
parent
f14e5608c0
commit
2786a14420
1 changed files with 75 additions and 67 deletions
142
MFRC522.py
142
MFRC522.py
|
@ -103,7 +103,6 @@ class MFRC522:
|
||||||
|
|
||||||
serNum = []
|
serNum = []
|
||||||
|
|
||||||
|
|
||||||
def __init__(self,spd=1000000):
|
def __init__(self,spd=1000000):
|
||||||
spi.openSPI(speed=spd)
|
spi.openSPI(speed=spd)
|
||||||
GPIO.setmode(GPIO.BOARD)
|
GPIO.setmode(GPIO.BOARD)
|
||||||
|
@ -157,48 +156,54 @@ class MFRC522:
|
||||||
self.Write_MFRC522(self.CommIEnReg, irqEn|0x80)
|
self.Write_MFRC522(self.CommIEnReg, irqEn|0x80)
|
||||||
self.ClearBitMask(self.CommIrqReg, 0x80)
|
self.ClearBitMask(self.CommIrqReg, 0x80)
|
||||||
self.SetBitMask(self.FIFOLevelReg, 0x80)
|
self.SetBitMask(self.FIFOLevelReg, 0x80)
|
||||||
|
|
||||||
self.Write_MFRC522(self.CommandReg, self.PCD_IDLE);
|
self.Write_MFRC522(self.CommandReg, self.PCD_IDLE);
|
||||||
|
|
||||||
while(i<len(sendData)):
|
while(i<len(sendData)):
|
||||||
self.Write_MFRC522(self.FIFODataReg, sendData[i])
|
self.Write_MFRC522(self.FIFODataReg, sendData[i])
|
||||||
i = i+1
|
i = i+1
|
||||||
|
|
||||||
self.Write_MFRC522(self.CommandReg, command)
|
self.Write_MFRC522(self.CommandReg, command)
|
||||||
|
|
||||||
|
|
||||||
if command == self.PCD_TRANSCEIVE:
|
if command == self.PCD_TRANSCEIVE:
|
||||||
self.SetBitMask(self.BitFramingReg, 0x80)
|
self.SetBitMask(self.BitFramingReg, 0x80)
|
||||||
|
|
||||||
i = 2000
|
i = 2000
|
||||||
while True:
|
while True:
|
||||||
n = self.Read_MFRC522(self.CommIrqReg)
|
n = self.Read_MFRC522(self.CommIrqReg)
|
||||||
i = i - 1
|
i = i - 1
|
||||||
if ~((i!=0) and ~(n&0x01) and ~(n&waitIRq)):
|
if ~((i!=0) and ~(n&0x01) and ~(n&waitIRq)):
|
||||||
break
|
break
|
||||||
|
|
||||||
self.ClearBitMask(self.BitFramingReg, 0x80)
|
self.ClearBitMask(self.BitFramingReg, 0x80)
|
||||||
|
|
||||||
if i != 0:
|
if i != 0:
|
||||||
if (self.Read_MFRC522(self.ErrorReg) & 0x1B)==0x00:
|
if (self.Read_MFRC522(self.ErrorReg) & 0x1B)==0x00:
|
||||||
status = self.MI_OK
|
status = self.MI_OK
|
||||||
if n & irqEn & 0x01:
|
|
||||||
status = self.MI_NOTAGERR
|
if n & irqEn & 0x01:
|
||||||
|
status = self.MI_NOTAGERR
|
||||||
|
|
||||||
if command == self.PCD_TRANSCEIVE:
|
if command == self.PCD_TRANSCEIVE:
|
||||||
n = self.Read_MFRC522(self.FIFOLevelReg)
|
n = self.Read_MFRC522(self.FIFOLevelReg)
|
||||||
lastBits = self.Read_MFRC522(self.ControlReg) & 0x07
|
lastBits = self.Read_MFRC522(self.ControlReg) & 0x07
|
||||||
if lastBits != 0:
|
if lastBits != 0:
|
||||||
backLen = (n-1)*8 + lastBits
|
backLen = (n-1)*8 + lastBits
|
||||||
else:
|
else:
|
||||||
backLen = n*8
|
backLen = n*8
|
||||||
if n == 0:
|
|
||||||
n = 1
|
if n == 0:
|
||||||
if n > self.MAX_LEN:
|
n = 1
|
||||||
n = self.MAX_LEN
|
if n > self.MAX_LEN:
|
||||||
|
n = self.MAX_LEN
|
||||||
i = 0
|
|
||||||
while i<n:
|
|
||||||
backData.append(self.Read_MFRC522(self.FIFODataReg))
|
|
||||||
i = i + 1;
|
|
||||||
else:
|
|
||||||
status = self.MI_ERR
|
|
||||||
|
|
||||||
|
i = 0
|
||||||
|
while i<n:
|
||||||
|
backData.append(self.Read_MFRC522(self.FIFODataReg))
|
||||||
|
i = i + 1;
|
||||||
|
else:
|
||||||
|
status = self.MI_ERR
|
||||||
|
|
||||||
return (status,backData,backLen)
|
return (status,backData,backLen)
|
||||||
|
|
||||||
|
|
||||||
|
@ -234,13 +239,13 @@ class MFRC522:
|
||||||
if(status == self.MI_OK):
|
if(status == self.MI_OK):
|
||||||
i = 0
|
i = 0
|
||||||
if len(backData)==5:
|
if len(backData)==5:
|
||||||
while i<4:
|
while i<4:
|
||||||
serNumCheck = serNumCheck ^ backData[i]
|
serNumCheck = serNumCheck ^ backData[i]
|
||||||
i = i + 1
|
i = i + 1
|
||||||
if serNumCheck != backData[i]:
|
if serNumCheck != backData[i]:
|
||||||
status = self.MI_ERR
|
status = self.MI_ERR
|
||||||
else:
|
else:
|
||||||
status = self.MI_ERR
|
status = self.MI_ERR
|
||||||
|
|
||||||
return (status,backData)
|
return (status,backData)
|
||||||
|
|
||||||
|
@ -257,7 +262,7 @@ class MFRC522:
|
||||||
n = self.Read_MFRC522(self.DivIrqReg)
|
n = self.Read_MFRC522(self.DivIrqReg)
|
||||||
i = i - 1
|
i = i - 1
|
||||||
if not ((i != 0) and not (n&0x04)):
|
if not ((i != 0) and not (n&0x04)):
|
||||||
break
|
break
|
||||||
pOutData = []
|
pOutData = []
|
||||||
pOutData.append(self.Read_MFRC522(self.CRCResultRegL))
|
pOutData.append(self.Read_MFRC522(self.CRCResultRegL))
|
||||||
pOutData.append(self.Read_MFRC522(self.CRCResultRegM))
|
pOutData.append(self.Read_MFRC522(self.CRCResultRegM))
|
||||||
|
@ -276,29 +281,32 @@ class MFRC522:
|
||||||
buf.append(pOut[0])
|
buf.append(pOut[0])
|
||||||
buf.append(pOut[1])
|
buf.append(pOut[1])
|
||||||
(status, backData, backLen) = self.MFRC522_ToCard(self.PCD_TRANSCEIVE, buf)
|
(status, backData, backLen) = self.MFRC522_ToCard(self.PCD_TRANSCEIVE, buf)
|
||||||
|
|
||||||
if (status == self.MI_OK) and (backLen == 0x18):
|
if (status == self.MI_OK) and (backLen == 0x18):
|
||||||
size = backData[0]
|
print "Size: " + str(backData[0])
|
||||||
|
return backData[0]
|
||||||
else:
|
else:
|
||||||
size = 0
|
return 0
|
||||||
return size
|
|
||||||
|
|
||||||
def MFRC522_Auth(self, authMode, BlockAddr, Sectorkey, serNum):
|
def MFRC522_Auth(self, authMode, BlockAddr, Sectorkey, serNum):
|
||||||
buff = []
|
buff = []
|
||||||
buff.append(authMode)
|
buff.append(authMode)
|
||||||
buff.append(BlockAddr)
|
buff.append(BlockAddr)
|
||||||
i = 0
|
i = 0
|
||||||
while(i < 6):
|
while(i < len(Sectorkey)):
|
||||||
buff.append(Sectorkey[i])
|
buff.append(Sectorkey[i])
|
||||||
i = i + 1
|
i = i + 1
|
||||||
i = 0
|
i = 0
|
||||||
while(i < 4):
|
while(i < len(serNum)):
|
||||||
buff.append(serNum[i])
|
buff.append(serNum[i])
|
||||||
i = i +1
|
i = i +1
|
||||||
(status, backData, backLen) = self.MFRC522_ToCard(self.PCD_AUTHENT,buff)
|
(status, backData, backLen) = self.MFRC522_ToCard(self.PCD_AUTHENT,buff)
|
||||||
if not(status == self.MI_OK):
|
if not(status == self.MI_OK):
|
||||||
print "AUTH ERROR!!"
|
print "AUTH ERROR!!"
|
||||||
if not(self.Read_MFRC522(self.Status2Reg) & 0x08):
|
if not (self.Read_MFRC522(self.Status2Reg) & 0x08) != 0:
|
||||||
print "AUTH ERROR(status2reg & 0x08 != 0"
|
print "AUTH ERROR(status2reg & 0x08) != 0"
|
||||||
|
|
||||||
|
return status
|
||||||
|
|
||||||
def MFRC522_Read(self, blockAddr):
|
def MFRC522_Read(self, blockAddr):
|
||||||
recvData = []
|
recvData = []
|
||||||
|
@ -309,12 +317,12 @@ class MFRC522:
|
||||||
recvData.append(pOut[1])
|
recvData.append(pOut[1])
|
||||||
(status, backData, backLen) = self.MFRC522_ToCard(self.PCD_TRANSCEIVE, recvData)
|
(status, backData, backLen) = self.MFRC522_ToCard(self.PCD_TRANSCEIVE, recvData)
|
||||||
if not(status == self.MI_OK):
|
if not(status == self.MI_OK):
|
||||||
print "Error while reading!"
|
print "Error while reading!"
|
||||||
|
|
||||||
print "Got data size: "+str(backLen)
|
print "Got data size: "+str(backLen)
|
||||||
i = 0
|
i = 0
|
||||||
if len(backData) == 16:
|
if len(backData) == 16:
|
||||||
print "Sector "+str(blockAddr)+" "+str(backData)
|
print "Sector "+str(blockAddr)+" "+str(backData)
|
||||||
|
|
||||||
def MFRC522_Write(self, blockAddr, writeData):
|
def MFRC522_Write(self, blockAddr, writeData):
|
||||||
buff = []
|
buff = []
|
||||||
|
@ -325,22 +333,23 @@ class MFRC522:
|
||||||
buff.append(crc[1])
|
buff.append(crc[1])
|
||||||
(status, backData, backLen) = self.MFRC522_ToCard(self.PCD_TRANSCEIVE, buff)
|
(status, backData, backLen) = self.MFRC522_ToCard(self.PCD_TRANSCEIVE, buff)
|
||||||
if not(status == self.MI_OK) or not(backLen == 4) or not((backData[0] & 0x0F) == 0x0A):
|
if not(status == self.MI_OK) or not(backLen == 4) or not((backData[0] & 0x0F) == 0x0A):
|
||||||
status = self.MI_ERR
|
status = self.MI_ERR
|
||||||
print str(backLen)+" backdata &0x0F == 0x0A "+str(backData[0]&0x0F)
|
|
||||||
|
print str(backLen)+" backdata &0x0F == 0x0A "+str(backData[0]&0x0F)
|
||||||
if status == self.MI_OK:
|
if status == self.MI_OK:
|
||||||
i = 0
|
i = 0
|
||||||
buf = []
|
buf = []
|
||||||
while i < 16:
|
while i < 16:
|
||||||
buf.append(writeData[i])
|
buf.append(writeData[i])
|
||||||
i = i + 1
|
i = i + 1
|
||||||
crc = self.CalulateCRC(buf)
|
crc = self.CalulateCRC(buf)
|
||||||
buf.append(crc[0])
|
buf.append(crc[0])
|
||||||
buf.append(crc[1])
|
buf.append(crc[1])
|
||||||
(status, backData, backLen) = self.MFRC522_ToCard(self.PCD_TRANSCEIVE,buf)
|
(status, backData, backLen) = self.MFRC522_ToCard(self.PCD_TRANSCEIVE,buf)
|
||||||
if not(status == self.MI_OK) or not(backLen == 4) or not((backData[0] & 0x0F) == 0x0A):
|
if not(status == self.MI_OK) or not(backLen == 4) or not((backData[0] & 0x0F) == 0x0A):
|
||||||
print "Error while writing"
|
print "Error while writing"
|
||||||
if status == self.MI_OK:
|
if status == self.MI_OK:
|
||||||
print "Data writen"
|
print "Data writen"
|
||||||
|
|
||||||
|
|
||||||
def MFRC522_Init(self):
|
def MFRC522_Init(self):
|
||||||
|
@ -378,15 +387,14 @@ while continue_reading:
|
||||||
(status,backData) = MIFAREReader.MFRC522_Anticoll()
|
(status,backData) = MIFAREReader.MFRC522_Anticoll()
|
||||||
if status == MIFAREReader.MI_OK:
|
if status == MIFAREReader.MI_OK:
|
||||||
print "Card read UID: "+str(backData[0])+","+str(backData[1])+","+str(backData[2])+","+str(backData[3])+","+str(backData[4])
|
print "Card read UID: "+str(backData[0])+","+str(backData[1])+","+str(backData[2])+","+str(backData[3])+","+str(backData[4])
|
||||||
|
|
||||||
|
key = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]
|
||||||
|
|
||||||
MIFAREReader.MFRC522_SelectTag(backData)
|
MIFAREReader.MFRC522_SelectTag(backData)
|
||||||
key = [0xFF,0xFF,0xFF,0xFF,0xFF]
|
|
||||||
|
status = MIFAREReader.MFRC522_Auth(MIFAREReader.PICC_AUTHENT1A, 11, key, backData)
|
||||||
|
if status == MIFAREReader.MI_OK:
|
||||||
MIFAREReader.MFRC522_Auth(0x60,0,keyA,backData)
|
print "AUTH OK"
|
||||||
MIFAREReader.MFRC522_Read(0)
|
else:
|
||||||
i = 1
|
print "AUTH ERROR"
|
||||||
while i < 64:
|
|
||||||
MIFAREReader.MFRC522_Auth(0x60,i,keyA,backData)
|
|
||||||
IFAREReader.MFRC522_Read(i)
|
|
||||||
i = i + 1
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue