0

I'm trying to use two SPI busses on Raspberry Pi 0 W. I found some hints around but I really cannot understand how to use it and if there are restrictions on it. On this thread it seems that the second SPI bus works and that it just need to put a couple of lines on /boot/config.txt to enable it:

dtparam=spi=on
dtoverlay=spi1-3cs

I tried it but as long as I can see nothing works. I have to use the first SPI bus to use an enc28j60 based board and the second to manage another device (by code trough /dev/spidev1.x device). Unluckly /dev/spidev1.x seems to be unreadable i.e. I can see it inside /dev/ folder but in my code I'm unable to perform any reading/writing on it ... the code just fails after open() call. Of course if I connect the second device on /dev/spidev0.0 (or /dev/spidev0.1) it works fine (but I'm unable to map enc28J60 board to use the second bus). BTW I'm using Raspbian Stretch. The question is: I'm doing It right? How can I check if the 2nd SPI bus is working besides trying tour access the second device?

4
  • What is your question? Two SPI buses are available (main bus with default 2 chip selects, aux bus with default 3 chip selects) and both work.
    – joan
    May 29, 2019 at 17:01
  • I just need the two lines mentioned above to enable and make BOTH buses working at the same time? Is there any restriction on the second bus (max bus clock rate etc) ?
    – weirdgyn
    May 29, 2019 at 17:22
  • Yes. 1) enable the bus. 2) tell your software to use that bus. You really need to clarify your actual question.
    – joan
    May 29, 2019 at 17:41
  • @weirdgyn Well, it is likely that you have not correctly set up the SPI second channel, or the second SPI channel does not work for you version of Rpi hardware and software (driver/kernel module, device tree overlays etc). I would recommend to first do the SPI loopback test for both the first and second channels - How to check if SPI is enabled? raspberrypi.stackexchange.com/questions/97869/… Why is SPI not working? raspberrypi.stackexchange.com/questions/96225/…
    – tlfong01
    May 30, 2019 at 4:33

1 Answer 1

1

Question

Trying to use two SPI busses on RpiZW

Put a line on /boot/config.txt to enable it "dtoverlay=spi1-3cs"

Nothing works

Answer

I googled and found that it is as easy as you say, add one line in /boot/config.txt. - "dtoverlay=spi1-3cs"

However I know that the dtoverlay thing works for one Rpi model and raspbian version may not work for another Rpi model and another raspbian version.

In other words, my Rpi3B+ stretch 9 can do 2 SPIs does not mean your RpiZW can also do.

Anyway, the above works for my Rpi3B+ stretch 9. I have used the following loopback test and found both SPI channels can loop back. The config.txt changes, "$ ls -l /dev/spi*" output and loopback sample outputs are also included in the program listing.

I also attach the SPI 2 pinout for your reference.

# spi_loopback_onebyte_v44_2019may3001 tlfong01 2019may30hkt1958 ***

# Rpi3B+ stretch 2019apr08, IDLE python 3.5.3 

# Test     - loopBackTest - SPI port send and receive one byte/two bytes/three bytes.
# Function - Send one byte/two bytes/three bytes to MOSI and read it back from MISO. 
# Setup    - Connet MOSI pin to MISO pin to form a loop.

# /boot/config.txt dtoverlay setting 
# dtparam=i2c_arm=on
# dtparam=spi=on
# dtoverlay=spi1-3cs

# /dev/spi listing
# ls -l /dev/spi*
# should show the following:
# 1. /dev/spidev0.0
# 2. /dev/spidev0.1
# 3. /dev/spidev1.0
# 4. /dev/spidev1.1
# 5. /dev/spidev1.2

from   time import sleep
import spidev

# *** SPI Channel 0 Config ***

spiPort00 = spidev.SpiDev()
spiPort00.open(0,0)
spiPort00.max_speed_hz = 100000

spiPort01 = spidev.SpiDev()
spiPort01.open(0,1)
spiPort01.max_speed_hz = 100000

# *** SPI Channel 1 Config ***

spiPort10 = spidev.SpiDev()
spiPort10.open(1,0)
spiPort10.max_speed_hz = 100000

spiPort11 = spidev.SpiDev()
spiPort11.open(1,1)
spiPort11.max_speed_hz = 100000

spiPort12 = spidev.SpiDev()
spiPort12.open(1,2)
spiPort12.max_speed_hz = 100000

# *** SPI Send/Receive OneByte/TwoBytes/ThreeBytes ***

def spiSendRecvOneByte(spiPort, sendByte):
    sendByteArray = [sendByte]
    recvByteArray = spiPort.xfer(sendByteArray)    
    return recvByteArray

# *** Loopback OneByte/TwoBytes/ThreeBytes ***

def loopbackOneByte(spiPort, sendByte):
    recvByteArray     = spiSendRecvOneByte(spiPort, sendByte)
    recvByte          = recvByteArray[0]
    print('\n  Begin loopbackOneByte(),....')
    #print('')
    print('      sendByte  = ', hex(sendByte))
    print('      recvByte  = ', hex(recvByte))
    #print('')
    print('  End   loopbackOneByte(),....', end = '')
    return

# *** Tests to loopback SPI Channel 0  ***

def testLoopbackOneByteSpiPort0():
    print('\nBegin loopbackOneByteSpiPort0(),....', end = '')
    loopbackOneByte(spiPort00, 0x5b)
    print('\nEnd   loopbackOneByteSpiPort0(),....', end = '')
    return

# *** Tests to loopback SPI Channel 1 ***

def testLoopbackOneByteSpiPort1():
    print('\nBegin   loopbackOneByteSpiPort1(),....', end = '')
    loopbackOneByte(spiPort10, 0x5b)
    print('\nEnd   loopbackOneByteSpiPort1(),....', end = '')
    return

# *** Main Tests ***

# *** Loopback Tests ***

#testLoopbackOneByteSpiPort0()
testLoopbackOneByteSpiPort1()

# *** Sample Output ***

''' Smple output 
Begin loopbackOneByteSpiPort0(),....
  Begin loopbackOneByte(),....
      sendByte  =  0x5b
      recvByte  =  0x5b
  End   loopbackOneByte(),....
End   loopbackOneByteSpiPort0(),....
'''
''' Smple output 
Begin   loopbackOneByteSpiPort1(),....
  Begin loopbackOneByte(),....
      sendByte  =  0x5b
      recvByte  =  0x5b
  End   loopbackOneByte(),....
End   loopbackOneByteSpiPort1(),....
'''

# *** End ***

spi pinout

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.