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@5:7e40afd8d533, 2014-04-02 (annotated)
- Committer:
- Tomo2k
- Date:
- Wed Apr 02 11:57:56 2014 +0000
- Revision:
- 5:7e40afd8d533
- Parent:
- 4:586d20c99dbf
- Child:
- 6:3b5b8a367f40
Documentation tweaks
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 | 3:2b5b03a3c0a5 | 36 | */ |
bikeNomad | 0:a8535703f23b | 37 | class WS2811 : public LedStrip |
bikeNomad | 0:a8535703f23b | 38 | { |
bikeNomad | 0:a8535703f23b | 39 | public: |
Tomo2k | 3:2b5b03a3c0a5 | 40 | /** Set up the LED strip |
Tomo2k | 3:2b5b03a3c0a5 | 41 | @param n Number of LEDs on the strip. Must be less than MAX_LEDS_PER_STRIP |
Tomo2k | 3:2b5b03a3c0a5 | 42 | @param pinNumber Pin number on PORTD. 0-7. |
Tomo2k | 3:2b5b03a3c0a5 | 43 | */ |
bikeNomad | 0:a8535703f23b | 44 | WS2811(unsigned n, unsigned pinNumber); |
bikeNomad | 0:a8535703f23b | 45 | |
bikeNomad | 0:a8535703f23b | 46 | virtual void begin(); |
bikeNomad | 0:a8535703f23b | 47 | virtual void show(); |
bikeNomad | 0:a8535703f23b | 48 | virtual void blank(); |
bikeNomad | 0:a8535703f23b | 49 | |
Tomo2k | 3:2b5b03a3c0a5 | 50 | /** Send a level update to all the WS2811 LED strips |
Tomo2k | 3:2b5b03a3c0a5 | 51 | * All updates happen in parallel, ensure all (max. 8) strips have complete data before calling this. |
Tomo2k | 3:2b5b03a3c0a5 | 52 | */ |
bikeNomad | 0:a8535703f23b | 53 | static void startDMA(); |
bikeNomad | 0:a8535703f23b | 54 | |
bikeNomad | 0:a8535703f23b | 55 | private: |
bikeNomad | 0:a8535703f23b | 56 | uint32_t pinMask; |
bikeNomad | 0:a8535703f23b | 57 | |
bikeNomad | 0:a8535703f23b | 58 | void writePixel(unsigned n, uint8_t *p); |
bikeNomad | 0:a8535703f23b | 59 | |
bikeNomad | 0:a8535703f23b | 60 | // Class Static: |
bikeNomad | 0:a8535703f23b | 61 | |
bikeNomad | 0:a8535703f23b | 62 | static bool initialized; |
bikeNomad | 0:a8535703f23b | 63 | static uint32_t enabledPins; |
bikeNomad | 0:a8535703f23b | 64 | static volatile bool dma_done; |
bikeNomad | 0:a8535703f23b | 65 | static void wait_for_dma_done() { while (!dma_done) __WFI(); } |
bikeNomad | 0:a8535703f23b | 66 | |
Tomo2k | 2:1c2c9c8788a8 | 67 | static void writeByte(uint8_t byte, uint32_t mask, uint8_t *dest); |
bikeNomad | 0:a8535703f23b | 68 | |
bikeNomad | 0:a8535703f23b | 69 | static void hw_init(); |
bikeNomad | 0:a8535703f23b | 70 | static void io_init(); |
bikeNomad | 0:a8535703f23b | 71 | static void clock_init(); |
bikeNomad | 0:a8535703f23b | 72 | static void dma_init(); |
bikeNomad | 0:a8535703f23b | 73 | static void tpm_init(); |
bikeNomad | 0:a8535703f23b | 74 | static void dma_data_init(); |
bikeNomad | 0:a8535703f23b | 75 | |
bikeNomad | 0:a8535703f23b | 76 | friend void TPM0_IRQHandler(); |
bikeNomad | 0:a8535703f23b | 77 | }; |
bikeNomad | 0:a8535703f23b | 78 | |
bikeNomad | 0:a8535703f23b | 79 | #endif |
bikeNomad | 0:a8535703f23b | 80 |