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.

542 lines
16KB

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