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.

671 lines
23KB

  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. SerialEventCheckingFunctionPointer HardwareSerial::serial_event_handler_checks[8] = {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
  65. #else
  66. SerialEventCheckingFunctionPointer HardwareSerial::serial_event_handler_checks[7] = {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
  67. #endif
  68. uint8_t HardwareSerial::serial_event_handlers_active = 0;
  69. #define CTRL_ENABLE (LPUART_CTRL_TE | LPUART_CTRL_RE | LPUART_CTRL_RIE | LPUART_CTRL_ILIE)
  70. #define CTRL_TX_ACTIVE (CTRL_ENABLE | LPUART_CTRL_TIE)
  71. #define CTRL_TX_COMPLETING (CTRL_ENABLE | LPUART_CTRL_TCIE)
  72. #define CTRL_TX_INACTIVE CTRL_ENABLE
  73. // Copied from T3.x - probably should move to other location.
  74. int nvic_execution_priority(void)
  75. {
  76. uint32_t priority=256;
  77. uint32_t primask, faultmask, basepri, ipsr;
  78. // full algorithm in ARM DDI0403D, page B1-639
  79. // this isn't quite complete, but hopefully good enough
  80. __asm__ volatile("mrs %0, faultmask\n" : "=r" (faultmask)::);
  81. if (faultmask) return -1;
  82. __asm__ volatile("mrs %0, primask\n" : "=r" (primask)::);
  83. if (primask) return 0;
  84. __asm__ volatile("mrs %0, ipsr\n" : "=r" (ipsr)::);
  85. if (ipsr) {
  86. if (ipsr < 16) priority = 0; // could be non-zero
  87. else priority = NVIC_GET_PRIORITY(ipsr - 16);
  88. }
  89. __asm__ volatile("mrs %0, basepri\n" : "=r" (basepri)::);
  90. if (basepri > 0 && basepri < priority) priority = basepri;
  91. return priority;
  92. }
  93. void HardwareSerial::begin(uint32_t baud, uint16_t format)
  94. {
  95. //printf("HardwareSerial begin\n");
  96. float base = (float)UART_CLOCK / (float)baud;
  97. float besterr = 1e20;
  98. int bestdiv = 1;
  99. int bestosr = 4;
  100. for (int osr=4; osr <= 32; osr++) {
  101. float div = base / (float)osr;
  102. int divint = (int)(div + 0.5f);
  103. if (divint < 1) divint = 1;
  104. else if (divint > 8191) divint = 8191;
  105. float err = ((float)divint - div) / div;
  106. if (err < 0.0f) err = -err;
  107. if (err <= besterr) {
  108. besterr = err;
  109. bestdiv = divint;
  110. bestosr = osr;
  111. }
  112. }
  113. //printf(" baud %d: osr=%d, div=%d\n", baud, bestosr, bestdiv);
  114. rx_buffer_head_ = 0;
  115. rx_buffer_tail_ = 0;
  116. tx_buffer_head_ = 0;
  117. tx_buffer_tail_ = 0;
  118. rts_low_watermark_ = rx_buffer_total_size_ - hardware->rts_low_watermark;
  119. rts_high_watermark_ = rx_buffer_total_size_ - hardware->rts_high_watermark;
  120. transmitting_ = 0;
  121. hardware->ccm_register |= hardware->ccm_value;
  122. // uint32_t fastio = IOMUXC_PAD_SRE | IOMUXC_PAD_DSE(3) | IOMUXC_PAD_SPEED(3);
  123. *(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;
  124. *(portConfigRegister(hardware->rx_pins[rx_pin_index_].pin)) = hardware->rx_pins[rx_pin_index_].mux_val;
  125. if (hardware->rx_pins[rx_pin_index_].select_input_register) {
  126. *(hardware->rx_pins[rx_pin_index_].select_input_register) = hardware->rx_pins[rx_pin_index_].select_val;
  127. }
  128. *(portControlRegister(hardware->tx_pins[tx_pin_index_].pin)) = IOMUXC_PAD_SRE | IOMUXC_PAD_DSE(3) | IOMUXC_PAD_SPEED(3);
  129. *(portConfigRegister(hardware->tx_pins[tx_pin_index_].pin)) = hardware->tx_pins[tx_pin_index_].mux_val;
  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. // Only if the user implemented their own...
  172. if (!(*hardware->serial_event_handler_default)) enableSerialEvents(); // Enable the processing of serialEvent for this object
  173. };
  174. inline void HardwareSerial::rts_assert()
  175. {
  176. DIRECT_WRITE_LOW(rts_pin_baseReg_, rts_pin_bitmask_);
  177. }
  178. inline void HardwareSerial::rts_deassert()
  179. {
  180. DIRECT_WRITE_HIGH(rts_pin_baseReg_, rts_pin_bitmask_);
  181. }
  182. void HardwareSerial::end(void)
  183. {
  184. if (!(hardware->ccm_register & hardware->ccm_value)) return;
  185. while (transmitting_) yield(); // wait for buffered data to send
  186. port->CTRL = 0; // disable the TX and RX ...
  187. // Not sure if this is best, but I think most IO pins default to Mode 5? which appears to be digital IO?
  188. *(portConfigRegister(hardware->rx_pins[rx_pin_index_].pin)) = 5;
  189. *(portConfigRegister(hardware->tx_pins[tx_pin_index_].pin)) = 5;
  190. // Might need to clear out other areas as well?
  191. rx_buffer_head_ = 0;
  192. rx_buffer_tail_ = 0;
  193. if (rts_pin_baseReg_) rts_deassert();
  194. //
  195. disableSerialEvents(); // disable the processing of serialEvent for this object
  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::processSerialEvents()
  525. {
  526. if (!serial_event_handlers_active) return; // bail quick if no one processing SerialEvents.
  527. uint8_t handlers_still_to_process = serial_event_handlers_active;
  528. for (uint8_t i = 0; i < 8; i++) {
  529. if (serial_event_handler_checks[i]) {
  530. (*serial_event_handler_checks[i])();
  531. if (--handlers_still_to_process == 0) return;
  532. }
  533. }
  534. }
  535. void HardwareSerial::enableSerialEvents()
  536. {
  537. if (!serial_event_handler_checks[hardware->serial_index]) {
  538. serial_event_handler_checks[hardware->serial_index] = hardware->serial_event_handler_check; // clear it out
  539. serial_event_handlers_active++;
  540. yield_active_check_flags |= YIELD_CHECK_HARDWARE_SERIAL;
  541. }
  542. }
  543. void HardwareSerial::disableSerialEvents()
  544. {
  545. if (serial_event_handler_checks[hardware->serial_index]) {
  546. serial_event_handler_checks[hardware->serial_index] = nullptr; // clear it out
  547. serial_event_handlers_active--;
  548. if (!serial_event_handlers_active) yield_active_check_flags &= ~YIELD_CHECK_HARDWARE_SERIAL;
  549. }
  550. }
  551. const pin_to_xbar_info_t PROGMEM pin_to_xbar_info[] = {
  552. {0, 17, 1, &IOMUXC_XBAR1_IN17_SELECT_INPUT, 0x1},
  553. {1, 16, 1, nullptr, 0},
  554. {2, 6, 3, &IOMUXC_XBAR1_IN06_SELECT_INPUT, 0x0},
  555. {3, 7, 3, &IOMUXC_XBAR1_IN07_SELECT_INPUT, 0x0},
  556. {4, 8, 3, &IOMUXC_XBAR1_IN08_SELECT_INPUT, 0x0},
  557. {5, 17, 3, &IOMUXC_XBAR1_IN17_SELECT_INPUT, 0x0},
  558. {7, 15, 1, nullptr, 0 },
  559. {8, 14, 1, nullptr, 0},
  560. {30, 23, 1, &IOMUXC_XBAR1_IN23_SELECT_INPUT, 0x0},
  561. {31, 22, 1, &IOMUXC_XBAR1_IN22_SELECT_INPUT, 0x0},
  562. {32, 10, 1, nullptr, 0},
  563. {33, 9, 3, &IOMUXC_XBAR1_IN09_SELECT_INPUT, 0x0},
  564. #ifdef ARDUINO_TEENSY41
  565. {36, 16, 1, nullptr, 0},
  566. {37, 17, 1, &IOMUXC_XBAR1_IN17_SELECT_INPUT, 0x3},
  567. {42, 7, 3, &IOMUXC_XBAR1_IN07_SELECT_INPUT, 0x1},
  568. {43, 6, 3, &IOMUXC_XBAR1_IN06_SELECT_INPUT, 0x1},
  569. {44, 5, 3, &IOMUXC_XBAR1_IN05_SELECT_INPUT, 0x1},
  570. {45, 4, 3, &IOMUXC_XBAR1_IN04_SELECT_INPUT, 0x1},
  571. {46, 9, 3, &IOMUXC_XBAR1_IN09_SELECT_INPUT, 0x1},
  572. {47, 8, 3, &IOMUXC_XBAR1_IN08_SELECT_INPUT, 0x1}
  573. #else
  574. {34, 7, 3, &IOMUXC_XBAR1_IN07_SELECT_INPUT, 0x1},
  575. {35, 6, 3, &IOMUXC_XBAR1_IN06_SELECT_INPUT, 0x1},
  576. {36, 5, 3, &IOMUXC_XBAR1_IN05_SELECT_INPUT, 0x1},
  577. {37, 4, 3, &IOMUXC_XBAR1_IN04_SELECT_INPUT, 0x1},
  578. {38, 9, 3, &IOMUXC_XBAR1_IN09_SELECT_INPUT, 0x1},
  579. {39, 8, 3, &IOMUXC_XBAR1_IN08_SELECT_INPUT, 0x1}
  580. #endif
  581. };
  582. const uint8_t PROGMEM count_pin_to_xbar_info = sizeof(pin_to_xbar_info)/sizeof(pin_to_xbar_info[0]);