Skip to content

SPI hardware (MFRC522) not working with Latest Jessie release (2016-05-10-raspbian-jessie) #1547

Closed

Description

@cscuilla

The rfid reader (MFRC522) does not work with the latest release of Jessie(2016-05-10). It works fine on the previous version (2016-03-18). I believe this is due to the new version of SPI.

On previous Jessie version (2016-03-18-raspbian-jessie) if I type lsmod after installing spi I get the following..
spi_bcm2708

On recent Jessie version (2016-05-10-raspbian-jessie) if I type lsmod after installing spi I get the following..
spi_bcm2835

Others, are having the same issue as described on the raspberry pi forum at this link..
https://www.raspberrypi.org/forums/viewtopic.php?f=107&t=148890

Activity

  1. pelwell commented on Jun 28, 2016

    @pelwell
    Contributor

    Can you describe the configuration of your system - any relevant entries in config.txt, cmdline,txt, /etc/modules or /etc/modprobe.d/*.conf?

    Also, can you explain the failure mode - what do you expect to happen, and what actually happens?

  2. cscuilla commented on Jun 28, 2016

    @cscuilla
    Author

    To get it working on previous Jessie Release (2016-03-18) I have to add the following to /boot/config.txt

    device_tree_param=spi=on
    dtoverlay=spi-bcm2708
    This does NOT work on latest Jessie release. I've also tried
    dtoverlay=spi-bcm7835

    The failure mode is as follows.. Once I run the script it appears to work as it is waiting for a card to be scanned, however when I scan a card, nothing is output.

    On previous Jessie(where card reader works), when I run the script and scan a card, the UID of the card is printed.

  3. pelwell commented on Jun 28, 2016

    @pelwell
    Contributor

    Can you confirm that with just dtparam=spi=on in config.txt (by which I mean no other spi-related settings) and spi-dev in /etc/modules that /dev/spi0.0 and /dev/spi0.1 appear but your script fails?

  4. pelwell commented on Jun 28, 2016

    @pelwell
    Contributor

    I've just ordered one of these devices, but it probably won't arrive before next week and I can't promise much time to look at this.

  5. cscuilla commented on Jun 28, 2016

    @cscuilla
    Author

    what do you mean by this part

    and spi-dev in /etc/modules that /dev/spi0.0 and /dev/spi0.1 appear but your script fails?
    Currently, if I open /etc/modules all I have is
    i2c-dev

  6. pelwell commented on Jun 28, 2016

    @pelwell
    Contributor

    Sorry, that's correct. SPI and I2C are strangely different in that respect the layered, user-space interface to I2C (i2c-dev) isn't loaded by Device Tree because it isn't hardware-specific, but the equivalent module for SPI (spidev) is.

    So, to be more accurate: can you confirm that with just dtparam=spi=on in config.txt (by which I mean no other spi-related settings) that /dev/spi0.0 and /dev/spi0.1 appear but your script fails?

  7. cscuilla commented on Jun 28, 2016

    @cscuilla
    Author

    OK, thanks, I am setting up a new memory card now with the latest release so I can confirm this for you.

  8. cscuilla commented on Jun 28, 2016

    @cscuilla
    Author

    I can confirm this.. In /boot/config.txt I have the following

    dtparam=spi=on

    If I go into the /dev folder and type "ls"
    I see spidev0.0 and spidev0.1

    When I start my script, I receive no output once I scan a card.

  9. cscuilla commented on Jun 28, 2016

    @cscuilla
    Author

    Thank you for ordering the hardware. I understand your time is limited but do appreciate the help. Here are the full steps to get the reader working on previous debian OS in case it helps you to troubleshoot..

    1. add to the bottom of /boot/config.txt
      • device_tree_param=spi=on
      • dtoverlay=spi-bcm2708
    2. Install python-dev
      • sudo apt-get install python-dev
    3. Install SPI
    4. Install Sample RFID Reader python library and sample script.
    5. Run sample script.
      • sudo python ~/MFRC522-python/read.py
  10. EECSdude commented on Jun 28, 2016

    @EECSdude

    I am running Raspberry Pi 2 B on Linux 4.4.11-v7+ #888 SMP Mon May 23 20:10:33 BST 2016 armv7l GNU/Linux with exact same behavior. I am online the next hour and happy to run any tests, provide any files.

    Scope traces show the following.
    GPIO 25, the RST line is always high, I don’t see it move.
    GPIO 8 is always low, I don’t see it move, although I do see digital noise similar to SCLK at the millivolt level.
    SCLK toggles the whole time Read.py is running, as expected.
    As an experiment, I rebooted then ran a few commands to toggle GPIO25 up and down to check my scope setup. When I ran the same script with 25 set to 8, I got a runtime warning in response to GPIO.setup(8, GPIO.OUT) “this channel is already in use”.

    I would expect to get that message if the GPIO was in CE0 mode, not a GPIO output as gpio readall says it is.

  11. pelwell commented on Jun 28, 2016

    @pelwell
    Contributor

    You say that GPIO 25 is the reset line, but the code appears to use GPIO 22 - is this a non-standard numbering issue, or have you modified your version of the code?

    As mentioned in the Forum thread, the spi-bcm2835 driver uses software-controlled CE lines rather than ones driven by the hardware block, to work around a hardware glitch, which means that the driver is claiming the pin for GPIO - this explains the contention.

    I see there is an spidev Python module that uses the spidev interface - does the spi module also do this, or is it a pure user-space software implementation?

  12. EECSdude commented on Jun 28, 2016

    @EECSdude

    I have not modifed Read.py. Interesting, I'll move the wire and see what happens.

    https://github.com/mxgxw/MFRC522-python shows:

    Name Pin # Pin name
    SDA 24 GPIO8
    SCK 23 GPIO11
    MOSI 19 GPIO10
    MISO 21 GPIO9
    IRQ None None
    GND Any Any Ground
    RST 22 GPIO25
    3.3V 1 3V3

  13. pelwell commented on Jun 28, 2016

    @pelwell
    Contributor

    I can answer the second question myself now - the SPI-Py (spi) module drives the spidev interface from C code.

  14. EECSdude commented on Jun 28, 2016

    @EECSdude

    PIN 22 is GPIO25, what pin configuration is the code using? Moving the wire now.

  15. cscuilla commented on Jun 28, 2016

    @cscuilla
    Author

    For me, The python script uses "board" as the mode for rip.gpio. So the reset is on pin 22 but GPIO number 25.

    I'm not sure what u mean in regards to the spidev Python module.

  16. 459 remaining items

  17. pelwell commented on May 24, 2019

    @pelwell
    Contributor

    That's true - with the newer SPI-Py, the openSPI method returns a device handle that needs to be passed to the transfer method, so something like:

      def __init__(self,spd=1000000):
        self.dev = spi.openSPI(speed=spd,mode=0x40)
        GPIO.setmode(GPIO.BOARD)
    ...
      def Write_MFRC522(self,addr,val):
        spi.transfer(self.dev,((addr<<1)&0x7E,val))
      
      def Read_MFRC522(self,addr):
        val = spi.transfer(self.dev,(((addr<<1)&0x7E) | 0x80,0))
        return val[1]
    
  18. cscuilla commented on May 24, 2019

    @cscuilla
    Author

    Thanks again @pelwell so to clarify, are you saying to make those changes to the MFRC522.py file and it should work with latest SpI-Py library?

  19. pelwell commented on May 24, 2019

    @pelwell
    Contributor

    Unless I've made a mistake (I haven't tested that patch), yes - that should fix it. It's a shame there wasn't a backwards-compatible way to make the SPI-Py changes.

  20. ksanchez15 commented on May 24, 2019

    @ksanchez15

    Thanks guys, but even with the old version of the library it still doesn't scan my card. @pelwell I followed your instructions (#2613) but right at step 1, I don't see options named SPI in Advanced Options (in raspi-config). Therefore, when I run dtoverlay -l I get No overlays loaded

  21. cscuilla commented on May 24, 2019

    @cscuilla
    Author

    @pelwell thanks again i will test early next week.. @lesanchez try "Interface Options" under raspi-config. Enable SPI, then reboot and try again.

  22. ksanchez15 commented on May 24, 2019

    @ksanchez15

    @cscuilla I just did that but the command dtoverlay - l still returns the same thing. It should be noted that I do see the line dtparam=spi=on in my /boot/config.txt folder.
    Also when I run lsmod | grep spi I get
    spidev 20480
    spi_bcm2835 20480 0

  23. ksanchez15 commented on May 24, 2019

    @ksanchez15

    @pelwell After your fix, I see get the following warning when I run Read.py:

    /home/pi/MFRC522-python/MFRC522.py:133: RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings.

  24. pelwell commented on May 24, 2019

    @pelwell
    Contributor

    dtoverlay -l will only list overlays (and DT parameters) applied by Linux at run time. Overlays applied by the firmware become part of the base DTB, so Linux isn't aware that is what they are.

  25. cscuilla commented on May 24, 2019

    @cscuilla
    Author

    @lesanchez It's probably your wiring or soldering that is the problem then.

  26. ksanchez15 commented on May 24, 2019

    @ksanchez15

    @cscuilla @pelwell all right thank you guys

  27. pelwell commented on May 24, 2019

    @pelwell
    Contributor

    That warning is because GPIO 25 (pin 22) has been left as an output by a previous failed run. Run raspi-gpio set 25 ip and try again. Once the script is working, provided you exit cleanly with Ctrl-C you shouldn't see that warning.

  28. ksanchez15 commented on May 24, 2019

    @ksanchez15

    I have quadruple check my wiring (even asked a colleague who is an electrical engineer) and everything is good. I have checked out the old version of SPI-Py and I'm using https://github.com/mxgxw/MFRC522-python's latest version.
    Still, the tag (which was sent with the RC522 board) doesn't trigger anything. Could the issue come from somewhere else?

  29. ksanchez15 commented on May 27, 2019

    @ksanchez15

    @pelwell I tried to run this and I get this output

    00: 00 82 84 86 88 8a 8c 8e
    08: 90 92 94 96 98 9a 9c 9e
    10: a0 a2 a4 a6 a8 aa ac ae
    18: b0 b2 b4 b6 b8 ba bc be
    20: c0 c2 c4 c6 c8 ca cc ce
    28: d0 d2 d4 d6 d8 da dc de
    30: e0 e2 e4 e6 e8 ea ec ee
    38: f0 f2 f4 f6 f8 fa fc fe
    

    which tells me that, at least, the spi part of the equation is working properly. Also, every time I run the script, the values change entirely.

  30. pelwell commented on May 27, 2019

    @pelwell
    Contributor

    The values the test app is reading back are the same as those it is sending. (If you are curious, you could try deliberately connecting MOSI to MISO to see what the results are.)

    Is the CS line connected correctly? And is the device powered. As part of the diagnostic process you can disconnect each pin, one at a time (but not GND) and observe the changes in the output.

  31. cscuilla commented on Jul 26, 2019

    @cscuilla
    Author

    That's true - with the newer SPI-Py, the openSPI method returns a device handle that needs to be passed to the transfer method, so something like:

      def __init__(self,spd=1000000):
        self.dev = spi.openSPI(speed=spd,mode=0x40)
        GPIO.setmode(GPIO.BOARD)
    ...
      def Write_MFRC522(self,addr,val):
        spi.transfer(self.dev,((addr<<1)&0x7E,val))
      
      def Read_MFRC522(self,addr):
        val = spi.transfer(self.dev,(((addr<<1)&0x7E) | 0x80,0))
        return val[1]
    

    @pelwell i can confirm your patch works with latest spi library! thanks again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions