ws2812.c 7.7 KB

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