MicroPython on ESP32 with SPIRAM support

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: MicroPython on ESP32 with SPIRAM support

Post by OutoftheBOTS_ » Wed Jan 03, 2018 10:23 am

@Loboris

I hate to be asking for your help agin but I seem to need it.

I am using a external quadrature encoder counter IC called LS7366R. I can't get it working on the ESP32 but when I hooked it up to the Raspberry Pi I had no problem getting it work properly so this tells me I am wiring it correctly and the chip does work.

Communication to the chip is via SPI, you write a command byte followed by 1 to 4 data bytes (can be either read or write depending on what set in command byte)

I have just hooked up the scope to the SPI pins while the program was running and the only pin that was rising and falling was the CS pin that I was driving so obviously I am doing something wrong with setting up and using the SPI.

Here is my code, it is very short

Code: Select all

from machine import Pin, 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)

while True:
  cs.value(0)
  spi2.write(b'\x60') #command to read counter
  data = spi2.read(4) #read 4 bytes
  cs.value(1)
  print (data)
  
  

OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: MicroPython on ESP32 with SPIRAM support

Post by OutoftheBOTS_ » Wed Jan 03, 2018 11:57 am

This code works fine on the ESP8266 so it must be how I am trying to use the SPI on the ESP32

Code: Select all

from machine import Pin, SPI
import time
import ustruct

spi2 = SPI(1,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_ms(200)

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


while True:
  cs.value(0)
  spi2.write(b'\x60') #command to read counter
  data = spi2.read(4) #read 4 bytes
  cs.value(1)
  counter = ustruct.unpack('>I', data)
  print (counter)
  time.sleep_ms(200)
  
  

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

Re: MicroPython on ESP32 with SPIRAM support

Post by Roberthh » Wed Jan 03, 2018 1:14 pm

I tried something like

Code: Select all

cmd = bytearray((0x60, 0, 0, 0, 0))
res = bytearray(5)
spi=SPI(1, sck=21, miso=22, mosi=23, baudrate = 1000000)
spi.write_readinto(cmd, res)
on a Wemos devices with the loboris port, and data and clock appear as expected. The Pins numbers were just grabbed out of the air.

loboris
Posts: 344
Joined: Fri Oct 02, 2015 6:19 pm

Re: MicroPython on ESP32 with SPIRAM support

Post by loboris » Wed Jan 03, 2018 4:39 pm

@OutoftheBOTS_

Your code should work, I'll check it.

As @Roberthh mentioned, try to use spi.write_readinto(cmd, res).

You can also configure the spi CS when you create the spi object:

Code: Select all

spi2 = SPI(1, baudrate=10000000, polarity=1, phase=0, sck=Pin(14), mosi=Pin(13), miso=Pin(12), cs=Pin(15))
and it will be handled by the driver, you don't need to activate/deactivate it manually..

The complete spi documentation will be available tommorow.

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

Re: MicroPython on ESP32 with SPIRAM support/partition table

Post by Roberthh » Wed Jan 03, 2018 5:28 pm

This partition table https://github.com/loboris/MicroPython_ ... ns_mpy.bin seems to be wrong. The app partition is defines with a size of 64k, should be 1408k. And the offset ofr the file system is wrong too. The same problem exists for the other versions.

loboris
Posts: 344
Joined: Fri Oct 02, 2015 6:19 pm

Re: MicroPython on ESP32 with SPIRAM support/partition table

Post by loboris » Wed Jan 03, 2018 6:18 pm

Roberthh wrote:
Wed Jan 03, 2018 5:28 pm
This partition table https://github.com/loboris/MicroPython_ ... ns_mpy.bin seems to be wrong. The app partition is defines with a size of 64k, should be 1408k. And the offset ofr the file system is wrong too. The same problem exists for the other versions.
Sorry, I was cleaning the repository, the full update with prebuilt firmware will be commited tomorrow.
The unusable firmware files are now deleted.

OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: MicroPython on ESP32 with SPIRAM support

Post by OutoftheBOTS_ » Wed Jan 03, 2018 9:53 pm

Ok I have just woken up and tried a few things but have to goto work now so will get back to more testing after work.

I typed RobertHH code and added import Pin, SPI into the PuTTy telnet Repl prompt and sure enough my scope shows there was a clock signal on pin 21 so next I tried running my program but it did not produce a clock signal on pin 14. Very strange, after work I will try other pins as see if it is a problem with my choice of pins

OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: MicroPython on ESP32 with SPIRAM support

Post by OutoftheBOTS_ » Thu Jan 04, 2018 5:52 am

So I have rewired my LS7366R to use the same pins as RobertHH used in his example and everything worked so it seems it was the choice of pins that I was using that caused the problem. I have played with a few of the options in menuconfig so maybe something else in the firmware conflicts with those pins ??

I also encounted another problem. After playing with the code a little to clean it up and running the reload fuction I got the error OSError: no free slots so I asuume this is because everytime I am reloading the program it is making another spi bus and keeping the old 1 alive.

I have cleaned up the code now to include the cs=15 in the driver instead of manually switching it and also included the suggestion for write_readinto() and also added try/expect to catch the keyboard interput and deint() the spi bus

I would be interested to know why the other pins wouldn't work for SPI

Code: Select all

from machine import Pin, SPI
import time
import ustruct

spi2=SPI(1, sck=21, miso=22, mosi=23, cs=15, baudrate = 1000000)

spi2.write(b'\x20') #clear counter
spi2.write(b'\x30') #clear status
spi2.write(b'\x88\x03') #set MDR0 to 4 counts per cycle
spi2.write(b'\x90\x00') #set MDR1 to 4 byte counter

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

try:
  while True:
    res= bytearray(5)
    spi2.write_readinto(cmd, res)
    counter = ustruct.unpack('>I', res[1:5])
    print (counter[0])
    time.sleep_ms(200)

except: spi2.deinit()
  
  

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

Re: MicroPython on ESP32 with SPIRAM support

Post by Roberthh » Thu Jan 04, 2018 7:03 am

Which Pins were you using before?

OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: MicroPython on ESP32 with SPIRAM support

Post by OutoftheBOTS_ » Thu Jan 04, 2018 7:22 am

From code posted above

Code: Select all

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)

Post Reply