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.

302 lines
9.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. #if defined(__IMXRT1062__) // Teensy 3.x
  24. uint32_t hwtrigger;
  25. #else
  26. uint32_t divisor, portconfig, hwtrigger;
  27. KINETISK_UART_t *uart;
  28. #endif
  29. switch (pin) {
  30. #if defined(KINETISK) // Teensy 3.x
  31. case 1: // Serial1
  32. case 5:
  33. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  34. case 26:
  35. #endif
  36. uart = &KINETISK_UART0;
  37. divisor = BAUD2DIV(4000000);
  38. portconfig = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);
  39. hwtrigger = DMAMUX_SOURCE_UART0_TX;
  40. SIM_SCGC4 |= SIM_SCGC4_UART0;
  41. break;
  42. case 10: // Serial2
  43. #if defined(__MK20DX128__) || defined(__MK20DX256__)
  44. case 31:
  45. #elif defined(__MK64FX512__) || defined(__MK66FX1M0__)
  46. case 58:
  47. #endif
  48. uart = &KINETISK_UART1;
  49. divisor = BAUD2DIV2(4000000);
  50. portconfig = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);
  51. hwtrigger = DMAMUX_SOURCE_UART1_TX;
  52. SIM_SCGC4 |= SIM_SCGC4_UART1;
  53. break;
  54. case 8: // Serial3
  55. uart = &KINETISK_UART2;
  56. divisor = BAUD2DIV3(4000000);
  57. portconfig = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);
  58. hwtrigger = DMAMUX_SOURCE_UART2_TX;
  59. SIM_SCGC4 |= SIM_SCGC4_UART2;
  60. break;
  61. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  62. case 32: // Serial4
  63. case 62:
  64. uart = &KINETISK_UART3;
  65. divisor = BAUD2DIV3(4000000);
  66. portconfig = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);
  67. hwtrigger = DMAMUX_SOURCE_UART3_TX;
  68. SIM_SCGC4 |= SIM_SCGC4_UART3;
  69. break;
  70. case 33: // Serial5
  71. uart = &KINETISK_UART4;
  72. divisor = BAUD2DIV3(4000000);
  73. portconfig = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);
  74. hwtrigger = DMAMUX_SOURCE_UART4_RXTX;
  75. SIM_SCGC1 |= SIM_SCGC1_UART4;
  76. break;
  77. #endif
  78. #if defined(__MK64FX512__)
  79. case 48: // Serial6
  80. uart = &KINETISK_UART5;
  81. divisor = BAUD2DIV3(4000000);
  82. portconfig = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);
  83. hwtrigger = DMAMUX_SOURCE_UART5_RXTX;
  84. SIM_SCGC1 |= SIM_SCGC1_UART5;
  85. break;
  86. #endif
  87. #elif defined(KINETISL) // Teensy LC
  88. case 1: // Serial1
  89. case 5:
  90. // NOT SURE HOW THIS WORKS ON LC?????
  91. uart = &KINETISK_UART0;
  92. divisor = 1;
  93. portconfig = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);
  94. hwtrigger = DMAMUX_SOURCE_UART0_TX;
  95. SIM_SCGC4 |= SIM_SCGC4_UART0;
  96. break;
  97. case 4:
  98. uart = &KINETISK_UART0;
  99. divisor = 1;
  100. portconfig = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(2);
  101. hwtrigger = DMAMUX_SOURCE_UART0_TX;
  102. SIM_SCGC4 |= SIM_SCGC4_UART0;
  103. break;
  104. case 24:
  105. uart = &KINETISK_UART0;
  106. divisor = 1;
  107. portconfig = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(4);
  108. hwtrigger = DMAMUX_SOURCE_UART0_TX;
  109. SIM_SCGC4 |= SIM_SCGC4_UART0;
  110. break;
  111. #elif defined(__IMXRT1062__)
  112. case 1: // Serial1
  113. uart = &IMXRT_LPUART6;
  114. CCM_CCGR3 |= CCM_CCGR3_LPUART6(CCM_CCGR_ON);
  115. hwtrigger = DMAMUX_SOURCE_LPUART6_TX;
  116. break;
  117. case 8: // Serial2
  118. uart = &IMXRT_LPUART4;
  119. CCM_CCGR1 |= CCM_CCGR1_LPUART4(CCM_CCGR_ON);
  120. hwtrigger = DMAMUX_SOURCE_LPUART4_TX;
  121. break;
  122. case 14: // Serial3
  123. uart = &IMXRT_LPUART2;
  124. CCM_CCGR0 |= CCM_CCGR0_LPUART2(CCM_CCGR_ON);
  125. hwtrigger = DMAMUX_SOURCE_LPUART2_TX;
  126. break;
  127. case 17: // Serial4
  128. uart = &IMXRT_LPUART3;
  129. CCM_CCGR0 |= CCM_CCGR0_LPUART3(CCM_CCGR_ON);
  130. hwtrigger = DMAMUX_SOURCE_LPUART4_TX;
  131. break;
  132. case 20: // Serial5
  133. case 39: // Serial5 alt
  134. uart = &IMXRT_LPUART8;
  135. CCM_CCGR6 |= CCM_CCGR6_LPUART8(CCM_CCGR_ON);
  136. hwtrigger = DMAMUX_SOURCE_LPUART8_TX;
  137. break;
  138. case 24: // Serial6
  139. uart = &IMXRT_LPUART1;
  140. CCM_CCGR5 |= CCM_CCGR5_LPUART1(CCM_CCGR_ON);
  141. hwtrigger = DMAMUX_SOURCE_LPUART1_TX;
  142. break;
  143. case 29: // Serial7
  144. uart = &IMXRT_LPUART7;
  145. CCM_CCGR5 |= CCM_CCGR5_LPUART7(CCM_CCGR_ON);
  146. hwtrigger = DMAMUX_SOURCE_LPUART7_TX;
  147. break;
  148. #endif
  149. default:
  150. return false; // pin not supported
  151. }
  152. if (!dma) {
  153. dma = new DMAChannel;
  154. if (!dma) return false; // unable to allocate DMA channel
  155. }
  156. #if defined(__IMXRT1062__)
  157. // Convert Baud
  158. // Computed values for 4mhz
  159. uart->CTRL = 0; // clear everything
  160. uart->BAUD = LPUART_BAUD_OSR(5) | LPUART_BAUD_SBR(1) | LPUART_BAUD_TDMAE; // set baud configure for transfer DMA
  161. uart->PINCFG = 0;
  162. uint16_t tx_fifo_size = (((uart->FIFO >> 4) & 0x7) << 2);
  163. uint8_t tx_water = (tx_fifo_size < 16) ? tx_fifo_size >> 1 : 7;
  164. // uart->WATER = LPUART_WATER_TXWATER(1); // guessing here?
  165. // uart->FIFO = 0; // disable the fifo.
  166. uart->WATER = LPUART_WATER_TXWATER(tx_water);
  167. uart->FIFO |= LPUART_FIFO_TXFE;
  168. uart->CTRL = (LPUART_CTRL_TE /*| LPUART_CTRL_TIE */ | LPUART_CTRL_TXINV); // enable transmitter and invert
  169. // We need to configure the TX pin now.
  170. *(portControlRegister(pin)) = IOMUXC_PAD_SRE | IOMUXC_PAD_DSE(3) | IOMUXC_PAD_SPEED(3);
  171. *(portConfigRegister(pin)) = 2; // from hardware table for each one, but I think they are all 2...
  172. dma->destination((volatile uint8_t&)uart->DATA);
  173. //Serial.printf("HWTrigger: %x\n", hwtrigger);
  174. #else
  175. #if defined(KINETISK)
  176. if (divisor < 32) divisor = 32;
  177. uart->BDH = (divisor >> 13) & 0x1F;
  178. uart->BDL = (divisor >> 5) & 0xFF;
  179. uart->C4 = divisor & 0x1F;
  180. #elif defined(KINETISL)
  181. uart->BDH = (divisor >> 8) & 0x1F;
  182. uart->BDL = divisor & 0xFF;
  183. uart->C4 = 11;
  184. #endif
  185. uart->C1 = 0;
  186. uart->C2 = UART_C2_TE | UART_C2_TIE;
  187. uart->C3 = UART_C3_TXINV;
  188. uart->C5 = UART_C5_TDMAS;
  189. #if defined(KINETISK)
  190. uart->PFIFO = 0; // TODO: is this ok for Serial3-6?
  191. #endif
  192. *(portConfigRegister(pin)) = portconfig;
  193. dma->destination(uart->D);
  194. #endif
  195. dma->triggerAtHardwareEvent(hwtrigger);
  196. memset(drawBuffer, 0, numled * 3);
  197. return true;
  198. }
  199. void WS2812Serial::show()
  200. {
  201. // wait if prior DMA still in progress
  202. #if defined(KINETISK)
  203. while ((DMA_ERQ & (1 << dma->channel))) {
  204. yield();
  205. }
  206. #elif defined(KINETISL)
  207. while ((dma->CFG->DCR & DMA_DCR_ERQ)) {
  208. yield();
  209. }
  210. #elif defined(__IMXRT1062__)
  211. //Serial.println("Show called");
  212. while ((DMA_ERQ & (1 << dma->channel))) {
  213. yield();
  214. }
  215. //Serial.println("After Yield");
  216. #endif
  217. // copy drawing buffer to frame buffer
  218. const uint8_t *p = drawBuffer;
  219. const uint8_t *end = p + (numled * 3);
  220. uint8_t *fb = frameBuffer;
  221. while (p < end) {
  222. uint8_t b = *p++;
  223. uint8_t g = *p++;
  224. uint8_t r = *p++;
  225. uint32_t n=0;
  226. switch (config) {
  227. case WS2812_RGB: n = (r << 16) | (g << 8) | b; break;
  228. case WS2812_RBG: n = (r << 16) | (b << 8) | g; break;
  229. case WS2812_GRB: n = (g << 16) | (r << 8) | b; break;
  230. case WS2812_GBR: n = (g << 16) | (b << 8) | r; break;
  231. case WS2812_BRG: n = (b << 16) | (r << 8) | g; break;
  232. case WS2812_BGR: n = (b << 16) | (g << 8) | r; break;
  233. }
  234. const uint8_t *stop = fb + 12;
  235. do {
  236. uint8_t x = 0x08;
  237. if (!(n & 0x00800000)) x |= 0x07;
  238. if (!(n & 0x00400000)) x |= 0xE0;
  239. n <<= 2;
  240. *fb++ = x;
  241. } while (fb < stop);
  242. }
  243. // wait 300us WS2812 reset time
  244. uint32_t min_elapsed = (numled * 30) + 300;
  245. if (min_elapsed < 2500) min_elapsed = 2500;
  246. uint32_t m;
  247. while (1) {
  248. m = micros();
  249. if ((m - prior_micros) > min_elapsed) break;
  250. yield();
  251. }
  252. prior_micros = m;
  253. // start DMA transfer to update LEDs :-)
  254. #if defined(KINETISK)
  255. dma->sourceBuffer(frameBuffer, numled * 12);
  256. dma->transferSize(1);
  257. dma->transferCount(numled * 12);
  258. dma->disableOnCompletion();
  259. dma->enable();
  260. #elif defined(KINETISL)
  261. dma->CFG->SAR = frameBuffer;
  262. dma->CFG->DSR_BCR = 0x01000000;
  263. dma->CFG->DSR_BCR = numled * 12;
  264. dma->CFG->DCR = DMA_DCR_ERQ | DMA_DCR_CS | DMA_DCR_SSIZE(1) |
  265. DMA_DCR_SINC | DMA_DCR_DSIZE(1) | DMA_DCR_D_REQ;
  266. #elif defined(__IMXRT1062__)
  267. // See if we need to muck with DMA cache...
  268. if ((uint32_t)frameBuffer >= 0x20200000u) arm_dcache_flush(frameBuffer, numled * 12);
  269. dma->sourceBuffer(frameBuffer, numled * 12);
  270. // dma->transferSize(1);
  271. dma->transferCount(numled * 12);
  272. dma->disableOnCompletion();
  273. /* Serial.printf("%x %x:", (uint32_t)dma, (uint32_t)dma->TCD);
  274. Serial.printf("SA:%x SO:%d AT:%x NB:%x SL:%d DA:%x DO: %d CI:%x DL:%x CS:%x BI:%x\n", (uint32_t)dma->TCD->SADDR,
  275. dma->TCD->SOFF, dma->TCD->ATTR, dma->TCD->NBYTES, dma->TCD->SLAST, (uint32_t)dma->TCD->DADDR,
  276. dma->TCD->DOFF, dma->TCD->CITER, dma->TCD->DLASTSGA, dma->TCD->CSR, dma->TCD->BITER);
  277. */
  278. uart->STAT = 0; // try clearing out the status
  279. dma->enable();
  280. #endif
  281. }