Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

576 lines
18KB

  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. //hardware->rx_mux_register = hardware->rx_mux_val;
  128. //hardware->tx_mux_register = hardware->tx_mux_val;
  129. port->BAUD = LPUART_BAUD_OSR(bestosr - 1) | LPUART_BAUD_SBR(bestdiv);
  130. port->PINCFG = 0;
  131. // Enable the transmitter, receiver and enable receiver interrupt
  132. attachInterruptVector(hardware->irq, hardware->irq_handler);
  133. NVIC_SET_PRIORITY(hardware->irq, hardware->irq_priority); // maybe should put into hardware...
  134. NVIC_ENABLE_IRQ(hardware->irq);
  135. uint16_t tx_fifo_size = (((port->FIFO >> 4) & 0x7) << 2);
  136. uint8_t tx_water = (tx_fifo_size < 16) ? tx_fifo_size >> 1 : 7;
  137. uint16_t rx_fifo_size = (((port->FIFO >> 0) & 0x7) << 2);
  138. uint8_t rx_water = (rx_fifo_size < 16) ? rx_fifo_size >> 1 : 7;
  139. /*
  140. Serial.printf("SerialX::begin stat:%x ctrl:%x fifo:%x water:%x\n", port->STAT, port->CTRL, port->FIFO, port->WATER );
  141. Serial.printf(" FIFO sizes: tx:%d rx:%d\n",tx_fifo_size, rx_fifo_size);
  142. Serial.printf(" Watermark tx:%d, rx: %d\n", tx_water, rx_water);
  143. */
  144. port->WATER = LPUART_WATER_RXWATER(rx_water) | LPUART_WATER_TXWATER(tx_water);
  145. port->FIFO |= LPUART_FIFO_TXFE | LPUART_FIFO_RXFE;
  146. // lets configure up our CTRL register value
  147. uint32_t ctrl = CTRL_TX_INACTIVE;
  148. // Now process the bits in the Format value passed in
  149. // Bits 0-2 - Parity plus 9 bit.
  150. ctrl |= (format & (LPUART_CTRL_PT | LPUART_CTRL_PE) ); // configure parity - turn off PT, PE, M and configure PT, PE
  151. if (format & 0x04) ctrl |= LPUART_CTRL_M; // 9 bits (might include parity)
  152. if ((format & 0x0F) == 0x04) ctrl |= LPUART_CTRL_R9T8; // 8N2 is 9 bit with 9th bit always 1
  153. // Bit 5 TXINVERT
  154. if (format & 0x20) ctrl |= LPUART_CTRL_TXINV; // tx invert
  155. // write out computed CTRL
  156. port->CTRL = ctrl;
  157. // Bit 3 10 bit - Will assume that begin already cleared it.
  158. // process some other bits which change other registers.
  159. if (format & 0x08) port->BAUD |= LPUART_BAUD_M10;
  160. // Bit 4 RXINVERT
  161. uint32_t c = port->STAT & ~LPUART_STAT_RXINV;
  162. if (format & 0x10) c |= LPUART_STAT_RXINV; // rx invert
  163. port->STAT = c;
  164. // bit 8 can turn on 2 stop bit mote
  165. if ( format & 0x100) port->BAUD |= LPUART_BAUD_SBNS;
  166. //Serial.printf(" stat:%x ctrl:%x fifo:%x water:%x\n", port->STAT, port->CTRL, port->FIFO, port->WATER );
  167. enableSerialEvents(); // Enable the processing of serialEvent for this object
  168. };
  169. inline void HardwareSerial::rts_assert()
  170. {
  171. DIRECT_WRITE_LOW(rts_pin_baseReg_, rts_pin_bitmask_);
  172. }
  173. inline void HardwareSerial::rts_deassert()
  174. {
  175. DIRECT_WRITE_HIGH(rts_pin_baseReg_, rts_pin_bitmask_);
  176. }
  177. void HardwareSerial::end(void)
  178. {
  179. if (!(hardware->ccm_register & hardware->ccm_value)) return;
  180. while (transmitting_) yield(); // wait for buffered data to send
  181. port->CTRL = 0; // disable the TX and RX ...
  182. // Not sure if this is best, but I think most IO pins default to Mode 5? which appears to be digital IO?
  183. *(portConfigRegister(hardware->rx_pins[rx_pin_index_].pin)) = 5;
  184. *(portConfigRegister(hardware->tx_pins[tx_pin_index_].pin)) = 5;
  185. // Might need to clear out other areas as well?
  186. rx_buffer_head_ = 0;
  187. rx_buffer_tail_ = 0;
  188. if (rts_pin_baseReg_) rts_deassert();
  189. //
  190. disableSerialEvents(); // disable the processing of serialEvent for this object
  191. }
  192. void HardwareSerial::transmitterEnable(uint8_t pin)
  193. {
  194. while (transmitting_) ;
  195. pinMode(pin, OUTPUT);
  196. transmit_pin_baseReg_ = PIN_TO_BASEREG(pin);
  197. transmit_pin_bitmask_ = PIN_TO_BITMASK(pin);
  198. DIRECT_WRITE_LOW(transmit_pin_baseReg_, transmit_pin_bitmask_);
  199. }
  200. void HardwareSerial::setRX(uint8_t pin)
  201. {
  202. if (pin != hardware->rx_pins[rx_pin_index_].pin) {
  203. for (uint8_t rx_pin_new_index = 0; rx_pin_new_index < cnt_rx_pins; rx_pin_new_index++) {
  204. if (pin == hardware->rx_pins[rx_pin_index_].pin) {
  205. // new pin - so lets maybe reset the old pin to INPUT? and then set new pin parameters
  206. *(portConfigRegister(hardware->rx_pins[rx_pin_index_].pin)) = 5;
  207. // now set new pin info.
  208. rx_pin_index_ = rx_pin_new_index;
  209. *(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;;
  210. *(portConfigRegister(hardware->rx_pins[rx_pin_index_].pin)) = hardware->rx_pins[rx_pin_index_].mux_val;
  211. if (hardware->rx_pins[rx_pin_index_].select_input_register) {
  212. *(hardware->rx_pins[rx_pin_index_].select_input_register) = hardware->rx_pins[rx_pin_index_].select_val;
  213. }
  214. break;
  215. }
  216. }
  217. }
  218. }
  219. void HardwareSerial::setTX(uint8_t pin, bool opendrain)
  220. {
  221. uint8_t tx_pin_new_index = tx_pin_index_;
  222. if (pin != hardware->tx_pins[tx_pin_index_].pin) {
  223. for (tx_pin_new_index = 0; tx_pin_new_index < cnt_tx_pins; tx_pin_new_index++) {
  224. if (pin == hardware->tx_pins[tx_pin_index_].pin) {
  225. break;
  226. }
  227. }
  228. if (tx_pin_new_index == cnt_tx_pins) return; // not a new valid pid...
  229. }
  230. // turn on or off opendrain mode.
  231. // new pin - so lets maybe reset the old pin to INPUT? and then set new pin parameters
  232. if (tx_pin_new_index != tx_pin_index_) {
  233. *(portConfigRegister(hardware->tx_pins[tx_pin_index_].pin)) = 5;
  234. *(portConfigRegister(hardware->tx_pins[tx_pin_new_index].pin)) = hardware->tx_pins[tx_pin_new_index].mux_val;
  235. }
  236. // now set new pin info.
  237. tx_pin_index_ = tx_pin_new_index;
  238. if (opendrain)
  239. *(portControlRegister(pin)) = IOMUXC_PAD_ODE | IOMUXC_PAD_DSE(3) | IOMUXC_PAD_SPEED(3);
  240. else
  241. *(portControlRegister(pin)) = IOMUXC_PAD_SRE | IOMUXC_PAD_DSE(3) | IOMUXC_PAD_SPEED(3);
  242. }
  243. bool HardwareSerial::attachRts(uint8_t pin)
  244. {
  245. if (!(hardware->ccm_register & hardware->ccm_value)) return 0;
  246. if (pin < CORE_NUM_DIGITAL) {
  247. rts_pin_baseReg_ = PIN_TO_BASEREG(pin);
  248. rts_pin_bitmask_ = PIN_TO_BITMASK(pin);
  249. pinMode(pin, OUTPUT);
  250. rts_assert();
  251. } else {
  252. rts_pin_baseReg_ = NULL;
  253. return 0;
  254. }
  255. return 1;
  256. }
  257. bool HardwareSerial::attachCts(uint8_t pin)
  258. {
  259. if (!(hardware->ccm_register & hardware->ccm_value)) return false;
  260. if ((pin != 0xff) && (pin == hardware->cts_pin)) {
  261. // Setup the IO pin as weak PULL down.
  262. *(portControlRegister(pin)) = IOMUXC_PAD_DSE(7) | IOMUXC_PAD_PKE | IOMUXC_PAD_PUE | IOMUXC_PAD_PUS(0) | IOMUXC_PAD_HYS;
  263. *(portConfigRegister(hardware->cts_pin)) = hardware->cts_mux_val;
  264. port->MODIR |= LPUART_MODIR_TXCTSE;
  265. return true;
  266. } else {
  267. port->MODIR &= ~LPUART_MODIR_TXCTSE;
  268. return false;
  269. }
  270. }
  271. void HardwareSerial::clear(void)
  272. {
  273. // BUGBUG:: deal with FIFO
  274. rx_buffer_head_ = rx_buffer_tail_;
  275. if (rts_pin_baseReg_) rts_assert();
  276. }
  277. int HardwareSerial::availableForWrite(void)
  278. {
  279. uint32_t head, tail;
  280. head = tx_buffer_head_;
  281. tail = tx_buffer_tail_;
  282. if (head >= tail) return tx_buffer_total_size_ - 1 - head + tail;
  283. return tail - head - 1;
  284. }
  285. int HardwareSerial::available(void)
  286. {
  287. uint32_t head, tail;
  288. head = rx_buffer_head_;
  289. tail = rx_buffer_tail_;
  290. if (head >= tail) return head - tail;
  291. return rx_buffer_total_size_ + head - tail;
  292. }
  293. void HardwareSerial::addStorageForRead(void *buffer, size_t length)
  294. {
  295. rx_buffer_storage_ = (BUFTYPE*)buffer;
  296. if (buffer) {
  297. rx_buffer_total_size_ = rx_buffer_total_size_ + length;
  298. } else {
  299. rx_buffer_total_size_ = rx_buffer_total_size_;
  300. }
  301. rts_low_watermark_ = rx_buffer_total_size_ - hardware->rts_low_watermark;
  302. rts_high_watermark_ = rx_buffer_total_size_ - hardware->rts_high_watermark;
  303. }
  304. void HardwareSerial::addStorageForWrite(void *buffer, size_t length)
  305. {
  306. tx_buffer_storage_ = (BUFTYPE*)buffer;
  307. if (buffer) {
  308. tx_buffer_total_size_ = tx_buffer_total_size_ + length;
  309. } else {
  310. tx_buffer_total_size_ = tx_buffer_total_size_;
  311. }
  312. }
  313. int HardwareSerial::peek(void)
  314. {
  315. uint32_t head, tail;
  316. head = rx_buffer_head_;
  317. tail = rx_buffer_tail_;
  318. if (head == tail) return -1;
  319. if (++tail >= rx_buffer_total_size_) tail = 0;
  320. if (tail < rx_buffer_size_) {
  321. return rx_buffer_[tail];
  322. } else {
  323. return rx_buffer_storage_[tail-rx_buffer_size_];
  324. }
  325. }
  326. int HardwareSerial::read(void)
  327. {
  328. uint32_t head, tail;
  329. int c;
  330. head = rx_buffer_head_;
  331. tail = rx_buffer_tail_;
  332. if (head == tail) return -1;
  333. if (++tail >= rx_buffer_total_size_) tail = 0;
  334. if (tail < rx_buffer_size_) {
  335. c = rx_buffer_[tail];
  336. } else {
  337. c = rx_buffer_storage_[tail-rx_buffer_size_];
  338. }
  339. rx_buffer_tail_ = tail;
  340. if (rts_pin_baseReg_) {
  341. uint32_t avail;
  342. if (head >= tail) avail = head - tail;
  343. else avail = rx_buffer_total_size_ + head - tail;
  344. if (avail <= rts_low_watermark_) rts_assert();
  345. }
  346. return c;
  347. }
  348. void HardwareSerial::flush(void)
  349. {
  350. while (transmitting_) yield(); // wait
  351. }
  352. size_t HardwareSerial::write(uint8_t c)
  353. {
  354. // use the 9 bit version (maybe 10 bit) do do the work.
  355. return write9bit(c);
  356. }
  357. size_t HardwareSerial::write9bit(uint32_t c)
  358. {
  359. uint32_t head, n;
  360. //digitalWrite(3, HIGH);
  361. //digitalWrite(5, HIGH);
  362. if (transmit_pin_baseReg_) DIRECT_WRITE_HIGH(transmit_pin_baseReg_, transmit_pin_bitmask_);
  363. head = tx_buffer_head_;
  364. if (++head >= tx_buffer_total_size_) head = 0;
  365. while (tx_buffer_tail_ == head) {
  366. int priority = nvic_execution_priority();
  367. if (priority <= hardware->irq_priority) {
  368. if ((port->STAT & LPUART_STAT_TDRE)) {
  369. uint32_t tail = tx_buffer_tail_;
  370. if (++tail >= tx_buffer_total_size_) tail = 0;
  371. if (tail < tx_buffer_size_) {
  372. n = tx_buffer_[tail];
  373. } else {
  374. n = tx_buffer_storage_[tail-tx_buffer_size_];
  375. }
  376. port->DATA = n;
  377. tx_buffer_tail_ = tail;
  378. }
  379. } else if (priority >= 256)
  380. {
  381. yield(); // wait
  382. }
  383. }
  384. //digitalWrite(5, LOW);
  385. //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_);
  386. if (head < tx_buffer_size_) {
  387. tx_buffer_[head] = c;
  388. } else {
  389. tx_buffer_storage_[head - tx_buffer_size_] = c;
  390. }
  391. __disable_irq();
  392. transmitting_ = 1;
  393. tx_buffer_head_ = head;
  394. port->CTRL |= LPUART_CTRL_TIE; // (may need to handle this issue)BITBAND_SET_BIT(LPUART0_CTRL, TIE_BIT);
  395. __enable_irq();
  396. //digitalWrite(3, LOW);
  397. return 1;
  398. }
  399. void HardwareSerial::IRQHandler()
  400. {
  401. //digitalWrite(4, HIGH);
  402. uint32_t head, tail, n;
  403. uint32_t ctrl;
  404. // See if we have stuff to read in.
  405. // Todo - Check idle.
  406. if (port->STAT & (LPUART_STAT_RDRF | LPUART_STAT_IDLE)) {
  407. // See how many bytes or pending.
  408. //digitalWrite(5, HIGH);
  409. uint8_t avail = (port->WATER >> 24) & 0x7;
  410. if (avail) {
  411. uint32_t newhead;
  412. head = rx_buffer_head_;
  413. tail = rx_buffer_tail_;
  414. do {
  415. n = port->DATA & 0x3ff; // Use only up to 10 bits of data
  416. newhead = head + 1;
  417. if (newhead >= rx_buffer_total_size_) newhead = 0;
  418. if (newhead != rx_buffer_tail_) {
  419. head = newhead;
  420. if (newhead < rx_buffer_size_) {
  421. rx_buffer_[head] = n;
  422. } else {
  423. rx_buffer_storage_[head-rx_buffer_size_] = n;
  424. }
  425. }
  426. } while (--avail > 0) ;
  427. rx_buffer_head_ = head;
  428. if (rts_pin_baseReg_) {
  429. uint32_t avail;
  430. if (head >= tail) avail = head - tail;
  431. else avail = rx_buffer_total_size_ + head - tail;
  432. if (avail >= rts_high_watermark_) rts_deassert();
  433. }
  434. }
  435. // If it was an idle status clear the idle
  436. if (port->STAT & LPUART_STAT_IDLE) {
  437. port->STAT |= LPUART_STAT_IDLE; // writing a 1 to idle should clear it.
  438. }
  439. //digitalWrite(5, LOW);
  440. }
  441. // See if we are transmitting and room in buffer.
  442. ctrl = port->CTRL;
  443. if ((ctrl & LPUART_CTRL_TIE) && (port->STAT & LPUART_STAT_TDRE))
  444. {
  445. //digitalWrite(3, HIGH);
  446. head = tx_buffer_head_;
  447. tail = tx_buffer_tail_;
  448. do {
  449. if (head == tail) break;
  450. if (++tail >= tx_buffer_total_size_) tail = 0;
  451. if (tail < tx_buffer_size_) {
  452. n = tx_buffer_[tail];
  453. } else {
  454. n = tx_buffer_storage_[tail-tx_buffer_size_];
  455. }
  456. port->DATA = n;
  457. } while (((port->WATER >> 8) & 0x7) < 4); // need to computer properly
  458. tx_buffer_tail_ = tail;
  459. if (head == tail) {
  460. port->CTRL &= ~LPUART_CTRL_TIE;
  461. port->CTRL |= LPUART_CTRL_TCIE; // Actually wondering if we can just leave this one on...
  462. }
  463. //digitalWrite(3, LOW);
  464. }
  465. if ((ctrl & LPUART_CTRL_TCIE) && (port->STAT & LPUART_STAT_TC))
  466. {
  467. transmitting_ = 0;
  468. if (transmit_pin_baseReg_) DIRECT_WRITE_LOW(transmit_pin_baseReg_, transmit_pin_bitmask_);
  469. port->CTRL &= ~LPUART_CTRL_TCIE;
  470. }
  471. //digitalWrite(4, LOW);
  472. }
  473. void HardwareSerial::processSerialEvents()
  474. {
  475. if (!serial_event_handlers_active) return; // bail quick if no one processing SerialEvents.
  476. uint8_t handlers_still_to_process = serial_event_handlers_active;
  477. for (uint8_t i = 0; i < 8; i++) {
  478. if (serial_event_handler_checks[i]) {
  479. (*serial_event_handler_checks[i])();
  480. if (--handlers_still_to_process == 0) return;
  481. }
  482. }
  483. }
  484. void HardwareSerial::enableSerialEvents()
  485. {
  486. if (!serial_event_handler_checks[hardware->serial_index]) {
  487. serial_event_handler_checks[hardware->serial_index] = hardware->serial_event_handler_check; // clear it out
  488. serial_event_handlers_active++;
  489. }
  490. }
  491. void HardwareSerial::disableSerialEvents()
  492. {
  493. if (serial_event_handler_checks[hardware->serial_index]) {
  494. serial_event_handler_checks[hardware->serial_index] = nullptr; // clear it out
  495. serial_event_handlers_active--;
  496. }
  497. }