Teensy 4.1 core updated for C++20
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

723 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_size_ + length;
  369. } else {
  370. rx_buffer_total_size_ = rx_buffer_size_;
  371. }
  372. // Make sure we don't end up indexing into no mans land.
  373. rx_buffer_head_ = 0;
  374. rx_buffer_tail_ = 0;
  375. rts_low_watermark_ = rx_buffer_total_size_ - hardware->rts_low_watermark;
  376. rts_high_watermark_ = rx_buffer_total_size_ - hardware->rts_high_watermark;
  377. }
  378. void HardwareSerial::addMemoryForWrite(void *buffer, size_t length)
  379. {
  380. tx_buffer_storage_ = (BUFTYPE*)buffer;
  381. if (buffer) {
  382. tx_buffer_total_size_ = tx_buffer_size_ + length;
  383. } else {
  384. tx_buffer_total_size_ = tx_buffer_size_;
  385. }
  386. // Make sure we don't end up indexing into no mans land.
  387. tx_buffer_head_ = 0;
  388. tx_buffer_tail_ = 0;
  389. }
  390. int HardwareSerial::peek(void)
  391. {
  392. uint32_t head, tail;
  393. head = rx_buffer_head_;
  394. tail = rx_buffer_tail_;
  395. if (head == tail) {
  396. __disable_irq();
  397. head = rx_buffer_head_; // reread head to make sure no ISR happened
  398. if (head == tail) {
  399. // Still empty Now check for stuff in FIFO Queue.
  400. int c = -1; // assume nothing to return
  401. if (port->WATER & 0x7000000) {
  402. c = port->DATA & 0x3ff; // Use only up to 10 bits of data
  403. // But we don't want to throw it away...
  404. // since queue is empty, just going to reset to front of queue...
  405. rx_buffer_head_ = 1;
  406. rx_buffer_tail_ = 0;
  407. rx_buffer_[1] = c;
  408. }
  409. __enable_irq();
  410. return c;
  411. }
  412. __enable_irq();
  413. }
  414. if (++tail >= rx_buffer_total_size_) tail = 0;
  415. if (tail < rx_buffer_size_) {
  416. return rx_buffer_[tail];
  417. } else {
  418. return rx_buffer_storage_[tail-rx_buffer_size_];
  419. }
  420. }
  421. int HardwareSerial::read(void)
  422. {
  423. uint32_t head, tail;
  424. int c;
  425. head = rx_buffer_head_;
  426. tail = rx_buffer_tail_;
  427. if (head == tail) {
  428. __disable_irq();
  429. head = rx_buffer_head_; // reread head to make sure no ISR happened
  430. if (head == tail) {
  431. // Still empty Now check for stuff in FIFO Queue.
  432. c = -1; // assume nothing to return
  433. if (port->WATER & 0x7000000) {
  434. c = port->DATA & 0x3ff; // Use only up to 10 bits of data
  435. }
  436. __enable_irq();
  437. return c;
  438. }
  439. __enable_irq();
  440. }
  441. if (++tail >= rx_buffer_total_size_) tail = 0;
  442. if (tail < rx_buffer_size_) {
  443. c = rx_buffer_[tail];
  444. } else {
  445. c = rx_buffer_storage_[tail-rx_buffer_size_];
  446. }
  447. rx_buffer_tail_ = tail;
  448. if (rts_pin_baseReg_) {
  449. uint32_t avail;
  450. if (head >= tail) avail = head - tail;
  451. else avail = rx_buffer_total_size_ + head - tail;
  452. if (avail <= rts_low_watermark_) rts_assert();
  453. }
  454. return c;
  455. }
  456. void HardwareSerial::flush(void)
  457. {
  458. while (transmitting_) yield(); // wait
  459. }
  460. size_t HardwareSerial::write(uint8_t c)
  461. {
  462. // use the 9 bit version (maybe 10 bit) do do the work.
  463. return write9bit(c);
  464. }
  465. size_t HardwareSerial::write9bit(uint32_t c)
  466. {
  467. uint32_t head, n;
  468. //digitalWrite(3, HIGH);
  469. //digitalWrite(5, HIGH);
  470. if (transmit_pin_baseReg_) DIRECT_WRITE_HIGH(transmit_pin_baseReg_, transmit_pin_bitmask_);
  471. if(half_duplex_mode_) {
  472. __disable_irq();
  473. port->CTRL |= LPUART_CTRL_TXDIR;
  474. __enable_irq();
  475. //digitalWriteFast(2, HIGH);
  476. }
  477. head = tx_buffer_head_;
  478. if (++head >= tx_buffer_total_size_) head = 0;
  479. while (tx_buffer_tail_ == head) {
  480. int priority = nvic_execution_priority();
  481. if (priority <= hardware->irq_priority) {
  482. if ((port->STAT & LPUART_STAT_TDRE)) {
  483. uint32_t tail = tx_buffer_tail_;
  484. if (++tail >= tx_buffer_total_size_) tail = 0;
  485. if (tail < tx_buffer_size_) {
  486. n = tx_buffer_[tail];
  487. } else {
  488. n = tx_buffer_storage_[tail-tx_buffer_size_];
  489. }
  490. port->DATA = n;
  491. tx_buffer_tail_ = tail;
  492. }
  493. } else if (priority >= 256)
  494. {
  495. yield(); // wait
  496. }
  497. }
  498. //digitalWrite(5, LOW);
  499. //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_);
  500. if (head < tx_buffer_size_) {
  501. tx_buffer_[head] = c;
  502. } else {
  503. tx_buffer_storage_[head - tx_buffer_size_] = c;
  504. }
  505. __disable_irq();
  506. transmitting_ = 1;
  507. tx_buffer_head_ = head;
  508. port->CTRL |= LPUART_CTRL_TIE; // (may need to handle this issue)BITBAND_SET_BIT(LPUART0_CTRL, TIE_BIT);
  509. __enable_irq();
  510. //digitalWrite(3, LOW);
  511. return 1;
  512. }
  513. void HardwareSerial::IRQHandler()
  514. {
  515. //digitalWrite(4, HIGH);
  516. uint32_t head, tail, n;
  517. uint32_t ctrl;
  518. // See if we have stuff to read in.
  519. // Todo - Check idle.
  520. if (port->STAT & (LPUART_STAT_RDRF | LPUART_STAT_IDLE)) {
  521. // See how many bytes or pending.
  522. //digitalWrite(5, HIGH);
  523. uint8_t avail = (port->WATER >> 24) & 0x7;
  524. if (avail) {
  525. uint32_t newhead;
  526. head = rx_buffer_head_;
  527. tail = rx_buffer_tail_;
  528. do {
  529. n = port->DATA & 0x3ff; // Use only up to 10 bits of data
  530. newhead = head + 1;
  531. if (newhead >= rx_buffer_total_size_) newhead = 0;
  532. if (newhead != rx_buffer_tail_) {
  533. head = newhead;
  534. if (newhead < rx_buffer_size_) {
  535. rx_buffer_[head] = n;
  536. } else {
  537. rx_buffer_storage_[head-rx_buffer_size_] = n;
  538. }
  539. }
  540. } while (--avail > 0) ;
  541. rx_buffer_head_ = head;
  542. if (rts_pin_baseReg_) {
  543. uint32_t avail;
  544. if (head >= tail) avail = head - tail;
  545. else avail = rx_buffer_total_size_ + head - tail;
  546. if (avail >= rts_high_watermark_) rts_deassert();
  547. }
  548. }
  549. // If it was an idle status clear the idle
  550. if (port->STAT & LPUART_STAT_IDLE) {
  551. port->STAT |= LPUART_STAT_IDLE; // writing a 1 to idle should clear it.
  552. }
  553. //digitalWrite(5, LOW);
  554. }
  555. // See if we are transmitting and room in buffer.
  556. ctrl = port->CTRL;
  557. if ((ctrl & LPUART_CTRL_TIE) && (port->STAT & LPUART_STAT_TDRE))
  558. {
  559. //digitalWrite(3, HIGH);
  560. head = tx_buffer_head_;
  561. tail = tx_buffer_tail_;
  562. do {
  563. if (head == tail) break;
  564. if (++tail >= tx_buffer_total_size_) tail = 0;
  565. if (tail < tx_buffer_size_) {
  566. n = tx_buffer_[tail];
  567. } else {
  568. n = tx_buffer_storage_[tail-tx_buffer_size_];
  569. }
  570. port->DATA = n;
  571. } while (((port->WATER >> 8) & 0x7) < 4); // need to computer properly
  572. tx_buffer_tail_ = tail;
  573. if (head == tail) {
  574. port->CTRL &= ~LPUART_CTRL_TIE;
  575. port->CTRL |= LPUART_CTRL_TCIE; // Actually wondering if we can just leave this one on...
  576. }
  577. //digitalWrite(3, LOW);
  578. }
  579. if ((ctrl & LPUART_CTRL_TCIE) && (port->STAT & LPUART_STAT_TC))
  580. {
  581. transmitting_ = 0;
  582. if (transmit_pin_baseReg_) DIRECT_WRITE_LOW(transmit_pin_baseReg_, transmit_pin_bitmask_);
  583. if(half_duplex_mode_) {
  584. __disable_irq();
  585. port->CTRL &= ~LPUART_CTRL_TXDIR;
  586. __enable_irq();
  587. //digitalWriteFast(2, LOW);
  588. }
  589. port->CTRL &= ~LPUART_CTRL_TCIE;
  590. }
  591. //digitalWrite(4, LOW);
  592. }
  593. void HardwareSerial::addToSerialEventsList() {
  594. for (uint8_t i = 0; i < s_count_serials_with_serial_events; i++) {
  595. if (s_serials_with_serial_events[i] == this) return; // already in the list.
  596. }
  597. s_serials_with_serial_events[s_count_serials_with_serial_events++] = this;
  598. yield_active_check_flags |= YIELD_CHECK_HARDWARE_SERIAL;
  599. }
  600. const pin_to_xbar_info_t PROGMEM pin_to_xbar_info[] = {
  601. {0, 17, 1, &IOMUXC_XBAR1_IN17_SELECT_INPUT, 0x1},
  602. {1, 16, 1, nullptr, 0},
  603. {2, 6, 3, &IOMUXC_XBAR1_IN06_SELECT_INPUT, 0x0},
  604. {3, 7, 3, &IOMUXC_XBAR1_IN07_SELECT_INPUT, 0x0},
  605. {4, 8, 3, &IOMUXC_XBAR1_IN08_SELECT_INPUT, 0x0},
  606. {5, 17, 3, &IOMUXC_XBAR1_IN17_SELECT_INPUT, 0x0},
  607. {7, 15, 1, nullptr, 0 },
  608. {8, 14, 1, nullptr, 0},
  609. {30, 23, 1, &IOMUXC_XBAR1_IN23_SELECT_INPUT, 0x0},
  610. {31, 22, 1, &IOMUXC_XBAR1_IN22_SELECT_INPUT, 0x0},
  611. {32, 10, 1, nullptr, 0},
  612. {33, 9, 3, &IOMUXC_XBAR1_IN09_SELECT_INPUT, 0x0},
  613. #ifdef ARDUINO_TEENSY41
  614. {36, 16, 1, nullptr, 0},
  615. {37, 17, 1, &IOMUXC_XBAR1_IN17_SELECT_INPUT, 0x3},
  616. {42, 7, 3, &IOMUXC_XBAR1_IN07_SELECT_INPUT, 0x1},
  617. {43, 6, 3, &IOMUXC_XBAR1_IN06_SELECT_INPUT, 0x1},
  618. {44, 5, 3, &IOMUXC_XBAR1_IN05_SELECT_INPUT, 0x1},
  619. {45, 4, 3, &IOMUXC_XBAR1_IN04_SELECT_INPUT, 0x1},
  620. {46, 9, 3, &IOMUXC_XBAR1_IN09_SELECT_INPUT, 0x1},
  621. {47, 8, 3, &IOMUXC_XBAR1_IN08_SELECT_INPUT, 0x1}
  622. #else
  623. {34, 7, 3, &IOMUXC_XBAR1_IN07_SELECT_INPUT, 0x1},
  624. {35, 6, 3, &IOMUXC_XBAR1_IN06_SELECT_INPUT, 0x1},
  625. {36, 5, 3, &IOMUXC_XBAR1_IN05_SELECT_INPUT, 0x1},
  626. {37, 4, 3, &IOMUXC_XBAR1_IN04_SELECT_INPUT, 0x1},
  627. {38, 9, 3, &IOMUXC_XBAR1_IN09_SELECT_INPUT, 0x1},
  628. {39, 8, 3, &IOMUXC_XBAR1_IN08_SELECT_INPUT, 0x1}
  629. #endif
  630. };
  631. const uint8_t PROGMEM count_pin_to_xbar_info = sizeof(pin_to_xbar_info)/sizeof(pin_to_xbar_info[0]);