]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/light_ws2812.c
Fixed NO_SUSPEND_POWER_DOWN handling
[qmk_firmware.git] / quantum / light_ws2812.c
1 /*
2 * light weight WS2812 lib V2.0b
3 *
4 * Controls WS2811/WS2812/WS2812B RGB-LEDs
5 * Author: Tim (cpldcpu@gmail.com)
6 *
7 * Jan 18th, 2014  v2.0b Initial Version
8 * Nov 29th, 2015  v2.3  Added SK6812RGBW support
9 *
10 * License: GNU GPL v2 (see License.txt)
11 */
12
13 #include "light_ws2812.h"
14 #include <avr/interrupt.h>
15 #include <avr/io.h>
16 #include <util/delay.h>
17 #include "debug.h"
18
19 // Setleds for standard RGB
20 void inline ws2812_setleds(struct cRGB *ledarray, uint16_t leds)
21 {
22    // ws2812_setleds_pin(ledarray,leds, _BV(ws2812_pin));
23    ws2812_setleds_pin(ledarray,leds, _BV(RGB_DI_PIN & 0xF));
24 }
25
26 void inline ws2812_setleds_pin(struct cRGB *ledarray, uint16_t leds, uint8_t pinmask)
27 {
28   // ws2812_DDRREG |= pinmask; // Enable DDR
29   // new universal format (DDR)
30   _SFR_IO8((RGB_DI_PIN >> 4) + 1) |= pinmask;
31
32   ws2812_sendarray_mask((uint8_t*)ledarray,leds+leds+leds,pinmask);
33   _delay_us(50);
34 }
35
36 // Setleds for SK6812RGBW
37 void inline ws2812_setleds_rgbw(struct cRGBW *ledarray, uint16_t leds)
38 {
39   // ws2812_DDRREG |= _BV(ws2812_pin); // Enable DDR
40   // new universal format (DDR)
41   _SFR_IO8((RGB_DI_PIN >> 4) + 1) |= _BV(RGB_DI_PIN & 0xF);
42
43   ws2812_sendarray_mask((uint8_t*)ledarray,leds<<2,_BV(RGB_DI_PIN & 0xF));
44   _delay_us(80);
45 }
46
47 void ws2812_sendarray(uint8_t *data,uint16_t datlen)
48 {
49   ws2812_sendarray_mask(data,datlen,_BV(RGB_DI_PIN & 0xF));
50 }
51
52 /*
53   This routine writes an array of bytes with RGB values to the Dataout pin
54   using the fast 800kHz clockless WS2811/2812 protocol.
55 */
56
57 // Timing in ns
58 #define w_zeropulse   350
59 #define w_onepulse    900
60 #define w_totalperiod 1250
61
62 // Fixed cycles used by the inner loop
63 #define w_fixedlow    2
64 #define w_fixedhigh   4
65 #define w_fixedtotal  8
66
67 // Insert NOPs to match the timing, if possible
68 #define w_zerocycles    (((F_CPU/1000)*w_zeropulse          )/1000000)
69 #define w_onecycles     (((F_CPU/1000)*w_onepulse    +500000)/1000000)
70 #define w_totalcycles   (((F_CPU/1000)*w_totalperiod +500000)/1000000)
71
72 // w1 - nops between rising edge and falling edge - low
73 #define w1 (w_zerocycles-w_fixedlow)
74 // w2   nops between fe low and fe high
75 #define w2 (w_onecycles-w_fixedhigh-w1)
76 // w3   nops to complete loop
77 #define w3 (w_totalcycles-w_fixedtotal-w1-w2)
78
79 #if w1>0
80   #define w1_nops w1
81 #else
82   #define w1_nops  0
83 #endif
84
85 // The only critical timing parameter is the minimum pulse length of the "0"
86 // Warn or throw error if this timing can not be met with current F_CPU settings.
87 #define w_lowtime ((w1_nops+w_fixedlow)*1000000)/(F_CPU/1000)
88 #if w_lowtime>550
89    #error "Light_ws2812: Sorry, the clock speed is too low. Did you set F_CPU correctly?"
90 #elif w_lowtime>450
91    #warning "Light_ws2812: The timing is critical and may only work on WS2812B, not on WS2812(S)."
92    #warning "Please consider a higher clockspeed, if possible"
93 #endif
94
95 #if w2>0
96 #define w2_nops w2
97 #else
98 #define w2_nops  0
99 #endif
100
101 #if w3>0
102 #define w3_nops w3
103 #else
104 #define w3_nops  0
105 #endif
106
107 #define w_nop1  "nop      \n\t"
108 #define w_nop2  "rjmp .+0 \n\t"
109 #define w_nop4  w_nop2 w_nop2
110 #define w_nop8  w_nop4 w_nop4
111 #define w_nop16 w_nop8 w_nop8
112
113 void inline ws2812_sendarray_mask(uint8_t *data,uint16_t datlen,uint8_t maskhi)
114 {
115   uint8_t curbyte,ctr,masklo;
116   uint8_t sreg_prev;
117
118   // masklo  =~maskhi&ws2812_PORTREG;
119   // maskhi |=        ws2812_PORTREG;
120   masklo  =~maskhi&_SFR_IO8((RGB_DI_PIN >> 4) + 2);
121   maskhi |=        _SFR_IO8((RGB_DI_PIN >> 4) + 2);
122   sreg_prev=SREG;
123   cli();
124
125   while (datlen--) {
126     curbyte=*data++;
127
128     asm volatile(
129     "       ldi   %0,8  \n\t"
130     "loop%=:            \n\t"
131     "       out   %2,%3 \n\t"    //  '1' [01] '0' [01] - re
132 #if (w1_nops&1)
133 w_nop1
134 #endif
135 #if (w1_nops&2)
136 w_nop2
137 #endif
138 #if (w1_nops&4)
139 w_nop4
140 #endif
141 #if (w1_nops&8)
142 w_nop8
143 #endif
144 #if (w1_nops&16)
145 w_nop16
146 #endif
147     "       sbrs  %1,7  \n\t"    //  '1' [03] '0' [02]
148     "       out   %2,%4 \n\t"    //  '1' [--] '0' [03] - fe-low
149     "       lsl   %1    \n\t"    //  '1' [04] '0' [04]
150 #if (w2_nops&1)
151   w_nop1
152 #endif
153 #if (w2_nops&2)
154   w_nop2
155 #endif
156 #if (w2_nops&4)
157   w_nop4
158 #endif
159 #if (w2_nops&8)
160   w_nop8
161 #endif
162 #if (w2_nops&16)
163   w_nop16
164 #endif
165     "       out   %2,%4 \n\t"    //  '1' [+1] '0' [+1] - fe-high
166 #if (w3_nops&1)
167 w_nop1
168 #endif
169 #if (w3_nops&2)
170 w_nop2
171 #endif
172 #if (w3_nops&4)
173 w_nop4
174 #endif
175 #if (w3_nops&8)
176 w_nop8
177 #endif
178 #if (w3_nops&16)
179 w_nop16
180 #endif
181
182     "       dec   %0    \n\t"    //  '1' [+2] '0' [+2]
183     "       brne  loop%=\n\t"    //  '1' [+3] '0' [+4]
184     :   "=&d" (ctr)
185     :   "r" (curbyte), "I" (_SFR_IO_ADDR(_SFR_IO8((RGB_DI_PIN >> 4) + 2))), "r" (maskhi), "r" (masklo)
186     );
187   }
188
189   SREG=sreg_prev;
190 }