Library allowing up to 16 strings of 60 WS2811 or WS2812 LEDs to be driven from a single FRDM-KL25Z board. Uses hardware DMA to do a full 800 KHz rate without much CPU burden.
After being frustrated by the SPI system's performance, I ended up using an approach inspired by Paul Stoffregen's OctoWS2811. This uses 3 of the 4 DMA channels triggered by the TPM0 timer PWM and overflow events.
This design will allow for up to 16 strings of up to 60 (limited by RAM space) WS2811/WS2812 LEDs to be driven on a single port. Adding more strings takes the same time to DMA, because the bits are output in parallel.
Here is my test program:
Import programMulti_WS2811_test
Test program for my Multi_WS2811 library that started out as a fork of heroic/WS2811. My library uses hardware DMA on the FRDM-KL25Z to drive up to 16 strings of WS2811 or WS2812 LEDs in parallel.
Here's 60 LEDs on a single string, at 10% brightness: https://www.icloud.com/sharedalbum/#B015oqs3qeGdFY
Note though that the 3.3V output from the FRDM-KL25Z's GPIO pins is OUT OF SPEC for driving the 5V WS2812 inputs, which require 3.5V for a logic HIGH signal. It only works on my board if I don't connect my scope or logic analyzer to the output pin. I recommend that you add a 5V buffer to the outputs to properly drive the LED strings. I added a CD4504 to do the 3.3 to 5V translation (mostly because I had one). You could use (say) a 74HCT244 to do 8 strings.
Each LED in a string takes 24/800e3 seconds to DMA, so if MAX_LEDS_PER_STRING is set to 60, then it takes 1.8 msec to actually do the DMA, plus 64 usec of guard time, or 1.87 msec per frame (538 frames/second). Of course, actually composing the frame will take most of the time in a real program.
The way I have my code set up, I can use up to 8 pins on PORTD. However, changing the defines at the top of WS2811.cpp will change the selected port.
Alternatively, you could use another port to get more strings. Watch out for pin mux conflicts, though.
Here are your choices:
- PORTE: 15 total: PTE0-PTE5, PTE20-PTE25, PTE29-PTE31
- PORTD: 8 total: PTD0-PTD7
- PORTC: 16 total: PTC0-PTC13, PTC16-17
- PORTB: 16 total: PTB0-PTB11, PTB16-19
- PORTA: 15 total: PTA0-PTA5, PTA12-PTA20
Here is how the DMA channels are interleaved:
The way I have it set up to generate the three phases of the required waveform is this:
I have timer TPM0 set up to generate events at overflow (OVF), at 250 nsec (CH0), and at 650 nsec (CH1). At 1250 nsec it resets to 0.
At timer count = 0, DMA0 fires, because it's triggered by TPM0's overflow (OVF) event. This results in the data lines being driven to a constant "1" level, as the data that DMA0 is programmed to transfer is a single, all-1's word. (This is the easiest way to explain what is happening; this is the way I'd wanted it to work, but I had to use as much precious RAM as for the RGB data to hold 1's to get it to work).
At 250 nsec, DMA1 fires, because it's triggered by TPM0's CH0 compare event. This drives either a 0 or 1 level to the pins, because DMA1 is programmed to transfer our data bytes to the pins.
At 650 nsec, DMA2 fires, because it's triggered by TPM0's CH1 compare event. This results in the data lines being driven to a constant "0" level, as the data that DMA2 is programmed to transfer is a single, all-0's word.
At 1250 nsec, the timer resets to 0, and the whole cycle repeats.
Because this library uses three of timer TPM0's six channels (and sets TPM0 to 800kHz), you will need to select TPM1 or TPM2 output pins if you want to use PwmOut pins in your program (for instance, for RC servos, which want a 50Hz frequency). If you just want to change discrete LED brightnesses, you can use TPM0's CH3, CH4, or CH5 pins. Just make sure that you set up your PwmOut instance at the same frequency.
Here is a table showing the assignment of timer resources to PwmOut capable pins in the FRDM-KL25Z:
KL25Z pin | Arduino name | Timer | Channel |
---|---|---|---|
PTA3 | TPM0 | CH0 | |
PTC1 | A5 | TPM0 | CH0 |
PTD0 | D10 | TPM0 | CH0 |
PTE24 | TPM0 | CH0 | |
PTA4 | D4 | TPM0 | CH1 |
PTC2 | A4 | TPM0 | CH1 |
PTD1 | D13/LED_BLUE | TPM0 | CH1 |
PTE25 | TPM0 | CH1 | |
PTA5 | D5 | TPM0 | CH2 |
PTC3 | TPM0 | CH2 | |
PTD2 | D11 | TPM0 | CH2 |
PTE29 | TPM0 | CH2 | |
PTC4 | TPM0 | CH3 | |
PTD3 | D12 | TPM0 | CH3 |
PTE30 | TPM0 | CH3 | |
PTC8 | D6 | TPM0 | CH4 |
PTD4 | D2 | TPM0 | CH4 |
PTE31 | TPM0 | CH4 | |
PTA0 | TPM0 | CH5 | |
PTC9 | D7 | TPM0 | CH5 |
PTD5 | D9 | TPM0 | CH5 |
PTE26 | TPM0 | CH5 | |
PTA12 | D3 | TPM1 | CH0 |
PTB0 | A0 | TPM1 | CH0 |
PTE20 | TPM1 | CH0 | |
PTA13 | D8 | TPM1 | CH1 |
PTB1 | A1 | TPM1 | CH1 |
PTE21 | TPM1 | CH1 | |
PTA1 | D0/USBRX | TPM2 | CH0 |
PTB18 | LED_RED | TPM2 | CH0 |
PTB2 | A2 | TPM2 | CH0 |
PTE22 | TPM2 | CH0 | |
PTA2 | D1/USBTX | TPM2 | CH1 |
PTB19 | LED_GREEN | TPM2 | CH1 |
PTB3 | A3 | TPM2 | CH1 |
PTE23 | TPM2 | CH1 |
Changes
Revision | Date | Who | Commit message |
---|---|---|---|
5:2c3b76ea0b40 | 2015-06-13 | Ned Konz |
Worked out flapping and color animation |
4:990838718b51 | 2015-06-12 | Ned Konz |
Dummy out wait_for_dma_done |
3:df4319053bfa | 2015-06-12 | Ned Konz |
Trying to get dma_done to work |
2:9447404f2d16 | 2015-06-11 | bikeNomad | fixed offline compilation by adding struct |
1:86a910560879 | 2015-06-11 | bikeNomad | changed WS2811 to template class to allow for lower memory usage. |
0:a8535703f23b | 2014-01-04 | bikeNomad | Initial revision of library. |