1

I have a Pi Zero 1.2 connected to an Adafruit 2.8" PiFTF screen and a RFID-RC522 board. I also have a thermal printer connected to TX/RX, but I don't think this plays a part in my question.

The TFT screen has a 26 pin header, designed for the older Pi boards. It also has a breakout connector underneath giving access to these same pins.

The RFID board is connected as per the instructions here: https://github.com/ondryaso/pi-rc522

The problem

My problem is that I can either have the TFT screen working, or the RFID, but not both at the same time. At the moment I have commented out this line in /boot/config.txt to work on the RFID:

#dtoverlay=pitft28-capacitive,rotate=270,speed=64000000,fps=30

I'm a bit stuck trying to figure out if it's possible to have both the TFT and the RFID connected and working. I understand there may be an alternative way to connect the RFID to a second SPI SPI1?

What have I tried?

I have enabled dtoverlay=spi1-1cs in /boot/config.txt and can see the following:

crw-rw---- 1 root spi 153, 0 Aug  2 19:01 spidev0.1
crw-rw---- 1 root spi 153, 1 Aug  2 19:01 spidev1.0

But when I run my RFIDUtil.py (from ondryaso GitHub), it errors out with:

$ sudo python rfid-util.py 
Traceback (most recent call last):
  File "rfid-util.py", line 5, in <module>
    rdr = RFID()
  File "/home/pi/Development/printer/rfid/pirc522/rfid.py", line 68, in __init__
    self.spi.open(bus, device)
IOError: [Errno 2] No such file or directory

The code is as follows:

from pirc522 import RFID
import signal
import time

rdr = RFID()
util = rdr.util()
# Set util debug to true - it will print what's going on
util.debug = True

while True:
    # Wait for tag
    rdr.wait_for_tag()

    # Request tag
    (error, data) = rdr.request()
    if not error:
        print("\nDetected")

        (error, uid) = rdr.anticoll()
        if not error:
            # Print UID
            print("Card read UID: "+str(uid[0])+","+str(uid[1])+","+str(uid[2])+","+str(uid[3]))

            # Set tag as used in util. This will call RFID.select_tag(uid)
            util.set_tag(uid)
            # Save authorization info (key B) to util. It doesn't call RFID.card_auth(), that's called when needed
            util.auth(rdr.auth_b, [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF])
            # Print contents of block 4 in format "S1B0: [contents in decimal]". RFID.card_auth() will be called now
            util.read_out(4)
            # Print it again - now auth won't be called, because it doesn't have to be
            util.read_out(4)
            # Print contents of different block - S1B2 - RFID.card_auth() will be called again
            util.read_out(6)
            # We can change authorization info if you have different key in other sector
            util.auth(rdr.auth_a, [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF])
            #If you want to use methods from RFID itself, you can use this for authorization
            # This will authorize for block 1 of sector 2 -> block 9
            # This is once more called only if it's not already authorized for this block
            util.do_auth(util.block_addr(2, 1))
            # Now we can do some "lower-level" stuff with block 9
            rdr.write(9, [0x01, 0x23, 0x45, 0x67, 0x89, 0x98, 0x76, 0x54, 0x32, 0x10, 0x69, 0x27, 0x46, 0x66, 0x66, 0x64])
            # We can rewrite specific bytes in block using this method. None means "don't change this byte"
            # Note that this won't do authorization, because we've already called do_auth for block 9
            util.rewrite(9, [None, None, 0xAB, 0xCD, 0xEF])
            # This will write S2B1: [0x01, 0x23, 0xAB, 0xCD, 0xEF, 0x98, 0x76......] because we've rewritten third, fourth and fifth byte
            util.read_out(9)
            # Let's see what do we have in whole tag
            util.dump()
            # We must stop crypto
            util.deauth()

These questions are similar, but haven't got me a working solution as yet.

