2 * light weight WS2812 lib V2.0b
4 * Controls WS2811/WS2812/WS2812B RGB-LEDs
5 * Author: Tim (cpldcpu@gmail.com)
7 * Jan 18th, 2014 v2.0b Initial Version
8 * Nov 29th, 2015 v2.3 Added SK6812RGBW support
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.
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.
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/>.
25 #include <avr/interrupt.h>
27 #include <util/delay.h>
30 #if !defined(LED_ARRAY) && defined(RGB_MATRIX_ENABLE)
32 LED_TYPE led[DRIVER_LED_TOTAL];
41 #define I2C_PORT PORTD
43 // Pins to be used in the bit banging
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);
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);
63 void I2C_WriteBit(unsigned char c)
88 // Inits bitbanging port, must be called before using the functions below
92 I2C_PORT &= ~ ((1 << I2C_DAT) | (1 << I2C_CLK));
100 // Send a START Condition
104 // set both to high at the same time
105 I2C_DDR &= ~ ((1 << I2C_DAT) | (1 << I2C_CLK));
106 _delay_us(I2C_DELAY);
109 _delay_us(I2C_DELAY);
112 _delay_us(I2C_DELAY);
115 // Send a STOP Condition
120 _delay_us(I2C_DELAY);
123 _delay_us(I2C_DELAY);
126 // write a byte to the I2C slave device
128 unsigned char I2C_Write(unsigned char c)
130 for (char i = 0; i < 8; i++)
132 I2C_WriteBit(c & 128);
139 _delay_us(I2C_DELAY);
140 _delay_us(I2C_DELAY);
142 // _delay_us(I2C_DELAY);
143 //return I2C_ReadBit();
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)
159 void ws2812_setled_all (uint8_t r, uint8_t g, uint8_t b)
161 for (int i = 0; i < RGBLED_NUM; i++) {
169 // Setleds for standard RGB
170 void inline ws2812_setleds(LED_TYPE *ledarray, uint16_t leds)
172 // ws2812_setleds_pin(ledarray,leds, _BV(ws2812_pin));
173 ws2812_setleds_pin(ledarray,leds, _BV(RGB_DI_PIN & 0xF));
176 void inline ws2812_setleds_pin(LED_TYPE *ledarray, uint16_t leds, uint8_t pinmask)
178 // ws2812_DDRREG |= pinmask; // Enable DDR
179 // new universal format (DDR)
180 _SFR_IO8((RGB_DI_PIN >> 4) + 1) |= pinmask;
182 ws2812_sendarray_mask((uint8_t*)ledarray,leds+leds+leds,pinmask);
186 // Setleds for SK6812RGBW
187 void inline ws2812_setleds_rgbw(LED_TYPE *ledarray, uint16_t leds)
191 uint8_t sreg_prev, twcr_prev;
199 uint16_t datlen = leds<<2;
201 uint8_t * data = (uint8_t*)ledarray;
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);
216 ws2812_sendarray_mask((uint8_t*)ledarray,leds<<2,_BV(RGB_DI_PIN & 0xF));
224 void ws2812_sendarray(uint8_t *data,uint16_t datlen)
226 ws2812_sendarray_mask(data,datlen,_BV(RGB_DI_PIN & 0xF));
230 This routine writes an array of bytes with RGB values to the Dataout pin
231 using the fast 800kHz clockless WS2811/2812 protocol.
235 #define w_zeropulse 350
236 #define w_onepulse 900
237 #define w_totalperiod 1250
239 // Fixed cycles used by the inner loop
241 #define w_fixedhigh 4
242 #define w_fixedtotal 8
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)
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)
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)
266 #error "Light_ws2812: Sorry, the clock speed is too low. Did you set F_CPU correctly?"
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"
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
290 void inline ws2812_sendarray_mask(uint8_t *data,uint16_t datlen,uint8_t maskhi)
292 uint8_t curbyte,ctr,masklo;
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);
308 " out %2,%3 \n\t" // '1' [01] '0' [01] - re
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]
342 " out %2,%4 \n\t" // '1' [+1] '0' [+1] - fe-high
359 " dec %0 \n\t" // '1' [+2] '0' [+2]
360 " brne loop%=\n\t" // '1' [+3] '0' [+4]
362 : "r" (curbyte), "I" (_SFR_IO_ADDR(_SFR_IO8((RGB_DI_PIN >> 4) + 2))), "r" (maskhi), "r" (masklo)