Teensy 4.1 core updated for C++20
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.

690 lines
19KB

  1. #include "HardwareSerial.h"
  2. #include "core_pins.h"
  3. #include "Arduino.h"
  4. //#include "debug/printf.h"
  5. /*typedef struct {
  6. const uint32_t VERID;
  7. const uint32_t PARAM;
  8. volatile uint32_t GLOBAL;
  9. volatile uint32_t PINCFG;
  10. volatile uint32_t BAUD;
  11. volatile uint32_t STAT;
  12. volatile uint32_t CTRL;
  13. volatile uint32_t DATA;
  14. volatile uint32_t MATCH;
  15. volatile uint32_t MODIR;
  16. volatile uint32_t FIFO;
  17. volatile uint32_t WATER;
  18. } IMXRT_LPUART_t; */
  19. //. From Onewire utility files
  20. #define PIN_TO_BASEREG(pin) (portOutputRegister(pin))
  21. #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin))
  22. #define IO_REG_TYPE uint32_t
  23. #define IO_REG_BASE_ATTR
  24. #define IO_REG_MASK_ATTR
  25. #define DIRECT_READ(base, mask) ((*((base)+2) & (mask)) ? 1 : 0)
  26. #define DIRECT_MODE_INPUT(base, mask) (*((base)+1) &= ~(mask))
  27. #define DIRECT_MODE_OUTPUT(base, mask) (*((base)+1) |= (mask))
  28. #define DIRECT_WRITE_LOW(base, mask) (*((base)+34) = (mask))
  29. #define DIRECT_WRITE_HIGH(base, mask) (*((base)+33) = (mask))
  30. #define UART_CLOCK 24000000
  31. #ifndef SERIAL1_TX_BUFFER_SIZE
  32. #define SERIAL1_TX_BUFFER_SIZE 64 // number of outgoing bytes to buffer
  33. #endif
  34. #ifndef SERIAL1_RX_BUFFER_SIZE
  35. #define SERIAL1_RX_BUFFER_SIZE 64 // number of incoming bytes to buffer
  36. #endif
  37. #ifndef SERIAL2_TX_BUFFER_SIZE
  38. #define SERIAL2_TX_BUFFER_SIZE 40 // number of outgoing bytes to buffer
  39. #endif
  40. #ifndef SERIAL2_RX_BUFFER_SIZE
  41. #define SERIAL2_RX_BUFFER_SIZE 64 // number of incoming bytes to buffer
  42. #endif
  43. #ifndef SERIAL3_TX_BUFFER_SIZE
  44. #define SERIAL3_TX_BUFFER_SIZE 40 // number of outgoing bytes to buffer
  45. #endif
  46. #ifndef SERIAL3_RX_BUFFER_SIZE
  47. #define SERIAL3_RX_BUFFER_SIZE 64 // number of incoming bytes to buffer
  48. #endif
  49. #ifndef SERIAL4_TX_BUFFER_SIZE
  50. #define SERIAL4_TX_BUFFER_SIZE 40 // number of outgoing bytes to buffer
  51. #endif
  52. #ifndef SERIAL4_RX_BUFFER_SIZE
  53. #define SERIAL4_RX_BUFFER_SIZE 64 // number of incoming bytes to buffer
  54. #endif
  55. #ifndef SERIAL5_TX_BUFFER_SIZE
  56. #define SERIAL5_TX_BUFFER_SIZE 40 // number of outgoing bytes to buffer
  57. #endif
  58. #ifndef SERIAL5_RX_BUFFER_SIZE
  59. #define SERIAL5_RX_BUFFER_SIZE 64 // number of incoming bytes to buffer
  60. #endif
  61. #ifndef SERIAL6_TX_BUFFER_SIZE
  62. #define SERIAL6_TX_BUFFER_SIZE 40 // number of outgoing bytes to buffer
  63. #endif
  64. #ifndef SERIAL6_RX_BUFFER_SIZE
  65. #define SERIAL6_RX_BUFFER_SIZE 64 // number of incoming bytes to buffer
  66. #endif
  67. #ifndef SERIAL7_TX_BUFFER_SIZE
  68. #define SERIAL7_TX_BUFFER_SIZE 40 // number of outgoing bytes to buffer
  69. #endif
  70. #ifndef SERIAL7_RX_BUFFER_SIZE
  71. #define SERIAL7_RX_BUFFER_SIZE 64 // number of incoming bytes to buffer
  72. #endif
  73. #ifndef SERIAL8_TX_BUFFER_SIZE
  74. #define SERIAL8_TX_BUFFER_SIZE 40 // number of outgoing bytes to buffer
  75. #endif
  76. #ifndef SERIAL8_RX_BUFFER_SIZE
  77. #define SERIAL8_RX_BUFFER_SIZE 64 // number of incoming bytes to buffer
  78. #endif
  79. #define IRQ_PRIORITY 64 // 0 = highest priority, 255 = lowest
  80. #define CTRL_ENABLE (LPUART_CTRL_TE | LPUART_CTRL_RE | LPUART_CTRL_RIE | LPUART_CTRL_ILIE)
  81. #define CTRL_TX_ACTIVE (CTRL_ENABLE | LPUART_CTRL_TIE)
  82. #define CTRL_TX_COMPLETING (CTRL_ENABLE | LPUART_CTRL_TCIE)
  83. #define CTRL_TX_INACTIVE CTRL_ENABLE
  84. // Copied from T3.x - probably should move to other location.
  85. int nvic_execution_priority(void)
  86. {
  87. uint32_t priority=256;
  88. uint32_t primask, faultmask, basepri, ipsr;
  89. // full algorithm in ARM DDI0403D, page B1-639
  90. // this isn't quite complete, but hopefully good enough
  91. __asm__ volatile("mrs %0, faultmask\n" : "=r" (faultmask)::);
  92. if (faultmask) return -1;
  93. __asm__ volatile("mrs %0, primask\n" : "=r" (primask)::);
  94. if (primask) return 0;
  95. __asm__ volatile("mrs %0, ipsr\n" : "=r" (ipsr)::);
  96. if (ipsr) {
  97. if (ipsr < 16) priority = 0; // could be non-zero
  98. else priority = NVIC_GET_PRIORITY(ipsr - 16);
  99. }
  100. __asm__ volatile("mrs %0, basepri\n" : "=r" (basepri)::);
  101. if (basepri > 0 && basepri < priority) priority = basepri;
  102. return priority;
  103. }
  104. void HardwareSerial::begin(uint32_t baud, uint8_t format)
  105. {
  106. //printf("HardwareSerial begin\n");
  107. float base = (float)UART_CLOCK / (float)baud;
  108. float besterr = 1e20;
  109. int bestdiv = 1;
  110. int bestosr = 4;
  111. for (int osr=4; osr <= 32; osr++) {
  112. float div = base / (float)osr;
  113. int divint = (int)(div + 0.5f);
  114. if (divint < 1) divint = 1;
  115. else if (divint > 8191) divint = 8191;
  116. float err = ((float)divint - div) / div;
  117. if (err < 0.0f) err = -err;
  118. if (err <= besterr) {
  119. besterr = err;
  120. bestdiv = divint;
  121. bestosr = osr;
  122. }
  123. }
  124. //printf(" baud %d: osr=%d, div=%d\n", baud, bestosr, bestdiv);
  125. rx_buffer_head_ = 0;
  126. rx_buffer_tail_ = 0;
  127. tx_buffer_head_ = 0;
  128. tx_buffer_tail_ = 0;
  129. rts_low_watermark_ = rx_buffer_total_size_ - hardware->rts_low_watermark;
  130. rts_high_watermark_ = rx_buffer_total_size_ - hardware->rts_high_watermark;
  131. transmitting_ = 0;
  132. hardware->ccm_register |= hardware->ccm_value;
  133. uint32_t fastio = IOMUXC_PAD_SRE | IOMUXC_PAD_DSE(3) | IOMUXC_PAD_SPEED(3);
  134. *(portControlRegister(hardware->rx_pin)) = fastio;
  135. *(portControlRegister(hardware->tx_pin)) = fastio;
  136. *(portConfigRegister(hardware->rx_pin)) = hardware->rx_mux_val;
  137. *(portConfigRegister(hardware->tx_pin)) = hardware->tx_mux_val;
  138. //hardware->rx_mux_register = hardware->rx_mux_val;
  139. //hardware->tx_mux_register = hardware->tx_mux_val;
  140. hardware->rx_select_input_register = hardware->rx_select_val;
  141. port->BAUD = LPUART_BAUD_OSR(bestosr - 1) | LPUART_BAUD_SBR(bestdiv);
  142. port->PINCFG = 0;
  143. // Enable the transmitter, receiver and enable receiver interrupt
  144. attachInterruptVector(hardware->irq, hardware->irq_handler);
  145. NVIC_SET_PRIORITY(hardware->irq, hardware->irq_priority); // maybe should put into hardware...
  146. NVIC_ENABLE_IRQ(hardware->irq);
  147. uint16_t tx_fifo_size = (((port->FIFO >> 4) & 0x7) << 2);
  148. uint8_t tx_water = (tx_fifo_size < 16) ? tx_fifo_size >> 1 : 7;
  149. uint16_t rx_fifo_size = (((port->FIFO >> 0) & 0x7) << 2);
  150. uint8_t rx_water = (rx_fifo_size < 16) ? rx_fifo_size >> 1 : 7;
  151. /*
  152. Serial.printf("SerialX::begin stat:%x ctrl:%x fifo:%x water:%x\n", port->STAT, port->CTRL, port->FIFO, port->WATER );
  153. Serial.printf(" FIFO sizes: tx:%d rx:%d\n",tx_fifo_size, rx_fifo_size);
  154. Serial.printf(" Watermark tx:%d, rx: %d\n", tx_water, rx_water);
  155. */
  156. port->WATER = LPUART_WATER_RXWATER(rx_water) | LPUART_WATER_TXWATER(tx_water);
  157. port->FIFO |= LPUART_FIFO_TXFE | LPUART_FIFO_RXFE;
  158. port->CTRL = CTRL_TX_INACTIVE;
  159. //Serial.printf(" stat:%x ctrl:%x fifo:%x water:%x\n", port->STAT, port->CTRL, port->FIFO, port->WATER );
  160. };
  161. inline void HardwareSerial::rts_assert()
  162. {
  163. DIRECT_WRITE_LOW(rts_pin_baseReg_, rts_pin_bitmask_);
  164. }
  165. inline void HardwareSerial::rts_deassert()
  166. {
  167. DIRECT_WRITE_HIGH(rts_pin_baseReg_, rts_pin_bitmask_);
  168. }
  169. void HardwareSerial::end(void)
  170. {
  171. }
  172. void HardwareSerial::transmitterEnable(uint8_t pin)
  173. {
  174. while (transmitting_) ;
  175. pinMode(pin, OUTPUT);
  176. transmit_pin_baseReg_ = PIN_TO_BASEREG(pin);
  177. transmit_pin_bitmask_ = PIN_TO_BITMASK(pin);
  178. DIRECT_WRITE_LOW(transmit_pin_baseReg_, transmit_pin_bitmask_);
  179. }
  180. void HardwareSerial::setRX(uint8_t pin)
  181. {
  182. // Currently none of these have multiple
  183. // possible RX pins
  184. }
  185. void HardwareSerial::setTX(uint8_t pin, bool opendrain)
  186. {
  187. // Currently none of these have multiple
  188. // possible TX pins
  189. }
  190. bool HardwareSerial::attachRts(uint8_t pin)
  191. {
  192. if (!(hardware->ccm_register & hardware->ccm_value)) return 0;
  193. if (pin < CORE_NUM_DIGITAL) {
  194. rts_pin_baseReg_ = PIN_TO_BASEREG(pin);
  195. rts_pin_bitmask_ = PIN_TO_BITMASK(pin);
  196. pinMode(pin, OUTPUT);
  197. rts_assert();
  198. } else {
  199. rts_pin_baseReg_ = NULL;
  200. return 0;
  201. }
  202. return 1;
  203. }
  204. bool HardwareSerial::attachCts(uint8_t pin)
  205. {
  206. if (!(hardware->ccm_register & hardware->ccm_value)) return false;
  207. if ((pin != 0xff) && (pin == hardware->cts_pin)) {
  208. *(portConfigRegister(hardware->cts_pin)) = hardware->cts_mux_val;
  209. port->MODIR |= LPUART_MODIR_TXCTSE;
  210. return true;
  211. } else {
  212. port->MODIR &= ~LPUART_MODIR_TXCTSE;
  213. return false;
  214. }
  215. }
  216. void HardwareSerial::clear(void)
  217. {
  218. // BUGBUG:: deal with FIFO
  219. rx_buffer_head_ = rx_buffer_tail_;
  220. if (rts_pin_baseReg_) rts_assert();
  221. }
  222. int HardwareSerial::availableForWrite(void)
  223. {
  224. uint32_t head, tail;
  225. head = tx_buffer_head_;
  226. tail = tx_buffer_tail_;
  227. if (head >= tail) return tx_buffer_total_size_ - 1 - head + tail;
  228. return tail - head - 1;
  229. }
  230. size_t HardwareSerial::write9bit(uint32_t c)
  231. {
  232. return 0;
  233. }
  234. int HardwareSerial::available(void)
  235. {
  236. uint32_t head, tail;
  237. head = rx_buffer_head_;
  238. tail = rx_buffer_tail_;
  239. if (head >= tail) return head - tail;
  240. return rx_buffer_total_size_ + head - tail;
  241. }
  242. void HardwareSerial::addStorageForRead(void *buffer, size_t length)
  243. {
  244. rx_buffer_storage_ = (BUFTYPE*)buffer;
  245. if (buffer) {
  246. rx_buffer_total_size_ = rx_buffer_total_size_ + length;
  247. } else {
  248. rx_buffer_total_size_ = rx_buffer_total_size_;
  249. }
  250. rts_low_watermark_ = rx_buffer_total_size_ - hardware->rts_low_watermark;
  251. rts_high_watermark_ = rx_buffer_total_size_ - hardware->rts_high_watermark;
  252. }
  253. void HardwareSerial::addStorageForWrite(void *buffer, size_t length)
  254. {
  255. tx_buffer_storage_ = (BUFTYPE*)buffer;
  256. if (buffer) {
  257. tx_buffer_total_size_ = tx_buffer_total_size_ + length;
  258. } else {
  259. tx_buffer_total_size_ = tx_buffer_total_size_;
  260. }
  261. }
  262. int HardwareSerial::peek(void)
  263. {
  264. uint32_t head, tail;
  265. head = rx_buffer_head_;
  266. tail = rx_buffer_tail_;
  267. if (head == tail) return -1;
  268. if (++tail >= rx_buffer_total_size_) tail = 0;
  269. if (tail < rx_buffer_size_) {
  270. return rx_buffer_[tail];
  271. } else {
  272. return rx_buffer_storage_[tail-rx_buffer_size_];
  273. }
  274. }
  275. int HardwareSerial::read(void)
  276. {
  277. uint32_t head, tail;
  278. int c;
  279. head = rx_buffer_head_;
  280. tail = rx_buffer_tail_;
  281. if (head == tail) return -1;
  282. if (++tail >= rx_buffer_total_size_) tail = 0;
  283. if (tail < rx_buffer_size_) {
  284. c = rx_buffer_[tail];
  285. } else {
  286. c = rx_buffer_storage_[tail-rx_buffer_size_];
  287. }
  288. rx_buffer_tail_ = tail;
  289. if (rts_pin_baseReg_) {
  290. uint32_t avail;
  291. if (head >= tail) avail = head - tail;
  292. else avail = rx_buffer_total_size_ + head - tail;
  293. if (avail <= rts_low_watermark_) rts_assert();
  294. }
  295. return c;
  296. }
  297. void HardwareSerial::flush(void)
  298. {
  299. while (transmitting_) yield(); // wait
  300. }
  301. size_t HardwareSerial::write(uint8_t c)
  302. {
  303. uint32_t head, n;
  304. //digitalWrite(3, HIGH);
  305. //digitalWrite(5, HIGH);
  306. if (transmit_pin_baseReg_) DIRECT_WRITE_HIGH(transmit_pin_baseReg_, transmit_pin_bitmask_);
  307. head = tx_buffer_head_;
  308. if (++head >= tx_buffer_total_size_) head = 0;
  309. while (tx_buffer_tail_ == head) {
  310. int priority = nvic_execution_priority();
  311. if (priority <= hardware->irq_priority) {
  312. if ((port->STAT & LPUART_STAT_TDRE)) {
  313. uint32_t tail = tx_buffer_tail_;
  314. if (++tail >= tx_buffer_total_size_) tail = 0;
  315. if (tail < tx_buffer_size_) {
  316. n = tx_buffer_[tail];
  317. } else {
  318. n = tx_buffer_storage_[tail-tx_buffer_size_];
  319. }
  320. port->DATA = n;
  321. tx_buffer_tail_ = tail;
  322. }
  323. } else if (priority >= 256)
  324. {
  325. yield(); // wait
  326. }
  327. }
  328. //digitalWrite(5, LOW);
  329. //Serial.printf("WR %x %d %d %d %x %x\n", c, head, tx_buffer_size_, tx_buffer_total_size_, (uint32_t)tx_buffer_, (uint32_t)tx_buffer_storage_);
  330. if (head < tx_buffer_size_) {
  331. tx_buffer_[head] = c;
  332. } else {
  333. tx_buffer_storage_[head - tx_buffer_size_] = c;
  334. }
  335. __disable_irq();
  336. transmitting_ = 1;
  337. tx_buffer_head_ = head;
  338. port->CTRL |= LPUART_CTRL_TIE; // (may need to handle this issue)BITBAND_SET_BIT(LPUART0_CTRL, TIE_BIT);
  339. __enable_irq();
  340. //digitalWrite(3, LOW);
  341. return 1;
  342. }
  343. void HardwareSerial::IRQHandler()
  344. {
  345. //digitalWrite(4, HIGH);
  346. uint32_t head, tail, n;
  347. uint32_t ctrl;
  348. // See if we have stuff to read in.
  349. // Todo - Check idle.
  350. if (port->STAT & (LPUART_STAT_RDRF | LPUART_STAT_IDLE)) {
  351. // See how many bytes or pending.
  352. //digitalWrite(5, HIGH);
  353. uint8_t avail = (port->WATER >> 24) & 0x7;
  354. if (avail) {
  355. uint32_t newhead;
  356. head = rx_buffer_head_;
  357. tail = rx_buffer_tail_;
  358. do {
  359. #if 1
  360. n = port->DATA; // get the byte...
  361. #else
  362. if (use9Bits_ && (port().C3 & 0x80)) {
  363. n = port().D | 0x100;
  364. } else {
  365. n = port().D;
  366. }
  367. #endif
  368. newhead = head + 1;
  369. if (newhead >= rx_buffer_total_size_) newhead = 0;
  370. if (newhead != rx_buffer_tail_) {
  371. head = newhead;
  372. if (newhead < rx_buffer_size_) {
  373. rx_buffer_[head] = n;
  374. } else {
  375. rx_buffer_storage_[head-rx_buffer_size_] = n;
  376. }
  377. }
  378. } while (--avail > 0) ;
  379. rx_buffer_head_ = head;
  380. if (rts_pin_baseReg_) {
  381. uint32_t avail;
  382. if (head >= tail) avail = head - tail;
  383. else avail = rx_buffer_total_size_ + head - tail;
  384. if (avail >= rts_high_watermark_) rts_deassert();
  385. }
  386. }
  387. // If it was an idle status clear the idle
  388. if (port->STAT & LPUART_STAT_IDLE) {
  389. port->STAT |= LPUART_STAT_IDLE; // writing a 1 to idle should clear it.
  390. }
  391. //digitalWrite(5, LOW);
  392. }
  393. // See if we are transmitting and room in buffer.
  394. ctrl = port->CTRL;
  395. if ((ctrl & LPUART_CTRL_TIE) && (port->STAT & LPUART_STAT_TDRE))
  396. {
  397. //digitalWrite(3, HIGH);
  398. head = tx_buffer_head_;
  399. tail = tx_buffer_tail_;
  400. do {
  401. if (head == tail) break;
  402. if (++tail >= tx_buffer_total_size_) tail = 0;
  403. if (tail < tx_buffer_size_) {
  404. n = tx_buffer_[tail];
  405. } else {
  406. n = tx_buffer_storage_[tail-tx_buffer_size_];
  407. }
  408. //if (use9Bits_) port().C3 = (port().C3 & ~0x40) | ((n & 0x100) >> 2);
  409. port->DATA = n;
  410. } while (((port->WATER >> 8) & 0x7) < 4); // need to computer properly
  411. tx_buffer_tail_ = tail;
  412. if (head == tail) port->CTRL = CTRL_TX_COMPLETING;
  413. //digitalWrite(3, LOW);
  414. }
  415. if ((ctrl & LPUART_CTRL_TCIE) && (port->STAT & LPUART_STAT_TC))
  416. {
  417. transmitting_ = 0;
  418. if (transmit_pin_baseReg_) DIRECT_WRITE_LOW(transmit_pin_baseReg_, transmit_pin_bitmask_);
  419. port->CTRL = CTRL_TX_INACTIVE;
  420. }
  421. //digitalWrite(4, LOW);
  422. }
  423. void IRQHandler_Serial1()
  424. {
  425. Serial1.IRQHandler();
  426. }
  427. void IRQHandler_Serial2()
  428. {
  429. Serial2.IRQHandler();
  430. }
  431. void IRQHandler_Serial3()
  432. {
  433. Serial3.IRQHandler();
  434. }
  435. void IRQHandler_Serial4()
  436. {
  437. Serial4.IRQHandler();
  438. }
  439. void IRQHandler_Serial5()
  440. {
  441. Serial5.IRQHandler();
  442. }
  443. void IRQHandler_Serial6()
  444. {
  445. Serial6.IRQHandler();
  446. }
  447. void IRQHandler_Serial7()
  448. {
  449. Serial7.IRQHandler();
  450. }
  451. void IRQHandler_Serial8()
  452. {
  453. Serial8.IRQHandler();
  454. }
  455. // Serial1
  456. static BUFTYPE tx_buffer1[SERIAL1_TX_BUFFER_SIZE];
  457. static BUFTYPE rx_buffer1[SERIAL1_RX_BUFFER_SIZE];
  458. const HardwareSerial::hardware_t UART6_Hardware = {
  459. IRQ_LPUART6, &IRQHandler_Serial1,
  460. CCM_CCGR3, CCM_CCGR3_LPUART6(CCM_CCGR_ON),
  461. 0, //IOMUXC_SW_MUX_CTL_PAD_GPIO_AD_B0_03, // pin 0
  462. 1, //IOMUXC_SW_MUX_CTL_PAD_GPIO_AD_B0_02, // pin 1
  463. 0xff, // No CTS pin
  464. IOMUXC_LPUART6_RX_SELECT_INPUT,
  465. 2, // page 473
  466. 2, // page 472
  467. 0, // No CTS
  468. 1, // page 861
  469. IRQ_PRIORITY, 38, 24, // IRQ, rts_low_watermark, rts_high_watermark
  470. };
  471. HardwareSerial Serial1(&IMXRT_LPUART6, &UART6_Hardware, tx_buffer1, SERIAL1_TX_BUFFER_SIZE,
  472. rx_buffer1, SERIAL1_RX_BUFFER_SIZE);
  473. // Serial2
  474. static BUFTYPE tx_buffer2[SERIAL2_TX_BUFFER_SIZE];
  475. static BUFTYPE rx_buffer2[SERIAL2_RX_BUFFER_SIZE];
  476. static HardwareSerial::hardware_t UART4_Hardware = {
  477. IRQ_LPUART4, &IRQHandler_Serial2,
  478. CCM_CCGR1, CCM_CCGR1_LPUART4(CCM_CCGR_ON),
  479. 6, //IOMUXC_SW_MUX_CTL_PAD_GPIO_B1_01, // pin 6
  480. 7, // IOMUXC_SW_MUX_CTL_PAD_GPIO_B1_00, // pin 7
  481. 0xff, // No CTS pin
  482. IOMUXC_LPUART4_RX_SELECT_INPUT,
  483. 2, // page 521
  484. 2, // page 520
  485. 0, // No CTS
  486. 2, // page 858
  487. IRQ_PRIORITY, 38, 24, // IRQ, rts_low_watermark, rts_high_watermark
  488. };
  489. HardwareSerial Serial2(&IMXRT_LPUART4, &UART4_Hardware, tx_buffer2, SERIAL2_TX_BUFFER_SIZE,
  490. rx_buffer2, SERIAL2_RX_BUFFER_SIZE);
  491. // Serial3
  492. static BUFTYPE tx_buffer3[SERIAL3_TX_BUFFER_SIZE];
  493. static BUFTYPE rx_buffer3[SERIAL3_RX_BUFFER_SIZE];
  494. static HardwareSerial::hardware_t UART2_Hardware = {
  495. IRQ_LPUART2, &IRQHandler_Serial3,
  496. CCM_CCGR0, CCM_CCGR0_LPUART2(CCM_CCGR_ON),
  497. 15, //IOMUXC_SW_MUX_CTL_PAD_GPIO_AD_B1_03, // pin 15
  498. 14, //IOMUXC_SW_MUX_CTL_PAD_GPIO_AD_B1_02, // pin 14
  499. 18, //IOMUXC_SW_MUX_CTL_PAD_GPIO_AD_B1_01, // 18
  500. IOMUXC_LPUART2_RX_SELECT_INPUT,
  501. 2, // page 491
  502. 2, // page 490
  503. 2, // page 473
  504. 1, // Page 855
  505. IRQ_PRIORITY, 38, 24, // IRQ, rts_low_watermark, rts_high_watermark
  506. };
  507. HardwareSerial Serial3(&IMXRT_LPUART2, &UART2_Hardware,tx_buffer3, SERIAL3_TX_BUFFER_SIZE,
  508. rx_buffer3, SERIAL3_RX_BUFFER_SIZE);
  509. // Serial4
  510. static BUFTYPE tx_buffer4[SERIAL4_TX_BUFFER_SIZE];
  511. static BUFTYPE rx_buffer4[SERIAL4_RX_BUFFER_SIZE];
  512. static HardwareSerial::hardware_t UART3_Hardware = {
  513. IRQ_LPUART3, &IRQHandler_Serial4,
  514. CCM_CCGR0, CCM_CCGR0_LPUART3(CCM_CCGR_ON),
  515. 16, //IOMUXC_SW_MUX_CTL_PAD_GPIO_AD_B1_07, // pin 16
  516. 17, //IOMUXC_SW_MUX_CTL_PAD_GPIO_AD_B1_06, // pin 17
  517. 0xff, // No CTS pin
  518. IOMUXC_LPUART3_RX_SELECT_INPUT,
  519. 2, // page 495
  520. 2, // page 494
  521. 0, // No CTS
  522. 0, // Page 857
  523. IRQ_PRIORITY, 38, 24, // IRQ, rts_low_watermark, rts_high_watermark
  524. };
  525. HardwareSerial Serial4(&IMXRT_LPUART3, &UART3_Hardware, tx_buffer4, SERIAL4_TX_BUFFER_SIZE,
  526. rx_buffer4, SERIAL4_RX_BUFFER_SIZE);
  527. // Serial5
  528. static BUFTYPE tx_buffer5[SERIAL5_TX_BUFFER_SIZE];
  529. static BUFTYPE rx_buffer5[SERIAL5_RX_BUFFER_SIZE];
  530. static HardwareSerial::hardware_t UART8_Hardware = {
  531. IRQ_LPUART8, &IRQHandler_Serial5,
  532. CCM_CCGR6, CCM_CCGR6_LPUART8(CCM_CCGR_ON),
  533. 21, //IOMUXC_SW_MUX_CTL_PAD_GPIO_AD_B1_11, // pin 21
  534. 20, //IOMUXC_SW_MUX_CTL_PAD_GPIO_AD_B1_10, // pin 20
  535. 0xff, // No CTS pin
  536. IOMUXC_LPUART8_RX_SELECT_INPUT,
  537. 2, // page 499
  538. 2, // page 498
  539. 0, // No CTS
  540. 1, // Page 864-5
  541. IRQ_PRIORITY, 38, 24, // IRQ, rts_low_watermark, rts_high_watermark
  542. };
  543. HardwareSerial Serial5(&IMXRT_LPUART8, &UART8_Hardware, tx_buffer5, SERIAL5_TX_BUFFER_SIZE,
  544. rx_buffer5, SERIAL5_RX_BUFFER_SIZE);
  545. // Serial6
  546. static BUFTYPE tx_buffer6[SERIAL6_TX_BUFFER_SIZE];
  547. static BUFTYPE rx_buffer6[SERIAL6_RX_BUFFER_SIZE];
  548. uint32_t IOMUXC_LPUART1_RX_SELECT_INPUT; // bugbug - does not exist so hack
  549. static HardwareSerial::hardware_t UART1_Hardware = {
  550. IRQ_LPUART1, &IRQHandler_Serial6,
  551. CCM_CCGR5, CCM_CCGR5_LPUART1(CCM_CCGR_ON),
  552. 25, //IOMUXC_SW_MUX_CTL_PAD_GPIO_AD_B0_13, // pin 25
  553. 24, //IOMUXC_SW_MUX_CTL_PAD_GPIO_AD_B0_12, // pin 24
  554. 0xff, // No CTS pin
  555. IOMUXC_LPUART1_RX_SELECT_INPUT,
  556. 2, // page 486
  557. 2, // page 485
  558. 0, // No CTS
  559. 0, // ??? Does not have one ???
  560. IRQ_PRIORITY, 38, 24, // IRQ, rts_low_watermark, rts_high_watermark
  561. };
  562. HardwareSerial Serial6(&IMXRT_LPUART1, &UART1_Hardware, tx_buffer6, SERIAL6_TX_BUFFER_SIZE,
  563. rx_buffer6, SERIAL6_RX_BUFFER_SIZE);
  564. // Serial7
  565. static BUFTYPE tx_buffer7[SERIAL7_TX_BUFFER_SIZE];
  566. static BUFTYPE rx_buffer7[SERIAL7_RX_BUFFER_SIZE];
  567. static HardwareSerial::hardware_t UART7_Hardware = {
  568. IRQ_LPUART7, &IRQHandler_Serial7,
  569. CCM_CCGR5, CCM_CCGR5_LPUART7(CCM_CCGR_ON),
  570. 28, //IOMUXC_SW_MUX_CTL_PAD_GPIO_EMC_32, // pin 28
  571. 29, //IOMUXC_SW_MUX_CTL_PAD_GPIO_EMC_31, // pin 29
  572. 0xff, // No CTS pin
  573. IOMUXC_LPUART7_RX_SELECT_INPUT,
  574. 2, // page 458
  575. 2, // page 457
  576. 0, // No CTS
  577. 1, // Page 863
  578. IRQ_PRIORITY, 38, 24, // IRQ, rts_low_watermark, rts_high_watermark
  579. };
  580. HardwareSerial Serial7(&IMXRT_LPUART7, &UART7_Hardware, tx_buffer7, SERIAL7_TX_BUFFER_SIZE,
  581. rx_buffer7, SERIAL7_RX_BUFFER_SIZE);
  582. // Serial8
  583. static BUFTYPE tx_buffer8[SERIAL8_TX_BUFFER_SIZE];
  584. static BUFTYPE rx_buffer8[SERIAL8_RX_BUFFER_SIZE];
  585. static HardwareSerial::hardware_t UART5_Hardware = {
  586. IRQ_LPUART5, &IRQHandler_Serial8,
  587. CCM_CCGR3, CCM_CCGR3_LPUART5(CCM_CCGR_ON),
  588. 30, //IOMUXC_SW_MUX_CTL_PAD_GPIO_EMC_24, // pin 30
  589. 31, // IOMUXC_SW_MUX_CTL_PAD_GPIO_EMC_23, // pin 31
  590. 0xff, // No CTS pin
  591. IOMUXC_LPUART5_RX_SELECT_INPUT,
  592. 2, // page 450
  593. 2, // page 449
  594. 0, // No CTS
  595. 0,
  596. IRQ_PRIORITY, 38, 24, // IRQ, rts_low_watermark, rts_high_watermark
  597. };
  598. HardwareSerial Serial8(&IMXRT_LPUART5, &UART5_Hardware, tx_buffer8, SERIAL8_TX_BUFFER_SIZE,
  599. rx_buffer8, SERIAL8_RX_BUFFER_SIZE);