You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

171 lines
5.2 KiB

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