atitan / MFRC522_Ruby

MFRC522 RFID Reader Library for Raspberry Pi
MIT License
17 stars 2 forks source link

MFRC522.new blocks when used on Gtk3 #4

Closed xetum closed 5 years ago

xetum commented 5 years ago

I'm trying to use mfrc522 with gtk3 on an RPi 3B+ with raspbian stretch. Code is

require 'gtk3'
require 'mfrc522'

win = Gtk::Window.new
win.window_position = :center
win.border_width = 5

win.signal_connect("destroy") { Gtk.main_quit }

rfid = MFRC522.new

win.show_all
Gtk.main

If I comment rfid = MFRC522.new gtk window is displayed. If I uncomment, execution blocks on rfid = MFRC522.new

mfrc522 code on console (without gtk) to read uid works well

atitan commented 5 years ago

Sorry, I am not familiar with GUI.

I think it's related to pi_piper, the underlying GPIO library.

I would suggest that you comment out everything in the initializer, and then uncomment one by one to see which one is blocking.

  def initialize(nrstpd = 24, chip = 0, spd = 4000000, timer = 256)
    chip_option = { 0 => PiPiper::Spi::CHIP_SELECT_0,
                    1 => PiPiper::Spi::CHIP_SELECT_1,
                    2 => PiPiper::Spi::CHIP_SELECT_BOTH,
                    3 => PiPiper::Spi::CHIP_SELECT_NONE }
    @spi_chip = chip_option[chip]
    @spi_spd = spd
    @timer = timer

    # Power it up
    nrstpd_pin = PiPiper::Pin.new(pin: nrstpd, direction: :out)
    nrstpd_pin.on
    sleep 1.0 / 20.0 # Wait 50ms

    soft_reset # Perform software reset

    pcd_config_reset # Set default setting

    antenna_on # Turn antenna on. They were disabled by the reset.
  end
xetum commented 5 years ago

It's a bit weird. I'd an old RPi3 B (Raspbian Jessie) with mfrc522-2.0.0 and it works well! It is also using pi_piper-2.0.0,

To resume;

RPi3 B+ raspbian stretch: mfrc522-2.0.0 pi_piper-2.0.0 it blocks RPi3 B raspbian jessie: mfrc522-2.0.0 pi_piper-2.0.0 works well

Any ideas?

atitan commented 5 years ago

How about making them both stretch or jessie?

That could be differences in the OS.

running jessie on 3B+: https://raspberrypi.stackexchange.com/questions/81725/raspbian-8-on-raspberry-pi-3-b

xetum commented 5 years ago

Yes, it's pi_piper's fault. I've reported it to jwhitehorn/pi_piper. In the meanwhile I've mocked mfrc522 so as to call a mfrc522 requests server. In this way it works

atitan commented 5 years ago

Replacing pi_piper with other GPIO library is also an option.

It is easy to do so because only 3 methods, (initialize, read_spi , write_spi), invoke GPIO/SPI operation.

xetum commented 5 years ago

[NOTE: this issue has been solved, please look at the end]

ok, I was doing some testing... Program is three lines:

require 'gtk3' require 'pi_piper/bcm2835' PiPiper::Bcm2835.init

program versions:

