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.

714 lines
24KB

  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. // Maybe different pin configs if half duplex
  125. half_duplex_mode_ = (format & SERIAL_HALF_DUPLEX) != 0;
  126. if (!half_duplex_mode_) {
  127. *(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;
  128. *(portConfigRegister(hardware->rx_pins[rx_pin_index_].pin)) = hardware->rx_pins[rx_pin_index_].mux_val;
  129. if (hardware->rx_pins[rx_pin_index_].select_input_register) {
  130. *(hardware->rx_pins[rx_pin_index_].select_input_register) = hardware->rx_pins[rx_pin_index_].select_val;
  131. }
  132. *(portControlRegister(hardware->tx_pins[tx_pin_index_].pin)) = IOMUXC_PAD_SRE | IOMUXC_PAD_DSE(3) | IOMUXC_PAD_SPEED(3);
  133. *(portConfigRegister(hardware->tx_pins[tx_pin_index_].pin)) = hardware->tx_pins[tx_pin_index_].mux_val;
  134. } else {
  135. // Half duplex maybe different pin pad config like PU...
  136. *(portControlRegister(hardware->tx_pins[tx_pin_index_].pin)) = IOMUXC_PAD_SRE | IOMUXC_PAD_DSE(3) | IOMUXC_PAD_SPEED(3)
  137. | IOMUXC_PAD_PKE | IOMUXC_PAD_PUE | IOMUXC_PAD_PUS(3);
  138. *(portConfigRegister(hardware->tx_pins[tx_pin_index_].pin)) = hardware->tx_pins[tx_pin_index_].mux_val;
  139. }
  140. if (hardware->tx_pins[tx_pin_index_].select_input_register) {
  141. *(hardware->tx_pins[tx_pin_index_].select_input_register) = hardware->tx_pins[tx_pin_index_].select_val;
  142. }
  143. //hardware->rx_mux_register = hardware->rx_mux_val;
  144. //hardware->tx_mux_register = hardware->tx_mux_val;
  145. port->BAUD = LPUART_BAUD_OSR(bestosr - 1) | LPUART_BAUD_SBR(bestdiv)
  146. | (bestosr <= 8 ? LPUART_BAUD_BOTHEDGE : 0);
  147. port->PINCFG = 0;
  148. // Enable the transmitter, receiver and enable receiver interrupt
  149. attachInterruptVector(hardware->irq, hardware->irq_handler);
  150. NVIC_SET_PRIORITY(hardware->irq, hardware->irq_priority); // maybe should put into hardware...
  151. NVIC_ENABLE_IRQ(hardware->irq);
  152. uint16_t tx_fifo_size = (((port->FIFO >> 4) & 0x7) << 2);
  153. uint8_t tx_water = (tx_fifo_size < 16) ? tx_fifo_size >> 1 : 7;
  154. uint16_t rx_fifo_size = (((port->FIFO >> 0) & 0x7) << 2);
  155. uint8_t rx_water = (rx_fifo_size < 16) ? rx_fifo_size >> 1 : 7;
  156. /*
  157. Serial.printf("SerialX::begin stat:%x ctrl:%x fifo:%x water:%x\n", port->STAT, port->CTRL, port->FIFO, port->WATER );
  158. Serial.printf(" FIFO sizes: tx:%d rx:%d\n",tx_fifo_size, rx_fifo_size);
  159. Serial.printf(" Watermark tx:%d, rx: %d\n", tx_water, rx_water);
  160. */
  161. port->WATER = LPUART_WATER_RXWATER(rx_water) | LPUART_WATER_TXWATER(tx_water);
  162. port->FIFO |= LPUART_FIFO_TXFE | LPUART_FIFO_RXFE;
  163. // lets configure up our CTRL register value
  164. uint32_t ctrl = CTRL_TX_INACTIVE;
  165. // Now process the bits in the Format value passed in
  166. // Bits 0-2 - Parity plus 9 bit.
  167. ctrl |= (format & (LPUART_CTRL_PT | LPUART_CTRL_PE) ); // configure parity - turn off PT, PE, M and configure PT, PE
  168. if (format & 0x04) ctrl |= LPUART_CTRL_M; // 9 bits (might include parity)
  169. if ((format & 0x0F) == 0x04) ctrl |= LPUART_CTRL_R9T8; // 8N2 is 9 bit with 9th bit always 1
  170. // Bit 5 TXINVERT
  171. if (format & 0x20) ctrl |= LPUART_CTRL_TXINV; // tx invert
  172. // Now see if the user asked for Half duplex:
  173. if (half_duplex_mode_) ctrl |= (LPUART_CTRL_LOOPS | LPUART_CTRL_RSRC);
  174. // write out computed CTRL
  175. port->CTRL = ctrl;
  176. // Bit 3 10 bit - Will assume that begin already cleared it.
  177. // process some other bits which change other registers.
  178. if (format & 0x08) port->BAUD |= LPUART_BAUD_M10;
  179. // Bit 4 RXINVERT
  180. uint32_t c = port->STAT & ~LPUART_STAT_RXINV;
  181. if (format & 0x10) c |= LPUART_STAT_RXINV; // rx invert
  182. port->STAT = c;
  183. // bit 8 can turn on 2 stop bit mote
  184. if ( format & 0x100) port->BAUD |= LPUART_BAUD_SBNS;
  185. //Serial.printf(" stat:%x ctrl:%x fifo:%x water:%x\n", port->STAT, port->CTRL, port->FIFO, port->WATER );
  186. // Only if the user implemented their own...
  187. if (!(*hardware->serial_event_handler_default)) addToSerialEventsList(); // Enable the processing of serialEvent for this object
  188. };
  189. inline void HardwareSerial::rts_assert()
  190. {
  191. DIRECT_WRITE_LOW(rts_pin_baseReg_, rts_pin_bitmask_);
  192. }
  193. inline void HardwareSerial::rts_deassert()
  194. {
  195. DIRECT_WRITE_HIGH(rts_pin_baseReg_, rts_pin_bitmask_);
  196. }
  197. void HardwareSerial::end(void)
  198. {
  199. if (!(hardware->ccm_register & hardware->ccm_value)) return;
  200. while (transmitting_) yield(); // wait for buffered data to send
  201. port->CTRL = 0; // disable the TX and RX ...
  202. // Not sure if this is best, but I think most IO pins default to Mode 5? which appears to be digital IO?
  203. *(portConfigRegister(hardware->rx_pins[rx_pin_index_].pin)) = 5;
  204. *(portConfigRegister(hardware->tx_pins[tx_pin_index_].pin)) = 5;
  205. // Might need to clear out other areas as well?
  206. rx_buffer_head_ = 0;
  207. rx_buffer_tail_ = 0;
  208. if (rts_pin_baseReg_) rts_deassert();
  209. //
  210. }
  211. void HardwareSerial::transmitterEnable(uint8_t pin)
  212. {
  213. while (transmitting_) ;
  214. pinMode(pin, OUTPUT);
  215. transmit_pin_baseReg_ = PIN_TO_BASEREG(pin);
  216. transmit_pin_bitmask_ = PIN_TO_BITMASK(pin);
  217. DIRECT_WRITE_LOW(transmit_pin_baseReg_, transmit_pin_bitmask_);
  218. }
  219. void HardwareSerial::setRX(uint8_t pin)
  220. {
  221. if (pin != hardware->rx_pins[rx_pin_index_].pin) {
  222. for (uint8_t rx_pin_new_index = 0; rx_pin_new_index < cnt_rx_pins; rx_pin_new_index++) {
  223. if (pin == hardware->rx_pins[rx_pin_new_index].pin) {
  224. // new pin - so lets maybe reset the old pin to INPUT? and then set new pin parameters
  225. // only change IO pins if done after begin has been called.
  226. if ((hardware->ccm_register & hardware->ccm_value)) {
  227. *(portConfigRegister(hardware->rx_pins[rx_pin_index_].pin)) = 5;
  228. // now set new pin info.
  229. *(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;;
  230. *(portConfigRegister(hardware->rx_pins[rx_pin_new_index].pin)) = hardware->rx_pins[rx_pin_new_index].mux_val;
  231. if (hardware->rx_pins[rx_pin_new_index].select_input_register) {
  232. *(hardware->rx_pins[rx_pin_new_index].select_input_register) = hardware->rx_pins[rx_pin_new_index].select_val;
  233. }
  234. }
  235. rx_pin_index_ = rx_pin_new_index;
  236. return; // done.
  237. }
  238. }
  239. // If we got to here and did not find a valid pin there. Maybe see if it is an XBar pin...
  240. for (uint8_t i = 0; i < count_pin_to_xbar_info; i++) {
  241. if (pin_to_xbar_info[i].pin == pin) {
  242. // So it is an XBAR pin set the XBAR..
  243. //Serial.printf("ACTS XB(%d), X(%u %u), MUX:%x\n", i, pin_to_xbar_info[i].xbar_in_index,
  244. // hardware->xbar_out_lpuartX_trig_input, pin_to_xbar_info[i].mux_val);
  245. CCM_CCGR2 |= CCM_CCGR2_XBAR1(CCM_CCGR_ON);
  246. xbar_connect(pin_to_xbar_info[i].xbar_in_index, hardware->xbar_out_lpuartX_trig_input);
  247. // We need to update port register to use this as the trigger
  248. port->PINCFG = LPUART_PINCFG_TRGSEL(1); // Trigger select as alternate RX
  249. // configure the pin.
  250. *(portControlRegister(pin)) = IOMUXC_PAD_DSE(7) | IOMUXC_PAD_PKE | IOMUXC_PAD_PUE | IOMUXC_PAD_PUS(3) | IOMUXC_PAD_HYS;;
  251. *(portConfigRegister(pin)) = pin_to_xbar_info[i].mux_val;
  252. port->MODIR |= LPUART_MODIR_TXCTSE;
  253. if (pin_to_xbar_info[i].select_input_register) *(pin_to_xbar_info[i].select_input_register) = pin_to_xbar_info[i].select_val;
  254. //Serial.printf("SerialX::begin stat:%x ctrl:%x fifo:%x water:%x\n", port->STAT, port->CTRL, port->FIFO, port->WATER );
  255. //Serial.printf(" PINCFG: %x MODIR: %x\n", port->PINCFG, port->MODIR);
  256. return;
  257. }
  258. }
  259. }
  260. }
  261. void HardwareSerial::setTX(uint8_t pin, bool opendrain)
  262. {
  263. uint8_t tx_pin_new_index = tx_pin_index_;
  264. if (pin != hardware->tx_pins[tx_pin_index_].pin) {
  265. for (tx_pin_new_index = 0; tx_pin_new_index < cnt_tx_pins; tx_pin_new_index++) {
  266. if (pin == hardware->tx_pins[tx_pin_new_index].pin) {
  267. break;
  268. }
  269. }
  270. if (tx_pin_new_index == cnt_tx_pins) return; // not a new valid pid...
  271. }
  272. // turn on or off opendrain mode.
  273. // new pin - so lets maybe reset the old pin to INPUT? and then set new pin parameters
  274. if ((hardware->ccm_register & hardware->ccm_value)) { // only do if we are already active.
  275. if (tx_pin_new_index != tx_pin_index_) {
  276. *(portConfigRegister(hardware->tx_pins[tx_pin_index_].pin)) = 5;
  277. *(portConfigRegister(hardware->tx_pins[tx_pin_new_index].pin)) = hardware->tx_pins[tx_pin_new_index].mux_val;
  278. }
  279. }
  280. // now set new pin info.
  281. tx_pin_index_ = tx_pin_new_index;
  282. if (opendrain)
  283. *(portControlRegister(pin)) = IOMUXC_PAD_ODE | IOMUXC_PAD_DSE(3) | IOMUXC_PAD_SPEED(3);
  284. else
  285. *(portControlRegister(pin)) = IOMUXC_PAD_SRE | IOMUXC_PAD_DSE(3) | IOMUXC_PAD_SPEED(3);
  286. }
  287. bool HardwareSerial::attachRts(uint8_t pin)
  288. {
  289. if (!(hardware->ccm_register & hardware->ccm_value)) return 0;
  290. if (pin < CORE_NUM_DIGITAL) {
  291. rts_pin_baseReg_ = PIN_TO_BASEREG(pin);
  292. rts_pin_bitmask_ = PIN_TO_BITMASK(pin);
  293. pinMode(pin, OUTPUT);
  294. rts_assert();
  295. } else {
  296. rts_pin_baseReg_ = NULL;
  297. return 0;
  298. }
  299. return 1;
  300. }
  301. bool HardwareSerial::attachCts(uint8_t pin)
  302. {
  303. if (!(hardware->ccm_register & hardware->ccm_value)) return false;
  304. if ((pin != 0xff) && (pin == hardware->cts_pin)) {
  305. // Setup the IO pin as weak PULL down.
  306. *(portControlRegister(pin)) = IOMUXC_PAD_DSE(7) | IOMUXC_PAD_PKE | IOMUXC_PAD_PUE | IOMUXC_PAD_PUS(0) | IOMUXC_PAD_HYS;
  307. *(portConfigRegister(hardware->cts_pin)) = hardware->cts_mux_val;
  308. port->MODIR |= LPUART_MODIR_TXCTSE;
  309. return true;
  310. } else {
  311. // See maybe this a pin we can use XBAR for.
  312. for (uint8_t i = 0; i < count_pin_to_xbar_info; i++) {
  313. if (pin_to_xbar_info[i].pin == pin) {
  314. // So it is an XBAR pin set the XBAR..
  315. //Serial.printf("ACTS XB(%d), X(%u %u), MUX:%x\n", i, pin_to_xbar_info[i].xbar_in_index,
  316. // hardware->xbar_out_lpuartX_trig_input, pin_to_xbar_info[i].mux_val);
  317. CCM_CCGR2 |= CCM_CCGR2_XBAR1(CCM_CCGR_ON);
  318. xbar_connect(pin_to_xbar_info[i].xbar_in_index, hardware->xbar_out_lpuartX_trig_input);
  319. // We need to update port register to use this as the trigger
  320. port->PINCFG = LPUART_PINCFG_TRGSEL(2); // Trigger select as alternate CTS pin
  321. // configure the pin.
  322. *(portControlRegister(pin)) = IOMUXC_PAD_DSE(7) | IOMUXC_PAD_PKE | IOMUXC_PAD_PUE | IOMUXC_PAD_PUS(0) | IOMUXC_PAD_HYS;
  323. *(portConfigRegister(pin)) = pin_to_xbar_info[i].mux_val;
  324. if (pin_to_xbar_info[i].select_input_register) *(pin_to_xbar_info[i].select_input_register) = pin_to_xbar_info[i].select_val;
  325. port->MODIR |= LPUART_MODIR_TXCTSE;
  326. //Serial.printf("SerialX::begin stat:%x ctrl:%x fifo:%x water:%x\n", port->STAT, port->CTRL, port->FIFO, port->WATER );
  327. //Serial.printf(" PINCFG: %x MODIR: %x\n", port->PINCFG, port->MODIR);
  328. return true;
  329. }
  330. }
  331. // Fell through so not valid pin for this.
  332. port->MODIR &= ~LPUART_MODIR_TXCTSE;
  333. return false;
  334. }
  335. }
  336. void HardwareSerial::clear(void)
  337. {
  338. // BUGBUG:: deal with FIFO
  339. rx_buffer_head_ = rx_buffer_tail_;
  340. if (rts_pin_baseReg_) rts_assert();
  341. }
  342. int HardwareSerial::availableForWrite(void)
  343. {
  344. uint32_t head, tail;
  345. head = tx_buffer_head_;
  346. tail = tx_buffer_tail_;
  347. if (head >= tail) return tx_buffer_total_size_ - 1 - head + tail;
  348. return tail - head - 1;
  349. }
  350. int HardwareSerial::available(void)
  351. {
  352. uint32_t head, tail;
  353. // WATER> 0 so IDLE involved may want to check if port has already has RX data to retrieve
  354. __disable_irq();
  355. head = rx_buffer_head_;
  356. tail = rx_buffer_tail_;
  357. int avail;
  358. if (head >= tail) avail = head - tail;
  359. else avail = rx_buffer_total_size_ + head - tail;
  360. avail += (port->WATER >> 24) & 0x7;
  361. __enable_irq();
  362. return avail;
  363. }
  364. void HardwareSerial::addMemoryForRead(void *buffer, size_t length)
  365. {
  366. rx_buffer_storage_ = (BUFTYPE*)buffer;
  367. if (buffer) {
  368. rx_buffer_total_size_ = rx_buffer_total_size_ + length;
  369. } else {
  370. rx_buffer_total_size_ = rx_buffer_total_size_;
  371. }
  372. rts_low_watermark_ = rx_buffer_total_size_ - hardware->rts_low_watermark;
  373. rts_high_watermark_ = rx_buffer_total_size_ - hardware->rts_high_watermark;
  374. }
  375. void HardwareSerial::addMemoryForWrite(void *buffer, size_t length)
  376. {
  377. tx_buffer_storage_ = (BUFTYPE*)buffer;
  378. if (buffer) {
  379. tx_buffer_total_size_ = tx_buffer_total_size_ + length;
  380. } else {
  381. tx_buffer_total_size_ = tx_buffer_total_size_;
  382. }
  383. }
  384. int HardwareSerial::peek(void)
  385. {
  386. uint32_t head, tail;
  387. head = rx_buffer_head_;
  388. tail = rx_buffer_tail_;
  389. if (head == tail) {
  390. __disable_irq();
  391. head = rx_buffer_head_; // reread head to make sure no ISR happened
  392. if (head == tail) {
  393. // Still empty Now check for stuff in FIFO Queue.
  394. int c = -1; // assume nothing to return
  395. if (port->WATER & 0x7000000) {
  396. c = port->DATA & 0x3ff; // Use only up to 10 bits of data
  397. // But we don't want to throw it away...
  398. // since queue is empty, just going to reset to front of queue...
  399. rx_buffer_head_ = 1;
  400. rx_buffer_tail_ = 0;
  401. rx_buffer_[1] = c;
  402. }
  403. __enable_irq();
  404. return c;
  405. }
  406. __enable_irq();
  407. }
  408. if (++tail >= rx_buffer_total_size_) tail = 0;
  409. if (tail < rx_buffer_size_) {
  410. return rx_buffer_[tail];
  411. } else {
  412. return rx_buffer_storage_[tail-rx_buffer_size_];
  413. }
  414. }
  415. int HardwareSerial::read(void)
  416. {
  417. uint32_t head, tail;
  418. int c;
  419. head = rx_buffer_head_;
  420. tail = rx_buffer_tail_;
  421. if (head == tail) {
  422. __disable_irq();
  423. head = rx_buffer_head_; // reread head to make sure no ISR happened
  424. if (head == tail) {
  425. // Still empty Now check for stuff in FIFO Queue.
  426. c = -1; // assume nothing to return
  427. if (port->WATER & 0x7000000) {
  428. c = port->DATA & 0x3ff; // Use only up to 10 bits of data
  429. }
  430. __enable_irq();
  431. return c;
  432. }
  433. __enable_irq();
  434. }
  435. if (++tail >= rx_buffer_total_size_) tail = 0;
  436. if (tail < rx_buffer_size_) {
  437. c = rx_buffer_[tail];
  438. } else {
  439. c = rx_buffer_storage_[tail-rx_buffer_size_];
  440. }
  441. rx_buffer_tail_ = tail;
  442. if (rts_pin_baseReg_) {
  443. uint32_t avail;
  444. if (head >= tail) avail = head - tail;
  445. else avail = rx_buffer_total_size_ + head - tail;
  446. if (avail <= rts_low_watermark_) rts_assert();
  447. }
  448. return c;
  449. }
  450. void HardwareSerial::flush(void)
  451. {
  452. while (transmitting_) yield(); // wait
  453. }
  454. size_t HardwareSerial::write(uint8_t c)
  455. {
  456. // use the 9 bit version (maybe 10 bit) do do the work.
  457. return write9bit(c);
  458. }
  459. size_t HardwareSerial::write9bit(uint32_t c)
  460. {
  461. uint32_t head, n;
  462. //digitalWrite(3, HIGH);
  463. //digitalWrite(5, HIGH);
  464. if (transmit_pin_baseReg_) DIRECT_WRITE_HIGH(transmit_pin_baseReg_, transmit_pin_bitmask_);
  465. if(half_duplex_mode_) {
  466. __disable_irq();
  467. port->CTRL |= LPUART_CTRL_TXDIR;
  468. __enable_irq();
  469. //digitalWriteFast(2, HIGH);
  470. }
  471. head = tx_buffer_head_;
  472. if (++head >= tx_buffer_total_size_) head = 0;
  473. while (tx_buffer_tail_ == head) {
  474. int priority = nvic_execution_priority();
  475. if (priority <= hardware->irq_priority) {
  476. if ((port->STAT & LPUART_STAT_TDRE)) {
  477. uint32_t tail = tx_buffer_tail_;
  478. if (++tail >= tx_buffer_total_size_) tail = 0;
  479. if (tail < tx_buffer_size_) {
  480. n = tx_buffer_[tail];
  481. } else {
  482. n = tx_buffer_storage_[tail-tx_buffer_size_];
  483. }
  484. port->DATA = n;
  485. tx_buffer_tail_ = tail;
  486. }
  487. } else if (priority >= 256)
  488. {
  489. yield(); // wait
  490. }
  491. }
  492. //digitalWrite(5, LOW);
  493. //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_);
  494. if (head < tx_buffer_size_) {
  495. tx_buffer_[head] = c;
  496. } else {
  497. tx_buffer_storage_[head - tx_buffer_size_] = c;
  498. }
  499. __disable_irq();
  500. transmitting_ = 1;
  501. tx_buffer_head_ = head;
  502. port->CTRL |= LPUART_CTRL_TIE; // (may need to handle this issue)BITBAND_SET_BIT(LPUART0_CTRL, TIE_BIT);
  503. __enable_irq();
  504. //digitalWrite(3, LOW);
  505. return 1;
  506. }
  507. void HardwareSerial::IRQHandler()
  508. {
  509. //digitalWrite(4, HIGH);
  510. uint32_t head, tail, n;
  511. uint32_t ctrl;
  512. // See if we have stuff to read in.
  513. // Todo - Check idle.
  514. if (port->STAT & (LPUART_STAT_RDRF | LPUART_STAT_IDLE)) {
  515. // See how many bytes or pending.
  516. //digitalWrite(5, HIGH);
  517. uint8_t avail = (port->WATER >> 24) & 0x7;
  518. if (avail) {
  519. uint32_t newhead;
  520. head = rx_buffer_head_;
  521. tail = rx_buffer_tail_;
  522. do {
  523. n = port->DATA & 0x3ff; // Use only up to 10 bits of data
  524. newhead = head + 1;
  525. if (newhead >= rx_buffer_total_size_) newhead = 0;
  526. if (newhead != rx_buffer_tail_) {
  527. head = newhead;
  528. if (newhead < rx_buffer_size_) {
  529. rx_buffer_[head] = n;
  530. } else {
  531. rx_buffer_storage_[head-rx_buffer_size_] = n;
  532. }
  533. }
  534. } while (--avail > 0) ;
  535. rx_buffer_head_ = head;
  536. if (rts_pin_baseReg_) {
  537. uint32_t avail;
  538. if (head >= tail) avail = head - tail;
  539. else avail = rx_buffer_total_size_ + head - tail;
  540. if (avail >= rts_high_watermark_) rts_deassert();
  541. }
  542. }
  543. // If it was an idle status clear the idle
  544. if (port->STAT & LPUART_STAT_IDLE) {
  545. port->STAT |= LPUART_STAT_IDLE; // writing a 1 to idle should clear it.
  546. }
  547. //digitalWrite(5, LOW);
  548. }
  549. // See if we are transmitting and room in buffer.
  550. ctrl = port->CTRL;
  551. if ((ctrl & LPUART_CTRL_TIE) && (port->STAT & LPUART_STAT_TDRE))
  552. {
  553. //digitalWrite(3, HIGH);
  554. head = tx_buffer_head_;
  555. tail = tx_buffer_tail_;
  556. do {
  557. if (head == tail) break;
  558. if (++tail >= tx_buffer_total_size_) tail = 0;
  559. if (tail < tx_buffer_size_) {
  560. n = tx_buffer_[tail];
  561. } else {
  562. n = tx_buffer_storage_[tail-tx_buffer_size_];
  563. }
  564. port->DATA = n;
  565. } while (((port->WATER >> 8) & 0x7) < 4); // need to computer properly
  566. tx_buffer_tail_ = tail;
  567. if (head == tail) {
  568. port->CTRL &= ~LPUART_CTRL_TIE;
  569. port->CTRL |= LPUART_CTRL_TCIE; // Actually wondering if we can just leave this one on...
  570. }
  571. //digitalWrite(3, LOW);
  572. }
  573. if ((ctrl & LPUART_CTRL_TCIE) && (port->STAT & LPUART_STAT_TC))
  574. {
  575. transmitting_ = 0;
  576. if (transmit_pin_baseReg_) DIRECT_WRITE_LOW(transmit_pin_baseReg_, transmit_pin_bitmask_);
  577. if(half_duplex_mode_) {
  578. __disable_irq();
  579. port->CTRL &= ~LPUART_CTRL_TXDIR;
  580. __enable_irq();
  581. //digitalWriteFast(2, LOW);
  582. }
  583. port->CTRL &= ~LPUART_CTRL_TCIE;
  584. }
  585. //digitalWrite(4, LOW);
  586. }
  587. void HardwareSerial::addToSerialEventsList() {
  588. s_serials_with_serial_events[s_count_serials_with_serial_events++] = this;
  589. yield_active_check_flags |= YIELD_CHECK_HARDWARE_SERIAL;
  590. }
  591. const pin_to_xbar_info_t PROGMEM pin_to_xbar_info[] = {
  592. {0, 17, 1, &IOMUXC_XBAR1_IN17_SELECT_INPUT, 0x1},
  593. {1, 16, 1, nullptr, 0},
  594. {2, 6, 3, &IOMUXC_XBAR1_IN06_SELECT_INPUT, 0x0},
  595. {3, 7, 3, &IOMUXC_XBAR1_IN07_SELECT_INPUT, 0x0},
  596. {4, 8, 3, &IOMUXC_XBAR1_IN08_SELECT_INPUT, 0x0},
  597. {5, 17, 3, &IOMUXC_XBAR1_IN17_SELECT_INPUT, 0x0},
  598. {7, 15, 1, nullptr, 0 },
  599. {8, 14, 1, nullptr, 0},
  600. {30, 23, 1, &IOMUXC_XBAR1_IN23_SELECT_INPUT, 0x0},
  601. {31, 22, 1, &IOMUXC_XBAR1_IN22_SELECT_INPUT, 0x0},
  602. {32, 10, 1, nullptr, 0},
  603. {33, 9, 3, &IOMUXC_XBAR1_IN09_SELECT_INPUT, 0x0},
  604. #ifdef ARDUINO_TEENSY41
  605. {36, 16, 1, nullptr, 0},
  606. {37, 17, 1, &IOMUXC_XBAR1_IN17_SELECT_INPUT, 0x3},
  607. {42, 7, 3, &IOMUXC_XBAR1_IN07_SELECT_INPUT, 0x1},
  608. {43, 6, 3, &IOMUXC_XBAR1_IN06_SELECT_INPUT, 0x1},
  609. {44, 5, 3, &IOMUXC_XBAR1_IN05_SELECT_INPUT, 0x1},
  610. {45, 4, 3, &IOMUXC_XBAR1_IN04_SELECT_INPUT, 0x1},
  611. {46, 9, 3, &IOMUXC_XBAR1_IN09_SELECT_INPUT, 0x1},
  612. {47, 8, 3, &IOMUXC_XBAR1_IN08_SELECT_INPUT, 0x1}
  613. #else
  614. {34, 7, 3, &IOMUXC_XBAR1_IN07_SELECT_INPUT, 0x1},
  615. {35, 6, 3, &IOMUXC_XBAR1_IN06_SELECT_INPUT, 0x1},
  616. {36, 5, 3, &IOMUXC_XBAR1_IN05_SELECT_INPUT, 0x1},
  617. {37, 4, 3, &IOMUXC_XBAR1_IN04_SELECT_INPUT, 0x1},
  618. {38, 9, 3, &IOMUXC_XBAR1_IN09_SELECT_INPUT, 0x1},
  619. {39, 8, 3, &IOMUXC_XBAR1_IN08_SELECT_INPUT, 0x1}
  620. #endif
  621. };
  622. const uint8_t PROGMEM count_pin_to_xbar_info = sizeof(pin_to_xbar_info)/sizeof(pin_to_xbar_info[0]);