diff --git a/README.md b/README.md index ff50f49..d89300b 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/examples/read.py b/examples/read.py index 783d554..101e943 100644 --- a/examples/read.py +++ b/examples/read.py @@ -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") @@ -43,4 +46,5 @@ def do_read(): print("Failed to select tag") except KeyboardInterrupt: - print("Bye") \ No newline at end of file + print("Bye") + diff --git a/examples/write.py b/examples/write.py index 7878d83..08fbff7 100644 --- a/examples/write.py +++ b/examples/write.py @@ -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") diff --git a/mfrc522.py b/mfrc522.py index 1833d75..56df011 100644 --- a/mfrc522.py +++ b/mfrc522.py @@ -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")