Added support for WiPy.

This commit is contained in:
Stefan Wendler
2016-05-31 21:15:22 +02:00
parent 2083fa5a69
commit 14f8f8b9bb
6 changed files with 54 additions and 26 deletions

View File

@@ -4,8 +4,9 @@
Basic class to access RFID readers of the type [MFRC522](http://www.nxp.com/documents/data_sheet/MFRC522.pdf). Basic class to access RFID readers of the type [MFRC522](http://www.nxp.com/documents/data_sheet/MFRC522.pdf).
This is basically a re-write of [this](https://github.com/mxgxw/MFRC522-python) Python port for the MFRC522. I This is basically a re-write of [this](https://github.com/mxgxw/MFRC522-python) Python port for the MFRC522. I
tried to strip things down and make them more "pythonic" so the result is small enough to run on tried to strip things down and make them more "pythonic" so the result is small enough to run on
[Micropython](https://github.com/micropython/micropython) boards. The only board I tried this so far is the [Micropython](https://github.com/micropython/micropython) boards. I tried the class so far on the
[ESP8266](https://github.com/micropython/micropython/tree/master/esp8266). [ESP8266](https://github.com/micropython/micropython/tree/master/esp8266) and
the [WiPy](https://github.com/micropython/micropython/tree/master/cc3200).
## Usage ## Usage
@@ -13,15 +14,15 @@ Put the modules ``mfrc522.py``, ``examples/read.py``, ``examples/write.py`` to t
For the ESP8266 there are multiple solutions to do that. E.g. use the For the ESP8266 there are multiple solutions to do that. E.g. use the
[WebREPL file transfer](https://github.com/micropython/webrepl), or [mpfshell](https://github.com/wendlers/mpfshell). [WebREPL file transfer](https://github.com/micropython/webrepl), or [mpfshell](https://github.com/wendlers/mpfshell).
The class expects the reader by default to be connected like this: I used the following pins for my setup:
| Signal | GPIO | Note | | Signal | GPIO ESP8266 | GPIO WiPy | Note |
| --------- | --------- | ------------------------------------- | | --------- | ------------ | ----------------------------------------------------- |
| sck | GPIO0 | | | sck | 0 | "GP14" | |
| mosi | GPIO2 | | | mosi | 2 | "GP16" | |
| miso | GPIO4 | | | miso | 4 | "GP15" | |
| rst | GPIO5 | | | rst | 5 | "GP22" | |
| cs | GPIO14 | Labeled SDA on most RFID-RC522 boards | | cs | 14 | "GP14" |Labeled SDA on most RFID-RC522 boards |
Now enter the REPL you could run one of the two exmaples: Now enter the REPL you could run one of the two exmaples:
@@ -30,7 +31,7 @@ For detecting, authenticating and reading from a card:
import read import read
read.do_read() read.do_read()
This will wait for a MifareClasskic 1k card. As soon the card is detected, it is authenticated, and This will wait for a MifareClassic 1k card. As soon the card is detected, it is authenticated, and
16 bytes are read from address 0x08. 16 bytes are read from address 0x08.
For detecting, authenticating and writing to a card: For detecting, authenticating and writing to a card:
@@ -38,5 +39,5 @@ For detecting, authenticating and writing to a card:
import write import write
write.do_write() write.do_write()
This will wait for a MifareClasskic 1k card. As soon the card is detected, it is authenticated, and This will wait for a MifareClassic 1k card. As soon the card is detected, it is authenticated, and
16 bytes written to address 0x08. 16 bytes written to address 0x08.

3
deploy_wipy.sh Executable file
View File

@@ -0,0 +1,3 @@
#!/usr/bin/env bash
mpfshell -c "open ttyUSB0; cd flash/lib; put mfrc522.py; lcd examples; put read.py; put write.py; ls" -n

View File

@@ -1,9 +1,15 @@
import mfrc522 import mfrc522
from os import uname
def do_read(): def do_read():
rdr = mfrc522.MFRC522() if uname()[0] == 'WiPy':
rdr = mfrc522.MFRC522("GP14", "GP16", "GP15", "GP22", "GP17")
elif uname()[0] == 'esp8266':
rdr = mfrc522.MFRC522(0, 2, 4, 5, 14)
else:
raise RuntimeError("Unsupported platform")
print("") print("")
print("Place card before reader to read from address 0x08") print("Place card before reader to read from address 0x08")
@@ -12,7 +18,7 @@ def do_read():
try: try:
while True: while True:
(stat, tag_type) = rdr.request(0x26) (stat, tag_type) = rdr.request(rdr.REQIDL)
if stat == rdr.OK: if stat == rdr.OK:
@@ -28,7 +34,7 @@ def do_read():
key = [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF] key = [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]
if rdr.auth(0x60, 8, key, raw_uid) == rdr.OK: if rdr.auth(rdr.AUTHENT1A, 8, key, raw_uid) == rdr.OK:
print("Address 8 data: %s" % rdr.read(8)) print("Address 8 data: %s" % rdr.read(8))
rdr.stop_crypto1() rdr.stop_crypto1()
else: else:

View File

@@ -1,9 +1,15 @@
import mfrc522 import mfrc522
from os import uname
def do_write(): def do_write():
rdr = mfrc522.MFRC522() if uname()[0] == 'WiPy':
rdr = mfrc522.MFRC522("GP14", "GP16", "GP15", "GP22", "GP17")
elif uname()[0] == 'esp8266':
rdr = mfrc522.MFRC522(0, 2, 4, 5, 14)
else:
raise RuntimeError("Unsupported platform")
print("") print("")
print("Place card before reader to write address 0x08") print("Place card before reader to write address 0x08")
@@ -12,7 +18,7 @@ def do_write():
try: try:
while True: while True:
(stat, tag_type) = rdr.request(0x26) (stat, tag_type) = rdr.request(rdr.REQIDL)
if stat == rdr.OK: if stat == rdr.OK:
@@ -28,7 +34,7 @@ def do_write():
key = [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF] key = [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]
if rdr.auth(0x60, 8, key, raw_uid) == rdr.OK: if rdr.auth(rdr.AUTHENT1A, 8, key, raw_uid) == rdr.OK:
stat = rdr.write(8, b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f") stat = rdr.write(8, b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f")
rdr.stop_crypto1() rdr.stop_crypto1()
if stat == rdr.OK: if stat == rdr.OK:

View File

@@ -1,4 +1,5 @@
from machine import Pin, SPI from machine import Pin, SPI
from os import uname
class MFRC522: class MFRC522:
@@ -7,19 +8,30 @@ class MFRC522:
NOTAGERR = 1 NOTAGERR = 1
ERR = 2 ERR = 2
def __init__(self): REQIDL = 0x26
REQALL = 0x52
AUTHENT1A = 0x60
AUTHENT1B = 0x61
self.sck = Pin(0, Pin.OUT) def __init__(self, sck, mosi, miso, rst, cs):
self.mosi = Pin(2, Pin.OUT)
self.miso = Pin(4) self.sck = Pin(sck, Pin.OUT)
self.rst = Pin(5, Pin.OUT) self.mosi = Pin(mosi, Pin.OUT)
self.cs = Pin(14, Pin.OUT) self.miso = Pin(miso)
self.rst = Pin(rst, Pin.OUT)
self.cs = Pin(cs, Pin.OUT)
self.rst.value(0) self.rst.value(0)
self.cs.value(1) self.cs.value(1)
self.spi = SPI(baudrate=100000, polarity=0, phase=0, sck=self.sck, mosi=self.mosi, miso=self.miso) if uname()[0] == 'WiPy':
self.spi.init() self.spi = SPI(0)
self.spi.init(SPI.MASTER, baudrate=1000000, pins=(self.sck, self.mosi, self.miso))
elif uname()[0] == 'esp8266':
self.spi = SPI(baudrate=100000, polarity=0, phase=0, sck=self.sck, mosi=self.mosi, miso=self.miso)
self.spi.init()
else:
raise RuntimeError("Unsupported platform")
self.rst.value(1) self.rst.value(1)
self.init() self.init()