Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

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