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

errors in mfrc552.py #21

Open
wants to merge 25 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
91 changes: 91 additions & 0 deletions Pico_example/CreateNdefTag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
from mfrc522 import MFRC522
from RfidAccess import RfidAccess
import utime




reader = MFRC522(spi_id=0,sck=2,miso=4,mosi=3,cs=1,rst=0)
access = RfidAccess()

print("")
print("Please place card on reader")
print("")

def checksum(data):
crc = 0xc7
for byte in data:
crc ^= byte
for _ in range(8):
msb = crc & 0x80
crc = (crc << 1) & 0xff
if msb:
crc ^= 0x1d
return crc


PreviousCard = [0]

try:
while True:

reader.init()
(stat, tag_type) = reader.request(reader.REQIDL)
if stat == reader.OK:
(stat, uid) = reader.SelectTagSN()
if uid == PreviousCard:
continue
if stat == reader.OK:
print("Card detected {} uid={}".format(hex(int.from_bytes(bytes(uid),"little",False)).upper(),reader.tohexstring(uid)))
defaultKey = [255,255,255,255,255,255]
firstSectorKey = [0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5]
nextSectorKey = [0xD3, 0xF7, 0xD3, 0xF7, 0xD3, 0xF7]

#set MAD sector
# first fill block permission
access.setTrailerAccess(keyA_Write=access.KEYB,access_Read=access.KEYAB,access_Write=access.KEYB,
keyB_Read=access.NEVER,keyB_Write=access.KEYB)
access.setBlockAccess(access.ALLBLOCK, access_Read=access.KEYAB, access_Write=access.KEYB,
access_Inc=access.NEVER, access_Dec=access.NEVER)
block3 = access.fillBlock3(keyA=firstSectorKey,keyB=defaultKey)
#Write the sector access
if reader.writeSectorBlock(uid,0,3,block3,keyA=defaultKey) == reader.ERR:
print("Writing MAD sector failed!")
else:
print(".",end="")
b1 = [0x14,0x01,0x03,0xE1,0x03,0xE1,0x03,0xE1,0x03,0xE1,0x03,0xE1,0x03,0xE1,0x03,0xE1]
b1[0] = checksum(b1[1:]) # I know this is already ok but just to demonstrate the CRC
reader.writeSectorBlock(uid,0,1,b1,keyB=defaultKey)
b2 = [0x03,0xE1,0x03,0xE1,0x03,0xE1,0x03,0xE1,0x03,0xE1,0x03,0xE1,0x03,0xE1,0x03,0xE1]
reader.writeSectorBlock(uid,0,2,b1,keyB=defaultKey)
#set permission for all other sectors
access.setTrailerAccess(keyA_Write=access.KEYB,access_Read=access.KEYAB,access_Write=access.KEYB,
keyB_Read=access.NEVER,keyB_Write=access.KEYB)
access.setBlockAccess(access.ALLBLOCK, access_Read=access.KEYAB, access_Write=access.KEYAB,
access_Inc=access.KEYAB, access_Dec=access.KEYAB)
block3 = access.fillBlock3(keyA=nextSectorKey,keyB=defaultKey)
#Write all next sectors access
for sector in range(1,16):
if reader.writeSectorBlock(uid,sector,3,block3,keyA=defaultKey) == reader.ERR:
print("\nWriting to sector ",sector," Failed!")
break
else:
print(".",end="")
#force sector 1 to be 1 record empty
block= 16 *[0]
block[2]=0xfe
if reader.writeSectorBlock(uid,1,0,block,keyB=defaultKey) == reader.ERR:
print("Unable to set first NDEF record!")
print("\nDone.")

previousCard=uid
break
else:
PreviousCard=[0]
utime.sleep_ms(50)

except KeyboardInterrupt:
print("Bye")



70 changes: 70 additions & 0 deletions Pico_example/EraseNdefTag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from mfrc522 import MFRC522
from RfidAccess import RfidAccess
import utime



reader = MFRC522(spi_id=0,sck=2,miso=4,mosi=3,cs=1,rst=0)
access = RfidAccess()

print("")
print("Please place card on reader")
print("")



PreviousCard = [0]

try:
while True:

reader.init()
(stat, tag_type) = reader.request(reader.REQIDL)
if stat == reader.OK:
(stat, uid) = reader.SelectTagSN()
if uid == PreviousCard:
continue

if stat == reader.OK:
print("Card detected {} uid={}".format(hex(int.from_bytes(bytes(uid),"little",False)).upper(),reader.tohexstring(uid)))
firstSectorKey = [0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5]
nextSectorKey = [0xD3, 0xF7, 0xD3, 0xF7, 0xD3, 0xF7]
defaultKey = [255,255,255,255,255,255]

# set default access
access.decodeAccess(0xff,0x07,0x80)
block3 = access.fillBlock3(keyA=defaultKey,keyB=defaultKey)
print("Reset Mad Sector (first sector)")
#reset first sector
reader.writeSectorBlock(uid,0,3,block3,keyB=defaultKey)
#erase block1 and 2
datablock = 16 * [0]
reader.writeSectorBlock(uid,0,1,datablock,keyB=defaultKey)
reader.writeSectorBlock(uid,0,2,datablock,keyB=defaultKey)

