Page 1 of 1

SPI write/read on ESP32

Posted: Wed Jan 03, 2018 9:13 am
by OutoftheBOTS_
I am wanting to write and read from a encoder counter IC LS7366R using SPI. After not being able to get it to work in MicroPython I hooked it up to my Raspberry Pi and wrote a short program and it worked fine in RPi version of python so it seems that my MicroPython program is the problem.

For me to read the 4 byte counter I have to write a command byte (to tell it to read the 4 bytes) then read back 4 bytes.

In python on the RPi I used this code

Code: Select all

data = (0x64, 0, 0, 0,0)
counter = spi.xfer2(data)
and in MicroPython I tried

Code: Select all

cs.value(0)
spi.write(b'\x60')
counter = spi.read(4)
cs.value(1)
RPi python returned correct data then I had to shift the bytes in to an integer but MicroPython always returned b'\xff\xff\xff\xff'

What am I doing wrong??

Re: SPI write/read on ESP32

Posted: Wed Jan 03, 2018 9:36 am
by Roberthh
Comparing the different scripts, You might have to use SPI.write_readinto(), like:

Code: Select all

cmd = bytearray((0x64, 0, 0, 0, 0))
res= bytearray(5)
spi.write_readinto(cmd, res)

Re: SPI write/read on ESP32

Posted: Wed Jan 03, 2018 10:03 am
by OutoftheBOTS_
Thanks for your suggestion

I tried it and got the same result. It is just a quick cut in Python to write some zeros to pulse the clock line as as it reads and writes at the same time in RPi Python. In the data sheet for the LS7366R it says the chip ignores the MOSI input while writing to MISO, it just needs the clock line to pulse.
Here's my Micropython code and u can see where I have copmment out a few other things that I tried, all had same result. I think that maybe I am using the SPI wrongly??

Code: Select all

from machine import Pin, PWM, SPI
import time

spi2 = SPI(1, baudrate=10000000, polarity=1, phase=0, sck=Pin(14), mosi=Pin(13), miso=Pin(12))
spi2.init(baudrate=10000000)

cs = Pin(15, Pin.OUT) 

cs.value(0)
spi2.write(b'\x20') #clear counter
cs.value(1)

cs.value(0)
spi2.write(b'\x30') #clear status
cs.value(1)

cs.value(0)
spi2.write(b'\x88\x03') #set MDR0 to 4 counts per cycle
cs.value(1)

time.sleep(1)

cs.value(0)
spi2.write(b'\x90\x00') #set MDR1 to 4 byte counter
cs.value(1)

buf = bytearray(4)
cmd = bytearray((0x64, 0, 0, 0, 0))

while True:
  cs.value(0)
  #spi2.write(b'\x60') #command to read counter
  #data = spi2.read(4) #read 4 bytes
  #spi2.readinto(buf, 0x60)
  res= bytearray(5)
  spi2.write_readinto(cmd, res)
  cs.value(1)
  print (res)
  
  

Re: SPI write/read on ESP32

Posted: Wed Jan 03, 2018 10:16 am
by OutoftheBOTS_
Ok I just put the scope on the SPI pins 1 at a time and the only pin that is doing anything is the CS pin that I am driving. I am using Loboris firmware so I might ask him.

Re: SPI write/read on ESP32

Posted: Thu Oct 24, 2019 5:11 pm
by eradicatore
Roberthh wrote:
Wed Jan 03, 2018 9:36 am
Comparing the different scripts, You might have to use SPI.write_readinto(), like:

Code: Select all

cmd = bytearray((0x64, 0, 0, 0, 0))
res= bytearray(5)
spi.write_readinto(cmd, res)
Thanks for this info! One important thing I just learned. If you have just one byte in the byte array, then this gets used as the length.

So:

Code: Select all

cmd = bytearray((0x02))
didn't give me what I wanted. it gave me a byte array of 2 bytes of 0x00. :D

This is the syntax to use for one byte (make it a list, with the comma):

Code: Select all

cmd = bytearray((0x02,))