I have access to PiZero 1.3 and PiZero W if that makes any difference? Any help greatly appreciated.

  • 'But when I run my RFIDUtil.py (from ondryaso GitHub), ': I can't find that file. How do you instantiate the rfid class? – Dirk Aug 02 '18 at 20:16
  • There is no file in the git repo, but the re are two example code snippets in the readme. I just copied the second one and named it rfid-util.py have a look under “Usage” on this page https://github.com/ondryaso/pi-rc522 – Alexander Holsgrove Aug 02 '18 at 20:39
  • For a meaningful answer we need to see your code. If you wrote the code correctly you would not have a problem,,, – Dirk Aug 02 '18 at 20:42
  • The code is identical as I am just trying to get the basics working before writing my own implementation. I have updated the question. Thanks – Alexander Holsgrove Aug 02 '18 at 20:46
  • 1
    That documentation is rubbish. It does not even show the RFID class parameters. If you use e.g. spi1.0 you have to add these parameters to rdr = RFID(). Full call could be(bus=0, device=0, speed=1000000, pin_rst=def_pin_rst, pin_ce=0, pin_irq=def_pin_irq, pin_mode = def_pin_mode) – Dirk Aug 02 '18 at 21:04
  • Thank you, I did just see the def __init__ with that list of parameters. I’m a bit out of my comfort zone here, but I assume device is the SPI setting? How do I know which to use for RFID and which is for the TFT? Do I need to adjust my wiring at all? I can post a diagram if it helps, but again I just followed the connections on the github page. – Alexander Holsgrove Aug 02 '18 at 21:26
  • The LCD uses SPI0.0 and the connector covers the first 26 pins. I would use SPI1.0 for the RFID. Also make sure that you select 'uncovered' GPIO for the extra signals (rst, irq and ce0) – Dirk Aug 03 '18 at 15:45
  • Thanks @Dirk I have managed to get everything working thanks to you. I posted an answer, but have one final question about SPI if you could please help? Setting dtoverlay=spi1-1cs gives me spidev0.1 and spidev1.0. – Alexander Holsgrove Aug 03 '18 at 22:15

1 Answers1

2

Using the answers from Dirk, I have got both my 2.8" TFT and RFID-RC522 working.

I have set dtoverlay=spi1-1cs in /boot/config.txt giving me:

$ ls /dev/spi*
/dev/spidev0.0
/dev/spidev0.1

I connected the TFT as normal and then assigned the RFID pins as follows:

RFID    Pi Pin  Pin Name
SDA     37      GPIO26 (dtparam sets this as CE0)
SCK     40      SPI1 SCLK
MOSI    38      SPI1 MOSI
MISO    35      SPI1 MISO
IRQ     29      GPIO5
GND     39      Ground
RST     31      GPIO6
3.3V    17      VDD_3V3

I then called the RFID class

def __init__(self, bus=0, device=0, speed=1000000, pin_rst=def_pin_rst,
            pin_ce=0, pin_irq=def_pin_irq, pin_mode = def_pin_mode):

Using this modified line in my code:

rdr = RFID(1, 0, 1000000, 31, 37, 29)

This now shows the TFT screen and lets me use the RFID.

  • 1
    dtparam=spi=on gives spidev0.0 and 0.1. 0.0 is used by the screen and 0.1 is still free. spi1-1cs creates spi1.0. Use it for RFID (bus=1, device=0). Default ce0 is GPIO18 (pin 12). To avoid first 26 use e.g. GPIO26 (pin 37). – Dirk Aug 03 '18 at 22:47
  • 1
    Use 'dtparam=spi1-1cs,cs0-pin=26' and use pin_ce=37 in call to RFID – Dirk Aug 03 '18 at 22:54
  • Just tested this and it works a treat. I've updated my answer to reflect your comments. Thanks again – Alexander Holsgrove Aug 04 '18 at 11:43
  • Please accept your own answer with a click on the tick on its left side. Only this will finish the question and it will not pop up again year for year. – Ingo Feb 11 '20 at 11:49