Keyboard firmwares for Atmel AVR and Cortex-M
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.

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