#reset all other sectors
for s in range(1,16):
# permission to default
print("Reset sector ",s)
reader.writeSectorBlock(uid,s,3,block3,keyB=defaultKey)
for b in range(3):
# put all data to zero block 0,1 and 2
reader.writeSectorBlock(uid,s,b,datablock,keyB=defaultKey)
# ok dump new data
print("\n---- Dump card using defaultkeyB [0xff, 0xff, 0xff, 0xff, 0xff, 0xff]\n")
reader.MFRC522_DumpClassic1K(uid, Start=0, End=64, keyB=defaultKey)
PreviousCard = uid
break
else:
pass
#print("Authentication error")
else:
PreviousCard=[0]
utime.sleep_ms(50)

except KeyboardInterrupt:
print("Bye")

print("Card done!")


61 changes: 61 additions & 0 deletions Pico_example/Pico_read.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from mfrc522 import MFRC522
import utime


def uidToString(uid):
mystring = ""
for i in uid:
mystring = "%02X" % i + mystring
return mystring


reader = MFRC522(spi_id=0,sck=2,miso=4,mosi=3,cs=1,rst=0)

print("")
print("Please place card on reader")
print("")



PreviousCard = [0]

try:
while True:

reader.init()

(stat, tag_type) = reader.request(reader.REQIDL)
#print('request stat:',stat,' tag_type:',tag_type)
if stat == reader.OK:
(stat, uid) = reader.SelectTagSN()
if uid == PreviousCard:
continue
if stat == reader.OK:
print("Card detected {} uid={}".format(hex(int.from_bytes(bytes(uid),"little",False)).upper(),reader.tohexstring(uid)))

if reader.IsNTAG():
print("Got NTAG{}".format(reader.NTAG))
reader.MFRC522_Dump_NTAG(Start=0,End=reader.NTAG_MaxPage)
#print("Write Page 5 to 0x1,0x2,0x3,0x4 in 2 second")
#utime.sleep(2)
#data = [1,2,3,4]
#reader.writeNTAGPage(5,data)
#reader.MFRC522_Dump_NTAG(Start=5,End=6)
else:
(stat, tag_type) = reader.request(reader.REQIDL)
if stat == reader.OK:
(stat, uid2) = reader.SelectTagSN()
if stat == reader.OK:
if uid != uid2:
continue
defaultKey = [255,255,255,255,255,255]
reader.MFRC522_DumpClassic1K(uid,Start=0, End=64, keyA=defaultKey)
PreviousCard = uid
else:
pass
else:
PreviousCard=[0]
utime.sleep_ms(50)

except KeyboardInterrupt:
pass
55 changes: 55 additions & 0 deletions Pico_example/Pico_write.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from mfrc522 import MFRC522

'''
BE AWARE that sectors(3,7,11,15,...,63) are access block.
if you want to change (sector % 4) == 3 you should
know how keys and permission work!
'''



def uidToString(uid):
mystring = ""
for i in uid:
mystring = "%02X" % i + mystring
return mystring

reader = MFRC522(spi_id=0,sck=2,miso=4,mosi=3,cs=1,rst=0)

print("")
print("Please place card on reader")
print("")

key = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]

try:
while True:

(stat, tag_type) = reader.request(reader.REQIDL)

if stat == reader.OK:
(stat, uid) = reader.SelectTagSN()
if stat == reader.OK:
print(uid)
print("Card detected %s" % uidToString(uid))
reader.MFRC522_DumpClassic1K(uid,keyA=key)
print("Test ! writing sector 2, block 0 (absolute block(8)")
print("with [ 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 ]")
absoluteBlock=8
value=[]
for i in range(16):
value.append(i)
status = reader.auth(reader.AUTHENT1A, absoluteBlock, key, uid)
if status == reader.OK:
status = reader.write(absoluteBlock,value)
if status == reader.OK:
reader.MFRC522_DumpClassic1K(uid,keyA=key)
else:
print("unable to write")
else:
print("Authentication error for writing")
break
except KeyboardInterrupt:
print("Bye")


47 changes: 47 additions & 0 deletions Pico_example/ReadNdefTag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from mfrc522 import MFRC522
import utime




reader = MFRC522(spi_id=0,sck=2,miso=4,mosi=3,cs=1,rst=0)

print("")
print("Place card into reader")
print("")



PreviousCard = [0]

try:
while True:

reader.init()
(stat, tag_type) = reader.request(reader.REQIDL)
if stat == reader.OK:
(stat, uid) = reader.SelectTagSN()
if uid == PreviousCard:
continue

if stat == reader.OK:
print("Card detected {} uid={}".format(hex(int.from_bytes(bytes(uid),"little",False)).upper(),reader.tohexstring(uid)))
firstSectorKey = [0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5]
nextSectorKey = [0xD3, 0xF7, 0xD3, 0xF7, 0xD3, 0xF7]
#defaultKey = [255,255,255,255,255,255]

#read MAD sector (first sector)
if reader.MFRC522_DumpClassic1K(uid, Start=0, End=4, keyA=firstSectorKey)== reader.OK:
#read the rest of the card
reader.MFRC522_DumpClassic1K(uid, Start=4, End=64, keyA=nextSectorKey)
print("Done")
PreviousCard = uid
else:
PreviousCard=[0]
utime.sleep_ms(50)

except KeyboardInterrupt:
print("Bye")



Loading