joosteto / ws2812-spi

python routines to program the WS2812 RGB LED chips on the raspberry, using the hardware SPI MOSI.
GNU Affero General Public License v3.0
59 stars 23 forks source link

Tweak the right xfer parameters #1

Open ymollard opened 7 years ago

ymollard commented 7 years ago

I am wondering how you tweaked the parameters of xfer(...), is there a logical explanation around these attempts to select the right speed or are they purely empirical? https://github.com/joosteto/ws2812-spi/blob/master/ws2812.py#L41-L52

Have you also considered keeping Chip-Select active with xfer2(...)? It seems it solved the flashing problems on my side.

joosteto commented 7 years ago

Thanks for you comment, interesting to hear someone has tried the library out:).

As you probably already assumed from the comments and many different values of the parameters I tried, the frequency is indeed purely empirical.

Also, thanks for the suggestion to keep the Chip-Select enabled, haven't tried that yet! Will try it sometime (although I have other projects at the moment).

ymollard commented 7 years ago

Self-correction: xfer2 does not prevent flashing, I'm still looking for a stable solution.

Do you know what this speed correspond to? The WS2812B's datasheet specifies a 800Kbps data rate, maybe there is something to conclude from that characteristics

penfold42 commented 7 years ago

The 800khz is a nominal frequency in the data sheet. What really matters is the timing of the high and low pulses which represent ones and zeros of led data. There's a great article at https://www.google.com.au/amp/s/wp.josh.com/2014/05/13/ws2812-neopixels-are-not-so-finicky-once-you-get-to-know-them/amp/?client=safari

The code I wrote for hyperion used 4 spi bits to represent each ws2812 bit so was running at between 2.4 and 3 MHz.

At extreme values too high or too low the lights would always be black or white. I can't remember which wa which.

I also found that if the core clock frequency changed, the leds would flicker. Have you got any dynamic turbo settings ? Is your Pi giving an under voltage warning ?

Do you have a level shifter on the data line ? Do you have a long cable run between the Pi and 1st led ?

ymollard commented 7 years ago

Thanks @penfold42 Indeed my Pi reports an undervoltage, for a reason I can't understand because my PSU can provide 20A and only 2 to 5A are consumed (Pi + 150 LEDs), maybe a capacitor close to the Pi would help. Also I might have CPU throttling because I'm using the default config of Raspbian.

I have no level shifter (the high level voltage is very borderline indeed). However after many weeks of trying to fix this bug from software I can no longer reproduce it now... very frustrating! Now I can make it flicker only with a lab PSU by limitating amperage to <1A but flickering is then immediately aggressive, not that episodic as I had before.

I am now running at 4.2MHz (same as the best I had before with flashes) and also 4 SPI bits, without problem. I can't get it...

ymollard commented 7 years ago

OK so I left the pretty high 4.2MHz for 3.2MHz since that should give a LED signal closer to their nominal frequency 800kHz. My oscilloscope mostly confirms that frequency on the MISO pin, except when most LEDs are off, so there is a lot of zeros, in that case the signal goes down to ~500kHz and the LEDs flicker pretty much as long as the strip is displayed black.

Would it be possible that the lib does not handle well the very short peaks of T0H = 0.4µs and that the mess start from here?

penfold42 commented 7 years ago

I doubt it's a problem with the libs.

We're (ab)using the spi controller and its DMA support to squirt the data out so the low level bit bashing is done by the kernel and spi controller.

Don't focus too much attention on the measured frequency - it's the pulse high and low times that matter to the leds

alxcp commented 4 years ago

Have spent 2 days for experiments with my OrangePi Zero and ws2812b stripe without level shifter. Best result - something about 2 from 3 refreshes is ok, but 1 of 3 refreshes stripe is flickering - shows "random" bright colors. "Random" is not natural random, because i see some stable too bright image with some structure, related to original image. Tried to tweak second xfer argument by cycle of the refreshing with increasing xfer argument value. Have found int(4/0.85e-6) as the value with best result, but not ideal. My stripe is powered with a 20A powering source (with diode), ground and data wire is connected to OrangePi, data wire connected through 330R resistor. Data wire is short - something about 15 cm. For now waiting for level shifter (74AHCT125), maybe it's help. Maybe anybody have any another ideas?