Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compatibility with new SPI package #99

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
# Capture SIGINT for cleanup when the script is aborted
def end_read(signal,frame):
global continue_reading
print "Ctrl+C captured, ending read."
print("Ctrl+C captured, ending read.")
continue_reading = False
GPIO.cleanup()

Expand All @@ -48,7 +48,7 @@ def end_read(signal,frame):

# If a card is found
if status == MIFAREReader.MI_OK:
print "Card detected"
print("Card detected")

# Get the UID of the card
(status,uid) = MIFAREReader.MFRC522_Anticoll()
Expand All @@ -57,7 +57,7 @@ def end_read(signal,frame):
if status == MIFAREReader.MI_OK:

# Print UID
print "Card read UID: %s,%s,%s,%s" % (uid[0], uid[1], uid[2], uid[3])
print("Card read UID: %s,%s,%s,%s" % (uid[0], uid[1], uid[2], uid[3]))

# This is the default key for authentication
key = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]
Expand Down
141 changes: 71 additions & 70 deletions MFRC522.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@
import spi
import signal
import time

class MFRC522:
NRSTPD = 22

MAX_LEN = 16

PCD_IDLE = 0x00
PCD_AUTHENT = 0x0E
PCD_RECEIVE = 0x08
PCD_TRANSMIT = 0x04
PCD_TRANSCEIVE = 0x0C
PCD_RESETPHASE = 0x0F
PCD_CALCCRC = 0x03

PICC_REQIDL = 0x26
PICC_REQALL = 0x52
PICC_ANTICOLL = 0x93
Expand All @@ -52,11 +52,11 @@ class MFRC522:
PICC_RESTORE = 0xC2
PICC_TRANSFER = 0xB0
PICC_HALT = 0x50

MI_OK = 0
MI_NOTAGERR = 1
MI_ERR = 2

Reserved00 = 0x00
CommandReg = 0x01
CommIEnReg = 0x02
Expand All @@ -73,7 +73,7 @@ class MFRC522:
BitFramingReg = 0x0D
CollReg = 0x0E
Reserved01 = 0x0F

Reserved10 = 0x10
ModeReg = 0x11
TxModeReg = 0x12
Expand All @@ -90,8 +90,8 @@ class MFRC522:
Reserved13 = 0x1D
Reserved14 = 0x1E
SerialSpeedReg = 0x1F
Reserved20 = 0x20

Reserved20 = 0x20
CRCResultRegM = 0x21
CRCResultRegL = 0x22
Reserved21 = 0x23
Expand All @@ -107,7 +107,7 @@ class MFRC522:
TReloadRegL = 0x2D
TCounterValueRegH = 0x2E
TCounterValueRegL = 0x2F

Reserved30 = 0x30
TestSel1Reg = 0x31
TestSel2Reg = 0x32
Expand All @@ -124,42 +124,43 @@ class MFRC522:
Reserved32 = 0x3D
Reserved33 = 0x3E
Reserved34 = 0x3F

serNum = []

device_0 = spi.openSPI(device="/dev/spidev0.0", mode=0, speed=1000000)

def __init__(self, dev='/dev/spidev0.0', spd=1000000):
spi.openSPI(device=dev,speed=spd)
spi.openSPI(device=dev,mode=0,speed=spd)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(self.NRSTPD, GPIO.OUT)
GPIO.output(self.NRSTPD, 1)
self.MFRC522_Init()

def MFRC522_Reset(self):
self.Write_MFRC522(self.CommandReg, self.PCD_RESETPHASE)

def Write_MFRC522(self, addr, val):
spi.transfer(((addr<<1)&0x7E,val))
spi.transfer(self.device_0, ((addr<<1)&0x7E,val))

def Read_MFRC522(self, addr):
val = spi.transfer((((addr<<1)&0x7E) | 0x80,0))
val = spi.transfer(self.device_0, (((addr<<1)&0x7E) | 0x80,0))
return val[1]

def SetBitMask(self, reg, mask):
tmp = self.Read_MFRC522(reg)
self.Write_MFRC522(reg, tmp | mask)

def ClearBitMask(self, reg, mask):
tmp = self.Read_MFRC522(reg);
self.Write_MFRC522(reg, tmp & (~mask))

def AntennaOn(self):
temp = self.Read_MFRC522(self.TxControlReg)
if(~(temp & 0x03)):
self.SetBitMask(self.TxControlReg, 0x03)

def AntennaOff(self):
self.ClearBitMask(self.TxControlReg, 0x03)

def MFRC522_ToCard(self,command,sendData):
backData = []
backLen = 0
Expand All @@ -169,58 +170,58 @@ def MFRC522_ToCard(self,command,sendData):
lastBits = None
n = 0
i = 0

if command == self.PCD_AUTHENT:
irqEn = 0x12
waitIRq = 0x10
if command == self.PCD_TRANSCEIVE:
irqEn = 0x77
waitIRq = 0x30

self.Write_MFRC522(self.CommIEnReg, irqEn|0x80)
self.ClearBitMask(self.CommIrqReg, 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)):
self.Write_MFRC522(self.FIFODataReg, sendData[i])
i = i+1

self.Write_MFRC522(self.CommandReg, command)

if command == self.PCD_TRANSCEIVE:
self.SetBitMask(self.BitFramingReg, 0x80)

i = 2000
while True:
n = self.Read_MFRC522(self.CommIrqReg)
i = i - 1
if ~((i!=0) and ~(n&0x01) and ~(n&waitIRq)):
break

self.ClearBitMask(self.BitFramingReg, 0x80)

