ws2812_bitbang.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 <avr/interrupt.h>
  24. #include <avr/io.h>
  25. #include <util/delay.h>
  26. #include "ws2812.h"
  27. #include "pin_defs.h"
  28. #define pinmask(pin) (_BV((pin)&0xF))
  29. /*
  30. * Forward declare internal functions
  31. *
  32. * The functions take a byte-array and send to the data output as WS2812 bitstream.
  33. * The length is the number of bytes to send - three per LED.
  34. */
  35. static inline void ws2812_sendarray_mask(uint8_t *data, uint16_t datlen, uint8_t masklo, uint8_t maskhi);
  36. void ws2812_init(void) {
  37. DDRx_ADDRESS(WS2812_DI_PIN) |= pinmask(WS2812_DI_PIN);
  38. }
  39. void ws2812_setleds(rgb_led_t *ledarray, uint16_t number_of_leds) {
  40. uint8_t masklo = ~(pinmask(WS2812_DI_PIN)) & PORTx_ADDRESS(WS2812_DI_PIN);
  41. uint8_t maskhi = pinmask(WS2812_DI_PIN) | PORTx_ADDRESS(WS2812_DI_PIN);
  42. ws2812_sendarray_mask((uint8_t *)ledarray, number_of_leds * sizeof(rgb_led_t), masklo, maskhi);
  43. _delay_us(WS2812_TRST_US);
  44. }
  45. /*
  46. This routine writes an array of bytes with RGB values to the Dataout pin
  47. using the fast 800kHz clockless WS2811/2812 protocol.
  48. */
  49. // Fixed cycles used by the inner loop
  50. #define w_fixedlow 2
  51. #define w_fixedhigh 4
  52. #define w_fixedtotal 8
  53. // Insert NOPs to match the timing, if possible
  54. #define w_zerocycles (((F_CPU / 1000) * WS2812_T0H) / 1000000)
  55. #define w_onecycles (((F_CPU / 1000) * WS2812_T1H + 500000) / 1000000)
  56. #define w_totalcycles (((F_CPU / 1000) * WS2812_TIMING + 500000) / 1000000)
  57. // w1_nops - nops between rising edge and falling edge - low
  58. #if w_zerocycles >= w_fixedlow
  59. # define w1_nops (w_zerocycles - w_fixedlow)
  60. #else
  61. # define w1_nops 0
  62. #endif
  63. // w2_nops - nops between fe low and fe high
  64. #if w_onecycles >= (w_fixedhigh + w1_nops)
  65. # define w2_nops (w_onecycles - w_fixedhigh - w1_nops)
  66. #else
  67. # define w2_nops 0
  68. #endif
  69. // w3_nops - nops to complete loop
  70. #if w_totalcycles >= (w_fixedtotal + w1_nops + w2_nops)
  71. # define w3_nops (w_totalcycles - w_fixedtotal - w1_nops - w2_nops)
  72. #else
  73. # define w3_nops 0
  74. #endif
  75. // The only critical timing parameter is the minimum pulse length of the "0"
  76. // Warn or throw error if this timing can not be met with current F_CPU settings.
  77. #define w_lowtime ((w1_nops + w_fixedlow) * 1000000) / (F_CPU / 1000)
  78. #if w_lowtime > 550
  79. # error "Light_ws2812: Sorry, the clock speed is too low. Did you set F_CPU correctly?"
  80. #elif w_lowtime > 450
  81. # warning "Light_ws2812: The timing is critical and may only work on WS2812B, not on WS2812(S)."
  82. # warning "Please consider a higher clockspeed, if possible"
  83. #endif
  84. #define w_nop1 "nop \n\t"
  85. #define w_nop2 "rjmp .+0 \n\t"
  86. #define w_nop4 w_nop2 w_nop2
  87. #define w_nop8 w_nop4 w_nop4
  88. #define w_nop16 w_nop8 w_nop8
  89. static inline void ws2812_sendarray_mask(uint8_t *data, uint16_t datlen, uint8_t masklo, uint8_t maskhi) {
  90. uint8_t curbyte, ctr, sreg_prev;
  91. sreg_prev = SREG;
  92. cli();
  93. while (datlen--) {
  94. curbyte = (*data++);
  95. asm volatile(" ldi %0,8 \n\t"
  96. "loop%=: \n\t"
  97. " out %2,%3 \n\t" // '1' [01] '0' [01] - re
  98. #if (w1_nops & 1)
  99. w_nop1
  100. #endif
  101. #if (w1_nops & 2)
  102. w_nop2
  103. #endif
  104. #if (w1_nops & 4)
  105. w_nop4
  106. #endif
  107. #if (w1_nops & 8)
  108. w_nop8
  109. #endif
  110. #if (w1_nops & 16)
  111. w_nop16
  112. #endif
  113. " sbrs %1,7 \n\t" // '1' [03] '0' [02]
  114. " out %2,%4 \n\t" // '1' [--] '0' [03] - fe-low
  115. " lsl %1 \n\t" // '1' [04] '0' [04]
  116. #if (w2_nops & 1)
  117. w_nop1
  118. #endif
  119. #if (w2_nops & 2)
  120. w_nop2
  121. #endif
  122. #if (w2_nops & 4)
  123. w_nop4
  124. #endif
  125. #if (w2_nops & 8)
  126. w_nop8
  127. #endif
  128. #if (w2_nops & 16)
  129. w_nop16
  130. #endif
  131. " out %2,%4 \n\t" // '1' [+1] '0' [+1] - fe-high
  132. #if (w3_nops & 1)
  133. w_nop1
  134. #endif
  135. #if (w3_nops & 2)
  136. w_nop2
  137. #endif
  138. #if (w3_nops & 4)
  139. w_nop4
  140. #endif
  141. #if (w3_nops & 8)
  142. w_nop8
  143. #endif
  144. #if (w3_nops & 16)
  145. w_nop16
  146. #endif
  147. " dec %0 \n\t" // '1' [+2] '0' [+2]
  148. " brne loop%=\n\t" // '1' [+3] '0' [+4]
  149. : "=&d"(ctr)
  150. : "r"(curbyte), "I"(_SFR_IO_ADDR(PORTx_ADDRESS(WS2812_DI_PIN))), "r"(maskhi), "r"(masklo));
  151. }
  152. SREG = sreg_prev;
  153. }