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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

light_ws2812.c 3.8KB

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