WS2801 with SPI on Micropython

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
astraios
Posts: 3
Joined: Wed May 07, 2014 7:48 pm

WS2801 with SPI on Micropython

Post by astraios » Sat Nov 01, 2014 7:28 pm

Hi!

I am trying to get my custom built RGB LED display running with my pyBoard. It's based on WS2803 chips which are basically 18-channel WS2801 (Protocol described here on page 6: http://www.jarzebski.pl/datasheets/WS2803.pdf).
I already tried bit banging the protocol which worked. But this is a bit slow, so I want to use SPI for the job. On my old Arduino-based controller I successfully used this SPI-code:

Code: Select all

/**
Writes the frame buffer's content to the display via SPI
**/
void WS2801Display::refresh(void)
{
    uint16_t byteCount = pixelCount * 3;

    for(uint16_t i=0; i<byteCount; i++) {
        SPDR = frameBuffer[i];
        while(!(SPSR & (1<<SPIF)));
    }

    delay(1); // Data is latched by holding clock pin low for 1 millisecond
}
SPI mode is set to SPI_MODE0, which equals polarity 0 and phase 0.

I am using SPI 2 and connected the WS2803's SCK to Y6 and DATA to Y8.
What I'm trying now on my pyBoard is:

Code: Select all

from pyb import SPI

spi = SPI(2, SPI.MASTER, baudrate=20000, polarity=0, phase=0)
framebuffer = bytearray(168*3) # 168 is the amount of rgb pixels in my display
for i in range(168*3):
    framebuffer[i] = 10 # write some data to the buffer, for testing
while True:
    spi.send(framebuffer)
    pyb.delay(1) # latch data
Unfortunately nothing happens on my display :(
Do you have any suggestions?

Thanks in advance!

Cheers,
Robert

Damien
Site Admin
Posts: 647
Joined: Mon Dec 09, 2013 5:02 pm

Re: WS2801 with SPI on Micropython

Post by Damien » Mon Nov 03, 2014 12:20 am

Which version of Micro Python are you using? There was recently a change to the SPI init function such that the phase argument changed. Old versions used 1 or 2 instead of the more conventional 0 or 1. In the version you are using you might need phase=1.

astraios
Posts: 3
Joined: Wed May 07, 2014 7:48 pm

Re: WS2801 with SPI on Micropython

Post by astraios » Thu Nov 06, 2014 6:51 pm

I am using v1.3.5 from 2014-10-29 on pyB v1.0.
It seems to be working now with both polarity and phase set to 0. I don't know why it didn't work before. Probably was a wiring issue.
It also seems to work with 20MHz clock speed. Nice :D
Thanks for the support!

Post Reply