Hardware SPI not working

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
smith.randallscott
Posts: 32
Joined: Mon May 13, 2019 12:42 pm

Hardware SPI not working

Post by smith.randallscott » Fri May 17, 2019 7:00 pm

I am brand new to Micro Python, and I have not been successful in getting the Hardware SPI bus working on pins 23, 19, 18, and 5. So far I have been unable to get the clock signal on the output of pin18. Is there any one that has this working?

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Hardware SPI not working

Post by jimmo » Sat May 18, 2019 12:24 am

Hi,
Can you share the code you're using to construct and use the machine.SPI instance.
Thanks

smith.randallscott
Posts: 32
Joined: Mon May 13, 2019 12:42 pm

Re: Hardware SPI not working

Post by smith.randallscott » Mon May 20, 2019 7:14 pm

Using a logic analyzer, it appears that the chip select line begins high, shifts low for 0.17ms, shifts high for 0.84ms, shifts low for 1.215ms then a 50kHz clock cycle is present for 0.31ms(on the chip select line), afterwards the chip select line remains low. Also I have not been able to determine the method for inserting a screenshot of the logic analyzer

from machine import Pin
from machine import SPI

cs = Pin(18, Pin.OUT)
cs.on()

miso = Pin(19, Pin.IN)
mosi = Pin(23, Pin.OUT)
sck = Pin(18, Pin.OUT)

spi = SPI(2, baudrate = 50000, sck = sck, mosi = mosi, miso = miso)

write80_buf = bytearray(2)
write80_buf[0] = 0x80
write80_buf[1] = 0x80

cs.off()

spi.write(write80_buf)

cs.on()

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Hardware SPI not working

Post by Roberthh » Mon May 20, 2019 7:24 pm

You used the same GPIO 18 for CS and clock in your script.

smith.randallscott
Posts: 32
Joined: Mon May 13, 2019 12:42 pm

Re: Hardware SPI not working

Post by smith.randallscott » Mon May 20, 2019 8:04 pm

Thanks I found that problem shortly after I posted the reply. What I am noticing now is that the chip select line starts high, as it should, but once the chip select line goes low (for the first time) it stays low and then pulses high for transmission of data, then returns low; I believe this is opposite of what it should be. Thanks

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Hardware SPI not working

Post by Roberthh » Mon May 20, 2019 8:06 pm

I do not like the on() and off() notation, because I never know if on means low or high. I use cs(1) for high and cs(0) for low.

smith.randallscott
Posts: 32
Joined: Mon May 13, 2019 12:42 pm

Re: Hardware SPI not working

Post by smith.randallscott » Mon May 20, 2019 8:39 pm

I have the same result when the on and off are changed to 1 and 0.

from machine import Pin
from machine import SPI

cs = Pin(5, Pin.OUT)
cs(1)

miso = Pin(19, Pin.IN)
mosi = Pin(23, Pin.OUT)
sck = Pin(18, Pin.OUT)

spi = SPI(2, baudrate = 50000, sck = sck, mosi = mosi, miso = miso)

write80_buf = bytearray(2)
write80_buf[0] = 0x80
write80_buf[1] = 0x80

cs(0)

spi.write(write80_buf)

cs(1)

cs(0)
spi.write(write80_buf)
cs(1)

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Hardware SPI not working

Post by jimmo » Mon May 20, 2019 9:27 pm

Out of curiosity does it work if you use software SPI instead? (i.e. just remove the `2` from the SPI constructor).

smith.randallscott
Posts: 32
Joined: Mon May 13, 2019 12:42 pm

Re: Hardware SPI not working

Post by smith.randallscott » Tue May 21, 2019 1:48 pm

I had two of the test leads connected incorrectly on my logic analyzer. I appear to have it working with software SPI. Thank you all

smith.randallscott
Posts: 32
Joined: Mon May 13, 2019 12:42 pm

Re: Hardware SPI not working

Post by smith.randallscott » Wed May 22, 2019 2:51 pm

After establishing what appears to SPI communication, the response from the MAX31865 does not seem to be corrected. I'm not sure how to trouble shoot this going forward. A work in progress I suppose.

Post Reply