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.
Turbinenreiter
Posts: 255
Joined: Sun May 04, 2014 8:54 am

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

Postby Turbinenreiter » Mon Feb 20, 2017 7:50 pm

I'm using the pyboard as SPI slave and a Raspberry Pi as Master, both running MicroPython and I'm seeing a lot of weirdness going on.

For example, I'm sending this (as byterray):

Code: Select all

[254, 202, 1, 0, 170, 15, 62, 32, 1]

and get his on the pyboard:

Code: Select all

[126, 74, 1, 0, 42, 15, 62, 32, 0]

That's a flip of the first bit. Always the first, never a different one. The first two bytes always have the bitflip, the other bites sometimes do, sometimes don't. This only happens from Pi to pyboard.
Needless to say, it didn't do that yesterday, only change I made was that the pyboard is now running from the SD card and not the internal flash.

At the same time, the data I send from the pyboard to the Raspberry Pi is fine, except when it isn't. Sometimes this happens:
send from the pyboard:

Code: Select all

[254, 202, 1, 0]

receive on the Pi:

Code: Select all

[0, 254, 202, 1]

It's only ever off by one, but not always. This happens in both directions.

This off-by-one-byte error also happens in a completely different project on completely different hardware with an atmel board running linux and an atxmega programmed in C++.

I'm using pyb.SPI on the pyboard, because machine doesn't seem to have a Slave mode.

Code: Select all

import micropython
micropython.alloc_emergency_exception_buf(100)
from pyb import SPI, Pin, ExtInt
spi = SPI(1, SPI.SLAVE, baudrate=10000)

def callback(trigger):
    spi.send_recv(data.bytes, control.bytes)
    data.struct.new = False

interrupt = ExtInt(cs, ExtInt.IRQ_FALLING, Pin.PULL_UP, callback)

while True:
   ...

User avatar
dhylands
Posts: 2253
Joined: Mon Jan 06, 2014 6:08 pm
Location: Shuswap, BC, Canada
Contact:

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

Postby dhylands » Mon Feb 20, 2017 10:00 pm

I would look at the various modes and make sure that both sides are being consistent about when the data is being clocked.

Normally, you want the data to driven on one edge (rising or falling) and latched on the opposite edge.

This is probably easiest to see on a logic analyzer if you sent a bitstream of alternating 0's and 1's then it's easy to see when the data changes versus the clock. There should be a clock edge that's in the middle of when the bit is 0 or 1. That's the edge the slave should be using.

User avatar
deshipu
Posts: 925
Joined: Thu May 28, 2015 5:54 pm

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

Postby deshipu » Mon Feb 20, 2017 10:07 pm

Did you make sure the polarity and phase are the same on both devices, and the clock is slow enough for both devices to work reliably?

Do you happen to know a logic analyzer that you could use to see what is actually there on the cables (and thus know which device is wrong)?

Turbinenreiter
Posts: 255
Joined: Sun May 04, 2014 8:54 am

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

Postby Turbinenreiter » Tue Feb 21, 2017 10:03 am

I checked the SPI mode again, changed my UNIX SPI library a bit, and am now sure that both devices sue the same mode.

Then I played around a bit, changing the modes to be the same, then to be different. Nothing changed, still bad communication.

Until I did a spi.deinit() before reseting the board. And now it's perfect. Also, now the SPI works when the mode matches and has errors when the mode mismatches.

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

I gotta say you guys give good workflow. I post a cry for help usually around midnight before giving up and going to sleep. Next morning with a fresh brain and your input my problems are solved quickly.

Now I'll order an logic analyzer.


Return to “The MicroPython pyboard”

Who is online

Users browsing this forum: fpp and 2 guests