ruby 2.3.3p222 (2016-11-21) [arm-linux-gnueabihf] gtk3-3.3.0 gem pi_piper-2.0.0 gem (with libbcm2835 1.49 library, see https://github.com/zsyed91/pi_piper)

hardware:

RPi 3B+ Raspbian Stretch

comments:

-This same program works well on RPi 3B (no plus) Raspbian Jessie

if I comment require 'gtk3' it works
if I comment PiPiper::Bcm2835.init it works

segfault log:

test_gtk.rb:15: [BUG] Segmentation fault at 0xe92d000e ruby 2.3.3p222 (2016-11-21) [arm-linux-gnueabihf]

-- Control frame information ----------------------------------------------- c:0003 p:---- s:0007 e:000006 CFUNC :init c:0002 p:0032 s:0004 E:001c70 EVAL test_gtk.rb:15 [FINISH] c:0001 p:0000 s:0002 E:001420 (none) [FINISH]

-- Ruby level backtrace information ---------------------------------------- test_gtk.rb:15:in

' test_gtk.rb:15:in init'

-- Other runtime information -----------------------------------------------

Loaded script: test_gtk.rb

Loaded features:

0 enumerator.so
1 thread.rb
2 rational.so
3 complex.so
4 /usr/lib/arm-linux-gnueabihf/ruby/2.3.0/enc/encdb.so
5 /usr/lib/arm-linux-gnueabihf/ruby/2.3.0/enc/trans/transdb.so
6 /usr/lib/ruby/2.3.0/unicode_normalize.rb
7 /usr/lib/arm-linux-gnueabihf/ruby/2.3.0/rbconfig.rb
...
143 /var/lib/gems/2.3.0/gems/ffi-1.9.25/lib/ffi/callback.rb
144 /var/lib/gems/2.3.0/gems/ffi-1.9.25/lib/ffi/io.rb
145 /var/lib/gems/2.3.0/gems/ffi-1.9.25/lib/ffi/autopointer.rb
146 /var/lib/gems/2.3.0/gems/ffi-1.9.25/lib/ffi/variadic.rb
147 /var/lib/gems/2.3.0/gems/ffi-1.9.25/lib/ffi/enum.rb
148 /var/lib/gems/2.3.0/gems/ffi-1.9.25/lib/ffi/ffi.rb
149 /var/lib/gems/2.3.0/gems/ffi-1.9.25/lib/ffi.rb
150 /var/lib/gems/2.3.0/gems/pi_piper-2.0.0/lib/pi_piper/bcm2835.rb

Process memory map:

00010000-00011000 r-xp 00000000 b3:02 147846 /usr/bin/ruby2.3 00020000-00021000 r--p 00000000 b3:02 147846 /usr/bin/ruby2.3 00021000-00022000 rw-p 00001000 b3:02 147846 /usr/bin/ruby2.3 01303000-01c01000 rw-p 00000000 00:00 0 [heap] 74fa1000-74fbe000 r-xp 00000000 b3:02 265396 /var/lib/gems/2.3.0/gems/ffi-1.9.25/lib/ffi_c.so ... 76f1b000-76f1c000 rw-p 00021000 b3:02 395 /lib/arm-linux-gnueabihf/ld-2.24.so 7e5c0000-7edbf000 rw-p 00000000 00:00 0 [stack] 7ef03000-7ef04000 r-xp 00000000 00:00 0 [sigpage] 7ef04000-7ef05000 r--p 00000000 00:00 0 [vvar] 7ef05000-7ef06000 r-xp 00000000 00:00 0 [vdso] ffff0000-ffff1000 r-xp 00000000 00:00 0 [vectors]

[NOTE] You may have encountered a bug in the Ruby interpreter or extension libraries. Bug reports are welcome. For details: http://www.ruby-lang.org/bugreport.html

[SOLVED] In the end I've found the culprit...: neither gtk3, nor pi_piper..., but ruby interpreter ruby 2.3.3p222 (2016-11-21) [arm-linux-gnueabihf]

This is unfortunate because this ruby version is the one which gets installed with sudo apt-get install ruby on RPi Raspbian Stretch. Notice that Raspbian Jessie installs ruby 2.1 and the bug doesn't appear

Conclusion: using rvm with ruby 2.5 should be strongly recommended. You can close the ticket. Thanks for your attention

atitan commented 5 years ago

The last time I tested this gem, I was using ruby 2.3 that comes from Raspbian Stretch on RPi 3(no plus).

I thought it was Raspbian Stretch 2017-11-29.

xetum commented 5 years ago

Which one? gtk3, pi_piper? It's the combination of gtk3 and PiPiper::Bcm2835.init on ruby 2.3.3p222 (2016-11-21) [arm-linux-gnueabihf] which makes the bug arise (and the culprit is the ruby interpreter). It works perfectly well on ruby 2.5

atitan commented 5 years ago

OK, I forgot I only used terminal to test it. It's not related to this issue.

I'll add a notice for those who want to use with GUI in the readme file.

xetum commented 5 years ago

reported as a bug: https://bugs.launchpad.net/raspbian/+bug/1803538