No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

183 líneas
4.6KB

  1. #include "WS2812Serial.h"
  2. bool WS2812Serial::begin()
  3. {
  4. uint32_t divisor, portconfig, hwtrigger;
  5. KINETISK_UART_t *uart;
  6. switch (pin) {
  7. #if defined(KINETISK) // Teensy 3.x
  8. case 1: // Serial1
  9. case 5:
  10. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  11. case 26:
  12. #endif
  13. uart = &KINETISK_UART0;
  14. divisor = BAUD2DIV(2400000);
  15. portconfig = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);
  16. hwtrigger = DMAMUX_SOURCE_UART0_TX;
  17. SIM_SCGC4 |= SIM_SCGC4_UART0;
  18. break;
  19. case 10: // Serial2
  20. #if defined(__MK20DX128__) || defined(__MK20DX256__)
  21. case 31:
  22. #elif defined(__MK64FX512__) || defined(__MK66FX1M0__)
  23. case 58:
  24. #endif
  25. uart = &KINETISK_UART1;
  26. divisor = BAUD2DIV2(2400000);
  27. portconfig = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);
  28. hwtrigger = DMAMUX_SOURCE_UART1_TX;
  29. SIM_SCGC4 |= SIM_SCGC4_UART1;
  30. break;
  31. case 8: // Serial3
  32. case 20:
  33. uart = &KINETISK_UART2;
  34. divisor = BAUD2DIV3(2400000);
  35. portconfig = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);
  36. hwtrigger = DMAMUX_SOURCE_UART2_TX;
  37. SIM_SCGC4 |= SIM_SCGC4_UART2;
  38. break;
  39. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  40. case 32: // Serial4
  41. case 62:
  42. uart = &KINETISK_UART3;
  43. divisor = BAUD2DIV3(2400000);
  44. portconfig = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);
  45. hwtrigger = DMAMUX_SOURCE_UART3_TX;
  46. SIM_SCGC4 |= SIM_SCGC4_UART3;
  47. break;
  48. case 33: // Serial5
  49. uart = &KINETISK_UART4;
  50. divisor = BAUD2DIV3(2400000);
  51. portconfig = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);
  52. hwtrigger = DMAMUX_SOURCE_UART4_RXTX;
  53. SIM_SCGC1 |= SIM_SCGC1_UART4;
  54. break;
  55. #endif
  56. #if defined(__MK64FX512__)
  57. case 48: // Serial6
  58. uart = &KINETISK_UART5;
  59. divisor = BAUD2DIV3(2400000);
  60. portconfig = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);
  61. hwtrigger = DMAMUX_SOURCE_UART5_RXTX;
  62. SIM_SCGC1 |= SIM_SCGC1_UART5;
  63. break;
  64. #endif
  65. #elif defined(KINETISL) // Teensy LC
  66. case 1: // Serial1
  67. case 5:
  68. uart = &KINETISK_UART0;
  69. divisor = 2;
  70. portconfig = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);
  71. hwtrigger = DMAMUX_SOURCE_UART0_TX;
  72. SIM_SCGC4 |= SIM_SCGC4_UART0;
  73. break;
  74. case 4:
  75. uart = &KINETISK_UART0;
  76. divisor = 2;
  77. portconfig = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(2);
  78. hwtrigger = DMAMUX_SOURCE_UART0_TX;
  79. SIM_SCGC4 |= SIM_SCGC4_UART0;
  80. break;
  81. case 24:
  82. uart = &KINETISK_UART0;
  83. divisor = 2;
  84. portconfig = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(4);
  85. hwtrigger = DMAMUX_SOURCE_UART0_TX;
  86. SIM_SCGC4 |= SIM_SCGC4_UART0;
  87. break;
  88. #endif
  89. default:
  90. return false; // pin not supported
  91. }
  92. if (!dma) {
  93. dma = new DMAChannel;
  94. if (!dma) return false; // unable to allocate DMA channel
  95. }
  96. #if defined(KINETISK)
  97. uart->BDH = (divisor >> 13) & 0x1F;
  98. uart->BDL = (divisor >> 5) & 0xFF;
  99. uart->C4 = divisor & 0x1F;
  100. #elif defined(KINETISL)
  101. uart->BDH = (divisor >> 8) & 0x1F;
  102. uart->BDL = divisor & 0xFF;
  103. uart->C4 = 9;
  104. #endif
  105. uart->C1 = 0;
  106. uart->C2 = UART_C2_TE | UART_C2_TIE;
  107. uart->C3 = UART_C3_TXINV;
  108. uart->C5 = UART_C5_TDMAS;
  109. #if defined(KINETISK)
  110. uart->PFIFO = 0; // TODO: is this ok for Serial3-6?
  111. #endif
  112. *(portConfigRegister(pin)) = portconfig;
  113. dma->destination(uart->D);
  114. dma->triggerAtHardwareEvent(hwtrigger);
  115. memset(drawBuffer, 0, numled * 3);
  116. return true;
  117. }
  118. void WS2812Serial::show()
  119. {
  120. // wait if prior DMA still in progress
  121. #if defined(KINETISK)
  122. while ((DMA_ERQ & (1 << dma->channel))) {
  123. yield();
  124. }
  125. #elif defined(KINETISL)
  126. while ((dma->CFG->DCR & DMA_DCR_ERQ)) {
  127. yield();
  128. }
  129. #elif defined(KINETISL)
  130. #endif
  131. // wait 300us WS2812 reset time
  132. uint32_t min_elapsed = ((numled * 34134) >> 10) + 300;
  133. if (min_elapsed < 2500) min_elapsed = 2500;
  134. uint32_t m;
  135. while (1) {
  136. m = micros();
  137. if ((m - prior_micros) > min_elapsed) break;
  138. yield();
  139. }
  140. prior_micros = m;
  141. const uint8_t *p = drawBuffer;
  142. const uint8_t *end = p + (numled * 3);
  143. uint8_t *fb = frameBuffer;
  144. while (p < end) {
  145. uint8_t b = *p++;
  146. uint8_t g = *p++;
  147. uint8_t r = *p++;
  148. uint32_t n=0;
  149. switch (config) {
  150. case WS2812_RGB: n = (r << 16) | (g << 8) | b; break;
  151. case WS2812_RBG: n = (r << 16) | (b << 8) | g; break;
  152. case WS2812_GRB: n = (g << 16) | (r << 8) | b; break;
  153. case WS2812_GBR: n = (g << 16) | (b << 8) | r; break;
  154. case WS2812_BRG: n = (b << 16) | (r << 8) | g; break;
  155. case WS2812_BGR: n = (b << 16) | (g << 8) | r; break;
  156. }
  157. const uint8_t *stop = fb + 8;
  158. do {
  159. uint8_t x = 0x92;
  160. if (!(n & 0x00800000)) x |= 0x01;
  161. if (!(n & 0x00400000)) x |= 0x04;
  162. if (!(n & 0x00200000)) x |= 0x20;
  163. n <<= 3;
  164. *fb++ = x;
  165. } while (fb < stop);
  166. }
  167. dma->sourceBuffer(frameBuffer, numled * 8);
  168. dma->transferSize(1);
  169. dma->transferCount(numled * 8);
  170. dma->disableOnCompletion();
  171. dma->enable();
  172. }