選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

WS2812Serial.cpp 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. 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. }