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.

648 lines
22KB

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