light_ws2812.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. #include "light_ws2812.h"
  13. #include <avr/interrupt.h>
  14. #include <avr/io.h>
  15. #include <util/delay.h>
  16. #include "debug.h"
  17. #define RGBW_BB_TWI 1
  18. #ifdef RGBW_BB_TWI
  19. // Port for the I2C
  20. #define I2C_DDR DDRD
  21. #define I2C_PIN PIND
  22. #define I2C_PORT PORTD
  23. // Pins to be used in the bit banging
  24. #define I2C_CLK 0
  25. #define I2C_DAT 1
  26. #define I2C_DATA_HI()\
  27. I2C_DDR &= ~ (1 << I2C_DAT);\
  28. I2C_PORT |= (1 << I2C_DAT);
  29. #define I2C_DATA_LO()\
  30. I2C_DDR |= (1 << I2C_DAT);\
  31. I2C_PORT &= ~ (1 << I2C_DAT);
  32. #define I2C_CLOCK_HI()\
  33. I2C_DDR &= ~ (1 << I2C_CLK);\
  34. I2C_PORT |= (1 << I2C_CLK);
  35. #define I2C_CLOCK_LO()\
  36. I2C_DDR |= (1 << I2C_CLK);\
  37. I2C_PORT &= ~ (1 << I2C_CLK);
  38. #define I2C_DELAY 1
  39. void I2C_WriteBit(unsigned char c)
  40. {
  41. if (c > 0)
  42. {
  43. I2C_DATA_HI();
  44. }
  45. else
  46. {
  47. I2C_DATA_LO();
  48. }
  49. I2C_CLOCK_HI();
  50. _delay_us(I2C_DELAY);
  51. I2C_CLOCK_LO();
  52. _delay_us(I2C_DELAY);
  53. if (c > 0)
  54. {
  55. I2C_DATA_LO();
  56. }
  57. _delay_us(I2C_DELAY);
  58. }
  59. // Inits bitbanging port, must be called before using the functions below
  60. //
  61. void I2C_Init()
  62. {
  63. I2C_PORT &= ~ ((1 << I2C_DAT) | (1 << I2C_CLK));
  64. I2C_CLOCK_HI();
  65. I2C_DATA_HI();
  66. _delay_us(I2C_DELAY);
  67. }
  68. // Send a START Condition
  69. //
  70. void I2C_Start()
  71. {
  72. // set both to high at the same time
  73. I2C_DDR &= ~ ((1 << I2C_DAT) | (1 << I2C_CLK));
  74. _delay_us(I2C_DELAY);
  75. I2C_DATA_LO();
  76. _delay_us(I2C_DELAY);
  77. I2C_CLOCK_LO();
  78. _delay_us(I2C_DELAY);
  79. }
  80. // Send a STOP Condition
  81. //
  82. void I2C_Stop()
  83. {
  84. I2C_CLOCK_HI();
  85. _delay_us(I2C_DELAY);
  86. I2C_DATA_HI();
  87. _delay_us(I2C_DELAY);
  88. }
  89. // write a byte to the I2C slave device
  90. //
  91. unsigned char I2C_Write(unsigned char c)
  92. {
  93. for (char i = 0; i < 8; i++)
  94. {
  95. I2C_WriteBit(c & 128);
  96. c <<= 1;
  97. }
  98. I2C_WriteBit(0);
  99. _delay_us(I2C_DELAY);
  100. _delay_us(I2C_DELAY);
  101. // _delay_us(I2C_DELAY);
  102. //return I2C_ReadBit();
  103. return 0;
  104. }
  105. #endif
  106. // Setleds for standard RGB
  107. void inline ws2812_setleds(struct cRGB *ledarray, uint16_t leds)
  108. {
  109. // ws2812_setleds_pin(ledarray,leds, _BV(ws2812_pin));
  110. ws2812_setleds_pin(ledarray,leds, _BV(RGB_DI_PIN & 0xF));
  111. }
  112. void inline ws2812_setleds_pin(struct cRGB *ledarray, uint16_t leds, uint8_t pinmask)
  113. {
  114. // ws2812_DDRREG |= pinmask; // Enable DDR
  115. // new universal format (DDR)
  116. _SFR_IO8((RGB_DI_PIN >> 4) + 1) |= pinmask;
  117. ws2812_sendarray_mask((uint8_t*)ledarray,leds+leds+leds,pinmask);
  118. _delay_us(50);
  119. }
  120. // Setleds for SK6812RGBW
  121. void inline ws2812_setleds_rgbw(struct cRGBW *ledarray, uint16_t leds)
  122. {
  123. #ifdef RGBW_BB_TWI
  124. cli();
  125. TWCR = 0;
  126. I2C_Init();
  127. I2C_Start();
  128. I2C_Write(0x84);
  129. uint16_t datlen = leds<<2;
  130. uint8_t curbyte;
  131. uint8_t * data = (uint8_t*)ledarray;
  132. while (datlen--) {
  133. curbyte=*data++;
  134. I2C_Write(curbyte);
  135. }
  136. I2C_Stop();
  137. sei();
  138. #else
  139. _delay_us(80);
  140. #endif
  141. // ws2812_DDRREG |= _BV(ws2812_pin); // Enable DDR
  142. // new universal format (DDR)
  143. _SFR_IO8((RGB_DI_PIN >> 4) + 1) |= _BV(RGB_DI_PIN & 0xF);
  144. ws2812_sendarray_mask((uint8_t*)ledarray,leds<<2,_BV(RGB_DI_PIN & 0xF));
  145. }
  146. void ws2812_sendarray(uint8_t *data,uint16_t datlen)
  147. {
  148. ws2812_sendarray_mask(data,datlen,_BV(RGB_DI_PIN & 0xF));
  149. }
  150. /*
  151. This routine writes an array of bytes with RGB values to the Dataout pin
  152. using the fast 800kHz clockless WS2811/2812 protocol.
  153. */
  154. // Timing in ns
  155. #define w_zeropulse 350
  156. #define w_onepulse 900
  157. #define w_totalperiod 1250
  158. // Fixed cycles used by the inner loop
  159. #define w_fixedlow 2
  160. #define w_fixedhigh 4
  161. #define w_fixedtotal 8
  162. // Insert NOPs to match the timing, if possible
  163. #define w_zerocycles (((F_CPU/1000)*w_zeropulse )/1000000)
  164. #define w_onecycles (((F_CPU/1000)*w_onepulse +500000)/1000000)
  165. #define w_totalcycles (((F_CPU/1000)*w_totalperiod +500000)/1000000)
  166. // w1 - nops between rising edge and falling edge - low
  167. #define w1 (w_zerocycles-w_fixedlow)
  168. // w2 nops between fe low and fe high
  169. #define w2 (w_onecycles-w_fixedhigh-w1)
  170. // w3 nops to complete loop
  171. #define w3 (w_totalcycles-w_fixedtotal-w1-w2)
  172. #if w1>0
  173. #define w1_nops w1
  174. #else
  175. #define w1_nops 0
  176. #endif
  177. // The only critical timing parameter is the minimum pulse length of the "0"
  178. // Warn or throw error if this timing can not be met with current F_CPU settings.
  179. #define w_lowtime ((w1_nops+w_fixedlow)*1000000)/(F_CPU/1000)
  180. #if w_lowtime>550
  181. #error "Light_ws2812: Sorry, the clock speed is too low. Did you set F_CPU correctly?"
  182. #elif w_lowtime>450
  183. #warning "Light_ws2812: The timing is critical and may only work on WS2812B, not on WS2812(S)."
  184. #warning "Please consider a higher clockspeed, if possible"
  185. #endif
  186. #if w2>0
  187. #define w2_nops w2
  188. #else
  189. #define w2_nops 0
  190. #endif
  191. #if w3>0
  192. #define w3_nops w3
  193. #else
  194. #define w3_nops 0
  195. #endif
  196. #define w_nop1 "nop \n\t"
  197. #define w_nop2 "rjmp .+0 \n\t"
  198. #define w_nop4 w_nop2 w_nop2
  199. #define w_nop8 w_nop4 w_nop4
  200. #define w_nop16 w_nop8 w_nop8
  201. void inline ws2812_sendarray_mask(uint8_t *data,uint16_t datlen,uint8_t maskhi)
  202. {
  203. uint8_t curbyte,ctr,masklo;
  204. uint8_t sreg_prev;
  205. // masklo =~maskhi&ws2812_PORTREG;
  206. // maskhi |= ws2812_PORTREG;
  207. masklo =~maskhi&_SFR_IO8((RGB_DI_PIN >> 4) + 2);
  208. maskhi |= _SFR_IO8((RGB_DI_PIN >> 4) + 2);
  209. sreg_prev=SREG;
  210. cli();
  211. while (datlen--) {
  212. curbyte=(*data++);
  213. asm volatile(
  214. " ldi %0,8 \n\t"
  215. "loop%=: \n\t"
  216. " out %2,%3 \n\t" // '1' [01] '0' [01] - re
  217. #if (w1_nops&1)
  218. w_nop1
  219. #endif
  220. #if (w1_nops&2)
  221. w_nop2
  222. #endif
  223. #if (w1_nops&4)
  224. w_nop4
  225. #endif
  226. #if (w1_nops&8)
  227. w_nop8
  228. #endif
  229. #if (w1_nops&16)
  230. w_nop16
  231. #endif
  232. " sbrs %1,7 \n\t" // '1' [03] '0' [02]
  233. " out %2,%4 \n\t" // '1' [--] '0' [03] - fe-low
  234. " lsl %1 \n\t" // '1' [04] '0' [04]
  235. #if (w2_nops&1)
  236. w_nop1
  237. #endif
  238. #if (w2_nops&2)
  239. w_nop2
  240. #endif
  241. #if (w2_nops&4)
  242. w_nop4
  243. #endif
  244. #if (w2_nops&8)
  245. w_nop8
  246. #endif
  247. #if (w2_nops&16)
  248. w_nop16
  249. #endif
  250. " out %2,%4 \n\t" // '1' [+1] '0' [+1] - fe-high
  251. #if (w3_nops&1)
  252. w_nop1
  253. #endif
  254. #if (w3_nops&2)
  255. w_nop2
  256. #endif
  257. #if (w3_nops&4)
  258. w_nop4
  259. #endif
  260. #if (w3_nops&8)
  261. w_nop8
  262. #endif
  263. #if (w3_nops&16)
  264. w_nop16
  265. #endif
  266. " dec %0 \n\t" // '1' [+2] '0' [+2]
  267. " brne loop%=\n\t" // '1' [+3] '0' [+4]
  268. : "=&d" (ctr)
  269. : "r" (curbyte), "I" (_SFR_IO_ADDR(_SFR_IO8((RGB_DI_PIN >> 4) + 2))), "r" (maskhi), "r" (masklo)
  270. );
  271. }
  272. SREG=sreg_prev;
  273. }