SPI write/read on ESP32

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
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??
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 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)
			
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.  

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,))
  • ↳   Announcements and News
  • ↳   Newsletter archive
  • ↳   Kickstarter logistics for the Micro Python campaign
  • The MicroPython Language
  • ↳   General Discussion and Questions
  • ↳   Programs, Libraries and Tools
  • ↳   Development of MicroPython
  • Boards Running MicroPython
  • ↳   MicroPython pyboard
  • ↳   Pyboard D-series
  • ↳   WiPy and CC3200 boards
  • ↳   ESP8266 boards
  • ↳   ESP32 boards
  • ↳   micro:bit boards
  • ↳   Raspberry Pi microcontroller boards
  • ↳   Other Boards
  • Hardware Projects and Component Drivers
  • ↳   Hardware Projects
  • ↳   Drivers for External Components
  •