ws2812.c 8.1 KB

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