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:55:14 2014 +0000
Revision:
4:586d20c99dbf
Parent:
3:2b5b03a3c0a5
Child:
5:7e40afd8d533
Documentation

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 4:586d20c99dbf 26 //! Maximum number of LEDs per strip
Tomo2k 2:1c2c9c8788a8 27 #define MAX_LEDS_PER_STRIP 240
bikeNomad 0:a8535703f23b 28
bikeNomad 0:a8535703f23b 29 extern "C" void DMA0_IRQHandler();
bikeNomad 0:a8535703f23b 30 extern "C" void TPM0_IRQHandler();
bikeNomad 0:a8535703f23b 31
Tomo2k 3:2b5b03a3c0a5 32 /**
Tomo2k 3:2b5b03a3c0a5 33 * WS2811/n
Tomo2k 3:2b5b03a3c0a5 34 * LED Strip controller
Tomo2k 3:2b5b03a3c0a5 35 */
bikeNomad 0:a8535703f23b 36 class WS2811 : public LedStrip
bikeNomad 0:a8535703f23b 37 {
bikeNomad 0:a8535703f23b 38 public:
Tomo2k 3:2b5b03a3c0a5 39 /** Set up the LED strip
Tomo2k 3:2b5b03a3c0a5 40 @param n Number of LEDs on the strip. Must be less than MAX_LEDS_PER_STRIP
Tomo2k 3:2b5b03a3c0a5 41 @param pinNumber Pin number on PORTD. 0-7.
Tomo2k 3:2b5b03a3c0a5 42 */
bikeNomad 0:a8535703f23b 43 WS2811(unsigned n, unsigned pinNumber);
bikeNomad 0:a8535703f23b 44
bikeNomad 0:a8535703f23b 45 virtual void begin();
bikeNomad 0:a8535703f23b 46 virtual void show();
bikeNomad 0:a8535703f23b 47 virtual void blank();
bikeNomad 0:a8535703f23b 48
Tomo2k 3:2b5b03a3c0a5 49 /** Send a level update to all the WS2811 LED strips
Tomo2k 3:2b5b03a3c0a5 50 * All updates happen in parallel, ensure all (max. 8) strips have complete data before calling this.
Tomo2k 3:2b5b03a3c0a5 51 */
bikeNomad 0:a8535703f23b 52 static void startDMA();
bikeNomad 0:a8535703f23b 53
bikeNomad 0:a8535703f23b 54 private:
bikeNomad 0:a8535703f23b 55 uint32_t pinMask;
bikeNomad 0:a8535703f23b 56
bikeNomad 0:a8535703f23b 57 void writePixel(unsigned n, uint8_t *p);
bikeNomad 0:a8535703f23b 58
bikeNomad 0:a8535703f23b 59 // Class Static:
bikeNomad 0:a8535703f23b 60
bikeNomad 0:a8535703f23b 61 static bool initialized;
bikeNomad 0:a8535703f23b 62 static uint32_t enabledPins;
bikeNomad 0:a8535703f23b 63 static volatile bool dma_done;
bikeNomad 0:a8535703f23b 64 static void wait_for_dma_done() { while (!dma_done) __WFI(); }
bikeNomad 0:a8535703f23b 65
Tomo2k 2:1c2c9c8788a8 66 static void writeByte(uint8_t byte, uint32_t mask, uint8_t *dest);
bikeNomad 0:a8535703f23b 67
bikeNomad 0:a8535703f23b 68 static void hw_init();
bikeNomad 0:a8535703f23b 69 static void io_init();
bikeNomad 0:a8535703f23b 70 static void clock_init();
bikeNomad 0:a8535703f23b 71 static void dma_init();
bikeNomad 0:a8535703f23b 72 static void tpm_init();
bikeNomad 0:a8535703f23b 73 static void dma_data_init();
bikeNomad 0:a8535703f23b 74
bikeNomad 0:a8535703f23b 75 friend void TPM0_IRQHandler();
bikeNomad 0:a8535703f23b 76 };
bikeNomad 0:a8535703f23b 77
bikeNomad 0:a8535703f23b 78 #endif
bikeNomad 0:a8535703f23b 79