Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

HardwareSerial.cpp 22KB

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