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.

преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 9 години
преди 9 години
преди 9 години
преди 11 години
преди 11 години
преди 11 години
преди 9 години
преди 9 години
преди 11 години
преди 9 години
преди 9 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 9 години
преди 11 години
преди 9 години
преди 11 години
преди 9 години
преди 11 години
преди 9 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 9 години
преди 11 години
преди 9 години
преди 11 години
преди 9 години
преди 9 години
преди 9 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 9 години
преди 11 години
преди 11 години
преди 9 години
преди 11 години
преди 9 години
преди 11 години
преди 9 години
преди 9 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 9 години
преди 9 години
преди 9 години
преди 11 години
преди 11 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2013 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 "kinetis.h"
  31. #include "core_pins.h"
  32. #include "HardwareSerial.h"
  33. ////////////////////////////////////////////////////////////////
  34. // Tunable parameters (relatively safe to edit these numbers)
  35. ////////////////////////////////////////////////////////////////
  36. #define TX_BUFFER_SIZE 64 // number of outgoing bytes to buffer
  37. #define RX_BUFFER_SIZE 64 // number of incoming bytes to buffer
  38. #define RTS_HIGH_WATERMARK 40 // RTS requests sender to pause
  39. #define RTS_LOW_WATERMARK 26 // RTS allows sender to resume
  40. #define IRQ_PRIORITY 64 // 0 = highest priority, 255 = lowest
  41. ////////////////////////////////////////////////////////////////
  42. // changes not recommended below this point....
  43. ////////////////////////////////////////////////////////////////
  44. #ifdef SERIAL_9BIT_SUPPORT
  45. static uint8_t use9Bits = 0;
  46. #define BUFTYPE uint16_t
  47. #else
  48. #define BUFTYPE uint8_t
  49. #define use9Bits 0
  50. #endif
  51. static volatile BUFTYPE tx_buffer[TX_BUFFER_SIZE];
  52. static volatile BUFTYPE rx_buffer[RX_BUFFER_SIZE];
  53. static volatile uint8_t transmitting = 0;
  54. #if defined(KINETISK)
  55. static volatile uint8_t *transmit_pin=NULL;
  56. #define transmit_assert() *transmit_pin = 1
  57. #define transmit_deassert() *transmit_pin = 0
  58. static volatile uint8_t *rts_pin=NULL;
  59. #define rts_assert() *rts_pin = 0
  60. #define rts_deassert() *rts_pin = 1
  61. #elif defined(KINETISL)
  62. static volatile uint8_t *transmit_pin=NULL;
  63. static uint8_t transmit_mask=0;
  64. #define transmit_assert() *(transmit_pin+4) = transmit_mask;
  65. #define transmit_deassert() *(transmit_pin+8) = transmit_mask;
  66. static volatile uint8_t *rts_pin=NULL;
  67. static uint8_t rts_mask=0;
  68. #define rts_assert() *(rts_pin+8) = rts_mask;
  69. #define rts_deassert() *(rts_pin+4) = rts_mask;
  70. #endif
  71. #if TX_BUFFER_SIZE > 255
  72. static volatile uint16_t tx_buffer_head = 0;
  73. static volatile uint16_t tx_buffer_tail = 0;
  74. #else
  75. static volatile uint8_t tx_buffer_head = 0;
  76. static volatile uint8_t tx_buffer_tail = 0;
  77. #endif
  78. #if RX_BUFFER_SIZE > 255
  79. static volatile uint16_t rx_buffer_head = 0;
  80. static volatile uint16_t rx_buffer_tail = 0;
  81. #else
  82. static volatile uint8_t rx_buffer_head = 0;
  83. static volatile uint8_t rx_buffer_tail = 0;
  84. #endif
  85. static uint8_t rx_pin_num = 0;
  86. static uint8_t tx_pin_num = 1;
  87. // UART0 and UART1 are clocked by F_CPU, UART2 is clocked by F_BUS
  88. // UART0 has 8 byte fifo, UART1 and UART2 have 1 byte buffer
  89. #ifdef HAS_KINETISK_UART0_FIFO
  90. #define C2_ENABLE UART_C2_TE | UART_C2_RE | UART_C2_RIE | UART_C2_ILIE
  91. #else
  92. #define C2_ENABLE UART_C2_TE | UART_C2_RE | UART_C2_RIE
  93. #endif
  94. #define C2_TX_ACTIVE C2_ENABLE | UART_C2_TIE
  95. #define C2_TX_COMPLETING C2_ENABLE | UART_C2_TCIE
  96. #define C2_TX_INACTIVE C2_ENABLE
  97. void serial_begin(uint32_t divisor)
  98. {
  99. SIM_SCGC4 |= SIM_SCGC4_UART0; // turn on clock, TODO: use bitband
  100. rx_buffer_head = 0;
  101. rx_buffer_tail = 0;
  102. tx_buffer_head = 0;
  103. tx_buffer_tail = 0;
  104. transmitting = 0;
  105. switch (rx_pin_num) {
  106. case 0: CORE_PIN0_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  107. case 21: CORE_PIN21_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  108. #if defined(KINETISL)
  109. case 3: CORE_PIN3_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(2); break;
  110. #endif
  111. }
  112. switch (tx_pin_num) {
  113. case 1: CORE_PIN1_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); break;
  114. case 5: CORE_PIN5_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); break;
  115. #if defined(KINETISL)
  116. case 4: CORE_PIN4_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(2); break;
  117. #endif
  118. }
  119. #if defined(HAS_KINETISK_UART0)
  120. UART0_BDH = (divisor >> 13) & 0x1F;
  121. UART0_BDL = (divisor >> 5) & 0xFF;
  122. UART0_C4 = divisor & 0x1F;
  123. #ifdef HAS_KINETISK_UART0_FIFO
  124. UART0_C1 = UART_C1_ILT;
  125. UART0_TWFIFO = 2; // tx watermark, causes S1_TDRE to set
  126. UART0_RWFIFO = 4; // rx watermark, causes S1_RDRF to set
  127. UART0_PFIFO = UART_PFIFO_TXFE | UART_PFIFO_RXFE;
  128. #else
  129. UART0_C1 = 0;
  130. UART0_PFIFO = 0;
  131. #endif
  132. #elif defined(HAS_KINETISL_UART0)
  133. UART0_BDH = (divisor >> 8) & 0x1F;
  134. UART0_BDL = divisor & 0xFF;
  135. UART0_C1 = 0;
  136. #endif
  137. UART0_C2 = C2_TX_INACTIVE;
  138. NVIC_SET_PRIORITY(IRQ_UART0_STATUS, IRQ_PRIORITY);
  139. NVIC_ENABLE_IRQ(IRQ_UART0_STATUS);
  140. }
  141. void serial_format(uint32_t format)
  142. {
  143. uint8_t c;
  144. c = UART0_C1;
  145. c = (c & ~0x13) | (format & 0x03); // configure parity
  146. if (format & 0x04) c |= 0x10; // 9 bits (might include parity)
  147. UART0_C1 = c;
  148. if ((format & 0x0F) == 0x04) UART0_C3 |= 0x40; // 8N2 is 9 bit with 9th bit always 1
  149. c = UART0_S2 & ~0x10;
  150. if (format & 0x10) c |= 0x10; // rx invert
  151. UART0_S2 = c;
  152. c = UART0_C3 & ~0x10;
  153. if (format & 0x20) c |= 0x10; // tx invert
  154. UART0_C3 = c;
  155. #ifdef SERIAL_9BIT_SUPPORT
  156. c = UART0_C4 & 0x1F;
  157. if (format & 0x08) c |= 0x20; // 9 bit mode with parity (requires 10 bits)
  158. UART0_C4 = c;
  159. use9Bits = format & 0x80;
  160. #endif
  161. }
  162. void serial_end(void)
  163. {
  164. if (!(SIM_SCGC4 & SIM_SCGC4_UART0)) return;
  165. while (transmitting) yield(); // wait for buffered data to send
  166. NVIC_DISABLE_IRQ(IRQ_UART0_STATUS);
  167. UART0_C2 = 0;
  168. CORE_PIN0_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
  169. CORE_PIN1_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
  170. rx_buffer_head = 0;
  171. rx_buffer_tail = 0;
  172. if (rts_pin) rts_deassert();
  173. }
  174. void serial_set_transmit_pin(uint8_t pin)
  175. {
  176. while (transmitting) ;
  177. pinMode(pin, OUTPUT);
  178. digitalWrite(pin, LOW);
  179. transmit_pin = portOutputRegister(pin);
  180. #if defined(KINETISL)
  181. transmit_mask = digitalPinToBitMask(pin);
  182. #endif
  183. }
  184. void serial_set_tx(uint8_t pin)
  185. {
  186. if (pin == tx_pin_num) return;
  187. if ((SIM_SCGC4 & SIM_SCGC4_UART0)) {
  188. switch (tx_pin_num) {
  189. case 1: CORE_PIN1_CONFIG = 0; break; // PTB17
  190. case 5: CORE_PIN5_CONFIG = 0; break; // PTD7
  191. #if defined(KINETISL)
  192. case 4: CORE_PIN4_CONFIG = 0; break; // PTA2
  193. #endif
  194. }
  195. switch (pin) {
  196. case 1: CORE_PIN1_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); break;
  197. case 5: CORE_PIN5_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); break;
  198. #if defined(KINETISL)
  199. case 4: CORE_PIN4_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(2); break;
  200. #endif
  201. }
  202. }
  203. tx_pin_num = pin;
  204. }
  205. void serial_set_rx(uint8_t pin)
  206. {
  207. if (pin == rx_pin_num) return;
  208. if ((SIM_SCGC4 & SIM_SCGC4_UART0)) {
  209. switch (rx_pin_num) {
  210. case 0: CORE_PIN0_CONFIG = 0; break; // PTB16
  211. case 21: CORE_PIN21_CONFIG = 0; break; // PTD6
  212. #if defined(KINETISL)
  213. case 3: CORE_PIN3_CONFIG = 0; break; // PTA1
  214. #endif
  215. }
  216. switch (pin) {
  217. case 0: CORE_PIN0_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  218. case 21: CORE_PIN21_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  219. #if defined(KINETISL)
  220. case 3: CORE_PIN3_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(2); break;
  221. #endif
  222. }
  223. }
  224. rx_pin_num = pin;
  225. }
  226. int serial_set_rts(uint8_t pin)
  227. {
  228. if (!(SIM_SCGC4 & SIM_SCGC4_UART0)) return 0;
  229. if (pin < CORE_NUM_DIGITAL) {
  230. rts_pin = portOutputRegister(pin);
  231. #if defined(KINETISL)
  232. rts_mask = digitalPinToBitMask(pin);
  233. #endif
  234. pinMode(pin, OUTPUT);
  235. rts_assert();
  236. } else {
  237. rts_pin = NULL;
  238. return 0;
  239. }
  240. /*
  241. if (pin == 6) {
  242. CORE_PIN6_CONFIG = PORT_PCR_MUX(3);
  243. } else if (pin == 19) {
  244. CORE_PIN19_CONFIG = PORT_PCR_MUX(3);
  245. } else {
  246. UART0_MODEM &= ~UART_MODEM_RXRTSE;
  247. return 0;
  248. }
  249. UART0_MODEM |= UART_MODEM_RXRTSE;
  250. */
  251. return 1;
  252. }
  253. int serial_set_cts(uint8_t pin)
  254. {
  255. #if defined(KINETISK)
  256. if (!(SIM_SCGC4 & SIM_SCGC4_UART0)) return 0;
  257. if (pin == 18) {
  258. CORE_PIN18_CONFIG = PORT_PCR_MUX(3) | PORT_PCR_PE; // weak pulldown
  259. } else if (pin == 20) {
  260. CORE_PIN20_CONFIG = PORT_PCR_MUX(3) | PORT_PCR_PE; // weak pulldown
  261. } else {
  262. UART0_MODEM &= ~UART_MODEM_TXCTSE;
  263. return 0;
  264. }
  265. UART0_MODEM |= UART_MODEM_TXCTSE;
  266. return 1;
  267. #else
  268. return 0;
  269. #endif
  270. }
  271. void serial_putchar(uint32_t c)
  272. {
  273. uint32_t head, n;
  274. if (!(SIM_SCGC4 & SIM_SCGC4_UART0)) return;
  275. if (transmit_pin) transmit_assert();
  276. head = tx_buffer_head;
  277. if (++head >= TX_BUFFER_SIZE) head = 0;
  278. while (tx_buffer_tail == head) {
  279. int priority = nvic_execution_priority();
  280. if (priority <= IRQ_PRIORITY) {
  281. if ((UART0_S1 & UART_S1_TDRE)) {
  282. uint32_t tail = tx_buffer_tail;
  283. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  284. n = tx_buffer[tail];
  285. if (use9Bits) UART0_C3 = (UART0_C3 & ~0x40) | ((n & 0x100) >> 2);
  286. UART0_D = n;
  287. tx_buffer_tail = tail;
  288. }
  289. } else if (priority >= 256) {
  290. yield();
  291. }
  292. }
  293. tx_buffer[head] = c;
  294. transmitting = 1;
  295. tx_buffer_head = head;
  296. UART0_C2 = C2_TX_ACTIVE;
  297. }
  298. #ifdef HAS_KINETISK_UART0_FIFO
  299. void serial_write(const void *buf, unsigned int count)
  300. {
  301. const uint8_t *p = (const uint8_t *)buf;
  302. const uint8_t *end = p + count;
  303. uint32_t head, n;
  304. if (!(SIM_SCGC4 & SIM_SCGC4_UART0)) return;
  305. if (transmit_pin) transmit_assert();
  306. while (p < end) {
  307. head = tx_buffer_head;
  308. if (++head >= TX_BUFFER_SIZE) head = 0;
  309. if (tx_buffer_tail == head) {
  310. UART0_C2 = C2_TX_ACTIVE;
  311. do {
  312. int priority = nvic_execution_priority();
  313. if (priority <= IRQ_PRIORITY) {
  314. if ((UART0_S1 & UART_S1_TDRE)) {
  315. uint32_t tail = tx_buffer_tail;
  316. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  317. n = tx_buffer[tail];
  318. if (use9Bits) UART0_C3 = (UART0_C3 & ~0x40) | ((n & 0x100) >> 2);
  319. UART0_D = n;
  320. tx_buffer_tail = tail;
  321. }
  322. } else if (priority >= 256) {
  323. yield();
  324. }
  325. } while (tx_buffer_tail == head);
  326. }
  327. tx_buffer[head] = *p++;
  328. transmitting = 1;
  329. tx_buffer_head = head;
  330. }
  331. UART0_C2 = C2_TX_ACTIVE;
  332. }
  333. #else
  334. void serial_write(const void *buf, unsigned int count)
  335. {
  336. const uint8_t *p = (const uint8_t *)buf;
  337. while (count-- > 0) serial_putchar(*p++);
  338. }
  339. #endif
  340. void serial_flush(void)
  341. {
  342. while (transmitting) yield(); // wait
  343. }
  344. int serial_write_buffer_free(void)
  345. {
  346. uint32_t head, tail;
  347. head = tx_buffer_head;
  348. tail = tx_buffer_tail;
  349. if (head >= tail) return TX_BUFFER_SIZE - 1 - head + tail;
  350. return tail - head - 1;
  351. }
  352. int serial_available(void)
  353. {
  354. uint32_t head, tail;
  355. head = rx_buffer_head;
  356. tail = rx_buffer_tail;
  357. if (head >= tail) return head - tail;
  358. return RX_BUFFER_SIZE + head - tail;
  359. }
  360. int serial_getchar(void)
  361. {
  362. uint32_t head, tail;
  363. int c;
  364. head = rx_buffer_head;
  365. tail = rx_buffer_tail;
  366. if (head == tail) return -1;
  367. if (++tail >= RX_BUFFER_SIZE) tail = 0;
  368. c = rx_buffer[tail];
  369. rx_buffer_tail = tail;
  370. if (rts_pin) {
  371. int avail;
  372. if (head >= tail) avail = head - tail;
  373. else avail = RX_BUFFER_SIZE + head - tail;
  374. if (avail <= RTS_LOW_WATERMARK) rts_assert();
  375. }
  376. return c;
  377. }
  378. int serial_peek(void)
  379. {
  380. uint32_t head, tail;
  381. head = rx_buffer_head;
  382. tail = rx_buffer_tail;
  383. if (head == tail) return -1;
  384. if (++tail >= RX_BUFFER_SIZE) tail = 0;
  385. return rx_buffer[tail];
  386. }
  387. void serial_clear(void)
  388. {
  389. #ifdef HAS_KINETISK_UART0_FIFO
  390. if (!(SIM_SCGC4 & SIM_SCGC4_UART0)) return;
  391. UART0_C2 &= ~(UART_C2_RE | UART_C2_RIE | UART_C2_ILIE);
  392. UART0_CFIFO = UART_CFIFO_RXFLUSH;
  393. UART0_C2 |= (UART_C2_RE | UART_C2_RIE | UART_C2_ILIE);
  394. #endif
  395. rx_buffer_head = rx_buffer_tail;
  396. if (rts_pin) rts_assert();
  397. }
  398. // status interrupt combines
  399. // Transmit data below watermark UART_S1_TDRE
  400. // Transmit complete UART_S1_TC
  401. // Idle line UART_S1_IDLE
  402. // Receive data above watermark UART_S1_RDRF
  403. // LIN break detect UART_S2_LBKDIF
  404. // RxD pin active edge UART_S2_RXEDGIF
  405. void uart0_status_isr(void)
  406. {
  407. uint32_t head, tail, n;
  408. uint8_t c;
  409. #ifdef HAS_KINETISK_UART0_FIFO
  410. uint32_t newhead;
  411. uint8_t avail;
  412. if (UART0_S1 & (UART_S1_RDRF | UART_S1_IDLE)) {
  413. __disable_irq();
  414. avail = UART0_RCFIFO;
  415. if (avail == 0) {
  416. // The only way to clear the IDLE interrupt flag is
  417. // to read the data register. But reading with no
  418. // data causes a FIFO underrun, which causes the
  419. // FIFO to return corrupted data. If anyone from
  420. // Freescale reads this, what a poor design! There
  421. // write should be a write-1-to-clear for IDLE.
  422. c = UART0_D;
  423. // flushing the fifo recovers from the underrun,
  424. // but there's a possible race condition where a
  425. // new character could be received between reading
  426. // RCFIFO == 0 and flushing the FIFO. To minimize
  427. // the chance, interrupts are disabled so a higher
  428. // priority interrupt (hopefully) doesn't delay.
  429. // TODO: change this to disabling the IDLE interrupt
  430. // which won't be simple, since we already manage
  431. // which transmit interrupts are enabled.
  432. UART0_CFIFO = UART_CFIFO_RXFLUSH;
  433. __enable_irq();
  434. } else {
  435. __enable_irq();
  436. head = rx_buffer_head;
  437. tail = rx_buffer_tail;
  438. do {
  439. if (use9Bits && (UART0_C3 & 0x80)) {
  440. n = UART0_D | 0x100;
  441. } else {
  442. n = UART0_D;
  443. }
  444. newhead = head + 1;
  445. if (newhead >= RX_BUFFER_SIZE) newhead = 0;
  446. if (newhead != tail) {
  447. head = newhead;
  448. rx_buffer[head] = n;
  449. }
  450. } while (--avail > 0);
  451. rx_buffer_head = head;
  452. if (rts_pin) {
  453. int avail;
  454. if (head >= tail) avail = head - tail;
  455. else avail = RX_BUFFER_SIZE + head - tail;
  456. if (avail >= RTS_HIGH_WATERMARK) rts_deassert();
  457. }
  458. }
  459. }
  460. c = UART0_C2;
  461. if ((c & UART_C2_TIE) && (UART0_S1 & UART_S1_TDRE)) {
  462. head = tx_buffer_head;
  463. tail = tx_buffer_tail;
  464. do {
  465. if (tail == head) break;
  466. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  467. avail = UART0_S1;
  468. n = tx_buffer[tail];
  469. if (use9Bits) UART0_C3 = (UART0_C3 & ~0x40) | ((n & 0x100) >> 2);
  470. UART0_D = n;
  471. } while (UART0_TCFIFO < 8);
  472. tx_buffer_tail = tail;
  473. if (UART0_S1 & UART_S1_TDRE) UART0_C2 = C2_TX_COMPLETING;
  474. }
  475. #else
  476. if (UART0_S1 & UART_S1_RDRF) {
  477. n = UART0_D;
  478. if (use9Bits && (UART0_C3 & 0x80)) n |= 0x100;
  479. head = rx_buffer_head + 1;
  480. if (head >= RX_BUFFER_SIZE) head = 0;
  481. if (head != rx_buffer_tail) {
  482. rx_buffer[head] = n;
  483. rx_buffer_head = head;
  484. }
  485. }
  486. c = UART0_C2;
  487. if ((c & UART_C2_TIE) && (UART0_S1 & UART_S1_TDRE)) {
  488. head = tx_buffer_head;
  489. tail = tx_buffer_tail;
  490. if (head == tail) {
  491. UART0_C2 = C2_TX_COMPLETING;
  492. } else {
  493. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  494. n = tx_buffer[tail];
  495. if (use9Bits) UART0_C3 = (UART0_C3 & ~0x40) | ((n & 0x100) >> 2);
  496. UART0_D = n;
  497. tx_buffer_tail = tail;
  498. }
  499. }
  500. #endif
  501. if ((c & UART_C2_TCIE) && (UART0_S1 & UART_S1_TC)) {
  502. transmitting = 0;
  503. if (transmit_pin) transmit_deassert();
  504. UART0_C2 = C2_TX_INACTIVE;
  505. }
  506. }
  507. void serial_print(const char *p)
  508. {
  509. while (*p) {
  510. char c = *p++;
  511. if (c == '\n') serial_putchar('\r');
  512. serial_putchar(c);
  513. }
  514. }
  515. static void serial_phex1(uint32_t n)
  516. {
  517. n &= 15;
  518. if (n < 10) {
  519. serial_putchar('0' + n);
  520. } else {
  521. serial_putchar('A' - 10 + n);
  522. }
  523. }
  524. void serial_phex(uint32_t n)
  525. {
  526. serial_phex1(n >> 4);
  527. serial_phex1(n);
  528. }
  529. void serial_phex16(uint32_t n)
  530. {
  531. serial_phex(n >> 8);
  532. serial_phex(n);
  533. }
  534. void serial_phex32(uint32_t n)
  535. {
  536. serial_phex(n >> 24);
  537. serial_phex(n >> 16);
  538. serial_phex(n >> 8);
  539. serial_phex(n);
  540. }