Skip to content

Esptrim #7

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

Open
wants to merge 3 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
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,22 @@ I used the following pins for my setup:

| 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 |

| sck | 14 | "GP14" |For hardware sck (esp) |
| mosi | 13 | "GP16" |For hardware mosi (esp) |
| miso | 12 | "GP15" |For hardware miso (esp) |
| rst | 2 | "GP22" | |
| cs | 16 | "GP14" |Labeled SDA on most RFID-RC522 boards |

Note for the hardware spi on the esp8266 the sck, mosi, and miso pins don't need to be specified for initalization,
only spiblk needs to be set to 1. In software mode they will need to be specified and spblk can be left unset.

Now enter the REPL you could run one of the two exmaples:

For detecting, authenticating and reading from a card:

import read
read.do_read()
read.do_read() #for software
read.do_read(1) #for esp hardware

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.
Expand Down
12 changes: 8 additions & 4 deletions examples/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
from os import uname


def do_read():
def do_read( esp=None):

if uname()[0] == 'WiPy':
rdr = mfrc522.MFRC522("GP14", "GP16", "GP15", "GP22", "GP17")
elif uname()[0] == 'esp8266':
rdr = mfrc522.MFRC522(0, 2, 4, 5, 14)
elif uname()[0] == 'esp8266' and esp != None:
if esp==1:
rdr = mfrc522.MFRC522(rst=2,cs=16,spiblk=1)
else:
rdr = mfrc522.MFRC522(rst=2,cs=16,sck=14,mosi=13,miso=12)
else:
raise RuntimeError("Unsupported platform")

Expand Down Expand Up @@ -43,4 +46,5 @@ def do_read():
print("Failed to select tag")

except KeyboardInterrupt:
print("Bye")
print("Bye")

7 changes: 5 additions & 2 deletions examples/write.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
from os import uname


def do_write():
def do_write(esp=None):

if uname()[0] == 'WiPy':
rdr = mfrc522.MFRC522("GP14", "GP16", "GP15", "GP22", "GP17")
elif uname()[0] == 'esp8266':
rdr = mfrc522.MFRC522(0, 2, 4, 5, 14)
if esp == 1:
rdr = mfrc522.MFRC522(rst=2,cs=16,spiblk=1)
else:
rdr = mfrc522.MFRC522(rst=2,cs=16,sck=14,mosi=13,miso=12)
else:
raise RuntimeError("Unsupported platform")

Expand Down
24 changes: 15 additions & 9 deletions mfrc522.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,31 @@ class MFRC522:
AUTHENT1A = 0x60
AUTHENT1B = 0x61

def __init__(self, sck, mosi, miso, rst, cs):

self.sck = Pin(sck, Pin.OUT)
self.mosi = Pin(mosi, Pin.OUT)
self.miso = Pin(miso)
def __init__(self, rst, cs, spiblk=None, sck=None, mosi=None, miso=None):
if spiblk == None and (sck != mosi != miso):
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)

board = uname()[0]

if board == 'WiPy' or board == 'LoPy' or board == 'FiPy':
self.spi = SPI(0)
if spiblk == None:
self.spi = SPI(0)
else:
self.spi = SPI(spiblk)
self.spi.init(SPI.MASTER, baudrate=1000000, pins=(self.sck, self.mosi, self.miso))
elif board == 'esp8266':
self.spi = SPI(baudrate=100000, polarity=0, phase=0, sck=self.sck, mosi=self.mosi, miso=self.miso)
self.spi.init()
if spiblk == None:
self.spi = SPI(baudrate=1000000, polarity=0, phase=0, sck=self.sck, mosi=self.mosi, miso=self.miso)
else:
self.spi = SPI(spiblk, baudrate=1000000, polarity=0, phase=0)
#self.spi.init()
else:
raise RuntimeError("Unsupported platform")

Expand Down