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.

619 lines
19KB

  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2019 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. #if defined(__IMXRT1052__)
  61. SerialEventCheckingFunctionPointer HardwareSerial::serial_event_handler_checks[8] = {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
  62. #else
  63. SerialEventCheckingFunctionPointer HardwareSerial::serial_event_handler_checks[7] = {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
  64. #endif
  65. uint8_t HardwareSerial::serial_event_handlers_active = 0;
  66. #define CTRL_ENABLE (LPUART_CTRL_TE | LPUART_CTRL_RE | LPUART_CTRL_RIE | LPUART_CTRL_ILIE)
  67. #define CTRL_TX_ACTIVE (CTRL_ENABLE | LPUART_CTRL_TIE)
  68. #define CTRL_TX_COMPLETING (CTRL_ENABLE | LPUART_CTRL_TCIE)
  69. #define CTRL_TX_INACTIVE CTRL_ENABLE
  70. // Copied from T3.x - probably should move to other location.
  71. int nvic_execution_priority(void)
  72. {
  73. uint32_t priority=256;
  74. uint32_t primask, faultmask, basepri, ipsr;
  75. // full algorithm in ARM DDI0403D, page B1-639
  76. // this isn't quite complete, but hopefully good enough
  77. __asm__ volatile("mrs %0, faultmask\n" : "=r" (faultmask)::);
  78. if (faultmask) return -1;
  79. __asm__ volatile("mrs %0, primask\n" : "=r" (primask)::);
  80. if (primask) return 0;
  81. __asm__ volatile("mrs %0, ipsr\n" : "=r" (ipsr)::);
  82. if (ipsr) {
  83. if (ipsr < 16) priority = 0; // could be non-zero
  84. else priority = NVIC_GET_PRIORITY(ipsr - 16);
  85. }
  86. __asm__ volatile("mrs %0, basepri\n" : "=r" (basepri)::);
  87. if (basepri > 0 && basepri < priority) priority = basepri;
  88. return priority;
  89. }
  90. void HardwareSerial::begin(uint32_t baud, uint16_t format)
  91. {
  92. //printf("HardwareSerial begin\n");
  93. float base = (float)UART_CLOCK / (float)baud;
  94. float besterr = 1e20;
  95. int bestdiv = 1;
  96. int bestosr = 4;
  97. for (int osr=4; osr <= 32; osr++) {
  98. float div = base / (float)osr;
  99. int divint = (int)(div + 0.5f);
  100. if (divint < 1) divint = 1;
  101. else if (divint > 8191) divint = 8191;
  102. float err = ((float)divint - div) / div;
  103. if (err < 0.0f) err = -err;
  104. if (err <= besterr) {
  105. besterr = err;
  106. bestdiv = divint;
  107. bestosr = osr;
  108. }
  109. }
  110. //printf(" baud %d: osr=%d, div=%d\n", baud, bestosr, bestdiv);
  111. rx_buffer_head_ = 0;
  112. rx_buffer_tail_ = 0;
  113. tx_buffer_head_ = 0;
  114. tx_buffer_tail_ = 0;
  115. rts_low_watermark_ = rx_buffer_total_size_ - hardware->rts_low_watermark;
  116. rts_high_watermark_ = rx_buffer_total_size_ - hardware->rts_high_watermark;
  117. transmitting_ = 0;
  118. hardware->ccm_register |= hardware->ccm_value;
  119. // uint32_t fastio = IOMUXC_PAD_SRE | IOMUXC_PAD_DSE(3) | IOMUXC_PAD_SPEED(3);
  120. *(portControlRegister(hardware->rx_pins[rx_pin_index_].pin)) = IOMUXC_PAD_DSE(7) | IOMUXC_PAD_PKE | IOMUXC_PAD_PUE | IOMUXC_PAD_PUS(3) | IOMUXC_PAD_HYS;
  121. *(portConfigRegister(hardware->rx_pins[rx_pin_index_].pin)) = hardware->rx_pins[rx_pin_index_].mux_val;
  122. if (hardware->rx_pins[rx_pin_index_].select_input_register) {
  123. *(hardware->rx_pins[rx_pin_index_].select_input_register) = hardware->rx_pins[rx_pin_index_].select_val;
  124. }
  125. *(portControlRegister(hardware->tx_pins[tx_pin_index_].pin)) = IOMUXC_PAD_SRE | IOMUXC_PAD_DSE(3) | IOMUXC_PAD_SPEED(3);
  126. *(portConfigRegister(hardware->tx_pins[tx_pin_index_].pin)) = hardware->tx_pins[tx_pin_index_].mux_val;
  127. if (hardware->tx_pins[tx_pin_index_].select_input_register) {
  128. *(hardware->tx_pins[tx_pin_index_].select_input_register) = hardware->tx_pins[tx_pin_index_].select_val;
  129. }
  130. //hardware->rx_mux_register = hardware->rx_mux_val;
  131. //hardware->tx_mux_register = hardware->tx_mux_val;
  132. port->BAUD = LPUART_BAUD_OSR(bestosr - 1) | LPUART_BAUD_SBR(bestdiv)
  133. | (bestosr <= 8 ? LPUART_BAUD_BOTHEDGE : 0);
  134. port->PINCFG = 0;
  135. // Enable the transmitter, receiver and enable receiver interrupt
  136. attachInterruptVector(hardware->irq, hardware->irq_handler);
  137. NVIC_SET_PRIORITY(hardware->irq, hardware->irq_priority); // maybe should put into hardware...
  138. NVIC_ENABLE_IRQ(hardware->irq);
  139. uint16_t tx_fifo_size = (((port->FIFO >> 4) & 0x7) << 2);
  140. uint8_t tx_water = (tx_fifo_size < 16) ? tx_fifo_size >> 1 : 7;
  141. uint16_t rx_fifo_size = (((port->FIFO >> 0) & 0x7) << 2);
  142. uint8_t rx_water = (rx_fifo_size < 16) ? rx_fifo_size >> 1 : 7;
  143. /*
  144. Serial.printf("SerialX::begin stat:%x ctrl:%x fifo:%x water:%x\n", port->STAT, port->CTRL, port->FIFO, port->WATER );
  145. Serial.printf(" FIFO sizes: tx:%d rx:%d\n",tx_fifo_size, rx_fifo_size);
  146. Serial.printf(" Watermark tx:%d, rx: %d\n", tx_water, rx_water);
  147. */
  148. port->WATER = LPUART_WATER_RXWATER(rx_water) | LPUART_WATER_TXWATER(tx_water);
  149. port->FIFO |= LPUART_FIFO_TXFE | LPUART_FIFO_RXFE;
  150. // lets configure up our CTRL register value
  151. uint32_t ctrl = CTRL_TX_INACTIVE;
  152. // Now process the bits in the Format value passed in
  153. // Bits 0-2 - Parity plus 9 bit.
  154. ctrl |= (format & (LPUART_CTRL_PT | LPUART_CTRL_PE) ); // configure parity - turn off PT, PE, M and configure PT, PE
  155. if (format & 0x04) ctrl |= LPUART_CTRL_M; // 9 bits (might include parity)
  156. if ((format & 0x0F) == 0x04) ctrl |= LPUART_CTRL_R9T8; // 8N2 is 9 bit with 9th bit always 1
  157. // Bit 5 TXINVERT
  158. if (format & 0x20) ctrl |= LPUART_CTRL_TXINV; // tx invert
  159. // write out computed CTRL
  160. port->CTRL = ctrl;
  161. // Bit 3 10 bit - Will assume that begin already cleared it.
  162. // process some other bits which change other registers.
  163. if (format & 0x08) port->BAUD |= LPUART_BAUD_M10;
  164. // Bit 4 RXINVERT
  165. uint32_t c = port->STAT & ~LPUART_STAT_RXINV;
  166. if (format & 0x10) c |= LPUART_STAT_RXINV; // rx invert
  167. port->STAT = c;
  168. // bit 8 can turn on 2 stop bit mote
  169. if ( format & 0x100) port->BAUD |= LPUART_BAUD_SBNS;
  170. //Serial.printf(" stat:%x ctrl:%x fifo:%x water:%x\n", port->STAT, port->CTRL, port->FIFO, port->WATER );
  171. enableSerialEvents(); // Enable the processing of serialEvent for this object
  172. };
  173. inline void HardwareSerial::rts_assert()
  174. {
  175. DIRECT_WRITE_LOW(rts_pin_baseReg_, rts_pin_bitmask_);
  176. }
  177. inline void HardwareSerial::rts_deassert()
  178. {
  179. DIRECT_WRITE_HIGH(rts_pin_baseReg_, rts_pin_bitmask_);
  180. }
  181. void HardwareSerial::end(void)
  182. {
  183. if (!(hardware->ccm_register & hardware->ccm_value)) return;
  184. while (transmitting_) yield(); // wait for buffered data to send
  185. port->CTRL = 0; // disable the TX and RX ...
  186. // Not sure if this is best, but I think most IO pins default to Mode 5? which appears to be digital IO?
  187. *(portConfigRegister(hardware->rx_pins[rx_pin_index_].pin)) = 5;
  188. *(portConfigRegister(hardware->tx_pins[tx_pin_index_].pin)) = 5;
  189. // Might need to clear out other areas as well?
  190. rx_buffer_head_ = 0;
  191. rx_buffer_tail_ = 0;
  192. if (rts_pin_baseReg_) rts_deassert();
  193. //
  194. disableSerialEvents(); // disable the processing of serialEvent for this object
  195. }
  196. void HardwareSerial::transmitterEnable(uint8_t pin)
  197. {
  198. while (transmitting_) ;
  199. pinMode(pin, OUTPUT);
  200. transmit_pin_baseReg_ = PIN_TO_BASEREG(pin);
  201. transmit_pin_bitmask_ = PIN_TO_BITMASK(pin);
  202. DIRECT_WRITE_LOW(transmit_pin_baseReg_, transmit_pin_bitmask_);
  203. }
  204. void HardwareSerial::setRX(uint8_t pin)
  205. {
  206. if (pin != hardware->rx_pins[rx_pin_index_].pin) {
  207. for (uint8_t rx_pin_new_index = 0; rx_pin_new_index < cnt_rx_pins; rx_pin_new_index++) {
  208. if (pin == hardware->rx_pins[rx_pin_index_].pin) {
  209. // new pin - so lets maybe reset the old pin to INPUT? and then set new pin parameters
  210. *(portConfigRegister(hardware->rx_pins[rx_pin_index_].pin)) = 5;
  211. // now set new pin info.
  212. rx_pin_index_ = rx_pin_new_index;
  213. *(portControlRegister(hardware->rx_pins[rx_pin_index_].pin)) = IOMUXC_PAD_DSE(7) | IOMUXC_PAD_PKE | IOMUXC_PAD_PUE | IOMUXC_PAD_PUS(3) | IOMUXC_PAD_HYS;;
  214. *(portConfigRegister(hardware->rx_pins[rx_pin_index_].pin)) = hardware->rx_pins[rx_pin_index_].mux_val;
  215. if (hardware->rx_pins[rx_pin_index_].select_input_register) {
  216. *(hardware->rx_pins[rx_pin_index_].select_input_register) = hardware->rx_pins[rx_pin_index_].select_val;
  217. }
  218. break;
  219. }
  220. }
  221. }
  222. }
  223. void HardwareSerial::setTX(uint8_t pin, bool opendrain)
  224. {
  225. uint8_t tx_pin_new_index = tx_pin_index_;
  226. if (pin != hardware->tx_pins[tx_pin_index_].pin) {
  227. for (tx_pin_new_index = 0; tx_pin_new_index < cnt_tx_pins; tx_pin_new_index++) {
  228. if (pin == hardware->tx_pins[tx_pin_index_].pin) {
  229. break;
  230. }
  231. }
  232. if (tx_pin_new_index == cnt_tx_pins) return; // not a new valid pid...
  233. }
  234. // turn on or off opendrain mode.
  235. // new pin - so lets maybe reset the old pin to INPUT? and then set new pin parameters
  236. if (tx_pin_new_index != tx_pin_index_) {
  237. *(portConfigRegister(hardware->tx_pins[tx_pin_index_].pin)) = 5;
  238. *(portConfigRegister(hardware->tx_pins[tx_pin_new_index].pin)) = hardware->tx_pins[tx_pin_new_index].mux_val;
  239. }
  240. // now set new pin info.
  241. tx_pin_index_ = tx_pin_new_index;
  242. if (opendrain)
  243. *(portControlRegister(pin)) = IOMUXC_PAD_ODE | IOMUXC_PAD_DSE(3) | IOMUXC_PAD_SPEED(3);
  244. else
  245. *(portControlRegister(pin)) = IOMUXC_PAD_SRE | IOMUXC_PAD_DSE(3) | IOMUXC_PAD_SPEED(3);
  246. }
  247. bool HardwareSerial::attachRts(uint8_t pin)
  248. {
  249. if (!(hardware->ccm_register & hardware->ccm_value)) return 0;
  250. if (pin < CORE_NUM_DIGITAL) {
  251. rts_pin_baseReg_ = PIN_TO_BASEREG(pin);
  252. rts_pin_bitmask_ = PIN_TO_BITMASK(pin);
  253. pinMode(pin, OUTPUT);
  254. rts_assert();
  255. } else {
  256. rts_pin_baseReg_ = NULL;
  257. return 0;
  258. }
  259. return 1;
  260. }
  261. bool HardwareSerial::attachCts(uint8_t pin)
  262. {
  263. if (!(hardware->ccm_register & hardware->ccm_value)) return false;
  264. if ((pin != 0xff) && (pin == hardware->cts_pin)) {
  265. // Setup the IO pin as weak PULL down.
  266. *(portControlRegister(pin)) = IOMUXC_PAD_DSE(7) | IOMUXC_PAD_PKE | IOMUXC_PAD_PUE | IOMUXC_PAD_PUS(0) | IOMUXC_PAD_HYS;
  267. *(portConfigRegister(hardware->cts_pin)) = hardware->cts_mux_val;
  268. port->MODIR |= LPUART_MODIR_TXCTSE;
  269. return true;
  270. } else {
  271. port->MODIR &= ~LPUART_MODIR_TXCTSE;
  272. return false;
  273. }
  274. }
  275. void HardwareSerial::clear(void)
  276. {
  277. // BUGBUG:: deal with FIFO
  278. rx_buffer_head_ = rx_buffer_tail_;
  279. if (rts_pin_baseReg_) rts_assert();
  280. }
  281. int HardwareSerial::availableForWrite(void)
  282. {
  283. uint32_t head, tail;
  284. head = tx_buffer_head_;
  285. tail = tx_buffer_tail_;
  286. if (head >= tail) return tx_buffer_total_size_ - 1 - head + tail;
  287. return tail - head - 1;
  288. }
  289. int HardwareSerial::available(void)
  290. {
  291. uint32_t head, tail;
  292. // WATER> 0 so IDLE involved may want to check if port has already has RX data to retrieve
  293. __disable_irq();
  294. head = rx_buffer_head_;
  295. tail = rx_buffer_tail_;
  296. int avail;
  297. if (head >= tail) avail = head - tail;
  298. else avail = rx_buffer_total_size_ + head - tail;
  299. avail += (port->WATER >> 24) & 0x7;
  300. __enable_irq();
  301. return avail;
  302. }
  303. void HardwareSerial::addStorageForRead(void *buffer, size_t length)
  304. {
  305. rx_buffer_storage_ = (BUFTYPE*)buffer;
  306. if (buffer) {
  307. rx_buffer_total_size_ = rx_buffer_total_size_ + length;
  308. } else {
  309. rx_buffer_total_size_ = rx_buffer_total_size_;
  310. }
  311. rts_low_watermark_ = rx_buffer_total_size_ - hardware->rts_low_watermark;
  312. rts_high_watermark_ = rx_buffer_total_size_ - hardware->rts_high_watermark;
  313. }
  314. void HardwareSerial::addStorageForWrite(void *buffer, size_t length)
  315. {
  316. tx_buffer_storage_ = (BUFTYPE*)buffer;
  317. if (buffer) {
  318. tx_buffer_total_size_ = tx_buffer_total_size_ + length;
  319. } else {
  320. tx_buffer_total_size_ = tx_buffer_total_size_;
  321. }
  322. }
  323. int HardwareSerial::peek(void)
  324. {
  325. uint32_t head, tail;
  326. head = rx_buffer_head_;
  327. tail = rx_buffer_tail_;
  328. if (head == tail) {
  329. __disable_irq();
  330. head = rx_buffer_head_; // reread head to make sure no ISR happened
  331. if (head == tail) {
  332. // Still empty Now check for stuff in FIFO Queue.
  333. int c = -1; // assume nothing to return
  334. if (port->WATER & 0x7000000) {
  335. c = port->DATA & 0x3ff; // Use only up to 10 bits of data
  336. // But we don't want to throw it away...
  337. // since queue is empty, just going to reset to front of queue...
  338. rx_buffer_head_ = 1;
  339. rx_buffer_tail_ = 0;
  340. rx_buffer_[1] = c;
  341. }
  342. __enable_irq();
  343. return c;
  344. }
  345. __enable_irq();
  346. }
  347. if (++tail >= rx_buffer_total_size_) tail = 0;
  348. if (tail < rx_buffer_size_) {
  349. return rx_buffer_[tail];
  350. } else {
  351. return rx_buffer_storage_[tail-rx_buffer_size_];
  352. }
  353. }
  354. int HardwareSerial::read(void)
  355. {
  356. uint32_t head, tail;
  357. int c;
  358. head = rx_buffer_head_;
  359. tail = rx_buffer_tail_;
  360. if (head == tail) {
  361. __disable_irq();
  362. head = rx_buffer_head_; // reread head to make sure no ISR happened
  363. if (head == tail) {
  364. // Still empty Now check for stuff in FIFO Queue.
  365. c = -1; // assume nothing to return
  366. if (port->WATER & 0x7000000) {
  367. c = port->DATA & 0x3ff; // Use only up to 10 bits of data
  368. }
  369. __enable_irq();
  370. return c;
  371. }
  372. __enable_irq();
  373. }
  374. if (++tail >= rx_buffer_total_size_) tail = 0;
  375. if (tail < rx_buffer_size_) {
  376. c = rx_buffer_[tail];
  377. } else {
  378. c = rx_buffer_storage_[tail-rx_buffer_size_];
  379. }
  380. rx_buffer_tail_ = tail;
  381. if (rts_pin_baseReg_) {
  382. uint32_t avail;
  383. if (head >= tail) avail = head - tail;
  384. else avail = rx_buffer_total_size_ + head - tail;
  385. if (avail <= rts_low_watermark_) rts_assert();
  386. }
  387. return c;
  388. }
  389. void HardwareSerial::flush(void)
  390. {
  391. while (transmitting_) yield(); // wait
  392. }
  393. size_t HardwareSerial::write(uint8_t c)
  394. {
  395. // use the 9 bit version (maybe 10 bit) do do the work.
  396. return write9bit(c);
  397. }
  398. size_t HardwareSerial::write9bit(uint32_t c)
  399. {
  400. uint32_t head, n;
  401. //digitalWrite(3, HIGH);
  402. //digitalWrite(5, HIGH);
  403. if (transmit_pin_baseReg_) DIRECT_WRITE_HIGH(transmit_pin_baseReg_, transmit_pin_bitmask_);
  404. head = tx_buffer_head_;
  405. if (++head >= tx_buffer_total_size_) head = 0;
  406. while (tx_buffer_tail_ == head) {
  407. int priority = nvic_execution_priority();
  408. if (priority <= hardware->irq_priority) {
  409. if ((port->STAT & LPUART_STAT_TDRE)) {
  410. uint32_t tail = tx_buffer_tail_;
  411. if (++tail >= tx_buffer_total_size_) tail = 0;
  412. if (tail < tx_buffer_size_) {
  413. n = tx_buffer_[tail];
  414. } else {
  415. n = tx_buffer_storage_[tail-tx_buffer_size_];
  416. }
  417. port->DATA = n;
  418. tx_buffer_tail_ = tail;
  419. }
  420. } else if (priority >= 256)
  421. {
  422. yield(); // wait
  423. }
  424. }
  425. //digitalWrite(5, LOW);
  426. //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_);
  427. if (head < tx_buffer_size_) {
  428. tx_buffer_[head] = c;
  429. } else {
  430. tx_buffer_storage_[head - tx_buffer_size_] = c;
  431. }
  432. __disable_irq();
  433. transmitting_ = 1;
  434. tx_buffer_head_ = head;
  435. port->CTRL |= LPUART_CTRL_TIE; // (may need to handle this issue)BITBAND_SET_BIT(LPUART0_CTRL, TIE_BIT);
  436. __enable_irq();
  437. //digitalWrite(3, LOW);
  438. return 1;
  439. }
  440. void HardwareSerial::IRQHandler()
  441. {
  442. //digitalWrite(4, HIGH);
  443. uint32_t head, tail, n;
  444. uint32_t ctrl;
  445. // See if we have stuff to read in.
  446. // Todo - Check idle.
  447. if (port->STAT & (LPUART_STAT_RDRF | LPUART_STAT_IDLE)) {
  448. // See how many bytes or pending.
  449. //digitalWrite(5, HIGH);
  450. uint8_t avail = (port->WATER >> 24) & 0x7;
  451. if (avail) {
  452. uint32_t newhead;
  453. head = rx_buffer_head_;
  454. tail = rx_buffer_tail_;
  455. do {
  456. n = port->DATA & 0x3ff; // Use only up to 10 bits of data
  457. newhead = head + 1;
  458. if (newhead >= rx_buffer_total_size_) newhead = 0;
  459. if (newhead != rx_buffer_tail_) {
  460. head = newhead;
  461. if (newhead < rx_buffer_size_) {
  462. rx_buffer_[head] = n;
  463. } else {
  464. rx_buffer_storage_[head-rx_buffer_size_] = n;
  465. }
  466. }
  467. } while (--avail > 0) ;
  468. rx_buffer_head_ = head;
  469. if (rts_pin_baseReg_) {
  470. uint32_t avail;
  471. if (head >= tail) avail = head - tail;
  472. else avail = rx_buffer_total_size_ + head - tail;
  473. if (avail >= rts_high_watermark_) rts_deassert();
  474. }
  475. }
  476. // If it was an idle status clear the idle
  477. if (port->STAT & LPUART_STAT_IDLE) {
  478. port->STAT |= LPUART_STAT_IDLE; // writing a 1 to idle should clear it.
  479. }
  480. //digitalWrite(5, LOW);
  481. }
  482. // See if we are transmitting and room in buffer.
  483. ctrl = port->CTRL;
  484. if ((ctrl & LPUART_CTRL_TIE) && (port->STAT & LPUART_STAT_TDRE))
  485. {
  486. //digitalWrite(3, HIGH);
  487. head = tx_buffer_head_;
  488. tail = tx_buffer_tail_;
  489. do {
  490. if (head == tail) break;
  491. if (++tail >= tx_buffer_total_size_) tail = 0;
  492. if (tail < tx_buffer_size_) {
  493. n = tx_buffer_[tail];
  494. } else {
  495. n = tx_buffer_storage_[tail-tx_buffer_size_];
  496. }
  497. port->DATA = n;
  498. } while (((port->WATER >> 8) & 0x7) < 4); // need to computer properly
  499. tx_buffer_tail_ = tail;
  500. if (head == tail) {
  501. port->CTRL &= ~LPUART_CTRL_TIE;
  502. port->CTRL |= LPUART_CTRL_TCIE; // Actually wondering if we can just leave this one on...
  503. }
  504. //digitalWrite(3, LOW);
  505. }
  506. if ((ctrl & LPUART_CTRL_TCIE) && (port->STAT & LPUART_STAT_TC))
  507. {
  508. transmitting_ = 0;
  509. if (transmit_pin_baseReg_) DIRECT_WRITE_LOW(transmit_pin_baseReg_, transmit_pin_bitmask_);
  510. port->CTRL &= ~LPUART_CTRL_TCIE;
  511. }
  512. //digitalWrite(4, LOW);
  513. }
  514. void HardwareSerial::processSerialEvents()
  515. {
  516. if (!serial_event_handlers_active) return; // bail quick if no one processing SerialEvents.
  517. uint8_t handlers_still_to_process = serial_event_handlers_active;
  518. for (uint8_t i = 0; i < 8; i++) {
  519. if (serial_event_handler_checks[i]) {
  520. (*serial_event_handler_checks[i])();
  521. if (--handlers_still_to_process == 0) return;
  522. }
  523. }
  524. }
  525. void HardwareSerial::enableSerialEvents()
  526. {
  527. if (!serial_event_handler_checks[hardware->serial_index]) {
  528. serial_event_handler_checks[hardware->serial_index] = hardware->serial_event_handler_check; // clear it out
  529. serial_event_handlers_active++;
  530. }
  531. }
  532. void HardwareSerial::disableSerialEvents()
  533. {
  534. if (serial_event_handler_checks[hardware->serial_index]) {
  535. serial_event_handler_checks[hardware->serial_index] = nullptr; // clear it out
  536. serial_event_handlers_active--;
  537. }
  538. }