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.

366 satır
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. 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_LPUART3_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. uint32_t microseconds_per_led, bytes_per_led;
  202. // wait if prior DMA still in progress
  203. #if defined(KINETISK)
  204. while ((DMA_ERQ & (1 << dma->channel))) {
  205. yield();
  206. }
  207. #elif defined(KINETISL)
  208. while ((dma->CFG->DCR & DMA_DCR_ERQ)) {
  209. yield();
  210. }
  211. #elif defined(__IMXRT1062__)
  212. //Serial.println("Show called");
  213. while ((DMA_ERQ & (1 << dma->channel))) {
  214. yield();
  215. }
  216. //Serial.println("After Yield");
  217. #endif
  218. // copy drawing buffer to frame buffer
  219. if (config < 6) {
  220. // RGB
  221. const uint8_t *p = drawBuffer;
  222. const uint8_t *end = p + (numled * 3);
  223. uint8_t *fb = frameBuffer;
  224. while (p < end) {
  225. uint8_t b = *p++;
  226. uint8_t g = *p++;
  227. uint8_t r = *p++;
  228. uint32_t mult = brightness + 1;
  229. b = (b * mult) >> 8;
  230. g = (g * mult) >> 8;
  231. r = (r * mult) >> 8;
  232. uint32_t n=0;
  233. switch (config) {
  234. case WS2812_RGB: n = (r << 16) | (g << 8) | b; break;
  235. case WS2812_RBG: n = (r << 16) | (b << 8) | g; break;
  236. case WS2812_GRB: n = (g << 16) | (r << 8) | b; break;
  237. case WS2812_GBR: n = (g << 16) | (b << 8) | r; break;
  238. case WS2812_BRG: n = (b << 16) | (r << 8) | g; break;
  239. case WS2812_BGR: n = (b << 16) | (g << 8) | r; break;
  240. }
  241. const uint8_t *stop = fb + 12;
  242. do {
  243. uint8_t x = 0x08;
  244. if (!(n & 0x00800000)) x |= 0x07;
  245. if (!(n & 0x00400000)) x |= 0xE0;
  246. n <<= 2;
  247. *fb++ = x;
  248. } while (fb < stop);
  249. }
  250. microseconds_per_led = 30;
  251. bytes_per_led = 12;
  252. } else {
  253. // RGBW
  254. const uint8_t *p = drawBuffer;
  255. const uint8_t *end = p + (numled * 4);
  256. uint8_t *fb = frameBuffer;
  257. while (p < end) {
  258. uint8_t b = *p++;
  259. uint8_t g = *p++;
  260. uint8_t r = *p++;
  261. uint8_t w = *p++;
  262. uint32_t mult = brightness + 1;
  263. b = (b * mult) >> 8;
  264. g = (g * mult) >> 8;
  265. r = (r * mult) >> 8;
  266. w = (w * mult) >> 8;
  267. uint32_t n=0;
  268. switch (config) {
  269. case WS2812_RGBW: n = (r << 24) | (g << 16) | (b << 8) | w; break;
  270. case WS2812_RBGW: n = (r << 24) | (b << 16) | (g << 8) | w; break;
  271. case WS2812_GRBW: n = (g << 24) | (r << 16) | (b << 8) | w; break;
  272. case WS2812_GBRW: n = (g << 24) | (b << 16) | (r << 8) | w; break;
  273. case WS2812_BRGW: n = (b << 24) | (r << 16) | (g << 8) | w; break;
  274. case WS2812_BGRW: n = (b << 24) | (b << 16) | (r << 8) | w; break;
  275. case WS2812_WRGB: n = (w << 24) | (r << 16) | (g << 8) | b; break;
  276. case WS2812_WRBG: n = (w << 24) | (r << 16) | (b << 8) | g; break;
  277. case WS2812_WGRB: n = (w << 24) | (g << 16) | (r << 8) | b; break;
  278. case WS2812_WGBR: n = (w << 24) | (g << 16) | (b << 8) | r; break;
  279. case WS2812_WBRG: n = (w << 24) | (b << 16) | (r << 8) | g; break;
  280. case WS2812_WBGR: n = (w << 24) | (b << 16) | (g << 8) | r; break;
  281. case WS2812_RWGB: n = (r << 24) | (w << 16) | (g << 8) | b; break;
  282. case WS2812_RWBG: n = (r << 24) | (w << 16) | (b << 8) | g; break;
  283. case WS2812_GWRB: n = (g << 24) | (w << 16) | (r << 8) | b; break;
  284. case WS2812_GWBR: n = (g << 24) | (w << 16) | (b << 8) | r; break;
  285. case WS2812_BWRG: n = (b << 24) | (w << 16) | (r << 8) | g; break;
  286. case WS2812_BWGR: n = (b << 24) | (w << 16) | (g << 8) | r; break;
  287. case WS2812_RGWB: n = (r << 24) | (g << 16) | (w << 8) | b; break;
  288. case WS2812_RBWG: n = (r << 24) | (b << 16) | (w << 8) | g; break;
  289. case WS2812_GRWB: n = (g << 24) | (r << 16) | (w << 8) | b; break;
  290. case WS2812_GBWR: n = (g << 24) | (b << 16) | (w << 8) | r; break;
  291. case WS2812_BRWG: n = (b << 24) | (r << 16) | (w << 8) | g; break;
  292. case WS2812_BGWR: n = (b << 24) | (g << 16) | (w << 8) | r; break;
  293. }
  294. const uint8_t *stop = fb + 16;
  295. do {
  296. uint8_t x = 0x08;
  297. if (!(n & 0x80000000)) x |= 0x07;
  298. if (!(n & 0x40000000)) x |= 0xE0;
  299. n <<= 2;
  300. *fb++ = x;
  301. } while (fb < stop);
  302. }
  303. microseconds_per_led = 40;
  304. bytes_per_led = 16;
  305. }
  306. // wait 300us WS2812 reset time
  307. uint32_t min_elapsed = (numled * microseconds_per_led) + 300;
  308. if (min_elapsed < 2500) min_elapsed = 2500;
  309. uint32_t m;
  310. while (1) {
  311. m = micros();
  312. if ((m - prior_micros) > min_elapsed) break;
  313. yield();
  314. }
  315. prior_micros = m;
  316. // start DMA transfer to update LEDs :-)
  317. #if defined(KINETISK)
  318. dma->sourceBuffer(frameBuffer, numled * bytes_per_led);
  319. dma->transferSize(1);
  320. dma->transferCount(numled * bytes_per_led);
  321. dma->disableOnCompletion();
  322. dma->enable();
  323. #elif defined(KINETISL)
  324. dma->CFG->SAR = frameBuffer;
  325. dma->CFG->DSR_BCR = 0x01000000;
  326. dma->CFG->DSR_BCR = numled * bytes_per_led;
  327. dma->CFG->DCR = DMA_DCR_ERQ | DMA_DCR_CS | DMA_DCR_SSIZE(1) |
  328. DMA_DCR_SINC | DMA_DCR_DSIZE(1) | DMA_DCR_D_REQ;
  329. #elif defined(__IMXRT1062__)
  330. // See if we need to muck with DMA cache...
  331. if ((uint32_t)frameBuffer >= 0x20200000u) arm_dcache_flush(frameBuffer, numled * bytes_per_led);
  332. dma->sourceBuffer(frameBuffer, numled * bytes_per_led);
  333. // dma->transferSize(1);
  334. dma->transferCount(numled * bytes_per_led);
  335. dma->disableOnCompletion();
  336. /* Serial.printf("%x %x:", (uint32_t)dma, (uint32_t)dma->TCD);
  337. 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,
  338. dma->TCD->SOFF, dma->TCD->ATTR, dma->TCD->NBYTES, dma->TCD->SLAST, (uint32_t)dma->TCD->DADDR,
  339. dma->TCD->DOFF, dma->TCD->CITER, dma->TCD->DLASTSGA, dma->TCD->CSR, dma->TCD->BITER);
  340. */
  341. uart->STAT = 0; // try clearing out the status
  342. dma->enable();
  343. #endif
  344. }