light_ws2812.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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(LED_TYPE *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(LED_TYPE *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(LED_TYPE *ledarray, uint16_t leds)
  122. {
  123. #ifdef RGBW_BB_TWI
  124. uint8_t sreg_prev, twcr_prev;
  125. sreg_prev=SREG;
  126. twcr_prev=TWCR;
  127. cli();
  128. TWCR &= ~(1<<TWEN);
  129. I2C_Init();
  130. I2C_Start();
  131. I2C_Write(0x84);
  132. uint16_t datlen = leds<<2;
  133. uint8_t curbyte;
  134. uint8_t * data = (uint8_t*)ledarray;
  135. while (datlen--) {
  136. curbyte=*data++;
  137. I2C_Write(curbyte);
  138. }
  139. I2C_Stop();
  140. SREG=sreg_prev;
  141. TWCR=twcr_prev;
  142. #endif
  143. // ws2812_DDRREG |= _BV(ws2812_pin); // Enable DDR
  144. // new universal format (DDR)
  145. _SFR_IO8((RGB_DI_PIN >> 4) + 1) |= _BV(RGB_DI_PIN & 0xF);
  146. ws2812_sendarray_mask((uint8_t*)ledarray,leds<<2,_BV(RGB_DI_PIN & 0xF));
  147. #ifndef RGBW_BB_TWI
  148. _delay_us(80);
  149. #endif
  150. }
  151. void ws2812_sendarray(uint8_t *data,uint16_t datlen)
  152. {
  153. ws2812_sendarray_mask(data,datlen,_BV(RGB_DI_PIN & 0xF));
  154. }
  155. /*
  156. This routine writes an array of bytes with RGB values to the Dataout pin
  157. using the fast 800kHz clockless WS2811/2812 protocol.
  158. */
  159. // Timing in ns
  160. #define w_zeropulse 350
  161. #define w_onepulse 900
  162. #define w_totalperiod 1250
  163. // Fixed cycles used by the inner loop
  164. #define w_fixedlow 2
  165. #define w_fixedhigh 4
  166. #define w_fixedtotal 8
  167. // Insert NOPs to match the timing, if possible
  168. #define w_zerocycles (((F_CPU/1000)*w_zeropulse )/1000000)
  169. #define w_onecycles (((F_CPU/1000)*w_onepulse +500000)/1000000)
  170. #define w_totalcycles (((F_CPU/1000)*w_totalperiod +500000)/1000000)
  171. // w1 - nops between rising edge and falling edge - low
  172. #define w1 (w_zerocycles-w_fixedlow)
  173. // w2 nops between fe low and fe high
  174. #define w2 (w_onecycles-w_fixedhigh-w1)
  175. // w3 nops to complete loop
  176. #define w3 (w_totalcycles-w_fixedtotal-w1-w2)
  177. #if w1>0
  178. #define w1_nops w1
  179. #else
  180. #define w1_nops 0
  181. #endif
  182. // The only critical timing parameter is the minimum pulse length of the "0"
  183. // Warn or throw error if this timing can not be met with current F_CPU settings.
  184. #define w_lowtime ((w1_nops+w_fixedlow)*1000000)/(F_CPU/1000)
  185. #if w_lowtime>550
  186. #error "Light_ws2812: Sorry, the clock speed is too low. Did you set F_CPU correctly?"
  187. #elif w_lowtime>450
  188. #warning "Light_ws2812: The timing is critical and may only work on WS2812B, not on WS2812(S)."
  189. #warning "Please consider a higher clockspeed, if possible"
  190. #endif
  191. #if w2>0
  192. #define w2_nops w2
  193. #else
  194. #define w2_nops 0
  195. #endif
  196. #if w3>0
  197. #define w3_nops w3
  198. #else
  199. #define w3_nops 0
  200. #endif
  201. #define w_nop1 "nop \n\t"
  202. #define w_nop2 "rjmp .+0 \n\t"
  203. #define w_nop4 w_nop2 w_nop2
  204. #define w_nop8 w_nop4 w_nop4
  205. #define w_nop16 w_nop8 w_nop8
  206. void inline ws2812_sendarray_mask(uint8_t *data,uint16_t datlen,uint8_t maskhi)
  207. {
  208. uint8_t curbyte,ctr,masklo;
  209. uint8_t sreg_prev;
  210. // masklo =~maskhi&ws2812_PORTREG;
  211. // maskhi |= ws2812_PORTREG;
  212. masklo =~maskhi&_SFR_IO8((RGB_DI_PIN >> 4) + 2);
  213. maskhi |= _SFR_IO8((RGB_DI_PIN >> 4) + 2);
  214. sreg_prev=SREG;
  215. cli();
  216. while (datlen--) {
  217. curbyte=(*data++);
  218. asm volatile(
  219. " ldi %0,8 \n\t"
  220. "loop%=: \n\t"
  221. " out %2,%3 \n\t" // '1' [01] '0' [01] - re
  222. #if (w1_nops&1)
  223. w_nop1
  224. #endif
  225. #if (w1_nops&2)
  226. w_nop2
  227. #endif
  228. #if (w1_nops&4)
  229. w_nop4
  230. #endif
  231. #if (w1_nops&8)
  232. w_nop8
  233. #endif
  234. #if (w1_nops&16)
  235. w_nop16
  236. #endif
  237. " sbrs %1,7 \n\t" // '1' [03] '0' [02]
  238. " out %2,%4 \n\t" // '1' [--] '0' [03] - fe-low
  239. " lsl %1 \n\t" // '1' [04] '0' [04]
  240. #if (w2_nops&1)
  241. w_nop1
  242. #endif
  243. #if (w2_nops&2)
  244. w_nop2
  245. #endif
  246. #if (w2_nops&4)
  247. w_nop4
  248. #endif
  249. #if (w2_nops&8)
  250. w_nop8
  251. #endif
  252. #if (w2_nops&16)
  253. w_nop16
  254. #endif
  255. " out %2,%4 \n\t" // '1' [+1] '0' [+1] - fe-high
  256. #if (w3_nops&1)
  257. w_nop1
  258. #endif
  259. #if (w3_nops&2)
  260. w_nop2
  261. #endif
  262. #if (w3_nops&4)
  263. w_nop4
  264. #endif
  265. #if (w3_nops&8)
  266. w_nop8
  267. #endif
  268. #if (w3_nops&16)
  269. w_nop16
  270. #endif
  271. " dec %0 \n\t" // '1' [+2] '0' [+2]
  272. " brne loop%=\n\t" // '1' [+3] '0' [+4]
  273. : "=&d" (ctr)
  274. : "r" (curbyte), "I" (_SFR_IO_ADDR(_SFR_IO8((RGB_DI_PIN >> 4) + 2))), "r" (maskhi), "r" (masklo)
  275. );
  276. }
  277. SREG=sreg_prev;
  278. }