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).
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
[Micropython](https://github.com/micropython/micropython) boards. The only board I tried this so far is the
[ESP8266](https://github.com/micropython/micropython/tree/master/esp8266).
[Micropython](https://github.com/micropython/micropython) boards. I tried the class so far on the
[ESP8266](https://github.com/micropython/micropython/tree/master/esp8266) and
the [WiPy](https://github.com/micropython/micropython/tree/master/cc3200).
## 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
[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 |
| --------- | --------- | ------------------------------------- |
| sck | GPIO0 | |
| mosi | GPIO2 | |
| miso | GPIO4 | |
| rst | GPIO5 | |
| cs | GPIO14 | Labeled SDA on most RFID-RC522 boards |
| Signal | GPIO ESP8266 | GPIO WiPy | Note |
| --------- | ------------ | ----------------------------------------------------- |
| sck | 0 | "GP14" | |
| mosi | 2 | "GP16" | |
| miso | 4 | "GP15" | |
| rst | 5 | "GP22" | |
| cs | 14 | "GP14" |Labeled SDA on most RFID-RC522 boards |
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
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.
For detecting, authenticating and writing to a card:
@ -38,5 +39,5 @@ For detecting, authenticating and writing to a card:
import 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.

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
from os import uname
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("Place card before reader to read from address 0x08")
@ -12,7 +18,7 @@ def do_read():
try:
while True:
(stat, tag_type) = rdr.request(0x26)
(stat, tag_type) = rdr.request(rdr.REQIDL)
if stat == rdr.OK:
@ -28,7 +34,7 @@ def do_read():
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))
rdr.stop_crypto1()
else:

View file

@ -1,9 +1,15 @@
import mfrc522
from os import uname
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("Place card before reader to write address 0x08")
@ -12,7 +18,7 @@ def do_write():
try:
while True:
(stat, tag_type) = rdr.request(0x26)
(stat, tag_type) = rdr.request(rdr.REQIDL)
if stat == rdr.OK:
@ -28,7 +34,7 @@ def do_write():
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")
rdr.stop_crypto1()
if stat == rdr.OK:

View file

@ -1,4 +1,5 @@
from machine import Pin, SPI
from os import uname
class MFRC522:
@ -7,19 +8,30 @@ class MFRC522:
NOTAGERR = 1
ERR = 2
def __init__(self):
REQIDL = 0x26
REQALL = 0x52
AUTHENT1A = 0x60
AUTHENT1B = 0x61
self.sck = Pin(0, Pin.OUT)
self.mosi = Pin(2, Pin.OUT)
self.miso = Pin(4)
self.rst = Pin(5, Pin.OUT)
self.cs = Pin(14, Pin.OUT)
def __init__(self, sck, mosi, miso, rst, cs):
self.sck = Pin(sck, Pin.OUT)
self.mosi = Pin(mosi, Pin.OUT)
self.miso = Pin(miso)
self.rst = Pin(rst, Pin.OUT)
self.cs = Pin(cs, Pin.OUT)
self.rst.value(0)
self.cs.value(1)
self.spi = SPI(baudrate=100000, polarity=0, phase=0, sck=self.sck, mosi=self.mosi, miso=self.miso)
self.spi.init()
if uname()[0] == 'WiPy':
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.init()