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
pythoncoder
Posts: 4241
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

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

rychong
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: https://docs.micropython.org/en/latest/ ... 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:
Conclusion:
* 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)
print(buf)
spi_slave.deinit()

while True:
while not trg.value():
spi_recv()
time.sleep_us(1000)

User avatar
RobH
Posts: 20
Joined: Fri Mar 23, 2018 3:37 pm
Location: Netherlands
Contact:

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

Post by RobH » Sat May 09, 2020 7:31 pm

Hi,
I'm trying to use two PyBoards as SPI master-slave combo and have a similar problem as mentioned by the OP.
When the master sends "01234567", the slave receives "`bdfhjln". When looking at the bit pattern the received data is shifted 1 bit left. Look for example the last character: ord('7') = 55, ord('n') = 110. When the data received by the slave is echoed back, the master receives (to my surprise!): "01234567" (well in most cases the first and second byte are slightly distorted, but the others chars are always correct). Looks like a synchronisation fault. I would be happy with an explanation how this can happen (and a suggestion how to solve the problem!)
As has been emphasized above strongly I verified that master and slave use the same combination of polarity and phase.

Rob.

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

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

Post by pythoncoder » Sun May 10, 2020 10:36 am

I suggest you look at my solution. This uses I2C to establish a bidirectional asynchronous interface between a Pyboard and an arbitrary MicroPython target. The interface is logically full duplex in that either end can initiate a transfer - although the physical interface is half duplex.

There are some interesting synchronisation problems described in the docs and code comments.
Peter Hinch

User avatar
RobH
Posts: 20
Joined: Fri Mar 23, 2018 3:37 pm
Location: Netherlands
Contact:

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

Post by RobH » Mon May 11, 2020 11:51 am

Thank you Peter for your suggestion! Interesting I2C implementation!

I didn't mention in my post that my coupling of 2 pyboards with SPI had as primary purpose to learn how to build an SPI-slave. My experiences so far and the comments in the forums are not encouraging! And since persevering might result in spending (not to say wasting) a lot of time... So I think I'll quit my attempt.

Regards, Rob.

Post Reply