SPI on ESP8266 . How implemnet ?!

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
BOB63
Posts: 58
Joined: Sat Jul 25, 2015 8:24 pm
Location: Monza , Italy

SPI on ESP8266 . How implemnet ?!

Post by BOB63 » Sun Jul 29, 2018 2:05 pm

Hi,
day ago I've raised a similar question related SPI and Py Board viewtopic.php?f=6&t=4950 and now I'm here again for the same reason on SPI using Adafruit Feather HUZZAH ESP8266.

The device connected to the spi , a BIG BUDDY TALKER http://www.engineeringshock.com/the-big ... -page.html require to set SPI so to use the firstbit=MSB and I'm using the hw interface from the board.

The code suggested by Damien as reply to my previous post seems don't work on ESP 8266 , so I'm following the documentation https://docs.micropython.org/en/latest/ ... achine-spi

and this is the code I'm using :

Code: Select all

from machine import Pin
from machine import SPI

spi = SPI(1)
spi.init(baudrate=600000,polarity=0, phase=0, bits=8, firstbit=SPI.MSB)

print(spi)
With this code I got this error on line 15 " spi.init(baudrate=600000,polarity=0, phase=0, bits=8, firstbit=SPI.MSB) " :

Cattura.PNG
Cattura.PNG (16.09 KiB) Viewed 5898 times


The question are :

1 )Where I'm wrong and how implement SPI on this board , if what is writte in the documentation isn't correct . :(
2) If there is problems with the wrong documentation , when we can hope to have fixed? :roll:
3) Why we must continue to have different sintax between micropython used on Py board and esp8266 ( for instance also
in the use of pin ...) and why don't think to have just one documentation with inside described the differences between the code to be used
on one board or another ? 8-)

Sometime the implementation of easy things sound frustrating ....
Thanks. Roberto

BOB63
Posts: 58
Joined: Sat Jul 25, 2015 8:24 pm
Location: Monza , Italy

Re: SPI on ESP8266 . How implemnet ?!

Post by BOB63 » Tue Jul 31, 2018 5:24 pm

Really any idea ?!
Thanks. Roberto

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

Re: SPI on ESP8266 . How implemnet ?!

Post by OutoftheBOTS_ » Tue Jul 31, 2018 10:04 pm

It has been a little while since I used ESP8266 but in the past I did use SPI on ESP8266 without problem.

I used to create the SPI and init in 1 line like the docs

Code: Select all

hspi = SPI(1, baudrate=80000000, polarity=0, phase=0)
If your having problem with the line of code

Code: Select all

spi.init(baudrate=600000,polarity=0, phase=0, bits=8, firstbit=SPI.MSB) 
I would suggest just setting 1 parameter at a time and see which 1 is causing problems.

Code: Select all

spi.init(baudrate=600000) 
spi.init(polarity=0)
spi.init(phase=0)
spi.init(bits=8)
spi.init(firstbit=SPI.MSB)

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: SPI on ESP8266 . How implemnet ?!

Post by pythoncoder » Thu Aug 02, 2018 6:58 am

I'm puzzled by the error message "ImportError: no module named 'BMEESPt.py'", also the reference to line 8 of boot.py in the traceback. I suggest you post boot.py, main.py. On SPI you could try soft SPI.
Peter Hinch
Index to my micropython libraries.

BOB63
Posts: 58
Joined: Sat Jul 25, 2015 8:24 pm
Location: Monza , Italy

Re: SPI on ESP8266 . How implemnet ?!

Post by BOB63 » Sun Aug 26, 2018 2:00 pm

Hi pythoncoder ,sorry for the delay in my reply ....

I've deepen a little bit the issue with SPI on ESP8266 vs SPI on PyBoard.
I've found some misleading description in the online documentation but will discus later.

Now the problem is about how SPI works on ESP8266 vs PyBorad.

For my project I must send to the vocal syntetizer two data , a command 152 followed by a number between 0-254 that represent which voice I want activate.

This is the code used on PyBoard and how the SPI send data on MOSI line :

Code: Select all

# Collegamenti interfaccia MOSI
# mosi   pin x8 (interf 1)  y8 (interf 2)
# clock  pin x6 (interf 1)  y6 (interf 2)

#chip selection
# cs1   pin y4
# cs2   pin y5
# cs3   pin y6
# cs4   pin y7

#-------------------------------------------------------------------------------
import pyb
import machine
import math
from pyb import Pin
from pyb import SPI

spi = SPI(1,SPI.MASTER, baudrate=600000, polarity=0, phase=0, bits=8, firstbit=SPI.MSB)
print(spi)

cs1 = Pin('Y4', Pin.OUT_PP)
cs1.high()

