ws2812.c 8.6 KB

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