Skip to content

Commit c5acf96

Browse files
authored
Merge pull request ondryaso#34 from cvtsi2sd/beaglebone
Added support for Beaglebone Black
2 parents 3383e5e + 09acbb7 commit c5acf96

File tree

2 files changed

+47
-19
lines changed

2 files changed

+47
-19
lines changed

README.md

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Python RC522 library
2-
pi-rc522 consists of two Python classes for controlling an SPI RFID module "RC522" using Raspberry Pi. You can get this module on AliExpress or Ebay for $3.
2+
pi-rc522 consists of two Python classes for controlling an SPI RFID module "RC522" using Raspberry Pi or Beaglebone Black. You can get this module on AliExpress or Ebay for $3.
33

44
Based on [MFRC522-python](https://github.com/mxgxw/MFRC522-python/blob/master/README.md).
55

@@ -14,7 +14,7 @@ git clone https://github.com/ondryaso/pi-rc522.git
1414
cd pi-rc522
1515
python setup.py install
1616
```
17-
You'll also need to install the [**spidev**](https://pypi.python.org/pypi/spidev) and [**RPi.GPIO**](https://pypi.python.org/pypi/RPi.GPIO) libraries.
17+
You'll also need to install the [**spidev**](https://pypi.python.org/pypi/spidev) and [**RPi.GPIO**](https://pypi.python.org/pypi/RPi.GPIO) libraries on Raspberry PI, and [**Adafruit_BBIO**](https://github.com/adafruit/adafruit-beaglebone-io-python) on Beaglebone Black (which should be installed by default).
1818

1919
[MIFARE datasheet](http://www.nxp.com/documents/data_sheet/MF1S503x.pdf) can be useful.
2020

@@ -24,23 +24,27 @@ Classic 1K MIFARE tag has **16 sectors**, each contains **4 blocks**. Each block
2424
## Connecting
2525
Connecting RC522 module to SPI is pretty easy. You can use [this neat website](http://pi.gadgetoid.com/pinout) for reference.
2626

27-
| Board pin name | Board pin | Physical RPi pin | RPi pin name |
28-
|----------------|-----------|------------------|--------------|
29-
| SDA | 1 | 24 | GPIO8, CE0 |
30-
| SCK | 2 | 23 | GPIO11, SCKL |
31-
| MOSI | 3 | 19 | GPIO10, MOSI |
32-
| MISO | 4 | 21 | GPIO9, MISO |
33-
| IRQ | 5 | 18 | GPIO24 |
34-
| GND | 6 | 6, 9, 20, 25 | Ground |
35-
| RST | 7 | 22 | GPIO25 |
36-
| 3.3V | 8 | 1,17 | 3V3 |
27+
| Board pin name | Board pin | Physical RPi pin | RPi pin name | Beaglebone Black pin name |
28+
|----------------|-----------|------------------|--------------| --------------------------|
29+
| SDA | 1 | 24 | GPIO8, CE0 | P9\_17, SPI0\_CS0 |
30+
| SCK | 2 | 23 | GPIO11, SCKL | P9\_22, SPI0\_SCLK |
31+
| MOSI | 3 | 19 | GPIO10, MOSI | P9\_18, SPI0\_D1 |
32+
| MISO | 4 | 21 | GPIO9, MISO | P9\_21, SPI0\_D0 |
33+
| IRQ | 5 | 18 | GPIO24 | P9\_15, GPIO\_48 |
34+
| GND | 6 | 6, 9, 20, 25 | Ground | Ground |
35+
| RST | 7 | 22 | GPIO25 | P9\_23, GPIO\_49 |
36+
| 3.3V | 8 | 1,17 | 3V3 | VDD\_3V3 |
3737

3838
You can also connect the SDA pin to CE1 (GPIO7, pin #26) and call the RFID constructor with *bus=0, device=1*
3939
and you can connect RST pin to any other free GPIO pin and call the constructor with *pin_rst=__BOARD numbering pin__*.
4040
Furthermore, the IRQ pin is configurable by passing *pin_irq=__BOARD numbering pin__*.
4141

4242
__NOTE:__ For RPi A+/B+/2/3 with 40 pin connector, SPI1/2 is available on top of SPI0. Kernel 4.4.x or higher and *dtoverlay* configuration is required. For SPI1/2, *pin_ce=__BOARD numbering pin__* is required.
4343

44+
__NOTE:__ On Beaglebone Black, use pin names (e.g. `"P9_17"`).
45+
46+
__NOTE:__ On Beaglebone Black, generally you have to enable the SPI for the spidev device to show up; you can enable SPI0 by doing `echo BB-SPIDEV0 > /sys/devices/bone_capemgr.9/slots`. SPI1 is available *only if you disable HDMI*.
47+
4448
You may change BOARD pinout to BCM py passing *pin_mode=RPi.GPIO.BCM*. Please note, that you then have to define all pins (irq+rst, ce if neccessary). Otherwise they would default to perhaps wrong pins (rst to pin 15/GPIO22, irq to pin 12/GPIO18).
4549

4650
## Usage

pirc522/rfid.py

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
11
import threading
2-
import spidev
3-
import RPi.GPIO as GPIO
2+
3+
RASPBERRY = object()
4+
BEAGLEBONE = object()
5+
board = RASPBERRY
6+
try:
7+
# Try with Raspberry PI imports first
8+
import spidev
9+
import RPi.GPIO as GPIO
10+
SPIClass = spidev.SpiDev
11+
def_pin_rst = 22
12+
def_pin_irq = 18
13+
def_pin_mode = GPIO.BOARD
14+
except ImportError:
15+
# If they failed, try with Beaglebone
16+
import Adafruit_BBIO.SPI as SPI
17+
import Adafruit_BBIO.GPIO as GPIO
18+
SPIClass = SPI.SPI
19+
board = BEAGLEBONE
20+
def_pin_rst = "P9_23"
21+
def_pin_irq = "P9_15"
22+
def_pin_mode = None
423

524
class RFID(object):
625
pin_rst = 22
@@ -39,17 +58,22 @@ class RFID(object):
3958
authed = False
4059
irq = threading.Event()
4160

42-
def __init__(self, bus=0, device=0, speed=1000000, pin_rst=22,
43-
pin_ce=0, pin_irq=18, pin_mode=GPIO.BOARD):
61+
def __init__(self, bus=0, device=0, speed=1000000, pin_rst=def_pin_rst,
62+
pin_ce=0, pin_irq=def_pin_irq, pin_mode = def_pin_mode):
4463
self.pin_rst = pin_rst
4564
self.pin_ce = pin_ce
4665
self.pin_irq = pin_irq
4766

48-
self.spi = spidev.SpiDev()
67+
self.spi = SPIClass()
4968
self.spi.open(bus, device)
50-
self.spi.max_speed_hz = speed
69+
if board == RASPBERRY:
70+
self.spi.max_speed_hz = speed
71+
else:
72+
self.spi.mode = 0
73+
self.spi.msh = speed
5174

52-
GPIO.setmode(pin_mode)
75+
if pin_mode is not None:
76+
GPIO.setmode(pin_mode)
5377
if pin_rst != 0:
5478
GPIO.setup(pin_rst, GPIO.OUT)
5579
GPIO.output(pin_rst, 1)

0 commit comments

Comments
 (0)