No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

HardwareSerial.cpp 16KB

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