light_ws2812.c 7.2 KB

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