Optimised fork of bikeNomad's WS2811 LED control library. Supports KL25Z and KL46Z

Dependents:   CubicHand

Fork of Multi_WS2811 by Ned Konz

Optimised to use far less RAM than the original.

Capable of running up to 8 strings of 240 LEDs each with plenty of RAM to spare on the KL46Z.

Should run at least three strings of 240 LEDs on the KL25Z (RAM limited)

Committer:
Tomo2k
Date:
Wed Apr 02 11:54:27 2014 +0000
Revision:
3:2b5b03a3c0a5
Parent:
2:1c2c9c8788a8
Child:
4:586d20c99dbf
Documentation updated

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bikeNomad 0:a8535703f23b 1 // Mbed library to control WS2801-based RGB LED Strips
bikeNomad 0:a8535703f23b 2 // some portions (c) 2011 Jelmer Tiete
bikeNomad 0:a8535703f23b 3 // This library is ported from the Arduino implementation of Adafruit Industries
bikeNomad 0:a8535703f23b 4 // found at: http://github.com/adafruit/LPD8806
bikeNomad 0:a8535703f23b 5 // and their strips: http://www.adafruit.com/products/306
bikeNomad 0:a8535703f23b 6 // Released under the MIT License: http://mbed.org/license/mit
bikeNomad 0:a8535703f23b 7 //
bikeNomad 0:a8535703f23b 8 /*****************************************************************************/
bikeNomad 0:a8535703f23b 9
bikeNomad 0:a8535703f23b 10 // Heavily modified by Jas Strong, 2012-10-04
bikeNomad 0:a8535703f23b 11 // Changed to use a virtual base class and to use software SPI.
bikeNomad 0:a8535703f23b 12 //
bikeNomad 0:a8535703f23b 13 // Modified by Ned Konz, December 2013.
bikeNomad 0:a8535703f23b 14 // Using three-phase DMA ala Paul Stoffegren's version.
Tomo2k 3:2b5b03a3c0a5 15 //
Tomo2k 3:2b5b03a3c0a5 16 // Modified by richard Thompson, Marhc 2014.
Tomo2k 3:2b5b03a3c0a5 17 // Uses 8-bit DMA transfers instead of 32-bit, uses 1/4 of the RAM.
Tomo2k 3:2b5b03a3c0a5 18 // Now capable of running 240 LEDs on one pin
bikeNomad 0:a8535703f23b 19
bikeNomad 0:a8535703f23b 20 #ifndef MBED_WS2811_H
bikeNomad 0:a8535703f23b 21 #define MBED_WS2811_H
bikeNomad 0:a8535703f23b 22
bikeNomad 0:a8535703f23b 23 #include "mbed.h"
bikeNomad 0:a8535703f23b 24 #include "LedStrip.h"
bikeNomad 0:a8535703f23b 25
Tomo2k 2:1c2c9c8788a8 26 #define MAX_LEDS_PER_STRIP 240
bikeNomad 0:a8535703f23b 27
bikeNomad 0:a8535703f23b 28 extern "C" void DMA0_IRQHandler();
bikeNomad 0:a8535703f23b 29 extern "C" void TPM0_IRQHandler();
bikeNomad 0:a8535703f23b 30
Tomo2k 3:2b5b03a3c0a5 31 /**
Tomo2k 3:2b5b03a3c0a5 32 * WS2811/n
Tomo2k 3:2b5b03a3c0a5 33 * LED Strip controller
Tomo2k 3:2b5b03a3c0a5 34 */
bikeNomad 0:a8535703f23b 35 class WS2811 : public LedStrip
bikeNomad 0:a8535703f23b 36 {
bikeNomad 0:a8535703f23b 37 public:
Tomo2k 3:2b5b03a3c0a5 38 /** Set up the LED strip
Tomo2k 3:2b5b03a3c0a5 39 @param n Number of LEDs on the strip. Must be less than MAX_LEDS_PER_STRIP
Tomo2k 3:2b5b03a3c0a5 40 @param pinNumber Pin number on PORTD. 0-7.
Tomo2k 3:2b5b03a3c0a5 41 */
bikeNomad 0:a8535703f23b 42 WS2811(unsigned n, unsigned pinNumber);
bikeNomad 0:a8535703f23b 43
bikeNomad 0:a8535703f23b 44 virtual void begin();
bikeNomad 0:a8535703f23b 45 virtual void show();
bikeNomad 0:a8535703f23b 46 virtual void blank();
bikeNomad 0:a8535703f23b 47
Tomo2k 3:2b5b03a3c0a5 48 /** Send a level update to all the WS2811 LED strips
Tomo2k 3:2b5b03a3c0a5 49 * All updates happen in parallel, ensure all (max. 8) strips have complete data before calling this.
Tomo2k 3:2b5b03a3c0a5 50 */
bikeNomad 0:a8535703f23b 51 static void startDMA();
bikeNomad 0:a8535703f23b 52
bikeNomad 0:a8535703f23b 53 private:
bikeNomad 0:a8535703f23b 54 uint32_t pinMask;
bikeNomad 0:a8535703f23b 55
bikeNomad 0:a8535703f23b 56 void writePixel(unsigned n, uint8_t *p);
bikeNomad 0:a8535703f23b 57
bikeNomad 0:a8535703f23b 58 // Class Static:
bikeNomad 0:a8535703f23b 59
bikeNomad 0:a8535703f23b 60 static bool initialized;
bikeNomad 0:a8535703f23b 61 static uint32_t enabledPins;
bikeNomad 0:a8535703f23b 62 static volatile bool dma_done;
bikeNomad 0:a8535703f23b 63 static void wait_for_dma_done() { while (!dma_done) __WFI(); }
bikeNomad 0:a8535703f23b 64
Tomo2k 2:1c2c9c8788a8 65 static void writeByte(uint8_t byte, uint32_t mask, uint8_t *dest);
bikeNomad 0:a8535703f23b 66
bikeNomad 0:a8535703f23b 67 static void hw_init();
bikeNomad 0:a8535703f23b 68 static void io_init();
bikeNomad 0:a8535703f23b 69 static void clock_init();
bikeNomad 0:a8535703f23b 70 static void dma_init();
bikeNomad 0:a8535703f23b 71 static void tpm_init();
bikeNomad 0:a8535703f23b 72 static void dma_data_init();
bikeNomad 0:a8535703f23b 73
bikeNomad 0:a8535703f23b 74 friend void TPM0_IRQHandler();
bikeNomad 0:a8535703f23b 75 };
bikeNomad 0:a8535703f23b 76
bikeNomad 0:a8535703f23b 77 #endif
bikeNomad 0:a8535703f23b 78