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.

213 lines
6.1KB

  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. uart = &KINETISK_UART2;
  52. divisor = BAUD2DIV3(4000000);
  53. portconfig = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);
  54. hwtrigger = DMAMUX_SOURCE_UART2_TX;
  55. SIM_SCGC4 |= SIM_SCGC4_UART2;
  56. break;
  57. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  58. case 32: // Serial4
  59. case 62:
  60. uart = &KINETISK_UART3;
  61. divisor = BAUD2DIV3(4000000);
  62. portconfig = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);
  63. hwtrigger = DMAMUX_SOURCE_UART3_TX;
  64. SIM_SCGC4 |= SIM_SCGC4_UART3;
  65. break;
  66. case 33: // Serial5
  67. uart = &KINETISK_UART4;
  68. divisor = BAUD2DIV3(4000000);
  69. portconfig = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);
  70. hwtrigger = DMAMUX_SOURCE_UART4_RXTX;
  71. SIM_SCGC1 |= SIM_SCGC1_UART4;
  72. break;
  73. #endif
  74. #if defined(__MK64FX512__)
  75. case 48: // Serial6
  76. uart = &KINETISK_UART5;
  77. divisor = BAUD2DIV3(4000000);
  78. portconfig = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);
  79. hwtrigger = DMAMUX_SOURCE_UART5_RXTX;
  80. SIM_SCGC1 |= SIM_SCGC1_UART5;
  81. break;
  82. #endif
  83. #elif defined(KINETISL) // Teensy LC
  84. case 1: // Serial1
  85. case 5:
  86. uart = &KINETISK_UART0;
  87. divisor = 1;
  88. portconfig = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);
  89. hwtrigger = DMAMUX_SOURCE_UART0_TX;
  90. SIM_SCGC4 |= SIM_SCGC4_UART0;
  91. break;
  92. case 4:
  93. uart = &KINETISK_UART0;
  94. divisor = 1;
  95. portconfig = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(2);
  96. hwtrigger = DMAMUX_SOURCE_UART0_TX;
  97. SIM_SCGC4 |= SIM_SCGC4_UART0;
  98. break;
  99. case 24:
  100. uart = &KINETISK_UART0;
  101. divisor = 1;
  102. portconfig = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(4);
  103. hwtrigger = DMAMUX_SOURCE_UART0_TX;
  104. SIM_SCGC4 |= SIM_SCGC4_UART0;
  105. break;
  106. #endif
  107. default:
  108. return false; // pin not supported
  109. }
  110. if (!dma) {
  111. dma = new DMAChannel;
  112. if (!dma) return false; // unable to allocate DMA channel
  113. }
  114. #if defined(KINETISK)
  115. if (divisor < 32) divisor = 32;
  116. uart->BDH = (divisor >> 13) & 0x1F;
  117. uart->BDL = (divisor >> 5) & 0xFF;
  118. uart->C4 = divisor & 0x1F;
  119. #elif defined(KINETISL)
  120. uart->BDH = (divisor >> 8) & 0x1F;
  121. uart->BDL = divisor & 0xFF;
  122. uart->C4 = 11;
  123. #endif
  124. uart->C1 = 0;
  125. uart->C2 = UART_C2_TE | UART_C2_TIE;
  126. uart->C3 = UART_C3_TXINV;
  127. uart->C5 = UART_C5_TDMAS;
  128. #if defined(KINETISK)
  129. uart->PFIFO = 0; // TODO: is this ok for Serial3-6?
  130. #endif
  131. *(portConfigRegister(pin)) = portconfig;
  132. dma->destination(uart->D);
  133. dma->triggerAtHardwareEvent(hwtrigger);
  134. memset(drawBuffer, 0, numled * 3);
  135. return true;
  136. }
  137. void WS2812Serial::show()
  138. {
  139. // wait if prior DMA still in progress
  140. #if defined(KINETISK)
  141. while ((DMA_ERQ & (1 << dma->channel))) {
  142. yield();
  143. }
  144. #elif defined(KINETISL)
  145. while ((dma->CFG->DCR & DMA_DCR_ERQ)) {
  146. yield();
  147. }
  148. #endif
  149. // copy drawing buffer to frame buffer
  150. const uint8_t *p = drawBuffer;
  151. const uint8_t *end = p + (numled * 3);
  152. uint8_t *fb = frameBuffer;
  153. while (p < end) {
  154. uint8_t b = *p++;
  155. uint8_t g = *p++;
  156. uint8_t r = *p++;
  157. uint32_t n=0;
  158. switch (config) {
  159. case WS2812_RGB: n = (r << 16) | (g << 8) | b; break;
  160. case WS2812_RBG: n = (r << 16) | (b << 8) | g; break;
  161. case WS2812_GRB: n = (g << 16) | (r << 8) | b; break;
  162. case WS2812_GBR: n = (g << 16) | (b << 8) | r; break;
  163. case WS2812_BRG: n = (b << 16) | (r << 8) | g; break;
  164. case WS2812_BGR: n = (b << 16) | (g << 8) | r; break;
  165. }
  166. const uint8_t *stop = fb + 12;
  167. do {
  168. uint8_t x = 0x08;
  169. if (!(n & 0x00800000)) x |= 0x07;
  170. if (!(n & 0x00400000)) x |= 0xE0;
  171. n <<= 2;
  172. *fb++ = x;
  173. } while (fb < stop);
  174. }
  175. // wait 300us WS2812 reset time
  176. uint32_t min_elapsed = (numled * 30) + 300;
  177. if (min_elapsed < 2500) min_elapsed = 2500;
  178. uint32_t m;
  179. while (1) {
  180. m = micros();
  181. if ((m - prior_micros) > min_elapsed) break;
  182. yield();
  183. }
  184. prior_micros = m;
  185. // start DMA transfer to update LEDs :-)
  186. #if defined(KINETISK)
  187. dma->sourceBuffer(frameBuffer, numled * 12);
  188. dma->transferSize(1);
  189. dma->transferCount(numled * 12);
  190. dma->disableOnCompletion();
  191. dma->enable();
  192. #elif defined(KINETISL)
  193. dma->CFG->SAR = frameBuffer;
  194. dma->CFG->DSR_BCR = 0x01000000;
  195. dma->CFG->DSR_BCR = numled * 12;
  196. dma->CFG->DCR = DMA_DCR_ERQ | DMA_DCR_CS | DMA_DCR_SSIZE(1) |
  197. DMA_DCR_SINC | DMA_DCR_DSIZE(1) | DMA_DCR_D_REQ;
  198. #endif
  199. }