cs2 = Pin('Y5', Pin.OUT_PP)
cs2.high()

cs3 = Pin('Y6', Pin.OUT_PP)
cs3.high()

cs4 = Pin('Y7', Pin.OUT_PP)
cs4.high()

while True:

      spi.send(b'152')
      spi.send(b'61')

      pyb.delay(1000)


This is how data are transmitted on MOSI ( data are showed as decimal value).
See picture SPI PyBoard.JPG

Image


Implementing a similar code on ESP8266 I've some problems because seems that SPI work in a different way .

This is the ESP8266 implementation:

Code: Select all

import machine


import time
import utime

from machine import Pin
from machine import SPI



spi = SPI(1, baudrate=600000, polarity=0, phase=0)  #  HW SPI

#spi = SPI(-1, baudrate=600000, polarity=0, phase=0, sck=Pin(14), mosi=Pin(13), miso=Pin(12),firstbit=SPI.MSB)


print(spi)


cs1 = Pin(2, Pin.OUT)
cs1.value(1)

cs2 = Pin(16, Pin.OUT)
cs2.value(1)

cs3 = Pin(0, Pin.OUT)
cs3.value(1)

cs4 = Pin(15, Pin.OUT)
cs4.value(1)



while True:

      cs1.value(0)
      spi.write(b'152')
      spi.write(b'61')
      cs1.value(1)


      time.sleep_ms(550)


This is how the SPI transfer the data ( values showed as decimal value):
See picture ESP8266 as pyboard.JPG
Image

ESP8266 split the values in single byte and send 3 byte for 152 and 2 for 61.


At this point I've tried to send ASCII characters equivalent to 152 (ÿ) and 61 (=) instead than declare the decimal value, and this is what happen if i change the code as :

Code: Select all

spi.write(b'ÿ')  #ascii 152
          spi.write(b'=')  #ascii 61
See picture ESP8266 alt152.JPG
Image

In whch way can I solve the issue ?
Is it due to different board hw or is something related to micropython implementation ?

Thanks Roberto.
Attachments
ESP8266 alt152.JPG
SPI TRansfer on ESP8266 sending Ascii 152 (ÿ) and 61 (=)
ESP8266 alt152.JPG (97.2 KiB) Viewed 5771 times
ESP8266 as pyboard.JPG
SPI TRansfer on ESP8266 sending 152 and 61
ESP8266 as pyboard.JPG (103.28 KiB) Viewed 5771 times
SPI PyBoard.JPG
SPI TRansfer on PyBoard
SPI PyBoard.JPG (129.84 KiB) Viewed 5771 times
Thanks. Roberto

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: SPI on ESP8266 . How implemnet ?!

Post by pythoncoder » Mon Aug 27, 2018 7:40 am

A couple of points. Firstly, you can use the machine module on the Pyboard. This would enable you to run the same SPI code on each, facilitating a direct comparison.

Secondly, you need to be clear exactly what you need to send to the device. b'152' is a 3 byte string whereas b'\x98' is a single character of value 152. Which does the device actually require?

You may want to experiment at the REPL to get a handle on this.

Code: Select all

>>> b'\x98'[0]
152
If you do need to send two characters, consider a bytearray:

Code: Select all

a = bytearray(2)
a[0] = 152
a[1] = 61
spi.write(a)
I'd avoid using characters like 'ÿ' in code. Extended ASCII may not always be handled correctly in editors/IDE's and suchlike.
Peter Hinch
Index to my micropython libraries.

BOB63
Posts: 58
Joined: Sat Jul 25, 2015 8:24 pm
Location: Monza , Italy

Re: SPI on ESP8266 . How implemnet ?!

Post by BOB63 » Mon Aug 27, 2018 6:44 pm

Hi Peter,
about the first point I'll try to do what you suggest and use on Pyboard the machine module instead than pyb module.

About the second point my voice synt require to receive just one byte with the command /x98, followed by another byte ( between 0 and 254 dec) .

On some examples code of Voice synth , I've seen that was used to send the decimal value 152 instead than hex value, so on pyboard using the pyb module I've seen that was working in the same way.

I was not aware that was possible send as bytecode an hex value and due that using on the Pyboard the pyb module spi was working correctly sending the decimal value instead that hex value , I was totally in confusion .... :roll:

Now on the code for ESP8266 as you suggested I've changed the code as below , sending the hex value , and now it works
Thank you so much as ususal !

Code: Select all

while True:

      cs1.value(0)
      print(cs1.value())
      spi.write(b'\x98')   # 152 dec
      spi.write(b'\x3D')   #61 dec
      cs1.value(1)


Again something new learned .

Thanks again.
Thanks. Roberto

Post Reply