light_ws2812.c 6.6 KB

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