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.

380 lines
12KB

  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. #if defined(ARDUINO_TEENSY41)
  114. case 53:
  115. #endif
  116. uart = &IMXRT_LPUART6;
  117. CCM_CCGR3 |= CCM_CCGR3_LPUART6(CCM_CCGR_ON);
  118. hwtrigger = DMAMUX_SOURCE_LPUART6_TX;
  119. break;
  120. case 8: // Serial2
  121. uart = &IMXRT_LPUART4;
  122. CCM_CCGR1 |= CCM_CCGR1_LPUART4(CCM_CCGR_ON);
  123. hwtrigger = DMAMUX_SOURCE_LPUART4_TX;
  124. break;
  125. case 14: // Serial3
  126. uart = &IMXRT_LPUART2;
  127. CCM_CCGR0 |= CCM_CCGR0_LPUART2(CCM_CCGR_ON);
  128. hwtrigger = DMAMUX_SOURCE_LPUART2_TX;
  129. break;
  130. case 17: // Serial4
  131. uart = &IMXRT_LPUART3;
  132. CCM_CCGR0 |= CCM_CCGR0_LPUART3(CCM_CCGR_ON);
  133. hwtrigger = DMAMUX_SOURCE_LPUART3_TX;
  134. break;
  135. case 20: // Serial5
  136. #if defined(ARDUINO_TEENSY40)
  137. case 39: // Serial5 alt
  138. #elif defined(ARDUINO_TEENSY41)
  139. case 47:
  140. #endif
  141. uart = &IMXRT_LPUART8;
  142. CCM_CCGR6 |= CCM_CCGR6_LPUART8(CCM_CCGR_ON);
  143. hwtrigger = DMAMUX_SOURCE_LPUART8_TX;
  144. break;
  145. case 24: // Serial6
  146. uart = &IMXRT_LPUART1;
  147. CCM_CCGR5 |= CCM_CCGR5_LPUART1(CCM_CCGR_ON);
  148. hwtrigger = DMAMUX_SOURCE_LPUART1_TX;
  149. break;
  150. case 29: // Serial7
  151. uart = &IMXRT_LPUART7;
  152. CCM_CCGR5 |= CCM_CCGR5_LPUART7(CCM_CCGR_ON);
  153. hwtrigger = DMAMUX_SOURCE_LPUART7_TX;
  154. break;
  155. #if defined(ARDUINO_TEENSY41)
  156. case 35:
  157. uart = &IMXRT_LPUART5;
  158. CCM_CCGR3 |= CCM_CCGR3_LPUART5(CCM_CCGR_ON);
  159. hwtrigger = DMAMUX_SOURCE_LPUART5_TX;
  160. break;
  161. #endif
  162. #endif
  163. default:
  164. return false; // pin not supported
  165. }
  166. if (!dma) {
  167. dma = new DMAChannel;
  168. if (!dma) return false; // unable to allocate DMA channel
  169. }
  170. #if defined(__IMXRT1062__)
  171. // Convert Baud
  172. // Computed values for 4mhz
  173. uart->CTRL = 0; // clear everything
  174. uart->BAUD = LPUART_BAUD_OSR(5) | LPUART_BAUD_SBR(1) | LPUART_BAUD_TDMAE; // set baud configure for transfer DMA
  175. uart->PINCFG = 0;
  176. uint16_t tx_fifo_size = (((uart->FIFO >> 4) & 0x7) << 2);
  177. uint8_t tx_water = (tx_fifo_size < 16) ? tx_fifo_size >> 1 : 7;
  178. // uart->WATER = LPUART_WATER_TXWATER(1); // guessing here?
  179. // uart->FIFO = 0; // disable the fifo.
  180. uart->WATER = LPUART_WATER_TXWATER(tx_water);
  181. uart->FIFO |= LPUART_FIFO_TXFE;
  182. uart->CTRL = (LPUART_CTRL_TE /*| LPUART_CTRL_TIE */ | LPUART_CTRL_TXINV); // enable transmitter and invert
  183. // We need to configure the TX pin now.
  184. *(portControlRegister(pin)) = IOMUXC_PAD_SRE | IOMUXC_PAD_DSE(3) | IOMUXC_PAD_SPEED(3);
  185. *(portConfigRegister(pin)) = 2; // from hardware table for each one, but I think they are all 2...
  186. dma->destination((volatile uint8_t&)uart->DATA);
  187. //Serial.printf("HWTrigger: %x\n", hwtrigger);
  188. #else
  189. #if defined(KINETISK)
  190. if (divisor < 32) divisor = 32;
  191. uart->BDH = (divisor >> 13) & 0x1F;
  192. uart->BDL = (divisor >> 5) & 0xFF;
  193. uart->C4 = divisor & 0x1F;
  194. #elif defined(KINETISL)
  195. uart->BDH = (divisor >> 8) & 0x1F;
  196. uart->BDL = divisor & 0xFF;
  197. uart->C4 = 11;
  198. #endif
  199. uart->C1 = 0;
  200. uart->C2 = UART_C2_TE | UART_C2_TIE;
  201. uart->C3 = UART_C3_TXINV;
  202. uart->C5 = UART_C5_TDMAS;
  203. #if defined(KINETISK)
  204. uart->PFIFO = 0; // TODO: is this ok for Serial3-6?
  205. #endif
  206. *(portConfigRegister(pin)) = portconfig;
  207. dma->destination(uart->D);
  208. #endif
  209. dma->triggerAtHardwareEvent(hwtrigger);
  210. memset(drawBuffer, 0, numled * 3);
  211. return true;
  212. }
  213. void WS2812Serial::show()
  214. {
  215. uint32_t microseconds_per_led, bytes_per_led;
  216. // wait if prior DMA still in progress
  217. #if defined(KINETISK)
  218. while ((DMA_ERQ & (1 << dma->channel))) {
  219. yield();
  220. }
  221. #elif defined(KINETISL)
  222. while ((dma->CFG->DCR & DMA_DCR_ERQ)) {
  223. yield();
  224. }
  225. #elif defined(__IMXRT1062__)
  226. //Serial.println("Show called");
  227. while ((DMA_ERQ & (1 << dma->channel))) {
  228. yield();
  229. }
  230. //Serial.println("After Yield");
  231. #endif
  232. // copy drawing buffer to frame buffer
  233. if (config < 6) {
  234. // RGB
  235. const uint8_t *p = drawBuffer;
  236. const uint8_t *end = p + (numled * 3);
  237. uint8_t *fb = frameBuffer;
  238. while (p < end) {
  239. uint8_t b = *p++;
  240. uint8_t g = *p++;
  241. uint8_t r = *p++;
  242. uint32_t mult = brightness + 1;
  243. b = (b * mult) >> 8;
  244. g = (g * mult) >> 8;
  245. r = (r * mult) >> 8;
  246. uint32_t n=0;
  247. switch (config) {
  248. case WS2812_RGB: n = (r << 16) | (g << 8) | b; break;
  249. case WS2812_RBG: n = (r << 16) | (b << 8) | g; break;
  250. case WS2812_GRB: n = (g << 16) | (r << 8) | b; break;
  251. case WS2812_GBR: n = (g << 16) | (b << 8) | r; break;
  252. case WS2812_BRG: n = (b << 16) | (r << 8) | g; break;
  253. case WS2812_BGR: n = (b << 16) | (g << 8) | r; break;
  254. }
  255. const uint8_t *stop = fb + 12;
  256. do {
  257. uint8_t x = 0x08;
  258. if (!(n & 0x00800000)) x |= 0x07;
  259. if (!(n & 0x00400000)) x |= 0xE0;
  260. n <<= 2;
  261. *fb++ = x;
  262. } while (fb < stop);
  263. }
  264. microseconds_per_led = 30;
  265. bytes_per_led = 12;
  266. } else {
  267. // RGBW
  268. const uint8_t *p = drawBuffer;
  269. const uint8_t *end = p + (numled * 4);
  270. uint8_t *fb = frameBuffer;
  271. while (p < end) {
  272. uint8_t b = *p++;
  273. uint8_t g = *p++;
  274. uint8_t r = *p++;
  275. uint8_t w = *p++;
  276. uint32_t mult = brightness + 1;
  277. b = (b * mult) >> 8;
  278. g = (g * mult) >> 8;
  279. r = (r * mult) >> 8;
  280. w = (w * mult) >> 8;
  281. uint32_t n=0;
  282. switch (config) {
  283. case WS2812_RGBW: n = (r << 24) | (g << 16) | (b << 8) | w; break;
  284. case WS2812_RBGW: n = (r << 24) | (b << 16) | (g << 8) | w; break;
  285. case WS2812_GRBW: n = (g << 24) | (r << 16) | (b << 8) | w; break;
  286. case WS2812_GBRW: n = (g << 24) | (b << 16) | (r << 8) | w; break;
  287. case WS2812_BRGW: n = (b << 24) | (r << 16) | (g << 8) | w; break;
  288. case WS2812_BGRW: n = (b << 24) | (b << 16) | (r << 8) | w; break;
  289. case WS2812_WRGB: n = (w << 24) | (r << 16) | (g << 8) | b; break;
  290. case WS2812_WRBG: n = (w << 24) | (r << 16) | (b << 8) | g; break;
  291. case WS2812_WGRB: n = (w << 24) | (g << 16) | (r << 8) | b; break;
  292. case WS2812_WGBR: n = (w << 24) | (g << 16) | (b << 8) | r; break;
  293. case WS2812_WBRG: n = (w << 24) | (b << 16) | (r << 8) | g; break;
  294. case WS2812_WBGR: n = (w << 24) | (b << 16) | (g << 8) | r; break;
  295. case WS2812_RWGB: n = (r << 24) | (w << 16) | (g << 8) | b; break;
  296. case WS2812_RWBG: n = (r << 24) | (w << 16) | (b << 8) | g; break;
  297. case WS2812_GWRB: n = (g << 24) | (w << 16) | (r << 8) | b; break;
  298. case WS2812_GWBR: n = (g << 24) | (w << 16) | (b << 8) | r; break;
  299. case WS2812_BWRG: n = (b << 24) | (w << 16) | (r << 8) | g; break;
  300. case WS2812_BWGR: n = (b << 24) | (w << 16) | (g << 8) | r; break;
  301. case WS2812_RGWB: n = (r << 24) | (g << 16) | (w << 8) | b; break;
  302. case WS2812_RBWG: n = (r << 24) | (b << 16) | (w << 8) | g; break;
  303. case WS2812_GRWB: n = (g << 24) | (r << 16) | (w << 8) | b; break;
  304. case WS2812_GBWR: n = (g << 24) | (b << 16) | (w << 8) | r; break;
  305. case WS2812_BRWG: n = (b << 24) | (r << 16) | (w << 8) | g; break;
  306. case WS2812_BGWR: n = (b << 24) | (g << 16) | (w << 8) | r; break;
  307. }
  308. const uint8_t *stop = fb + 16;
  309. do {
  310. uint8_t x = 0x08;
  311. if (!(n & 0x80000000)) x |= 0x07;
  312. if (!(n & 0x40000000)) x |= 0xE0;
  313. n <<= 2;
  314. *fb++ = x;
  315. } while (fb < stop);
  316. }
  317. microseconds_per_led = 40;
  318. bytes_per_led = 16;
  319. }
  320. // wait 300us WS2812 reset time
  321. uint32_t min_elapsed = (numled * microseconds_per_led) + 300;
  322. if (min_elapsed < 2500) min_elapsed = 2500;
  323. uint32_t m;
  324. while (1) {
  325. m = micros();
  326. if ((m - prior_micros) > min_elapsed) break;
  327. yield();
  328. }
  329. prior_micros = m;
  330. // start DMA transfer to update LEDs :-)
  331. #if defined(KINETISK)
  332. dma->sourceBuffer(frameBuffer, numled * bytes_per_led);
  333. dma->transferSize(1);
  334. dma->transferCount(numled * bytes_per_led);
  335. dma->disableOnCompletion();
  336. dma->enable();
  337. #elif defined(KINETISL)
  338. dma->CFG->SAR = frameBuffer;
  339. dma->CFG->DSR_BCR = 0x01000000;
  340. dma->CFG->DSR_BCR = numled * bytes_per_led;
  341. dma->CFG->DCR = DMA_DCR_ERQ | DMA_DCR_CS | DMA_DCR_SSIZE(1) |
  342. DMA_DCR_SINC | DMA_DCR_DSIZE(1) | DMA_DCR_D_REQ;
  343. #elif defined(__IMXRT1062__)
  344. // See if we need to muck with DMA cache...
  345. if ((uint32_t)frameBuffer >= 0x20200000u) arm_dcache_flush(frameBuffer, numled * bytes_per_led);
  346. dma->sourceBuffer(frameBuffer, numled * bytes_per_led);
  347. // dma->transferSize(1);
  348. dma->transferCount(numled * bytes_per_led);
  349. dma->disableOnCompletion();
  350. /* Serial.printf("%x %x:", (uint32_t)dma, (uint32_t)dma->TCD);
  351. 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,
  352. dma->TCD->SOFF, dma->TCD->ATTR, dma->TCD->NBYTES, dma->TCD->SLAST, (uint32_t)dma->TCD->DADDR,
  353. dma->TCD->DOFF, dma->TCD->CITER, dma->TCD->DLASTSGA, dma->TCD->CSR, dma->TCD->BITER);
  354. */
  355. uart->STAT = 0; // try clearing out the status
  356. dma->enable();
  357. #endif
  358. }