ws2812.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. // ws2812_DDRREG |= pinmask; // Enable DDR
  120. // new universal format (DDR)
  121. _SFR_IO8((RGB_DI_PIN >> 4) + 1) |= pinmask;
  122. ws2812_sendarray_mask((uint8_t *)ledarray, leds + leds + leds, pinmask);
  123. _delay_us(50);
  124. }
  125. // Setleds for SK6812RGBW
  126. void inline ws2812_setleds_rgbw(LED_TYPE *ledarray, uint16_t leds) {
  127. #ifdef RGBW_BB_TWI
  128. uint8_t sreg_prev, twcr_prev;
  129. sreg_prev = SREG;
  130. twcr_prev = TWCR;
  131. cli();
  132. TWCR &= ~(1 << TWEN);
  133. I2C_Init();
  134. I2C_Start();
  135. I2C_Write(0x84);
  136. uint16_t datlen = leds << 2;
  137. uint8_t curbyte;
  138. uint8_t *data = (uint8_t *)ledarray;
  139. while (datlen--) {
  140. curbyte = *data++;
  141. I2C_Write(curbyte);
  142. }
  143. I2C_Stop();
  144. SREG = sreg_prev;
  145. TWCR = twcr_prev;
  146. #endif
  147. // ws2812_DDRREG |= _BV(ws2812_pin); // Enable DDR
  148. // new universal format (DDR)
  149. _SFR_IO8((RGB_DI_PIN >> 4) + 1) |= _BV(RGB_DI_PIN & 0xF);
  150. ws2812_sendarray_mask((uint8_t *)ledarray, leds << 2, _BV(RGB_DI_PIN & 0xF));
  151. #ifndef RGBW_BB_TWI
  152. _delay_us(80);
  153. #endif
  154. }
  155. void ws2812_sendarray(uint8_t *data, uint16_t datlen) { ws2812_sendarray_mask(data, datlen, _BV(RGB_DI_PIN & 0xF)); }
  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. uint8_t curbyte, ctr, masklo;
  209. uint8_t sreg_prev;
  210. // masklo =~maskhi&ws2812_PORTREG;
  211. // maskhi |= ws2812_PORTREG;
  212. masklo = ~maskhi & _SFR_IO8((RGB_DI_PIN >> 4) + 2);
  213. maskhi |= _SFR_IO8((RGB_DI_PIN >> 4) + 2);
  214. sreg_prev = SREG;
  215. cli();
  216. while (datlen--) {
  217. curbyte = (*data++);
  218. asm volatile(" ldi %0,8 \n\t"
  219. "loop%=: \n\t"
  220. " out %2,%3 \n\t" // '1' [01] '0' [01] - re
  221. #if (w1_nops & 1)
  222. w_nop1
  223. #endif
  224. #if (w1_nops & 2)
  225. w_nop2
  226. #endif
  227. #if (w1_nops & 4)
  228. w_nop4
  229. #endif
  230. #if (w1_nops & 8)
  231. w_nop8
  232. #endif
  233. #if (w1_nops & 16)
  234. w_nop16
  235. #endif
  236. " sbrs %1,7 \n\t" // '1' [03] '0' [02]
  237. " out %2,%4 \n\t" // '1' [--] '0' [03] - fe-low
  238. " lsl %1 \n\t" // '1' [04] '0' [04]
  239. #if (w2_nops & 1)
  240. w_nop1
  241. #endif
  242. #if (w2_nops & 2)
  243. w_nop2
  244. #endif
  245. #if (w2_nops & 4)
  246. w_nop4
  247. #endif
  248. #if (w2_nops & 8)
  249. w_nop8
  250. #endif
  251. #if (w2_nops & 16)
  252. w_nop16
  253. #endif
  254. " out %2,%4 \n\t" // '1' [+1] '0' [+1] - fe-high
  255. #if (w3_nops & 1)
  256. w_nop1
  257. #endif
  258. #if (w3_nops & 2)
  259. w_nop2
  260. #endif
  261. #if (w3_nops & 4)
  262. w_nop4
  263. #endif
  264. #if (w3_nops & 8)
  265. w_nop8
  266. #endif
  267. #if (w3_nops & 16)
  268. w_nop16
  269. #endif
  270. " dec %0 \n\t" // '1' [+2] '0' [+2]
  271. " brne loop%=\n\t" // '1' [+3] '0' [+4]
  272. : "=&d"(ctr)
  273. : "r"(curbyte), "I"(_SFR_IO_ADDR(_SFR_IO8((RGB_DI_PIN >> 4) + 2))), "r"(maskhi), "r"(masklo));
  274. }
  275. SREG = sreg_prev;
  276. }