After a while, I finally get back to play with my WiPy
So, I want to use a RFID reader module: the MFRC522 (http://www.nxp.com/documents/data_sheet/MFRC522.pdf)
I found a python library on github: https://github.com/mxgxw/MFRC522-python
And I want to make it work in micropython.
I have configured the PINs and all:
Code: Select all
def __init__(self, spd=1000000):
# first assign CLK, MISO, MOSI, CS to the correct pins
self.pin_clk = Pin(self.CLK, mode=Pin.ALT, alt=7) # CLK
Pin(self.MISO, mode=Pin.ALT, alt=7) # MISO
Pin(self.MOSI, mode=Pin.ALT, alt=7) # MOSI
Pin(self.CS, mode=Pin.ALT, alt=7) # NSS/CS
self.spi = SPI(0)
self.spi.init(mode=SPI.MASTER, baudrate=spd, polarity=0, phase=0, firstbit=SPI.MSB)
self.pin_reset = Pin(self.RESET, mode=Pin.OUT)
self.pin_reset.value(1)
self.MFRC522_Init()
Then, the init is:
Code: Select all
def MFRC522_Init(self):
self.pin_reset.value(1)
self.pin_reset.value(0)
self.pin_reset.value(1)
self.MFRC522_Reset();
print("Test begin")
self.Write_MFRC522(self.TModeReg, 0x8D)
print("{} should be 0x8D".format(hex(self.Read_MFRC522(self.TModeReg))))
print("Test end")
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()
Code: Select all
print("Test begin")
self.Write_MFRC522(self.TModeReg, 0x8D)
print("{} should be 0x8D".format(hex(self.Read_MFRC522(self.TModeReg))))
print("Test end")
Code: Select all
def Write_MFRC522(self, addr, val):
# 0(MSB = Write) ADDR1 ADDR2 ADDR3 ADDR4 ADDR5 ADDR6 0(LSB = 0) DATA
spiBytes = bytearray(2)
spiBytes[0] = ((addr<<1)&0x7E)
spiBytes[1] = val
return(self.spi.write(spiBytes))
def Read_MFRC522(self, addr):
# 1(MSB = Read) ADDR1 ADDR2 ADDR3 ADDR4 ADDR5 ADDR6 0(LSB = 0)
spiAddr = bytearray(2)
spiAddr[0] = ((addr<<1)&0x7E|0x80)
spiAddr[1] = 0x00
data = bytearray(2)
self.spi.write_readinto(spiAddr, data)
print("read {} from {}".format(data, spiAddr))
return data[1]
To read, the behavior is similiar: each byte send is the address on which we want to read. The first bit of the byte (MSB) shall be 1 and the last bit (LSB) shall be 0, the others 6 bits are the address.
Now, to have the result, you need to read the next byte: you write the address on byte 0 and you have the value of the address in the byte 1.
To have the full detail, see page 10 and 11 of the doc I gave above
But when I execute it on my WiPy, I have:
Code: Select all
>>> execfile('nfc.py')
>>> import nfc
>>> MIFAREReader = nfc.MFRC522()
Test begin
read bytearray(b'\xff\xff') from bytearray(b'\xd4\x00')
0xff should be 0x8D
Test end
If you have any idea, I would be grateful