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.

WS2812Serial.cpp 12KB

7 年之前
7 年之前
7 年之前
7 年之前
7 年之前
7 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. uint32_t pinmuxval = 2; // most of them use mux ALT2
  186. #if defined(ARDUINO_TEENSY41)
  187. if (pin == 35) pinmuxval = 1;
  188. #endif
  189. *(portConfigRegister(pin)) = pinmuxval;
  190. dma->destination((volatile uint8_t&)uart->DATA);
  191. //Serial.printf("HWTrigger: %x\n", hwtrigger);
  192. #else
  193. #if defined(KINETISK)
  194. if (divisor < 32) divisor = 32;
  195. uart->BDH = (divisor >> 13) & 0x1F;
  196. uart->BDL = (divisor >> 5) & 0xFF;
  197. uart->C4 = divisor & 0x1F;
  198. #elif defined(KINETISL)
  199. uart->BDH = (divisor >> 8) & 0x1F;
  200. uart->BDL = divisor & 0xFF;
  201. uart->C4 = 11;
  202. #endif
  203. uart->C1 = 0;
  204. uart->C2 = UART_C2_TE | UART_C2_TIE;
  205. uart->C3 = UART_C3_TXINV;
  206. uart->C5 = UART_C5_TDMAS;
  207. #if defined(KINETISK)
  208. uart->PFIFO = 0; // TODO: is this ok for Serial3-6?
  209. #endif
  210. *(portConfigRegister(pin)) = portconfig;
  211. dma->destination(uart->D);
  212. #endif
  213. dma->triggerAtHardwareEvent(hwtrigger);
  214. memset(drawBuffer, 0, numled * 3);
  215. return true;
  216. }
  217. void WS2812Serial::show()
  218. {
  219. uint32_t microseconds_per_led, bytes_per_led;
  220. // wait if prior DMA still in progress
  221. #if defined(KINETISK)
  222. while ((DMA_ERQ & (1 << dma->channel))) {
  223. yield();
  224. }
  225. #elif defined(KINETISL)
  226. while ((dma->CFG->DCR & DMA_DCR_ERQ)) {
  227. yield();
  228. }
  229. #elif defined(__IMXRT1062__)
  230. //Serial.println("Show called");
  231. while ((DMA_ERQ & (1 << dma->channel))) {
  232. yield();
  233. }
  234. //Serial.println("After Yield");
  235. #endif
  236. // copy drawing buffer to frame buffer
  237. if (config < 6) {
  238. // RGB
  239. const uint8_t *p = drawBuffer;
  240. const uint8_t *end = p + (numled * 3);
  241. uint8_t *fb = frameBuffer;
  242. while (p < end) {
  243. uint8_t b = *p++;
  244. uint8_t g = *p++;
  245. uint8_t r = *p++;
  246. uint32_t mult = brightness + 1;
  247. b = (b * mult) >> 8;
  248. g = (g * mult) >> 8;
  249. r = (r * mult) >> 8;
  250. uint32_t n=0;
  251. switch (config) {
  252. case WS2812_RGB: n = (r << 16) | (g << 8) | b; break;
  253. case WS2812_RBG: n = (r << 16) | (b << 8) | g; break;
  254. case WS2812_GRB: n = (g << 16) | (r << 8) | b; break;
  255. case WS2812_GBR: n = (g << 16) | (b << 8) | r; break;
  256. case WS2812_BRG: n = (b << 16) | (r << 8) | g; break;
  257. case WS2812_BGR: n = (b << 16) | (g << 8) | r; break;
  258. }
  259. const uint8_t *stop = fb + 12;
  260. do {
  261. uint8_t x = 0x08;
  262. if (!(n & 0x00800000)) x |= 0x07;
  263. if (!(n & 0x00400000)) x |= 0xE0;
  264. n <<= 2;
  265. *fb++ = x;
  266. } while (fb < stop);
  267. }
  268. microseconds_per_led = 30;
  269. bytes_per_led = 12;
  270. } else {
  271. // RGBW
  272. const uint8_t *p = drawBuffer;
  273. const uint8_t *end = p + (numled * 4);
  274. uint8_t *fb = frameBuffer;
  275. while (p < end) {
  276. uint8_t b = *p++;
  277. uint8_t g = *p++;
  278. uint8_t r = *p++;
  279. uint8_t w = *p++;
  280. uint32_t mult = brightness + 1;
  281. b = (b * mult) >> 8;
  282. g = (g * mult) >> 8;
  283. r = (r * mult) >> 8;
  284. w = (w * mult) >> 8;
  285. uint32_t n=0;
  286. switch (config) {
  287. case WS2812_RGBW: n = (r << 24) | (g << 16) | (b << 8) | w; break;
  288. case WS2812_RBGW: n = (r << 24) | (b << 16) | (g << 8) | w; break;
  289. case WS2812_GRBW: n = (g << 24) | (r << 16) | (b << 8) | w; break;
  290. case WS2812_GBRW: n = (g << 24) | (b << 16) | (r << 8) | w; break;
  291. case WS2812_BRGW: n = (b << 24) | (r << 16) | (g << 8) | w; break;
  292. case WS2812_BGRW: n = (b << 24) | (b << 16) | (r << 8) | w; break;
  293. case WS2812_WRGB: n = (w << 24) | (r << 16) | (g << 8) | b; break;
  294. case WS2812_WRBG: n = (w << 24) | (r << 16) | (b << 8) | g; break;
  295. case WS2812_WGRB: n = (w << 24) | (g << 16) | (r << 8) | b; break;
  296. case WS2812_WGBR: n = (w << 24) | (g << 16) | (b << 8) | r; break;
  297. case WS2812_WBRG: n = (w << 24) | (b << 16) | (r << 8) | g; break;
  298. case WS2812_WBGR: n = (w << 24) | (b << 16) | (g << 8) | r; break;
  299. case WS2812_RWGB: n = (r << 24) | (w << 16) | (g << 8) | b; break;
  300. case WS2812_RWBG: n = (r << 24) | (w << 16) | (b << 8) | g; break;
  301. case WS2812_GWRB: n = (g << 24) | (w << 16) | (r << 8) | b; break;
  302. case WS2812_GWBR: n = (g << 24) | (w << 16) | (b << 8) | r; break;
  303. case WS2812_BWRG: n = (b << 24) | (w << 16) | (r << 8) | g; break;
  304. case WS2812_BWGR: n = (b << 24) | (w << 16) | (g << 8) | r; break;
  305. case WS2812_RGWB: n = (r << 24) | (g << 16) | (w << 8) | b; break;
  306. case WS2812_RBWG: n = (r << 24) | (b << 16) | (w << 8) | g; break;
  307. case WS2812_GRWB: n = (g << 24) | (r << 16) | (w << 8) | b; break;
  308. case WS2812_GBWR: n = (g << 24) | (b << 16) | (w << 8) | r; break;
  309. case WS2812_BRWG: n = (b << 24) | (r << 16) | (w << 8) | g; break;
  310. case WS2812_BGWR: n = (b << 24) | (g << 16) | (w << 8) | r; break;
  311. }
  312. const uint8_t *stop = fb + 16;
  313. do {
  314. uint8_t x = 0x08;
  315. if (!(n & 0x80000000)) x |= 0x07;
  316. if (!(n & 0x40000000)) x |= 0xE0;
  317. n <<= 2;
  318. *fb++ = x;
  319. } while (fb < stop);
  320. }
  321. microseconds_per_led = 40;
  322. bytes_per_led = 16;
  323. }
  324. // wait 300us WS2812 reset time
  325. uint32_t min_elapsed = (numled * microseconds_per_led) + 300;
  326. if (min_elapsed < 2500) min_elapsed = 2500;
  327. uint32_t m;
  328. while (1) {
  329. m = micros();
  330. if ((m - prior_micros) > min_elapsed) break;
  331. yield();
  332. }
  333. prior_micros = m;
  334. // start DMA transfer to update LEDs :-)
  335. #if defined(KINETISK)
  336. dma->sourceBuffer(frameBuffer, numled * bytes_per_led);
  337. dma->transferSize(1);
  338. dma->transferCount(numled * bytes_per_led);
  339. dma->disableOnCompletion();
  340. dma->enable();
  341. #elif defined(KINETISL)
  342. dma->CFG->SAR = frameBuffer;
  343. dma->CFG->DSR_BCR = 0x01000000;
  344. dma->CFG->DSR_BCR = numled * bytes_per_led;
  345. dma->CFG->DCR = DMA_DCR_ERQ | DMA_DCR_CS | DMA_DCR_SSIZE(1) |
  346. DMA_DCR_SINC | DMA_DCR_DSIZE(1) | DMA_DCR_D_REQ;
  347. #elif defined(__IMXRT1062__)
  348. // See if we need to muck with DMA cache...
  349. if ((uint32_t)frameBuffer >= 0x20200000u) arm_dcache_flush(frameBuffer, numled * bytes_per_led);
  350. dma->sourceBuffer(frameBuffer, numled * bytes_per_led);
  351. // dma->transferSize(1);
  352. dma->transferCount(numled * bytes_per_led);
  353. dma->disableOnCompletion();
  354. /* Serial.printf("%x %x:", (uint32_t)dma, (uint32_t)dma->TCD);
  355. 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,
  356. dma->TCD->SOFF, dma->TCD->ATTR, dma->TCD->NBYTES, dma->TCD->SLAST, (uint32_t)dma->TCD->DADDR,
  357. dma->TCD->DOFF, dma->TCD->CITER, dma->TCD->DLASTSGA, dma->TCD->CSR, dma->TCD->BITER);
  358. */
  359. uart->STAT = 0; // try clearing out the status
  360. dma->enable();
  361. #endif
  362. }