Stalled SPI Transfer

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
Markus Gritsch
Posts: 41
Joined: Fri May 16, 2014 9:04 pm

Stalled SPI Transfer

Post by Markus Gritsch » Thu Jun 12, 2014 6:18 pm

Hi,

I wrote a small script to drive 10 WS2812 LEDs. From time to time an error in the transferred color codes appears, and I tracked it down to a random stalling of the SPI transfer. Channel 0 is MOSI and Channel 1 is SCK. The transfer gets interrupted two times:
2014-06-11_22-21-21-722.png
2014-06-11_22-21-21-722.png (94.69 KiB) Viewed 7903 times
For driving such LEDs, this is not good, of course, since the timing is quite tight, and halting the transfer for too long gets interpreted as the end of the transfer and the new color values get latched.

Any idea why this happens, and how to prevent it?

The complete script can be found in this post: http://forum.micropython.org/viewtopic. ... t=108#p580

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

Re: Stalled SPI Transfer

Post by dhylands » Thu Jun 12, 2014 6:40 pm

Is this from a single invocation of spi.send ?

If not, then I would expect there to be a pause between each call to spi.send (even if this were coded in C).

The typical way of combating this is to have some type of FIFO. If the HW doesn't provide one, then you can sometimes implement one using DMA.

User avatar
Markus Gritsch
Posts: 41
Joined: Fri May 16, 2014 9:04 pm

Re: Stalled SPI Transfer

Post by Markus Gritsch » Thu Jun 12, 2014 6:45 pm

Yes, this is a single SPI.send() transfer. It does not get interrupted most of the time, but now and then (on the scale of 1 stalled out of 100 unstalled) an interruption happens.

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

Re: Stalled SPI Transfer

Post by dhylands » Thu Jun 12, 2014 7:22 pm

OK - spi.send winds up calling HAL_SPI_Transmit which basically just has a loop in C which send 16 bits at a time to the SPI peripheral.

I'm going to guess that the gaps are caused by interrupts (millisecond tick timer, USB, etc).

So I think that you have 2 options:
1 - disable interrupts around the call to spi.send (pyb.disable_irq()/pyb.enable_irq())
2 - Fix spi.send to use DMA

Disabling interrupts for long periods of time may cause other problems. For example, disabling interrupts for more than a millisecond may cause timer ticks to get missed.

User avatar
Markus Gritsch
Posts: 41
Joined: Fri May 16, 2014 9:04 pm

Re: Stalled SPI Transfer

Post by Markus Gritsch » Thu Jun 12, 2014 7:41 pm

dhylands wrote:1 - disable interrupts around the call to spi.send (pyb.disable_irq()/pyb.enable_irq())
Thanks, this seems to fix the problem! I have it running for quite some time now, without any stalls :)
I added a ticket/feature request for option 2: https://github.com/micropython/micropython/issues/685

BTW, thanks for doing such a great job on this forum. You and pfalcon seem to run this almost exclusively.

Post Reply