Optimised fork of bikeNomad's WS2811 LED control library. Supports KL25Z and KL46Z
Fork of Multi_WS2811 by
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)
WS2811.h@6:3b5b8a367f40, 2014-04-02 (annotated)
- Committer:
- Tomo2k
- Date:
- Wed Apr 02 12:16:42 2014 +0000
- Revision:
- 6:3b5b8a367f40
- Parent:
- 5:7e40afd8d533
- Child:
- 7:58623ad7f310
Updated documentation to include RAM usage
Who changed what in which revision?
User | Revision | Line number | New 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 | 5:7e40afd8d533 | 33 | * WS2811/WS2812/WS2812B |
Tomo2k | 5:7e40afd8d533 | 34 | * LED Strip controller\n |
Tomo2k | 5:7e40afd8d533 | 35 | * For FRDM-KL25Z and FRDM-KL46Z |
Tomo2k | 6:3b5b8a367f40 | 36 | |
Tomo2k | 6:3b5b8a367f40 | 37 | RAM usage:\n |
Tomo2k | 6:3b5b8a367f40 | 38 | Per individual LED: 3 bytes (malloc'd) for RGB data |
Tomo2k | 6:3b5b8a367f40 | 39 | |
Tomo2k | 6:3b5b8a367f40 | 40 | Strip length per LED:\n |
Tomo2k | 6:3b5b8a367f40 | 41 | 24 bytes (static) for bit data\n |
Tomo2k | 6:3b5b8a367f40 | 42 | + 24 bytes (static) for ones data\n |
Tomo2k | 6:3b5b8a367f40 | 43 | = 48 bytes |
Tomo2k | 6:3b5b8a367f40 | 44 | |
Tomo2k | 6:3b5b8a367f40 | 45 | 240 LEDs max per string = 11,520 bytes static |
Tomo2k | 6:3b5b8a367f40 | 46 | |
Tomo2k | 6:3b5b8a367f40 | 47 | One string:\n |
Tomo2k | 6:3b5b8a367f40 | 48 | 240 LEDs : 11520 + 240*3 = 12,240 bytes |
Tomo2k | 6:3b5b8a367f40 | 49 | |
Tomo2k | 6:3b5b8a367f40 | 50 | Eight strings:\n |
Tomo2k | 6:3b5b8a367f40 | 51 | 240*8 LEDs: 11520 + (240*3) * 8 = 17,280 bytes |
Tomo2k | 3:2b5b03a3c0a5 | 52 | */ |
bikeNomad | 0:a8535703f23b | 53 | class WS2811 : public LedStrip |
bikeNomad | 0:a8535703f23b | 54 | { |
bikeNomad | 0:a8535703f23b | 55 | public: |
Tomo2k | 3:2b5b03a3c0a5 | 56 | /** Set up the LED strip |
Tomo2k | 3:2b5b03a3c0a5 | 57 | @param n Number of LEDs on the strip. Must be less than MAX_LEDS_PER_STRIP |
Tomo2k | 3:2b5b03a3c0a5 | 58 | @param pinNumber Pin number on PORTD. 0-7. |
Tomo2k | 3:2b5b03a3c0a5 | 59 | */ |
bikeNomad | 0:a8535703f23b | 60 | WS2811(unsigned n, unsigned pinNumber); |
bikeNomad | 0:a8535703f23b | 61 | |
bikeNomad | 0:a8535703f23b | 62 | virtual void begin(); |
bikeNomad | 0:a8535703f23b | 63 | virtual void show(); |
bikeNomad | 0:a8535703f23b | 64 | virtual void blank(); |
bikeNomad | 0:a8535703f23b | 65 | |
Tomo2k | 3:2b5b03a3c0a5 | 66 | /** Send a level update to all the WS2811 LED strips |
Tomo2k | 3:2b5b03a3c0a5 | 67 | * All updates happen in parallel, ensure all (max. 8) strips have complete data before calling this. |
Tomo2k | 3:2b5b03a3c0a5 | 68 | */ |
bikeNomad | 0:a8535703f23b | 69 | static void startDMA(); |
bikeNomad | 0:a8535703f23b | 70 | |
bikeNomad | 0:a8535703f23b | 71 | private: |
bikeNomad | 0:a8535703f23b | 72 | uint32_t pinMask; |
bikeNomad | 0:a8535703f23b | 73 | |
bikeNomad | 0:a8535703f23b | 74 | void writePixel(unsigned n, uint8_t *p); |
bikeNomad | 0:a8535703f23b | 75 | |
bikeNomad | 0:a8535703f23b | 76 | // Class Static: |
bikeNomad | 0:a8535703f23b | 77 | |
bikeNomad | 0:a8535703f23b | 78 | static bool initialized; |
bikeNomad | 0:a8535703f23b | 79 | static uint32_t enabledPins; |
bikeNomad | 0:a8535703f23b | 80 | static volatile bool dma_done; |
bikeNomad | 0:a8535703f23b | 81 | static void wait_for_dma_done() { while (!dma_done) __WFI(); } |
bikeNomad | 0:a8535703f23b | 82 | |
Tomo2k | 2:1c2c9c8788a8 | 83 | static void writeByte(uint8_t byte, uint32_t mask, uint8_t *dest); |
bikeNomad | 0:a8535703f23b | 84 | |
bikeNomad | 0:a8535703f23b | 85 | static void hw_init(); |
bikeNomad | 0:a8535703f23b | 86 | static void io_init(); |
bikeNomad | 0:a8535703f23b | 87 | static void clock_init(); |
bikeNomad | 0:a8535703f23b | 88 | static void dma_init(); |
bikeNomad | 0:a8535703f23b | 89 | static void tpm_init(); |
bikeNomad | 0:a8535703f23b | 90 | static void dma_data_init(); |
bikeNomad | 0:a8535703f23b | 91 | |
bikeNomad | 0:a8535703f23b | 92 | friend void TPM0_IRQHandler(); |
bikeNomad | 0:a8535703f23b | 93 | }; |
bikeNomad | 0:a8535703f23b | 94 | |
bikeNomad | 0:a8535703f23b | 95 | #endif |
bikeNomad | 0:a8535703f23b | 96 |