]> git.donarmstrong.com Git - qmk_firmware.git/blob - drivers/avr/ws2812.c
Usbasploader bootloader option addition (#6304)
[qmk_firmware.git] / drivers / avr / 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 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "ws2812.h"
25 #include <avr/interrupt.h>
26 #include <avr/io.h>
27 #include <util/delay.h>
28 #include "debug.h"
29
30 #if !defined(LED_ARRAY) && defined(RGB_MATRIX_ENABLE)
31 // LED color buffer
32 LED_TYPE led[DRIVER_LED_TOTAL];
33   #define LED_ARRAY led
34 #endif
35
36 #ifdef RGBW_BB_TWI
37
38 // Port for the I2C
39 #define I2C_DDR DDRD
40 #define I2C_PIN PIND
41 #define I2C_PORT PORTD
42
43 // Pins to be used in the bit banging
44 #define I2C_CLK 0
45 #define I2C_DAT 1
46
47 #define I2C_DATA_HI()\
48 I2C_DDR &= ~ (1 << I2C_DAT);\
49 I2C_PORT |= (1 << I2C_DAT);
50 #define I2C_DATA_LO()\
51 I2C_DDR |= (1 << I2C_DAT);\
52 I2C_PORT &= ~ (1 << I2C_DAT);
53
54 #define I2C_CLOCK_HI()\
55 I2C_DDR &= ~ (1 << I2C_CLK);\
56 I2C_PORT |= (1 << I2C_CLK);
57 #define I2C_CLOCK_LO()\
58 I2C_DDR |= (1 << I2C_CLK);\
59 I2C_PORT &= ~ (1 << I2C_CLK);
60
61 #define I2C_DELAY 1
62
63 void I2C_WriteBit(unsigned char c)
64 {
65     if (c > 0)
66     {
67         I2C_DATA_HI();
68     }
69     else
70     {
71         I2C_DATA_LO();
72     }
73
74     I2C_CLOCK_HI();
75     _delay_us(I2C_DELAY);
76
77     I2C_CLOCK_LO();
78     _delay_us(I2C_DELAY);
79
80     if (c > 0)
81     {
82         I2C_DATA_LO();
83     }
84
85     _delay_us(I2C_DELAY);
86 }
87
88 // Inits bitbanging port, must be called before using the functions below
89 //
90 void I2C_Init(void)
91 {
92     I2C_PORT &= ~ ((1 << I2C_DAT) | (1 << I2C_CLK));
93
94     I2C_CLOCK_HI();
95     I2C_DATA_HI();
96
97     _delay_us(I2C_DELAY);
98 }
99
100 // Send a START Condition
101 //
102 void I2C_Start(void)
103 {
104     // set both to high at the same time
105     I2C_DDR &= ~ ((1 << I2C_DAT) | (1 << I2C_CLK));
106     _delay_us(I2C_DELAY);
107
108     I2C_DATA_LO();
109     _delay_us(I2C_DELAY);
110
111     I2C_CLOCK_LO();
112     _delay_us(I2C_DELAY);
113 }
114
115 // Send a STOP Condition
116 //
117 void I2C_Stop(void)
118 {
119     I2C_CLOCK_HI();
120     _delay_us(I2C_DELAY);
121
122     I2C_DATA_HI();
123     _delay_us(I2C_DELAY);
124 }
125
126 // write a byte to the I2C slave device
127 //
128 unsigned char I2C_Write(unsigned char c)
129 {
130     for (char i = 0; i < 8; i++)
131     {
132         I2C_WriteBit(c & 128);
133
134         c <<= 1;
135     }
136
137
138     I2C_WriteBit(0);
139     _delay_us(I2C_DELAY);
140     _delay_us(I2C_DELAY);
141
142     // _delay_us(I2C_DELAY);
143     //return I2C_ReadBit();
144     return 0;
145 }
146
147
148 #endif
149
150 #ifdef RGB_MATRIX_ENABLE
151 // Set an led in the buffer to a color
152 void inline ws2812_setled(int i, uint8_t r, uint8_t g, uint8_t b)
153 {
154     led[i].r = r;
155     led[i].g = g;
156     led[i].b = b;
157 }
158
159 void ws2812_setled_all  (uint8_t r, uint8_t g, uint8_t b)
160 {
161   for (int i = 0; i < sizeof(led)/sizeof(led[0]); i++) {
162     led[i].r = r;
163     led[i].g = g;
164     led[i].b = b;
165   }
166 }
167 #endif
168
169 // Setleds for standard RGB
170 void inline ws2812_setleds(LED_TYPE *ledarray, uint16_t leds)
171 {
172    // ws2812_setleds_pin(ledarray,leds, _BV(ws2812_pin));
173    ws2812_setleds_pin(ledarray,leds, _BV(RGB_DI_PIN & 0xF));
174 }
175
176 void inline ws2812_setleds_pin(LED_TYPE *ledarray, uint16_t leds, uint8_t pinmask)
177 {
178   // ws2812_DDRREG |= pinmask; // Enable DDR
179   // new universal format (DDR)
180   _SFR_IO8((RGB_DI_PIN >> 4) + 1) |= pinmask;
181
182   ws2812_sendarray_mask((uint8_t*)ledarray,leds+leds+leds,pinmask);
183   _delay_us(50);
184 }
185
186 // Setleds for SK6812RGBW
187 void inline ws2812_setleds_rgbw(LED_TYPE *ledarray, uint16_t leds)
188 {
189
190   #ifdef RGBW_BB_TWI
191     uint8_t sreg_prev, twcr_prev;
192     sreg_prev=SREG;
193     twcr_prev=TWCR;
194     cli();
195     TWCR &= ~(1<<TWEN);
196     I2C_Init();
197     I2C_Start();
198     I2C_Write(0x84);
199     uint16_t datlen = leds<<2;
200     uint8_t curbyte;
201     uint8_t * data = (uint8_t*)ledarray;
202     while (datlen--) {
203       curbyte=*data++;
204       I2C_Write(curbyte);
205     }
206     I2C_Stop();
207     SREG=sreg_prev;
208     TWCR=twcr_prev;
209   #endif
210
211
212   // ws2812_DDRREG |= _BV(ws2812_pin); // Enable DDR
213   // new universal format (DDR)
214   _SFR_IO8((RGB_DI_PIN >> 4) + 1) |= _BV(RGB_DI_PIN & 0xF);
215
216   ws2812_sendarray_mask((uint8_t*)ledarray,leds<<2,_BV(RGB_DI_PIN & 0xF));
217
218
219   #ifndef RGBW_BB_TWI
220     _delay_us(80);
221   #endif
222 }
223
224 void ws2812_sendarray(uint8_t *data,uint16_t datlen)
225 {
226   ws2812_sendarray_mask(data,datlen,_BV(RGB_DI_PIN & 0xF));
227 }
228
229 /*
230   This routine writes an array of bytes with RGB values to the Dataout pin
231   using the fast 800kHz clockless WS2811/2812 protocol.
232 */
233
234 // Timing in ns
235 #define w_zeropulse   350
236 #define w_onepulse    900
237 #define w_totalperiod 1250
238
239 // Fixed cycles used by the inner loop
240 #define w_fixedlow    2
241 #define w_fixedhigh   4
242 #define w_fixedtotal  8
243
244 // Insert NOPs to match the timing, if possible
245 #define w_zerocycles    (((F_CPU/1000)*w_zeropulse          )/1000000)
246 #define w_onecycles     (((F_CPU/1000)*w_onepulse    +500000)/1000000)
247 #define w_totalcycles   (((F_CPU/1000)*w_totalperiod +500000)/1000000)
248
249 // w1 - nops between rising edge and falling edge - low
250 #define w1 (w_zerocycles-w_fixedlow)
251 // w2   nops between fe low and fe high
252 #define w2 (w_onecycles-w_fixedhigh-w1)
253 // w3   nops to complete loop
254 #define w3 (w_totalcycles-w_fixedtotal-w1-w2)
255
256 #if w1>0
257   #define w1_nops w1
258 #else
259   #define w1_nops  0
260 #endif
261
262 // The only critical timing parameter is the minimum pulse length of the "0"
263 // Warn or throw error if this timing can not be met with current F_CPU settings.
264 #define w_lowtime ((w1_nops+w_fixedlow)*1000000)/(F_CPU/1000)
265 #if w_lowtime>550
266    #error "Light_ws2812: Sorry, the clock speed is too low. Did you set F_CPU correctly?"
267 #elif w_lowtime>450
268    #warning "Light_ws2812: The timing is critical and may only work on WS2812B, not on WS2812(S)."
269    #warning "Please consider a higher clockspeed, if possible"
270 #endif
271
272 #if w2>0
273 #define w2_nops w2
274 #else
275 #define w2_nops  0
276 #endif
277
278 #if w3>0
279 #define w3_nops w3
280 #else
281 #define w3_nops  0
282 #endif
283
284 #define w_nop1  "nop      \n\t"
285 #define w_nop2  "rjmp .+0 \n\t"
286 #define w_nop4  w_nop2 w_nop2
287 #define w_nop8  w_nop4 w_nop4
288 #define w_nop16 w_nop8 w_nop8
289
290 void inline ws2812_sendarray_mask(uint8_t *data,uint16_t datlen,uint8_t maskhi)
291 {
292   uint8_t curbyte,ctr,masklo;
293   uint8_t sreg_prev;
294
295   // masklo  =~maskhi&ws2812_PORTREG;
296   // maskhi |=        ws2812_PORTREG;
297   masklo  =~maskhi&_SFR_IO8((RGB_DI_PIN >> 4) + 2);
298   maskhi |=        _SFR_IO8((RGB_DI_PIN >> 4) + 2);
299   sreg_prev=SREG;
300   cli();
301
302   while (datlen--) {
303     curbyte=(*data++);
304
305     asm volatile(
306     "       ldi   %0,8  \n\t"
307     "loop%=:            \n\t"
308     "       out   %2,%3 \n\t"    //  '1' [01] '0' [01] - re
309 #if (w1_nops&1)
310 w_nop1
311 #endif
312 #if (w1_nops&2)
313 w_nop2
314 #endif
315 #if (w1_nops&4)
316 w_nop4
317 #endif
318 #if (w1_nops&8)
319 w_nop8
320 #endif
321 #if (w1_nops&16)
322 w_nop16
323 #endif
324     "       sbrs  %1,7  \n\t"    //  '1' [03] '0' [02]
325     "       out   %2,%4 \n\t"    //  '1' [--] '0' [03] - fe-low
326     "       lsl   %1    \n\t"    //  '1' [04] '0' [04]
327 #if (w2_nops&1)
328   w_nop1
329 #endif
330 #if (w2_nops&2)
331   w_nop2
332 #endif
333 #if (w2_nops&4)
334   w_nop4
335 #endif
336 #if (w2_nops&8)
337   w_nop8
338 #endif
339 #if (w2_nops&16)
340   w_nop16
341 #endif
342     "       out   %2,%4 \n\t"    //  '1' [+1] '0' [+1] - fe-high
343 #if (w3_nops&1)
344 w_nop1
345 #endif
346 #if (w3_nops&2)
347 w_nop2
348 #endif
349 #if (w3_nops&4)
350 w_nop4
351 #endif
352 #if (w3_nops&8)
353 w_nop8
354 #endif
355 #if (w3_nops&16)
356 w_nop16
357 #endif
358
359     "       dec   %0    \n\t"    //  '1' [+2] '0' [+2]
360     "       brne  loop%=\n\t"    //  '1' [+3] '0' [+4]
361     :   "=&d" (ctr)
362     :   "r" (curbyte), "I" (_SFR_IO_ADDR(_SFR_IO8((RGB_DI_PIN >> 4) + 2))), "r" (maskhi), "r" (masklo)
363     );
364   }
365
366   SREG=sreg_prev;
367 }