see: http://mbed.org/users/okini3939/notebook/led_strip/

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LEDStrip_WS2812.cpp Source File

LEDStrip_WS2812.cpp

00001 /*
00002  * WS2812 tape led IC
00003  *
00004  *          0.35us   0.8us    (+-150ns)
00005  *  0:     |^^^^^|__________|
00006  *
00007  *             0.7us   0.6us  (+-150ns)
00008  *  1:     |^^^^^^^^^^|_____|
00009  *
00010  *               >50us
00011  *  RESET: |________________|
00012  */
00013 #include "mbed.h"
00014 #include "LEDStrip.h"
00015 
00016 SPI tape(p11, p12, p13);
00017 
00018 #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
00019     LPC_SSP_TypeDef *_ssp = LPC_SSP0;
00020 #elif defined(TARGET_LPC11U24)
00021     LPC_SSPx_Type *_ssp = LPC_SSP1;
00022 #endif
00023 
00024 int num = 100;
00025 int *data;
00026 volatile int busy = 0, wakeup = 0;
00027 
00028 
00029 extern "C"
00030 #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
00031 void SSP0_IRQHandler() {
00032 #elif defined(TARGET_LPC11U24)
00033 void SSP1_IRQHandler() {
00034 #endif
00035     static int addr = 0, bit = 0x800000;
00036 repeat:
00037     if (busy) {
00038         // led data
00039       while (_ssp->SR & (1<<1)) { // TNF
00040         if (data[addr] & bit) {
00041             // 1
00042             _ssp->DR = 0x01f;
00043         } else {
00044             // 0
00045             _ssp->DR = 0x07f;
00046         }
00047         bit = bit >> 1;
00048         if (bit == 0) {
00049             bit = 0x800000;
00050             addr ++;
00051             if (addr >= num) {
00052                 addr = 0;
00053                 busy = 0;
00054                 goto repeat;
00055             }
00056         }
00057       }
00058     } else {
00059         // blank
00060         while (_ssp->SR & (1<<1)) { // TNF
00061             _ssp->DR = 0xfff;
00062             if (addr < 50) {
00063                 addr ++;
00064             } else {
00065                 addr = 0;
00066                 if (wakeup) {
00067                     busy = 1;
00068                     wakeup = 0;
00069                     goto repeat;
00070                 }
00071             }
00072         }
00073     }
00074 }
00075 
00076 void tapeInit (int freq, int n) {
00077 
00078     num = n;
00079 //    data = new int(num);
00080     data = (int*)malloc(sizeof(int) * num);
00081     for (int i = 0; i < num; i ++) {
00082         data[i] = 0;
00083     }
00084 
00085     tape.format(10, 1);
00086     if (freq) {
00087         tape.frequency(freq * 1000);
00088     } else {
00089         tape.frequency(8000000);
00090     }
00091 #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
00092     NVIC_SetVector(SSP0_IRQn, (uint32_t)SSP0_IRQHandler);
00093     NVIC_SetPriority(SSP0_IRQn, 0);
00094     NVIC_EnableIRQ(SSP0_IRQn);
00095 #elif defined(TARGET_LPC11U24)
00096     NVIC_SetVector(SSP1_IRQn, (uint32_t)SSP1_IRQHandler);
00097     NVIC_SetPriority(SSP1_IRQn, 0);
00098     NVIC_EnableIRQ(SSP1_IRQn);
00099 #endif
00100     _ssp->IMSC |= (1<<3); // TXIM
00101 }
00102 
00103 void tapeSet (int n, int dat) {
00104     if (n >= 0 && n < num) {
00105         // RGB -> GRB
00106         data[n] = ((dat & 0xff0000) >> 8) | ((dat & 0xff00) << 8) | (dat & 0xff);
00107     }
00108 }
00109 
00110 void tapeSend () {
00111     if (busy) {
00112         while (busy);
00113         wait_us(50);
00114     }
00115     wakeup = 1;
00116     while (wakeup);
00117 }
00118 
00119 int tapeGet (int n) {
00120     return ((data[n] & 0xff0000) >> 8) | ((data[n] & 0xff00) << 8) | (data[n] & 0xff);
00121 }