if i != 0:
if (self.Read_MFRC522(self.ErrorReg) & 0x1B)==0x00:
status = self.MI_OK

if n & irqEn & 0x01:
status = self.MI_NOTAGERR

if command == self.PCD_TRANSCEIVE:
n = self.Read_MFRC522(self.FIFOLevelReg)
lastBits = self.Read_MFRC522(self.ControlReg) & 0x07
if lastBits != 0:
backLen = (n-1)*8 + lastBits
else:
backLen = n*8

if n == 0:
n = 1
if n > self.MAX_LEN:
n = self.MAX_LEN

i = 0
while i<n:
backData.append(self.Read_MFRC522(self.FIFODataReg))
Expand All @@ -229,37 +230,37 @@ def MFRC522_ToCard(self,command,sendData):
status = self.MI_ERR

return (status,backData,backLen)


def MFRC522_Request(self, reqMode):
status = None
backBits = None
TagType = []

self.Write_MFRC522(self.BitFramingReg, 0x07)

TagType.append(reqMode);
(status,backData,backBits) = self.MFRC522_ToCard(self.PCD_TRANSCEIVE, TagType)

if ((status != self.MI_OK) | (backBits != 0x10)):
status = self.MI_ERR

return (status,backBits)


def MFRC522_Anticoll(self):
backData = []
serNumCheck = 0

serNum = []

self.Write_MFRC522(self.BitFramingReg, 0x00)

serNum.append(self.PICC_ANTICOLL)
serNum.append(0x20)

(status,backData,backBits) = self.MFRC522_ToCard(self.PCD_TRANSCEIVE,serNum)

if(status == self.MI_OK):
i = 0
if len(backData)==5:
Expand All @@ -270,9 +271,9 @@ def MFRC522_Anticoll(self):
status = self.MI_ERR
else:
status = self.MI_ERR

return (status,backData)

def CalulateCRC(self, pIndata):
self.ClearBitMask(self.DivIrqReg, 0x04)
self.SetBitMask(self.FIFOLevelReg, 0x80);
Expand All @@ -291,7 +292,7 @@ def CalulateCRC(self, pIndata):
pOutData.append(self.Read_MFRC522(self.CRCResultRegL))
pOutData.append(self.Read_MFRC522(self.CRCResultRegM))
return pOutData

def MFRC522_SelectTag(self, serNum):
backData = []
buf = []
Expand All @@ -305,13 +306,13 @@ def MFRC522_SelectTag(self, serNum):
buf.append(pOut[0])
buf.append(pOut[1])
(status, backData, backLen) = self.MFRC522_ToCard(self.PCD_TRANSCEIVE, buf)

if (status == self.MI_OK) and (backLen == 0x18):
print "Size: " + str(backData[0])
print("Size: " + str(backData[0]))
return backData[0]
else:
return 0

def MFRC522_Auth(self, authMode, BlockAddr, Sectorkey, serNum):
buff = []

Expand All @@ -338,13 +339,13 @@ def MFRC522_Auth(self, authMode, BlockAddr, Sectorkey, serNum):

# Check if an error occurred
if not(status == self.MI_OK):
print "AUTH ERROR!!"
print("AUTH ERROR!!")
if not (self.Read_MFRC522(self.Status2Reg) & 0x08) != 0:
print "AUTH ERROR(status2reg & 0x08) != 0"
print("AUTH ERROR(status2reg & 0x08) != 0")

# Return the status
return status

def MFRC522_StopCrypto1(self):
self.ClearBitMask(self.Status2Reg, 0x08)

Expand All @@ -357,11 +358,11 @@ def MFRC522_Read(self, blockAddr):
recvData.append(pOut[1])
(status, backData, backLen) = self.MFRC522_ToCard(self.PCD_TRANSCEIVE, recvData)
if not(status == self.MI_OK):
print "Error while reading!"
print("Error while reading!")
i = 0
if len(backData) == 16:
print "Sector "+str(blockAddr)+" "+str(backData)
print("Sector "+str(blockAddr)+" "+str(backData))

def MFRC522_Write(self, blockAddr, writeData):
buff = []
buff.append(self.PICC_WRITE)
Expand All @@ -372,8 +373,8 @@ def MFRC522_Write(self, blockAddr, writeData):
(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):
status = self.MI_ERR
print "%s backdata &0x0F == 0x0A %s" % (backLen, backData[0]&0x0F)

print("%s backdata &0x0F == 0x0A %s" % (backLen, backData[0]&0x0F))
if status == self.MI_OK:
i = 0
buf = []
Expand All @@ -385,9 +386,9 @@ def MFRC522_Write(self, blockAddr, writeData):
buf.append(crc[1])
(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):
print "Error while writing"
print("Error while writing")
if status == self.MI_OK:
print "Data written"
print("Data written")

def MFRC522_DumpClassic1K(self, key, uid):
i = 0
Expand All @@ -397,20 +398,20 @@ def MFRC522_DumpClassic1K(self, key, uid):
if status == self.MI_OK:
self.MFRC522_Read(i)
else:
print "Authentication error"
print("Authentication error")
i = i+1

def MFRC522_Init(self):
GPIO.output(self.NRSTPD, 1)

self.MFRC522_Reset();


self.Write_MFRC522(self.TModeReg, 0x8D)
self.Write_MFRC522(self.TPrescalerReg, 0x3E)
self.Write_MFRC522(self.TReloadRegL, 30)
self.Write_MFRC522(self.TReloadRegH, 0)

self.Write_MFRC522(self.TxAutoReg, 0x40)
self.Write_MFRC522(self.ModeReg, 0x3D)
self.AntennaOn()
Loading