light_ws2812.c 6.7 KB

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