Consistent bitflips when using pyboard as SPI slave and other SPI weirdnesses

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
User avatar
Posts: 4030
Joined: Fri Jul 18, 2014 8:01 am
Location: UK

Re: Consistent bitflips when using pyboard as SPI slave and other SPI weirdnesses

Post by pythoncoder » Wed Apr 26, 2017 5:45 am

An interrupt driven SPI slave on the ESP8266 is likely only to work at very low data rates. This is because interrupt latency on the ESP is dreadful: even when overclocked to 160MHz I have measured an interrupt latency of 643µs. As I mentioned above, the design of SPI assumes slaves to be implemented in hardware. The protocol has no handshaking so the slave must respond in a timely fashion to unexpected data coming from the master. An interrupt driven approach would normally be the way to go but the ESP8266 is a challenging target (as is the ESP32).
Peter Hinch

Posts: 1
Joined: Thu Apr 25, 2019 3:23 pm

Re: Consistent bitflips when using pyboard as SPI slave and other SPI weirdnesses

Post by rychong » Tue Sep 03, 2019 5:20 pm

gercha2 wrote:
Sun Mar 26, 2017 7:48 pm
Thank you!!!, I solved it. At last I got: "/x01/x02/x03/x04"
I didn´t know where to put (spi.deinit), because I got a Type Error. So fortunately your ad was very useful. I missed where SPI stops reading for synchronization. Then I follow some procedures described in: ... b.SPI.html
Pyboard as Slave receiving data from Raspberry pi 2(SPI - Master):
1. import SPI from pyb
2. SPI initialization (e.g. spi = SPI(2,SPI.SLAVE, baudrate=16000000, polarity=0, phase=0,...); I pesonally used SPI(2)
(Important to both MASTER and SLAVE be matched in Polarity and phase) as mention Turbinenreiter:
* make sure you are using the same mode on both devices
* deinit the spi object before you reload the script with a changed SPI mode
3. receive data from Master (Depend on the number of bytes to receive)
4. spi.deinit()
5. print(data)
Hi, gercha2, i meet the same problem with you, i have set both SPI-Master(Raspberry) and SPI-Slave(Pyb) work on SPI mode 0, and close it after receive, but it still error:
1. if the master send b'1234', then the slave receive b'4123'
2. if the master send b'abcdefgh', then the slave receive b'habcdefg'

Besides, i found that spi.recv() of pyb can't work in the callback function, i can only use it with while loop, can you paste both of your error code and the correct code after change?

here is my code that with the same problem:

from pyb import SPI, ExtInt, Pin
import time

spi = pyb.SPI(2)
trg = pyb.Pin(pyb.Pin.cpu.A6, Pin.IN, pull=Pin.PULL_UP)

def spi_recv(spi_slave=spi):
spi_slave.init(pyb.SPI.SLAVE, polarity=0, phase=0,bits=8, firstbit=SPI.MSB, crc=None)
buf = spi_slave.recv(8,timeout=5000)

while True:
while not trg.value():

Post Reply