light_ws2812.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. * License: GNU GPL v2 (see License.txt)
  11. */
  12. #include "light_ws2812.h"
  13. #include <avr/interrupt.h>
  14. #include <avr/io.h>
  15. #include <util/delay.h>
  16. #include "debug.h"
  17. // Setleds for standard RGB
  18. void inline ws2812_setleds(struct cRGB *ledarray, uint16_t leds)
  19. {
  20. // ws2812_setleds_pin(ledarray,leds, _BV(ws2812_pin));
  21. ws2812_setleds_pin(ledarray,leds, _BV(RGB_DI_PIN & 0xF));
  22. }
  23. void inline ws2812_setleds_pin(struct cRGB *ledarray, uint16_t leds, uint8_t pinmask)
  24. {
  25. // ws2812_DDRREG |= pinmask; // Enable DDR
  26. // new universal format (DDR)
  27. _SFR_IO8((RGB_DI_PIN >> 4) + 1) |= pinmask;
  28. ws2812_sendarray_mask((uint8_t*)ledarray,leds+leds+leds,pinmask);
  29. _delay_us(50);
  30. }
  31. // Setleds for SK6812RGBW
  32. void inline ws2812_setleds_rgbw(struct cRGBW *ledarray, uint16_t leds)
  33. {
  34. // ws2812_DDRREG |= _BV(ws2812_pin); // Enable DDR
  35. // new universal format (DDR)
  36. _SFR_IO8((RGB_DI_PIN >> 4) + 1) |= _BV(RGB_DI_PIN & 0xF);
  37. ws2812_sendarray_mask((uint8_t*)ledarray,leds<<2,_BV(RGB_DI_PIN & 0xF));
  38. _delay_us(80);
  39. }
  40. void ws2812_sendarray(uint8_t *data,uint16_t datlen)
  41. {
  42. ws2812_sendarray_mask(data,datlen,_BV(RGB_DI_PIN & 0xF));
  43. }
  44. /*
  45. This routine writes an array of bytes with RGB values to the Dataout pin
  46. using the fast 800kHz clockless WS2811/2812 protocol.
  47. */
  48. // Timing in ns
  49. #define w_zeropulse 350
  50. #define w_onepulse 900
  51. #define w_totalperiod 1250
  52. // Fixed cycles used by the inner loop
  53. #define w_fixedlow 2
  54. #define w_fixedhigh 4
  55. #define w_fixedtotal 8
  56. // Insert NOPs to match the timing, if possible
  57. #define w_zerocycles (((F_CPU/1000)*w_zeropulse )/1000000)
  58. #define w_onecycles (((F_CPU/1000)*w_onepulse +500000)/1000000)
  59. #define w_totalcycles (((F_CPU/1000)*w_totalperiod +500000)/1000000)
  60. // w1 - nops between rising edge and falling edge - low
  61. #define w1 (w_zerocycles-w_fixedlow)
  62. // w2 nops between fe low and fe high
  63. #define w2 (w_onecycles-w_fixedhigh-w1)
  64. // w3 nops to complete loop
  65. #define w3 (w_totalcycles-w_fixedtotal-w1-w2)
  66. #if w1>0
  67. #define w1_nops w1
  68. #else
  69. #define w1_nops 0
  70. #endif
  71. // The only critical timing parameter is the minimum pulse length of the "0"
  72. // Warn or throw error if this timing can not be met with current F_CPU settings.
  73. #define w_lowtime ((w1_nops+w_fixedlow)*1000000)/(F_CPU/1000)
  74. #if w_lowtime>550
  75. #error "Light_ws2812: Sorry, the clock speed is too low. Did you set F_CPU correctly?"
  76. #elif w_lowtime>450
  77. #warning "Light_ws2812: The timing is critical and may only work on WS2812B, not on WS2812(S)."
  78. #warning "Please consider a higher clockspeed, if possible"
  79. #endif
  80. #if w2>0
  81. #define w2_nops w2
  82. #else
  83. #define w2_nops 0
  84. #endif
  85. #if w3>0
  86. #define w3_nops w3
  87. #else
  88. #define w3_nops 0
  89. #endif
  90. #define w_nop1 "nop \n\t"
  91. #define w_nop2 "rjmp .+0 \n\t"
  92. #define w_nop4 w_nop2 w_nop2
  93. #define w_nop8 w_nop4 w_nop4
  94. #define w_nop16 w_nop8 w_nop8
  95. void inline ws2812_sendarray_mask(uint8_t *data,uint16_t datlen,uint8_t maskhi)
  96. {
  97. uint8_t curbyte,ctr,masklo;
  98. uint8_t sreg_prev;
  99. // masklo =~maskhi&ws2812_PORTREG;
  100. // maskhi |= ws2812_PORTREG;
  101. masklo =~maskhi&_SFR_IO8((RGB_DI_PIN >> 4) + 2);
  102. maskhi |= _SFR_IO8((RGB_DI_PIN >> 4) + 2);
  103. sreg_prev=SREG;
  104. cli();
  105. while (datlen--) {
  106. curbyte=*data++;
  107. asm volatile(
  108. " ldi %0,8 \n\t"
  109. "loop%=: \n\t"
  110. " out %2,%3 \n\t" // '1' [01] '0' [01] - re
  111. #if (w1_nops&1)
  112. w_nop1
  113. #endif
  114. #if (w1_nops&2)
  115. w_nop2
  116. #endif
  117. #if (w1_nops&4)
  118. w_nop4
  119. #endif
  120. #if (w1_nops&8)
  121. w_nop8
  122. #endif
  123. #if (w1_nops&16)
  124. w_nop16
  125. #endif
  126. " sbrs %1,7 \n\t" // '1' [03] '0' [02]
  127. " out %2,%4 \n\t" // '1' [--] '0' [03] - fe-low
  128. " lsl %1 \n\t" // '1' [04] '0' [04]
  129. #if (w2_nops&1)
  130. w_nop1
  131. #endif
  132. #if (w2_nops&2)
  133. w_nop2
  134. #endif
  135. #if (w2_nops&4)
  136. w_nop4
  137. #endif
  138. #if (w2_nops&8)
  139. w_nop8
  140. #endif
  141. #if (w2_nops&16)
  142. w_nop16
  143. #endif
  144. " out %2,%4 \n\t" // '1' [+1] '0' [+1] - fe-high
  145. #if (w3_nops&1)
  146. w_nop1
  147. #endif
  148. #if (w3_nops&2)
  149. w_nop2
  150. #endif
  151. #if (w3_nops&4)
  152. w_nop4
  153. #endif
  154. #if (w3_nops&8)
  155. w_nop8
  156. #endif
  157. #if (w3_nops&16)
  158. w_nop16
  159. #endif
  160. " dec %0 \n\t" // '1' [+2] '0' [+2]
  161. " brne loop%=\n\t" // '1' [+3] '0' [+4]
  162. : "=&d" (ctr)
  163. : "r" (curbyte), "I" (_SFR_IO_ADDR(_SFR_IO8((RGB_DI_PIN >> 4) + 2))), "r" (maskhi), "r" (masklo)
  164. );
  165. }
  166. SREG=sreg_prev;
  167. }