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.

HardwareSerial.cpp 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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. #define CTRL_ENABLE (LPUART_CTRL_TE | LPUART_CTRL_RE | LPUART_CTRL_RIE | LPUART_CTRL_ILIE)
  61. #define CTRL_TX_ACTIVE (CTRL_ENABLE | LPUART_CTRL_TIE)
  62. #define CTRL_TX_COMPLETING (CTRL_ENABLE | LPUART_CTRL_TCIE)
  63. #define CTRL_TX_INACTIVE CTRL_ENABLE
  64. // Copied from T3.x - probably should move to other location.
  65. int nvic_execution_priority(void)
  66. {
  67. uint32_t priority=256;
  68. uint32_t primask, faultmask, basepri, ipsr;
  69. // full algorithm in ARM DDI0403D, page B1-639
  70. // this isn't quite complete, but hopefully good enough
  71. __asm__ volatile("mrs %0, faultmask\n" : "=r" (faultmask)::);
  72. if (faultmask) return -1;
  73. __asm__ volatile("mrs %0, primask\n" : "=r" (primask)::);
  74. if (primask) return 0;
  75. __asm__ volatile("mrs %0, ipsr\n" : "=r" (ipsr)::);
  76. if (ipsr) {
  77. if (ipsr < 16) priority = 0; // could be non-zero
  78. else priority = NVIC_GET_PRIORITY(ipsr - 16);
  79. }
  80. __asm__ volatile("mrs %0, basepri\n" : "=r" (basepri)::);
  81. if (basepri > 0 && basepri < priority) priority = basepri;
  82. return priority;
  83. }
  84. void HardwareSerial::begin(uint32_t baud, uint16_t format)
  85. {
  86. //printf("HardwareSerial begin\n");
  87. float base = (float)UART_CLOCK / (float)baud;
  88. float besterr = 1e20;
  89. int bestdiv = 1;
  90. int bestosr = 4;
  91. for (int osr=4; osr <= 32; osr++) {
  92. float div = base / (float)osr;
  93. int divint = (int)(div + 0.5f);
  94. if (divint < 1) divint = 1;
  95. else if (divint > 8191) divint = 8191;
  96. float err = ((float)divint - div) / div;
  97. if (err < 0.0f) err = -err;
  98. if (err <= besterr) {
  99. besterr = err;
  100. bestdiv = divint;
  101. bestosr = osr;
  102. }
  103. }
  104. //printf(" baud %d: osr=%d, div=%d\n", baud, bestosr, bestdiv);
  105. rx_buffer_head_ = 0;
  106. rx_buffer_tail_ = 0;
  107. tx_buffer_head_ = 0;
  108. tx_buffer_tail_ = 0;
  109. rts_low_watermark_ = rx_buffer_total_size_ - hardware->rts_low_watermark;
  110. rts_high_watermark_ = rx_buffer_total_size_ - hardware->rts_high_watermark;
  111. transmitting_ = 0;
  112. hardware->ccm_register |= hardware->ccm_value;
  113. uint32_t fastio = IOMUXC_PAD_SRE | IOMUXC_PAD_DSE(3) | IOMUXC_PAD_SPEED(3);
  114. *(portControlRegister(hardware->rx_pin)) = fastio;
  115. *(portControlRegister(hardware->tx_pin)) = fastio;
  116. *(portConfigRegister(hardware->rx_pin)) = hardware->rx_mux_val;
  117. *(portConfigRegister(hardware->tx_pin)) = hardware->tx_mux_val;
  118. //hardware->rx_mux_register = hardware->rx_mux_val;
  119. //hardware->tx_mux_register = hardware->tx_mux_val;
  120. hardware->rx_select_input_register = hardware->rx_select_val;
  121. port->BAUD = LPUART_BAUD_OSR(bestosr - 1) | LPUART_BAUD_SBR(bestdiv);
  122. port->PINCFG = 0;
  123. // Enable the transmitter, receiver and enable receiver interrupt
  124. attachInterruptVector(hardware->irq, hardware->irq_handler);
  125. NVIC_SET_PRIORITY(hardware->irq, hardware->irq_priority); // maybe should put into hardware...
  126. NVIC_ENABLE_IRQ(hardware->irq);
  127. uint16_t tx_fifo_size = (((port->FIFO >> 4) & 0x7) << 2);
  128. uint8_t tx_water = (tx_fifo_size < 16) ? tx_fifo_size >> 1 : 7;
  129. uint16_t rx_fifo_size = (((port->FIFO >> 0) & 0x7) << 2);
  130. uint8_t rx_water = (rx_fifo_size < 16) ? rx_fifo_size >> 1 : 7;
  131. /*
  132. Serial.printf("SerialX::begin stat:%x ctrl:%x fifo:%x water:%x\n", port->STAT, port->CTRL, port->FIFO, port->WATER );
  133. Serial.printf(" FIFO sizes: tx:%d rx:%d\n",tx_fifo_size, rx_fifo_size);
  134. Serial.printf(" Watermark tx:%d, rx: %d\n", tx_water, rx_water);
  135. */
  136. port->WATER = LPUART_WATER_RXWATER(rx_water) | LPUART_WATER_TXWATER(tx_water);
  137. port->FIFO |= LPUART_FIFO_TXFE | LPUART_FIFO_RXFE;
  138. // lets configure up our CTRL register value
  139. uint32_t ctrl = CTRL_TX_INACTIVE;
  140. // Now process the bits in the Format value passed in
  141. // Bits 0-2 - Parity plus 9 bit.
  142. ctrl |= (format & (LPUART_CTRL_PT | LPUART_CTRL_PE) ); // configure parity - turn off PT, PE, M and configure PT, PE
  143. if (format & 0x04) ctrl |= LPUART_CTRL_M; // 9 bits (might include parity)
  144. if ((format & 0x0F) == 0x04) ctrl |= LPUART_CTRL_R9T8; // 8N2 is 9 bit with 9th bit always 1
  145. // Bit 5 TXINVERT
  146. if (format & 0x20) ctrl |= LPUART_CTRL_TXINV; // tx invert
  147. // write out computed CTRL
  148. port->CTRL = ctrl;
  149. // Bit 3 10 bit - Will assume that begin already cleared it.
  150. // process some other bits which change other registers.
  151. if (format & 0x08) port->BAUD |= LPUART_BAUD_M10;
  152. // Bit 4 RXINVERT
  153. uint32_t c = port->STAT & ~LPUART_STAT_RXINV;
  154. if (format & 0x10) c |= LPUART_STAT_RXINV; // rx invert
  155. port->STAT = c;
  156. // bit 8 can turn on 2 stop bit mote
  157. if ( format & 0x100) port->BAUD |= LPUART_BAUD_SBNS;
  158. //Serial.printf(" stat:%x ctrl:%x fifo:%x water:%x\n", port->STAT, port->CTRL, port->FIFO, port->WATER );
  159. };
  160. inline void HardwareSerial::rts_assert()
  161. {
  162. DIRECT_WRITE_LOW(rts_pin_baseReg_, rts_pin_bitmask_);
  163. }
  164. inline void HardwareSerial::rts_deassert()
  165. {
  166. DIRECT_WRITE_HIGH(rts_pin_baseReg_, rts_pin_bitmask_);
  167. }
  168. void HardwareSerial::end(void)
  169. {
  170. if (!(hardware->ccm_register & hardware->ccm_value)) return;
  171. while (transmitting_) yield(); // wait for buffered data to send
  172. port->CTRL = 0; // disable the TX and RX ...
  173. // Not sure if this is best, but I think most IO pins default to Mode 5? which appears to be digital IO?
  174. *(portConfigRegister(hardware->rx_pin)) = 5;
  175. *(portConfigRegister(hardware->tx_pin)) = 5;
  176. // Might need to clear out other areas as well?
  177. rx_buffer_head_ = 0;
  178. rx_buffer_tail_ = 0;
  179. if (rts_pin_baseReg_) rts_deassert();
  180. //
  181. }
  182. void HardwareSerial::transmitterEnable(uint8_t pin)
  183. {
  184. while (transmitting_) ;
  185. pinMode(pin, OUTPUT);
  186. transmit_pin_baseReg_ = PIN_TO_BASEREG(pin);
  187. transmit_pin_bitmask_ = PIN_TO_BITMASK(pin);
  188. DIRECT_WRITE_LOW(transmit_pin_baseReg_, transmit_pin_bitmask_);
  189. }
  190. void HardwareSerial::setRX(uint8_t pin)
  191. {
  192. // Currently none of these have multiple
  193. // possible RX pins
  194. }
  195. void HardwareSerial::setTX(uint8_t pin, bool opendrain)
  196. {
  197. // Currently none of these have multiple
  198. // possible TX pins
  199. }
  200. bool HardwareSerial::attachRts(uint8_t pin)
  201. {
  202. if (!(hardware->ccm_register & hardware->ccm_value)) return 0;
  203. if (pin < CORE_NUM_DIGITAL) {
  204. rts_pin_baseReg_ = PIN_TO_BASEREG(pin);
  205. rts_pin_bitmask_ = PIN_TO_BITMASK(pin);
  206. pinMode(pin, OUTPUT);
  207. rts_assert();
  208. } else {
  209. rts_pin_baseReg_ = NULL;
  210. return 0;
  211. }
  212. return 1;
  213. }
  214. bool HardwareSerial::attachCts(uint8_t pin)
  215. {
  216. if (!(hardware->ccm_register & hardware->ccm_value)) return false;
  217. if ((pin != 0xff) && (pin == hardware->cts_pin)) {
  218. // Setup the IO pin as weak PULL down.
  219. *(portControlRegister(pin)) = IOMUXC_PAD_DSE(7) | IOMUXC_PAD_PKE | IOMUXC_PAD_PUE | IOMUXC_PAD_PUS(0) | IOMUXC_PAD_HYS;
  220. *(portConfigRegister(hardware->cts_pin)) = hardware->cts_mux_val;
  221. port->MODIR |= LPUART_MODIR_TXCTSE;
  222. return true;
  223. } else {
  224. port->MODIR &= ~LPUART_MODIR_TXCTSE;
  225. return false;
  226. }
  227. }
  228. void HardwareSerial::clear(void)
  229. {
  230. // BUGBUG:: deal with FIFO
  231. rx_buffer_head_ = rx_buffer_tail_;
  232. if (rts_pin_baseReg_) rts_assert();
  233. }
  234. int HardwareSerial::availableForWrite(void)
  235. {
  236. uint32_t head, tail;
  237. head = tx_buffer_head_;
  238. tail = tx_buffer_tail_;
  239. if (head >= tail) return tx_buffer_total_size_ - 1 - head + tail;
  240. return tail - head - 1;
  241. }
  242. int HardwareSerial::available(void)
  243. {
  244. uint32_t head, tail;
  245. head = rx_buffer_head_;
  246. tail = rx_buffer_tail_;
  247. if (head >= tail) return head - tail;
  248. return rx_buffer_total_size_ + head - tail;
  249. }
  250. void HardwareSerial::addStorageForRead(void *buffer, size_t length)
  251. {
  252. rx_buffer_storage_ = (BUFTYPE*)buffer;
  253. if (buffer) {
  254. rx_buffer_total_size_ = rx_buffer_total_size_ + length;
  255. } else {
  256. rx_buffer_total_size_ = rx_buffer_total_size_;
  257. }
  258. rts_low_watermark_ = rx_buffer_total_size_ - hardware->rts_low_watermark;
  259. rts_high_watermark_ = rx_buffer_total_size_ - hardware->rts_high_watermark;
  260. }
  261. void HardwareSerial::addStorageForWrite(void *buffer, size_t length)
  262. {
  263. tx_buffer_storage_ = (BUFTYPE*)buffer;
  264. if (buffer) {
  265. tx_buffer_total_size_ = tx_buffer_total_size_ + length;
  266. } else {
  267. tx_buffer_total_size_ = tx_buffer_total_size_;
  268. }
  269. }
  270. int HardwareSerial::peek(void)
  271. {
  272. uint32_t head, tail;
  273. head = rx_buffer_head_;
  274. tail = rx_buffer_tail_;
  275. if (head == tail) return -1;
  276. if (++tail >= rx_buffer_total_size_) tail = 0;
  277. if (tail < rx_buffer_size_) {
  278. return rx_buffer_[tail];
  279. } else {
  280. return rx_buffer_storage_[tail-rx_buffer_size_];
  281. }
  282. }
  283. int HardwareSerial::read(void)
  284. {
  285. uint32_t head, tail;
  286. int c;
  287. head = rx_buffer_head_;
  288. tail = rx_buffer_tail_;
  289. if (head == tail) return -1;
  290. if (++tail >= rx_buffer_total_size_) tail = 0;
  291. if (tail < rx_buffer_size_) {
  292. c = rx_buffer_[tail];
  293. } else {
  294. c = rx_buffer_storage_[tail-rx_buffer_size_];
  295. }
  296. rx_buffer_tail_ = tail;
  297. if (rts_pin_baseReg_) {
  298. uint32_t avail;
  299. if (head >= tail) avail = head - tail;
  300. else avail = rx_buffer_total_size_ + head - tail;
  301. if (avail <= rts_low_watermark_) rts_assert();
  302. }
  303. return c;
  304. }
  305. void HardwareSerial::flush(void)
  306. {
  307. while (transmitting_) yield(); // wait
  308. }
  309. size_t HardwareSerial::write(uint8_t c)
  310. {
  311. // use the 9 bit version (maybe 10 bit) do do the work.
  312. return write9bit(c);
  313. }
  314. size_t HardwareSerial::write9bit(uint32_t c)
  315. {
  316. uint32_t head, n;
  317. //digitalWrite(3, HIGH);
  318. //digitalWrite(5, HIGH);
  319. if (transmit_pin_baseReg_) DIRECT_WRITE_HIGH(transmit_pin_baseReg_, transmit_pin_bitmask_);
  320. head = tx_buffer_head_;
  321. if (++head >= tx_buffer_total_size_) head = 0;
  322. while (tx_buffer_tail_ == head) {
  323. int priority = nvic_execution_priority();
  324. if (priority <= hardware->irq_priority) {
  325. if ((port->STAT & LPUART_STAT_TDRE)) {
  326. uint32_t tail = tx_buffer_tail_;
  327. if (++tail >= tx_buffer_total_size_) tail = 0;
  328. if (tail < tx_buffer_size_) {
  329. n = tx_buffer_[tail];
  330. } else {
  331. n = tx_buffer_storage_[tail-tx_buffer_size_];
  332. }
  333. port->DATA = n;
  334. tx_buffer_tail_ = tail;
  335. }
  336. } else if (priority >= 256)
  337. {
  338. yield(); // wait
  339. }
  340. }
  341. //digitalWrite(5, LOW);
  342. //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_);
  343. if (head < tx_buffer_size_) {
  344. tx_buffer_[head] = c;
  345. } else {
  346. tx_buffer_storage_[head - tx_buffer_size_] = c;
  347. }
  348. __disable_irq();
  349. transmitting_ = 1;
  350. tx_buffer_head_ = head;
  351. port->CTRL |= LPUART_CTRL_TIE; // (may need to handle this issue)BITBAND_SET_BIT(LPUART0_CTRL, TIE_BIT);
  352. __enable_irq();
  353. //digitalWrite(3, LOW);
  354. return 1;
  355. }
  356. void HardwareSerial::IRQHandler()
  357. {
  358. //digitalWrite(4, HIGH);
  359. uint32_t head, tail, n;
  360. uint32_t ctrl;
  361. // See if we have stuff to read in.
  362. // Todo - Check idle.
  363. if (port->STAT & (LPUART_STAT_RDRF | LPUART_STAT_IDLE)) {
  364. // See how many bytes or pending.
  365. //digitalWrite(5, HIGH);
  366. uint8_t avail = (port->WATER >> 24) & 0x7;
  367. if (avail) {
  368. uint32_t newhead;
  369. head = rx_buffer_head_;
  370. tail = rx_buffer_tail_;
  371. do {
  372. n = port->DATA & 0x3ff; // Use only up to 10 bits of data
  373. newhead = head + 1;
  374. if (newhead >= rx_buffer_total_size_) newhead = 0;
  375. if (newhead != rx_buffer_tail_) {
  376. head = newhead;
  377. if (newhead < rx_buffer_size_) {
  378. rx_buffer_[head] = n;
  379. } else {
  380. rx_buffer_storage_[head-rx_buffer_size_] = n;
  381. }
  382. }
  383. } while (--avail > 0) ;
  384. rx_buffer_head_ = head;
  385. if (rts_pin_baseReg_) {
  386. uint32_t avail;
  387. if (head >= tail) avail = head - tail;
  388. else avail = rx_buffer_total_size_ + head - tail;
  389. if (avail >= rts_high_watermark_) rts_deassert();
  390. }
  391. }
  392. // If it was an idle status clear the idle
  393. if (port->STAT & LPUART_STAT_IDLE) {
  394. port->STAT |= LPUART_STAT_IDLE; // writing a 1 to idle should clear it.
  395. }
  396. //digitalWrite(5, LOW);
  397. }
  398. // See if we are transmitting and room in buffer.
  399. ctrl = port->CTRL;
  400. if ((ctrl & LPUART_CTRL_TIE) && (port->STAT & LPUART_STAT_TDRE))
  401. {
  402. //digitalWrite(3, HIGH);
  403. head = tx_buffer_head_;
  404. tail = tx_buffer_tail_;
  405. do {
  406. if (head == tail) break;
  407. if (++tail >= tx_buffer_total_size_) tail = 0;
  408. if (tail < tx_buffer_size_) {
  409. n = tx_buffer_[tail];
  410. } else {
  411. n = tx_buffer_storage_[tail-tx_buffer_size_];
  412. }
  413. port->DATA = n;
  414. } while (((port->WATER >> 8) & 0x7) < 4); // need to computer properly
  415. tx_buffer_tail_ = tail;
  416. if (head == tail) {
  417. port->CTRL &= ~LPUART_CTRL_TIE;
  418. port->CTRL |= LPUART_CTRL_TCIE; // Actually wondering if we can just leave this one on...
  419. }
  420. //digitalWrite(3, LOW);
  421. }
  422. if ((ctrl & LPUART_CTRL_TCIE) && (port->STAT & LPUART_STAT_TC))
  423. {
  424. transmitting_ = 0;
  425. if (transmit_pin_baseReg_) DIRECT_WRITE_LOW(transmit_pin_baseReg_, transmit_pin_bitmask_);
  426. port->CTRL &= ~LPUART_CTRL_TCIE;
  427. }
  428. //digitalWrite(4, LOW);
  429. }