0

Using two SPI devices
Moderators: adafruit_support_bill, adafruit

Forum rules
Talk about Adafruit Raspberry Pi® accessories! Please do not ask for Linux support, this is for Adafruit products only! For Raspberry Pi help please visit: http://www.raspberrypi.org/phpBB3/
Please be positive and constructive with your questions and comments.

Using two SPI devices

by raspadafruit on Fri Oct 19, 2018 5:34 am

Hi !

I'm a begginer with SPI, and I would like to use an "adafruit lsm9ds1 accelerometer plus gyro plus magnetometer 9 dof breakout" and an "Adafruit RFM9X LoRa Packet Radio Breakout" on a Raspberry Pi 3.

I did all the librairies install and managed to make each of them separatly by using the "normal pinout" : 19-21-23 (https://pinout.xyz/pinout/spi). But when I try to use the 35-38-40 at the same time, it doesn't work.

Here is my code :

Code: Select all | TOGGLE FULL SIZE
CS = digitalio.DigitalInOut(board.D24)
RESET = digitalio.DigitalInOut(board.D17)

spi = busio.SPI(board.SCK,board.MOSI,board.MISO)
spi2 = busio.SPI(board.D21,board.D20,board.D19)

csag = DigitalInOut(board.D16)
csag.direction = Direction.OUTPUT
csag.value = True

csm = DigitalInOut(board.D26)
csm.direction = Direction.OUTPUT
csm.value = True

sensor = adafruit_lsm9ds1.LSM9DS1_SPI(spi2,csag,cm)
rfm9x = adafruit_rfm9x.RFM9x(spi,CS,RESET,915)



Wirings : (https://pinout.xyz/pinout/spi)

LoRa board :
Vin - 17
GND - 25
SCK - 23
MISO - 19
MOSI - 21
CS - 18
Reset - 11

lsm9ds1 board :
Vin - 1
GND - 9
SCL (replacing SCK) - 40
SDOAG (replacing MISO) - 35
SDA (replacing MOSI) - 38
CSM - 37
CSAG - 36

I have the following error :

Code: Select all | TOGGLE FULL SIZE
Could not open SPI device - check if SPI is enabled in kernel!
Traceback (most recent call last):
  File "main_capteur.py", line 217, in <module>
    sensor = adafruit_lsm9ds1.LSM9DS1_SPI(spi2,csag,csm)
  File "/home/pi/.local/lib/python3.5/site-packages/adafruit_lsm9ds1.py", line 411, in __init__
    super().__init__()
  File "/home/pi/.local/lib/python3.5/site-packages/adafruit_lsm9ds1.py", line 158, in __init__
    self._write_u8(_XGTYPE, _LSM9DS1_REGISTER_CTRL_REG8, 0x05)
  File "/home/pi/.local/lib/python3.5/site-packages/adafruit_lsm9ds1.py", line 442, in _write_u8
    spi.write(self._BUFFER, end=2)
  File "/home/pi/.local/lib/python3.5/site-packages/busio.py", line 118, in write
    return self._spi.write(buf, start, end)
  File "/home/pi/.local/lib/python3.5/site-packages/adafruit_blinka/microcontroller/raspi_23/spi.py", line 39, in write
    self._spi.open(self._port, 0)
FileNotFoundError: [Errno 2] No such file or directory


Am I doing this correctly ? Am I using the correct pinout ?

raspadafruit
 
Posts: 27
Joined: Mon Aug 13, 2018 6:00 am

Re: Using two SPI devices

by adafruit_support_mike on Sat Oct 20, 2018 12:33 am

You can use the same MISO, MOSI, and SCK pins for both boards. SPI is designed to let multiple devices share those signals.

You need separate CS pins though. The RasPi uses those to select the device it wants to talk to. The best choices will be the CE0 and CE1 pins (GPIO 24 and 26), since those are identified as CS signals by the Raspbian SPI code:

https://pinout.xyz

adafruit_support_mike
 
Posts: 53554
Joined: Thu Feb 11, 2010 2:51 pm

Re: Using two SPI devices

by raspadafruit on Sat Oct 20, 2018 3:30 am

Hi,

Thanks for your reply, so it means that I've to use a breadboard to share miso mosi and sck while using different cs ?

I'll try it and get back to you !

Thanks !

raspadafruit
 
Posts: 27
Joined: Mon Aug 13, 2018 6:00 am

Re: Using two SPI devices

by raspadafruit on Mon Oct 29, 2018 8:02 am

Hi,

I tried using a breadboard to share the SCK, MOSI and MISO. However for the CS, I want to use D26 for example but in the code I have to give a csag and a csm. I don't know what they represent.

I tried to use D26 for csm and D16 for csag but i have the following error : "RuntimeError: Could not find LSM9DS1, check wiring!"

Can someone explain what are csag and csm and what's wrong with what I did ?

Thanks

raspadafruit
 
Posts: 27
Joined: Mon Aug 13, 2018 6:00 am

Re: Using two SPI devices

by adafruit_support_mike on Thu Nov 01, 2018 1:53 am

From the code in your first post, it looks like 'csag' and 'csm' are just variable names for the objects that talk to the SPI devices.

What kind of device are you using?

adafruit_support_mike
 
Posts: 53554
Joined: Thu Feb 11, 2010 2:51 pm

Re: Using two SPI devices

by jerryn on Thu Nov 01, 2018 2:18 am

Just a heads up. If you are using “blinka” on. Raspberry Pi you cannot use CE0 or CE1 as the SPI chip select. Use another GPIO pin. See. https://learn.adafruit.com/circuitpython-on-raspberrypi-linux/spi-sensors-devices

jerryn
 
Posts: 781
Joined: Sat Sep 14, 2013 9:05 am

Re: Using two SPI devices

by raspadafruit on Thu Nov 01, 2018 4:31 am

Ok, I will read that, thanks !

raspadafruit
 
Posts: 27
Joined: Mon Aug 13, 2018 6:00 am

Re: Using two SPI devices

by raspadafruit on Tue Nov 13, 2018 10:10 am

Hi,

I'm getting back to you (I know it has been a bit long but I could't work on that sooner) because I'm still having trouble ! :(
I tried two methods and got two different result both not correct.
Just to remember, I'm trying to use an "Adafruit RFM9X LoRa Packet Radio Breakout" and an "Adafruit lsm9ds1 accelerometer plus gyro plus magnetometer 9 dof breakout" simultaneously on a Raspberry pi 3.

So I tried the link you gave me : https://learn.adafruit.com/circuitpython-on-raspberrypi-linux/spi-sensors-devices
As it's written I tried to use the "second" SPI Port. I edited the /boot/config.txt file and I changed my code to :

Code: Select all | TOGGLE FULL SIZE
CS    = digitalio.DigitalInOut(board.D24)
RESET = digitalio.DigitalInOut(board.D23)

spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
spi2 = busio.SPI(board.SCK_1, MOSI=board.MOSI_1, MISO=board.MISO_1)

csag = DigitalInOut(board.D16)
csag.direction = Direction.OUTPUT
csag.value = True
csm = DigitalInOut(board.D17)
csm.direction = Direction.OUTPUT
csm.value = True
sensor = adafruit_lsm9ds1.LSM9DS1_SPI(spi2,csag,csm)
rfm9x = adafruit_rfm9x.RFM9x(spi, CS, RESET, RADIO_FREQ_MHZ)


My wirings : What I call physical is the number of the pin from 1 to 40 (https://pinout.xyz/pinout)

LoRa board :
Vin physical 17(shared with a breadboard) - GND physical 25 - SCK physical 23 - MISO physical 21 - MOSI physical 19 - CS physical 18 - Reset physical 16

Lsm9ds1 Board :
Vin physical 17 (shared with a breadboard) - GND physical 9 - SCL (replacing SCK) physical 40 - SDA (replacing MOSI) physical 38 -SDOAG and SDOM (replacing MISO and both on the same pin thanks to a breadboard) physical 35 - CSM physical 11 - CSAG physical 36

I rebooted and run the code, but I have the following error :
Code: Select all | TOGGLE FULL SIZE
Traceback (most recent call last):
  File "main_capteur.py", line 39, in <module>
    sensor = adafruit_lsm9ds1.LSM9DS1_SPI(spi2,csag,csm)
  File "/home/pi/.local/lib/python3.5/site-packages/adafruit_lsm9ds1.py", line 411, in __init__
    super().__init__()
  File "/home/pi/.local/lib/python3.5/site-packages/adafruit_lsm9ds1.py", line 158, in __init__
    self._write_u8(_XGTYPE, _LSM9DS1_REGISTER_CTRL_REG8, 0x05)
  File "/home/pi/.local/lib/python3.5/site-packages/adafruit_lsm9ds1.py", line 442, in _write_u8
    spi.write(self._BUFFER, end=2)
  File "/home/pi/.local/lib/python3.5/site-packages/busio.py", line 118, in write
    return self._spi.write(buf, start, end)
  File "/home/pi/.local/lib/python3.5/site-packages/adafruit_blinka/microcontroller/raspi_23/spi.py", line 47, in write
    self._spi.writebytes([x for x in buf[start:end]])
  File "/home/pi/.local/lib/python3.5/site-packages/adafruit_blinka/microcontroller/raspi_23/spi.py", line 47, in <listcomp>
    self._spi.writebytes([x for x in buf[start:end]])
OSError: [Errno 22] Invalid argument


So I tried another way which didn't work either :

I tried to use the same MISO, MOSI, SCK and change only the CS as suggested, so for the code part I have :
Code: Select all | TOGGLE FULL SIZE
CS    = digitalio.DigitalInOut(board.D24)
RESET = digitalio.DigitalInOut(board.D23)

spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
spi2 = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
csag = DigitalInOut(board.D16)
csag.direction = Direction.OUTPUT
csag.value = True
csm = DigitalInOut(board.D17)
csm.direction = Direction.OUTPUT
csm.value = True
sensor = adafruit_lsm9ds1.LSM9DS1_SPI(spi2,csag,csm)
rfm9x = adafruit_rfm9x.RFM9x(spi, CS, RESET, RADIO_FREQ_MHZ)


And for the wiring :

LoRa board :
Vin physical 17(shared with a breadboard) - GND physical 25 - SCK physical 23 ( shared) - MISO physical 21 ( shared) -- MOSI physical 19 ( shared) -- CS physical 18 - Reset physical 16

Lsm9ds1 Board :
Vin physical 17 (shared with a breadboard) - GND physical 9 - SCL (replacing SCK - shared with a breadboard) physical 23 - SDA (replacing MOSI) physical 19 -SDOAG and SDOM (replacing MISO and both on the same pin thanks to a breadboard) physical 21 - CSM physical 37 - CSAG physical 36

For this solution, I didn't have any error HOWEVER values returned by the lsm9ds1 were completed wrong (knowing that i managed to have correct value with i2c) when the rfm9x was used, but correct as soon as the rfm9x.receive() method was descativated. Seems to be an interference between the two spi.

Raspadafruit.

raspadafruit
 
Posts: 27
Joined: Mon Aug 13, 2018 6:00 am

Please be positive and constructive with your questions and comments.