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 * License: GNU GPL v2 (see License.txt)
13 #include "light_ws2812.h"
14 #include <avr/interrupt.h>
16 #include <util/delay.h>
26 #define I2C_PORT PORTD
28 // Pins to be used in the bit banging
32 #define I2C_DATA_HI()\
33 I2C_DDR &= ~ (1 << I2C_DAT);\
34 I2C_PORT |= (1 << I2C_DAT);
35 #define I2C_DATA_LO()\
36 I2C_DDR |= (1 << I2C_DAT);\
37 I2C_PORT &= ~ (1 << I2C_DAT);
39 #define I2C_CLOCK_HI()\
40 I2C_DDR &= ~ (1 << I2C_CLK);\
41 I2C_PORT |= (1 << I2C_CLK);
42 #define I2C_CLOCK_LO()\
43 I2C_DDR |= (1 << I2C_CLK);\
44 I2C_PORT &= ~ (1 << I2C_CLK);
48 void I2C_WriteBit(unsigned char c)
73 // Inits bitbanging port, must be called before using the functions below
77 I2C_PORT &= ~ ((1 << I2C_DAT) | (1 << I2C_CLK));
85 // Send a START Condition
89 // set both to high at the same time
90 I2C_DDR &= ~ ((1 << I2C_DAT) | (1 << I2C_CLK));
100 // Send a STOP Condition
105 _delay_us(I2C_DELAY);
108 _delay_us(I2C_DELAY);
111 // write a byte to the I2C slave device
113 unsigned char I2C_Write(unsigned char c)
115 for (char i = 0; i < 8; i++)
117 I2C_WriteBit(c & 128);
124 _delay_us(I2C_DELAY);
125 _delay_us(I2C_DELAY);
127 // _delay_us(I2C_DELAY);
128 //return I2C_ReadBit();
135 // Setleds for standard RGB
136 void inline ws2812_setleds(struct cRGB *ledarray, uint16_t leds)
138 // ws2812_setleds_pin(ledarray,leds, _BV(ws2812_pin));
139 ws2812_setleds_pin(ledarray,leds, _BV(RGB_DI_PIN & 0xF));
142 void inline ws2812_setleds_pin(struct cRGB *ledarray, uint16_t leds, uint8_t pinmask)
144 // ws2812_DDRREG |= pinmask; // Enable DDR
145 // new universal format (DDR)
146 _SFR_IO8((RGB_DI_PIN >> 4) + 1) |= pinmask;
148 ws2812_sendarray_mask((uint8_t*)ledarray,leds+leds+leds,pinmask);
152 // Setleds for SK6812RGBW
153 void inline ws2812_setleds_rgbw(struct cRGBW *ledarray, uint16_t leds)
155 // ws2812_DDRREG |= _BV(ws2812_pin); // Enable DDR
156 // new universal format (DDR)
157 _SFR_IO8((RGB_DI_PIN >> 4) + 1) |= _BV(RGB_DI_PIN & 0xF);
159 ws2812_sendarray_mask((uint8_t*)ledarray,leds<<2,_BV(RGB_DI_PIN & 0xF));
167 uint16_t datlen = leds<<2;
169 uint8_t * data = (uint8_t*)ledarray;
172 I2C_Write(curbyte % 0x10);
182 void ws2812_sendarray(uint8_t *data,uint16_t datlen)
184 ws2812_sendarray_mask(data,datlen,_BV(RGB_DI_PIN & 0xF));
188 This routine writes an array of bytes with RGB values to the Dataout pin
189 using the fast 800kHz clockless WS2811/2812 protocol.
193 #define w_zeropulse 350
194 #define w_onepulse 900
195 #define w_totalperiod 1250
197 // Fixed cycles used by the inner loop
199 #define w_fixedhigh 4
200 #define w_fixedtotal 8
202 // Insert NOPs to match the timing, if possible
203 #define w_zerocycles (((F_CPU/1000)*w_zeropulse )/1000000)
204 #define w_onecycles (((F_CPU/1000)*w_onepulse +500000)/1000000)
205 #define w_totalcycles (((F_CPU/1000)*w_totalperiod +500000)/1000000)
207 // w1 - nops between rising edge and falling edge - low
208 #define w1 (w_zerocycles-w_fixedlow)
209 // w2 nops between fe low and fe high
210 #define w2 (w_onecycles-w_fixedhigh-w1)
211 // w3 nops to complete loop
212 #define w3 (w_totalcycles-w_fixedtotal-w1-w2)
220 // The only critical timing parameter is the minimum pulse length of the "0"
221 // Warn or throw error if this timing can not be met with current F_CPU settings.
222 #define w_lowtime ((w1_nops+w_fixedlow)*1000000)/(F_CPU/1000)
224 #error "Light_ws2812: Sorry, the clock speed is too low. Did you set F_CPU correctly?"
226 #warning "Light_ws2812: The timing is critical and may only work on WS2812B, not on WS2812(S)."
227 #warning "Please consider a higher clockspeed, if possible"
242 #define w_nop1 "nop \n\t"
243 #define w_nop2 "rjmp .+0 \n\t"
244 #define w_nop4 w_nop2 w_nop2
245 #define w_nop8 w_nop4 w_nop4
246 #define w_nop16 w_nop8 w_nop8
248 void inline ws2812_sendarray_mask(uint8_t *data,uint16_t datlen,uint8_t maskhi)
250 uint8_t curbyte,ctr,masklo;
253 // masklo =~maskhi&ws2812_PORTREG;
254 // maskhi |= ws2812_PORTREG;
255 masklo =~maskhi&_SFR_IO8((RGB_DI_PIN >> 4) + 2);
256 maskhi |= _SFR_IO8((RGB_DI_PIN >> 4) + 2);
261 curbyte=(*data++) % 0x10;
266 " out %2,%3 \n\t" // '1' [01] '0' [01] - re
282 " sbrs %1,7 \n\t" // '1' [03] '0' [02]
283 " out %2,%4 \n\t" // '1' [--] '0' [03] - fe-low
284 " lsl %1 \n\t" // '1' [04] '0' [04]
300 " out %2,%4 \n\t" // '1' [+1] '0' [+1] - fe-high
317 " dec %0 \n\t" // '1' [+2] '0' [+2]
318 " brne loop%=\n\t" // '1' [+3] '0' [+4]
320 : "r" (curbyte), "I" (_SFR_IO_ADDR(_SFR_IO8((RGB_DI_PIN >> 4) + 2))), "r" (maskhi), "r" (masklo)