Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

WS2812Serial.cpp 6.1KB

7 lat temu
7 lat temu
7 lat temu
7 lat temu
7 lat temu
7 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /* WS2812Serial - Non-blocking WS2812 LED Display Library
  2. https://github.com/PaulStoffregen/WS2812Serial
  3. Copyright (c) 2017 Paul Stoffregen, PJRC.COM, LLC
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. #include "WS2812Serial.h"
  21. bool WS2812Serial::begin()
  22. {
  23. uint32_t divisor, portconfig, hwtrigger;
  24. KINETISK_UART_t *uart;
  25. switch (pin) {
  26. #if defined(KINETISK) // Teensy 3.x
  27. case 1: // Serial1
  28. case 5:
  29. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  30. case 26:
  31. #endif
  32. uart = &KINETISK_UART0;
  33. divisor = BAUD2DIV(4000000);
  34. portconfig = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);
  35. hwtrigger = DMAMUX_SOURCE_UART0_TX;
  36. SIM_SCGC4 |= SIM_SCGC4_UART0;
  37. break;
  38. case 10: // Serial2
  39. #if defined(__MK20DX128__) || defined(__MK20DX256__)
  40. case 31:
  41. #elif defined(__MK64FX512__) || defined(__MK66FX1M0__)
  42. case 58:
  43. #endif
  44. uart = &KINETISK_UART1;
  45. divisor = BAUD2DIV2(4000000);
  46. portconfig = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);
  47. hwtrigger = DMAMUX_SOURCE_UART1_TX;
  48. SIM_SCGC4 |= SIM_SCGC4_UART1;
  49. break;
  50. case 8: // Serial3
  51. case 20:
  52. uart = &KINETISK_UART2;
  53. divisor = BAUD2DIV3(4000000);
  54. portconfig = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);
  55. hwtrigger = DMAMUX_SOURCE_UART2_TX;
  56. SIM_SCGC4 |= SIM_SCGC4_UART2;
  57. break;
  58. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  59. case 32: // Serial4
  60. case 62:
  61. uart = &KINETISK_UART3;
  62. divisor = BAUD2DIV3(4000000);
  63. portconfig = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);
  64. hwtrigger = DMAMUX_SOURCE_UART3_TX;
  65. SIM_SCGC4 |= SIM_SCGC4_UART3;
  66. break;
  67. case 33: // Serial5
  68. uart = &KINETISK_UART4;
  69. divisor = BAUD2DIV3(4000000);
  70. portconfig = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);
  71. hwtrigger = DMAMUX_SOURCE_UART4_RXTX;
  72. SIM_SCGC1 |= SIM_SCGC1_UART4;
  73. break;
  74. #endif
  75. #if defined(__MK64FX512__)
  76. case 48: // Serial6
  77. uart = &KINETISK_UART5;
  78. divisor = BAUD2DIV3(4000000);
  79. portconfig = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);
  80. hwtrigger = DMAMUX_SOURCE_UART5_RXTX;
  81. SIM_SCGC1 |= SIM_SCGC1_UART5;
  82. break;
  83. #endif
  84. #elif defined(KINETISL) // Teensy LC
  85. case 1: // Serial1
  86. case 5:
  87. uart = &KINETISK_UART0;
  88. divisor = 1;
  89. portconfig = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);
  90. hwtrigger = DMAMUX_SOURCE_UART0_TX;
  91. SIM_SCGC4 |= SIM_SCGC4_UART0;
  92. break;
  93. case 4:
  94. uart = &KINETISK_UART0;
  95. divisor = 1;
  96. portconfig = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(2);
  97. hwtrigger = DMAMUX_SOURCE_UART0_TX;
  98. SIM_SCGC4 |= SIM_SCGC4_UART0;
  99. break;
  100. case 24:
  101. uart = &KINETISK_UART0;
  102. divisor = 1;
  103. portconfig = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(4);
  104. hwtrigger = DMAMUX_SOURCE_UART0_TX;
  105. SIM_SCGC4 |= SIM_SCGC4_UART0;
  106. break;
  107. #endif
  108. default:
  109. return false; // pin not supported
  110. }
  111. if (!dma) {
  112. dma = new DMAChannel;
  113. if (!dma) return false; // unable to allocate DMA channel
  114. }
  115. #if defined(KINETISK)
  116. if (divisor < 32) divisor = 32;
  117. uart->BDH = (divisor >> 13) & 0x1F;
  118. uart->BDL = (divisor >> 5) & 0xFF;
  119. uart->C4 = divisor & 0x1F;
  120. #elif defined(KINETISL)
  121. uart->BDH = (divisor >> 8) & 0x1F;
  122. uart->BDL = divisor & 0xFF;
  123. uart->C4 = 11;
  124. #endif
  125. uart->C1 = 0;
  126. uart->C2 = UART_C2_TE | UART_C2_TIE;
  127. uart->C3 = UART_C3_TXINV;
  128. uart->C5 = UART_C5_TDMAS;
  129. #if defined(KINETISK)
  130. uart->PFIFO = 0; // TODO: is this ok for Serial3-6?
  131. #endif
  132. *(portConfigRegister(pin)) = portconfig;
  133. dma->destination(uart->D);
  134. dma->triggerAtHardwareEvent(hwtrigger);
  135. memset(drawBuffer, 0, numled * 3);
  136. return true;
  137. }
  138. void WS2812Serial::show()
  139. {
  140. // wait if prior DMA still in progress
  141. #if defined(KINETISK)
  142. while ((DMA_ERQ & (1 << dma->channel))) {
  143. yield();
  144. }
  145. #elif defined(KINETISL)
  146. while ((dma->CFG->DCR & DMA_DCR_ERQ)) {
  147. yield();
  148. }
  149. #endif
  150. // copy drawing buffer to frame buffer
  151. const uint8_t *p = drawBuffer;
  152. const uint8_t *end = p + (numled * 3);
  153. uint8_t *fb = frameBuffer;
  154. while (p < end) {
  155. uint8_t b = *p++;
  156. uint8_t g = *p++;
  157. uint8_t r = *p++;
  158. uint32_t n=0;
  159. switch (config) {
  160. case WS2812_RGB: n = (r << 16) | (g << 8) | b; break;
  161. case WS2812_RBG: n = (r << 16) | (b << 8) | g; break;
  162. case WS2812_GRB: n = (g << 16) | (r << 8) | b; break;
  163. case WS2812_GBR: n = (g << 16) | (b << 8) | r; break;
  164. case WS2812_BRG: n = (b << 16) | (r << 8) | g; break;
  165. case WS2812_BGR: n = (b << 16) | (g << 8) | r; break;
  166. }
  167. const uint8_t *stop = fb + 12;
  168. do {
  169. uint8_t x = 0x08;
  170. if (!(n & 0x00800000)) x |= 0x07;
  171. if (!(n & 0x00400000)) x |= 0xE0;
  172. n <<= 2;
  173. *fb++ = x;
  174. } while (fb < stop);
  175. }
  176. // wait 300us WS2812 reset time
  177. uint32_t min_elapsed = (numled * 30) + 300;
  178. if (min_elapsed < 2500) min_elapsed = 2500;
  179. uint32_t m;
  180. while (1) {
  181. m = micros();
  182. if ((m - prior_micros) > min_elapsed) break;
  183. yield();
  184. }
  185. prior_micros = m;
  186. // start DMA transfer to update LEDs :-)
  187. #if defined(KINETISK)
  188. dma->sourceBuffer(frameBuffer, numled * 12);
  189. dma->transferSize(1);
  190. dma->transferCount(numled * 12);
  191. dma->disableOnCompletion();
  192. dma->enable();
  193. #elif defined(KINETISL)
  194. dma->CFG->SAR = frameBuffer;
  195. dma->CFG->DSR_BCR = 0x01000000;
  196. dma->CFG->DSR_BCR = numled * 12;
  197. dma->CFG->DCR = DMA_DCR_ERQ | DMA_DCR_CS | DMA_DCR_SSIZE(1) |
  198. DMA_DCR_SINC | DMA_DCR_DSIZE(1) | DMA_DCR_D_REQ;
  199. #endif
  200. }