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.

Dependents:   Multi_WS2811_test

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:

/media/uploads/bikeNomad/ws2812.png

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 pinArduino nameTimerChannel
PTA3TPM0CH0
PTC1A5TPM0CH0
PTD0D10TPM0CH0
PTE24TPM0CH0
PTA4D4TPM0CH1
PTC2A4TPM0CH1
PTD1D13/LED_BLUETPM0CH1
PTE25TPM0CH1
PTA5D5TPM0CH2
PTC3TPM0CH2
PTD2D11TPM0CH2
PTE29TPM0CH2
PTC4TPM0CH3
PTD3D12TPM0CH3
PTE30TPM0CH3
PTC8D6TPM0CH4
PTD4D2TPM0CH4
PTE31TPM0CH4
PTA0TPM0CH5
PTC9D7TPM0CH5
PTD5D9TPM0CH5
PTE26TPM0CH5
PTA12D3TPM1CH0
PTB0A0TPM1CH0
PTE20TPM1CH0
PTA13D8TPM1CH1
PTB1A1TPM1CH1
PTE21TPM1CH1
PTA1D0/USBRXTPM2CH0
PTB18LED_REDTPM2CH0
PTB2A2TPM2CH0
PTE22TPM2CH0
PTA2D1/USBTXTPM2CH1
PTB19LED_GREENTPM2CH1
PTB3A3TPM2CH1
PTE23TPM2CH1
Committer:
Ned Konz
Date:
Sat Jun 13 00:18:32 2015 -0700
Revision:
5:2c3b76ea0b40
Parent:
1:86a910560879
Worked out flapping and color animation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bikeNomad 0:a8535703f23b 1 #include <math.h>
bikeNomad 0:a8535703f23b 2 #include "Colors.h"
bikeNomad 0:a8535703f23b 3
bikeNomad 0:a8535703f23b 4 void HSBtoRGB(float hue, float saturation, float brightness, uint8_t *pr, uint8_t *pg, uint8_t *pb)
bikeNomad 0:a8535703f23b 5 {
bikeNomad 0:a8535703f23b 6 uint8_t r = 0, g = 0, b = 0;
bikeNomad 0:a8535703f23b 7 if (saturation == 0) {
bikeNomad 0:a8535703f23b 8 r = g = b = (uint8_t) (brightness * 255.0f + 0.5f);
bikeNomad 0:a8535703f23b 9 } else {
bikeNomad 0:a8535703f23b 10 float h = (hue - (float)floor(hue)) * 6.0f;
bikeNomad 0:a8535703f23b 11 float f = h - (float)floor(h);
bikeNomad 0:a8535703f23b 12 float p = brightness * (1.0f - saturation);
bikeNomad 0:a8535703f23b 13 float q = brightness * (1.0f - saturation * f);
bikeNomad 0:a8535703f23b 14 float t = brightness * (1.0f - (saturation * (1.0f - f)));
bikeNomad 0:a8535703f23b 15 switch ((int) h) {
bikeNomad 0:a8535703f23b 16 case 0:
bikeNomad 0:a8535703f23b 17 r = (int) (brightness * 255.0f + 0.5f);
bikeNomad 0:a8535703f23b 18 g = (int) (t * 255.0f + 0.5f);
bikeNomad 0:a8535703f23b 19 b = (int) (p * 255.0f + 0.5f);
bikeNomad 0:a8535703f23b 20 break;
bikeNomad 0:a8535703f23b 21 case 1:
bikeNomad 0:a8535703f23b 22 r = (int) (q * 255.0f + 0.5f);
bikeNomad 0:a8535703f23b 23 g = (int) (brightness * 255.0f + 0.5f);
bikeNomad 0:a8535703f23b 24 b = (int) (p * 255.0f + 0.5f);
bikeNomad 0:a8535703f23b 25 break;
bikeNomad 0:a8535703f23b 26 case 2:
bikeNomad 0:a8535703f23b 27 r = (int) (p * 255.0f + 0.5f);
bikeNomad 0:a8535703f23b 28 g = (int) (brightness * 255.0f + 0.5f);
bikeNomad 0:a8535703f23b 29 b = (int) (t * 255.0f + 0.5f);
bikeNomad 0:a8535703f23b 30 break;
bikeNomad 0:a8535703f23b 31 case 3:
bikeNomad 0:a8535703f23b 32 r = (int) (p * 255.0f + 0.5f);
bikeNomad 0:a8535703f23b 33 g = (int) (q * 255.0f + 0.5f);
bikeNomad 0:a8535703f23b 34 b = (int) (brightness * 255.0f + 0.5f);
bikeNomad 0:a8535703f23b 35 break;
bikeNomad 0:a8535703f23b 36 case 4:
bikeNomad 0:a8535703f23b 37 r = (int) (t * 255.0f + 0.5f);
bikeNomad 0:a8535703f23b 38 g = (int) (p * 255.0f + 0.5f);
bikeNomad 0:a8535703f23b 39 b = (int) (brightness * 255.0f + 0.5f);
bikeNomad 0:a8535703f23b 40 break;
bikeNomad 0:a8535703f23b 41 case 5:
bikeNomad 0:a8535703f23b 42 r = (int) (brightness * 255.0f + 0.5f);
bikeNomad 0:a8535703f23b 43 g = (int) (p * 255.0f + 0.5f);
bikeNomad 0:a8535703f23b 44 b = (int) (q * 255.0f + 0.5f);
bikeNomad 0:a8535703f23b 45 break;
bikeNomad 0:a8535703f23b 46 }
bikeNomad 0:a8535703f23b 47 }
bikeNomad 0:a8535703f23b 48 *pr = r;
bikeNomad 0:a8535703f23b 49 *pg = g;
bikeNomad 0:a8535703f23b 50 *pb = b;
bikeNomad 0:a8535703f23b 51 }
bikeNomad 0:a8535703f23b 52
bikeNomad 0:a8535703f23b 53 float* RGBtoHSB(uint8_t r, uint8_t g, uint8_t b, float* hsbvals)
bikeNomad 0:a8535703f23b 54 {
bikeNomad 0:a8535703f23b 55 float hue, saturation, brightness;
bikeNomad 0:a8535703f23b 56 if (!hsbvals) {
bikeNomad 0:a8535703f23b 57 hsbvals = new float[3];
bikeNomad 0:a8535703f23b 58 }
bikeNomad 0:a8535703f23b 59 uint8_t cmax = (r > g) ? r : g;
bikeNomad 0:a8535703f23b 60 if (b > cmax) cmax = b;
bikeNomad 0:a8535703f23b 61 uint8_t cmin = (r < g) ? r : g;
bikeNomad 0:a8535703f23b 62 if (b < cmin) cmin = b;
bikeNomad 0:a8535703f23b 63
bikeNomad 0:a8535703f23b 64 brightness = ((float) cmax) / 255.0f;
bikeNomad 0:a8535703f23b 65 if (cmax != 0)
bikeNomad 0:a8535703f23b 66 saturation = ((float) (cmax - cmin)) / ((float) cmax);
bikeNomad 0:a8535703f23b 67 else
bikeNomad 0:a8535703f23b 68 saturation = 0;
bikeNomad 0:a8535703f23b 69 if (saturation == 0)
bikeNomad 0:a8535703f23b 70 hue = 0;
bikeNomad 0:a8535703f23b 71 else {
bikeNomad 0:a8535703f23b 72 float redc = ((float) (cmax - r)) / ((float) (cmax - cmin));
bikeNomad 0:a8535703f23b 73 float greenc = ((float) (cmax - g)) / ((float) (cmax - cmin));
bikeNomad 0:a8535703f23b 74 float bluec = ((float) (cmax - b)) / ((float) (cmax - cmin));
bikeNomad 0:a8535703f23b 75 if (r == cmax)
bikeNomad 0:a8535703f23b 76 hue = bluec - greenc;
bikeNomad 0:a8535703f23b 77 else if (g == cmax)
bikeNomad 0:a8535703f23b 78 hue = 2.0f + redc - bluec;
bikeNomad 0:a8535703f23b 79 else
bikeNomad 0:a8535703f23b 80 hue = 4.0f + greenc - redc;
bikeNomad 0:a8535703f23b 81 hue = hue / 6.0f;
bikeNomad 0:a8535703f23b 82 if (hue < 0)
bikeNomad 0:a8535703f23b 83 hue = hue + 1.0f;
bikeNomad 0:a8535703f23b 84 }
bikeNomad 0:a8535703f23b 85 hsbvals[0] = hue;
bikeNomad 0:a8535703f23b 86 hsbvals[1] = saturation;
bikeNomad 0:a8535703f23b 87 hsbvals[2] = brightness;
bikeNomad 0:a8535703f23b 88 return hsbvals;
bikeNomad 0:a8535703f23b 89 }