weiqing121
Posts: 11
Joined: Tue Aug 09, 2016 2:18 pm

computer module 3+, i need to use SPI1

Fri Oct 18, 2019 4:08 pm

Hi All, I am using the computer module 3+ and the Adafruit MAX31865 board for the RTD measuring temperature. I need to use the SPI1, since the SPI0 is used for the new haven display. I try to enable the SPI1 in the /boot/config.txt
dtparam=spi=on
# enable the spi1, all the three CS.
dtoverlay=spi1-3cs

Here is the GPIO configure after boot up, if I check use the raspi-gpio get
for the SPI0:
GPIO 7: level=1 fsel=1 func=OUTPUT
GPIO 8: level=1 fsel=1 func=OUTPUT
GPIO 9: level=1 fsel=4 alt=0 func=SPI0_MISO
GPIO 10: level=1 fsel=4 alt=0 func=SPI0_MOSI
GPIO 11: level=0 fsel=4 alt=0 func=SPI0_SCLK

for the SPI1:
GPIO 16: level=1 fsel=1 func=OUTPUT
GPIO 17: level=1 fsel=1 func=OUTPUT
GPIO 18: level=1 fsel=1 func=OUTPUT
GPIO 19: level=0 fsel=3 alt=4 func=SPI1_MISO
GPIO 20: level=0 fsel=3 alt=4 func=SPI1_MOSI
GPIO 21: level=0 fsel=3 alt=4 func=SPI1_SCLK

I also checked the spidev files, using the line ls /dev/spidev*, I get:
/dev/spidev0.0 /dev/spidev0.1 /dev/spidev1.0 /dev/spidev1.1 /dev/spidev1.2


Here is the python code that I am using:

import board
import busio
import digitalio
import time
from realpi import *
from adafruit_bus_device.spi_device import SPIDevice
#from lib.spidev import *
import adafruit_max31865

#spi = busio.SPI(clock = board.SCK, MOSI = board.MOSI, MISO = board.MISO)
spi = busio.SPI(clock = board.SCK_1, MOSI = board.MOSI_1, MISO = board.MISO_1)
cs = digitalio.DigitalInOut(board.D18) # Chip select of the MAX31865 board.
sensor = adafruit_max31865.MAX31865(spi, cs, wires=3)

for i in range(10):
print('temperature: ', sensor.temperature)
time.sleep(1)

It gives me the error, no matter I use GPIO 18, 17 or 16:
SystemError: <method 'writebytes2' of 'SpiDev' objects> returned a result with an error set

But it works, if I use the SPI0, by changing the line to
spi = busio.SPI(clock = board.SCK, MOSI = board.MOSI, MISO = board.MISO)
and
cs = digitalio.DigitalInOut(board.D8) # Chip select of the MAX31865 board.

Greatly appreciated if you can share any insight, or direct me to some one or some source. Thank you!!!!

weiqing121
Posts: 11
Joined: Tue Aug 09, 2016 2:18 pm

Re: computer module 3+, i need to use SPI1

Wed Oct 23, 2019 3:02 pm

Here is my current way. The SPI1 communication is no longer giving error.
The /boot/configure.txt as:
dtparam=spi=on
dtoverlay=spi1-3cs

The main problem is the adafruit_max31865 package. It wont allow to change the polarity and phase of the SPIDevice. They are hard coded as polarity = 0, phase = 1.
It works for SPI0, but wont work for SPI1. I changed them to polarity = 1, phase = 0, then it is working. I am not fully understand why. Could be the default low setting on CS pins of SPI1.

Would be greatly appreciated if some one can explain why.

User avatar
joan
Posts: 14392
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: computer module 3+, i need to use SPI1

Wed Oct 23, 2019 3:05 pm

By experiment modes 1 and 3 do not appear to work on the auxiliary SPI. See warning in

http://abyz.me.uk/rpi/pigpio/pigs.html#SPIO

justinikus
Posts: 1
Joined: Wed Nov 13, 2019 6:50 pm

Re: computer module 3+, i need to use SPI1

Wed Nov 13, 2019 7:03 pm

I am attempting to implement a similar scenario on a Pi 3, but for me spi0 is being used for CAN communications, and I need two RTD PT100's reading on the aux spi bus.

You mentioned:
"The main problem is the adafruit_max31865 package. It wont allow to change the polarity and phase of the SPIDevice. They are hard coded as polarity = 0, phase = 1.
It works for SPI0, but wont work for SPI1. I changed them to polarity = 1, phase = 0, then it is working."

Can you explain precisely what you did to implement "change to polarity = 1, phase = 0," to get it to work?
What did you alter?

Return to “Compute Module”