Teensy 4.1 core updated for C++20
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

570 行
16KB

  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 40 // 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. #if defined(KINETISK)
  86. static uint8_t rx_pin_num = 9;
  87. static uint8_t tx_pin_num = 10;
  88. #endif
  89. // UART0 and UART1 are clocked by F_CPU, UART2 is clocked by F_BUS
  90. // UART0 has 8 byte fifo, UART1 and UART2 have 1 byte buffer
  91. #ifdef HAS_KINETISK_UART1_FIFO
  92. #define C2_ENABLE UART_C2_TE | UART_C2_RE | UART_C2_RIE | UART_C2_ILIE
  93. #else
  94. #define C2_ENABLE UART_C2_TE | UART_C2_RE | UART_C2_RIE
  95. #endif
  96. #define C2_TX_ACTIVE C2_ENABLE | UART_C2_TIE
  97. #define C2_TX_COMPLETING C2_ENABLE | UART_C2_TCIE
  98. #define C2_TX_INACTIVE C2_ENABLE
  99. void serial2_begin(uint32_t divisor)
  100. {
  101. SIM_SCGC4 |= SIM_SCGC4_UART1; // turn on clock, TODO: use bitband
  102. rx_buffer_head = 0;
  103. rx_buffer_tail = 0;
  104. tx_buffer_head = 0;
  105. tx_buffer_tail = 0;
  106. transmitting = 0;
  107. #if defined(KINETISK)
  108. switch (rx_pin_num) {
  109. case 9: CORE_PIN9_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  110. case 26: CORE_PIN26_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  111. }
  112. switch (tx_pin_num) {
  113. case 10: CORE_PIN10_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); break;
  114. case 31: CORE_PIN31_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); break;
  115. }
  116. #elif defined(KINETISL)
  117. CORE_PIN9_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3);
  118. CORE_PIN10_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);
  119. #endif
  120. #if defined(HAS_KINETISK_UART1)
  121. UART1_BDH = (divisor >> 13) & 0x1F;
  122. UART1_BDL = (divisor >> 5) & 0xFF;
  123. UART1_C4 = divisor & 0x1F;
  124. #ifdef HAS_KINETISK_UART1_FIFO
  125. UART1_C1 = UART_C1_ILT;
  126. UART1_TWFIFO = 2; // tx watermark, causes S1_TDRE to set
  127. UART1_RWFIFO = 4; // rx watermark, causes S1_RDRF to set
  128. UART1_PFIFO = UART_PFIFO_TXFE | UART_PFIFO_RXFE;
  129. #else
  130. UART1_C1 = 0;
  131. UART1_PFIFO = 0;
  132. #endif
  133. #elif defined(HAS_KINETISL_UART1)
  134. UART1_BDH = (divisor >> 8) & 0x1F;
  135. UART1_BDL = divisor & 0xFF;
  136. UART1_C1 = 0;
  137. #endif
  138. UART1_C2 = C2_TX_INACTIVE;
  139. NVIC_SET_PRIORITY(IRQ_UART1_STATUS, IRQ_PRIORITY);
  140. NVIC_ENABLE_IRQ(IRQ_UART1_STATUS);
  141. }
  142. void serial2_format(uint32_t format)
  143. {
  144. uint8_t c;
  145. c = UART1_C1;
  146. c = (c & ~0x13) | (format & 0x03); // configure parity
  147. if (format & 0x04) c |= 0x10; // 9 bits (might include parity)
  148. UART1_C1 = c;
  149. if ((format & 0x0F) == 0x04) UART1_C3 |= 0x40; // 8N2 is 9 bit with 9th bit always 1
  150. c = UART1_S2 & ~0x10;
  151. if (format & 0x10) c |= 0x10; // rx invert
  152. UART1_S2 = c;
  153. c = UART1_C3 & ~0x10;
  154. if (format & 0x20) c |= 0x10; // tx invert
  155. UART1_C3 = c;
  156. #ifdef SERIAL_9BIT_SUPPORT
  157. c = UART1_C4 & 0x1F;
  158. if (format & 0x08) c |= 0x20; // 9 bit mode with parity (requires 10 bits)
  159. UART1_C4 = c;
  160. use9Bits = format & 0x80;
  161. #endif
  162. // UART1_C1.0 = parity, 0=even, 1=odd
  163. // UART1_C1.1 = parity, 0=disable, 1=enable
  164. // UART1_C1.4 = mode, 1=9bit, 0=8bit
  165. // UART1_C4.5 = mode, 1=10bit, 0=8bit
  166. // UART1_C3.4 = txinv, 0=normal, 1=inverted
  167. // UART1_S2.4 = rxinv, 0=normal, 1=inverted
  168. }
  169. void serial2_end(void)
  170. {
  171. if (!(SIM_SCGC4 & SIM_SCGC4_UART1)) return;
  172. while (transmitting) yield(); // wait for buffered data to send
  173. NVIC_DISABLE_IRQ(IRQ_UART1_STATUS);
  174. UART1_C2 = 0;
  175. #if defined(KINETISK)
  176. switch (rx_pin_num) {
  177. case 9: CORE_PIN9_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break; // PTC3
  178. #if !(defined(__MK64FX512__) || defined(__MK66FX1M0__)) // not on T3.5 or T3.6
  179. case 26: CORE_PIN26_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break; // PTE1
  180. #endif
  181. }
  182. switch (tx_pin_num & 127) {
  183. case 10: CORE_PIN10_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break; // PTC4
  184. #if !(defined(__MK64FX512__) || defined(__MK66FX1M0__)) // not on T3.5 or T3.6
  185. case 31: CORE_PIN31_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break; // PTE0
  186. #endif
  187. }
  188. #elif defined(KINETISL)
  189. CORE_PIN9_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); // PTC3
  190. CORE_PIN10_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); // PTC4
  191. #endif
  192. rx_buffer_head = 0;
  193. rx_buffer_tail = 0;
  194. if (rts_pin) rts_deassert();
  195. }
  196. void serial2_set_transmit_pin(uint8_t pin)
  197. {
  198. while (transmitting) ;
  199. pinMode(pin, OUTPUT);
  200. digitalWrite(pin, LOW);
  201. transmit_pin = portOutputRegister(pin);
  202. #if defined(KINETISL)
  203. transmit_mask = digitalPinToBitMask(pin);
  204. #endif
  205. }
  206. void serial2_set_tx(uint8_t pin, uint8_t opendrain)
  207. {
  208. #if defined(KINETISK)
  209. uint32_t cfg;
  210. if (opendrain) pin |= 128;
  211. if (pin == tx_pin_num) return;
  212. if ((SIM_SCGC4 & SIM_SCGC4_UART1)) {
  213. switch (tx_pin_num & 127) {
  214. case 10: CORE_PIN10_CONFIG = 0; break; // PTC4
  215. #if !(defined(__MK64FX512__) || defined(__MK66FX1M0__)) // not on T3.5 or T3.6
  216. case 31: CORE_PIN31_CONFIG = 0; break; // PTE0
  217. #endif
  218. }
  219. if (opendrain) {
  220. cfg = PORT_PCR_DSE | PORT_PCR_ODE;
  221. } else {
  222. cfg = PORT_PCR_DSE | PORT_PCR_SRE;
  223. }
  224. switch (pin & 127) {
  225. case 10: CORE_PIN10_CONFIG = cfg | PORT_PCR_MUX(3); break;
  226. #if !(defined(__MK64FX512__) || defined(__MK66FX1M0__)) // not on T3.5 or T3.6
  227. case 31: CORE_PIN31_CONFIG = cfg | PORT_PCR_MUX(3); break;
  228. #endif
  229. }
  230. }
  231. tx_pin_num = pin;
  232. #endif
  233. }
  234. void serial2_set_rx(uint8_t pin)
  235. {
  236. #if defined(KINETISK)
  237. if (pin == rx_pin_num) return;
  238. if ((SIM_SCGC4 & SIM_SCGC4_UART1)) {
  239. switch (rx_pin_num) {
  240. case 9: CORE_PIN9_CONFIG = 0; break; // PTC3
  241. #if !(defined(__MK64FX512__) || defined(__MK66FX1M0__)) // not on T3.5 or T3.6
  242. case 26: CORE_PIN26_CONFIG = 0; break; // PTE1
  243. #endif
  244. }
  245. switch (pin) {
  246. case 9: CORE_PIN9_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  247. #if !(defined(__MK64FX512__) || defined(__MK66FX1M0__)) // not on T3.5 or T3.6
  248. case 26: CORE_PIN26_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  249. #endif
  250. }
  251. }
  252. rx_pin_num = pin;
  253. #endif
  254. }
  255. int serial2_set_rts(uint8_t pin)
  256. {
  257. if (!(SIM_SCGC4 & SIM_SCGC4_UART1)) return 0;
  258. if (pin < CORE_NUM_DIGITAL) {
  259. rts_pin = portOutputRegister(pin);
  260. #if defined(KINETISL)
  261. rts_mask = digitalPinToBitMask(pin);
  262. #endif
  263. pinMode(pin, OUTPUT);
  264. rts_assert();
  265. } else {
  266. rts_pin = NULL;
  267. return 0;
  268. }
  269. /*
  270. if (!(SIM_SCGC4 & SIM_SCGC4_UART1)) return 0;
  271. if (pin == 22) {
  272. CORE_PIN22_CONFIG = PORT_PCR_MUX(3);
  273. } else {
  274. UART1_MODEM &= ~UART_MODEM_RXRTSE;
  275. return 0;
  276. }
  277. UART1_MODEM |= UART_MODEM_RXRTSE;
  278. */
  279. return 1;
  280. }
  281. int serial2_set_cts(uint8_t pin)
  282. {
  283. #if defined(KINETISK)
  284. if (!(SIM_SCGC4 & SIM_SCGC4_UART1)) return 0;
  285. if (pin == 23) {
  286. CORE_PIN23_CONFIG = PORT_PCR_MUX(3) | PORT_PCR_PE; // weak pulldown
  287. } else {
  288. UART1_MODEM &= ~UART_MODEM_TXCTSE;
  289. return 0;
  290. }
  291. UART1_MODEM |= UART_MODEM_TXCTSE;
  292. return 1;
  293. #else
  294. return 0;
  295. #endif
  296. }
  297. void serial2_putchar(uint32_t c)
  298. {
  299. uint32_t head, n;
  300. if (!(SIM_SCGC4 & SIM_SCGC4_UART1)) return;
  301. if (transmit_pin) transmit_assert();
  302. head = tx_buffer_head;
  303. if (++head >= TX_BUFFER_SIZE) head = 0;
  304. while (tx_buffer_tail == head) {
  305. int priority = nvic_execution_priority();
  306. if (priority <= IRQ_PRIORITY) {
  307. if ((UART1_S1 & UART_S1_TDRE)) {
  308. uint32_t tail = tx_buffer_tail;
  309. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  310. n = tx_buffer[tail];
  311. if (use9Bits) UART1_C3 = (UART1_C3 & ~0x40) | ((n & 0x100) >> 2);
  312. UART1_D = n;
  313. tx_buffer_tail = tail;
  314. }
  315. } else if (priority >= 256) {
  316. yield(); // wait
  317. }
  318. }
  319. tx_buffer[head] = c;
  320. transmitting = 1;
  321. tx_buffer_head = head;
  322. UART1_C2 = C2_TX_ACTIVE;
  323. }
  324. #ifdef HAS_KINETISK_UART1_FIFO
  325. void serial2_write(const void *buf, unsigned int count)
  326. {
  327. const uint8_t *p = (const uint8_t *)buf;
  328. const uint8_t *end = p + count;
  329. uint32_t head, n;
  330. if (!(SIM_SCGC4 & SIM_SCGC4_UART1)) return;
  331. if (transmit_pin) transmit_assert();
  332. while (p < end) {
  333. head = tx_buffer_head;
  334. if (++head >= TX_BUFFER_SIZE) head = 0;
  335. if (tx_buffer_tail == head) {
  336. UART1_C2 = C2_TX_ACTIVE;
  337. do {
  338. int priority = nvic_execution_priority();
  339. if (priority <= IRQ_PRIORITY) {
  340. if ((UART1_S1 & UART_S1_TDRE)) {
  341. uint32_t tail = tx_buffer_tail;
  342. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  343. n = tx_buffer[tail];
  344. if (use9Bits) UART1_C3 = (UART1_C3 & ~0x40) | ((n & 0x100) >> 2);
  345. UART1_D = n;
  346. tx_buffer_tail = tail;
  347. }
  348. } else if (priority >= 256) {
  349. yield();
  350. }
  351. } while (tx_buffer_tail == head);
  352. }
  353. tx_buffer[head] = *p++;
  354. transmitting = 1;
  355. tx_buffer_head = head;
  356. }
  357. UART1_C2 = C2_TX_ACTIVE;
  358. }
  359. #else
  360. void serial2_write(const void *buf, unsigned int count)
  361. {
  362. const uint8_t *p = (const uint8_t *)buf;
  363. while (count-- > 0) serial2_putchar(*p++);
  364. }
  365. #endif
  366. void serial2_flush(void)
  367. {
  368. while (transmitting) yield(); // wait
  369. }
  370. int serial2_write_buffer_free(void)
  371. {
  372. uint32_t head, tail;
  373. head = tx_buffer_head;
  374. tail = tx_buffer_tail;
  375. if (head >= tail) return TX_BUFFER_SIZE - 1 - head + tail;
  376. return tail - head - 1;
  377. }
  378. int serial2_available(void)
  379. {
  380. uint32_t head, tail;
  381. head = rx_buffer_head;
  382. tail = rx_buffer_tail;
  383. if (head >= tail) return head - tail;
  384. return RX_BUFFER_SIZE + head - tail;
  385. }
  386. int serial2_getchar(void)
  387. {
  388. uint32_t head, tail;
  389. int c;
  390. head = rx_buffer_head;
  391. tail = rx_buffer_tail;
  392. if (head == tail) return -1;
  393. if (++tail >= RX_BUFFER_SIZE) tail = 0;
  394. c = rx_buffer[tail];
  395. rx_buffer_tail = tail;
  396. if (rts_pin) {
  397. int avail;
  398. if (head >= tail) avail = head - tail;
  399. else avail = RX_BUFFER_SIZE + head - tail;
  400. if (avail <= RTS_LOW_WATERMARK) rts_assert();
  401. }
  402. return c;
  403. }
  404. int serial2_peek(void)
  405. {
  406. uint32_t head, tail;
  407. head = rx_buffer_head;
  408. tail = rx_buffer_tail;
  409. if (head == tail) return -1;
  410. if (++tail >= RX_BUFFER_SIZE) tail = 0;
  411. return rx_buffer[tail];
  412. }
  413. void serial2_clear(void)
  414. {
  415. #ifdef HAS_KINETISK_UART1_FIFO
  416. if (!(SIM_SCGC4 & SIM_SCGC4_UART1)) return;
  417. UART1_C2 &= ~(UART_C2_RE | UART_C2_RIE | UART_C2_ILIE);
  418. UART1_CFIFO = UART_CFIFO_RXFLUSH;
  419. UART1_C2 |= (UART_C2_RE | UART_C2_RIE | UART_C2_ILIE);
  420. #endif
  421. rx_buffer_head = rx_buffer_tail;
  422. if (rts_pin) rts_assert();
  423. }
  424. // status interrupt combines
  425. // Transmit data below watermark UART_S1_TDRE
  426. // Transmit complete UART_S1_TC
  427. // Idle line UART_S1_IDLE
  428. // Receive data above watermark UART_S1_RDRF
  429. // LIN break detect UART_S2_LBKDIF
  430. // RxD pin active edge UART_S2_RXEDGIF
  431. void uart1_status_isr(void)
  432. {
  433. uint32_t head, tail, n;
  434. uint8_t c;
  435. #ifdef HAS_KINETISK_UART1_FIFO
  436. uint32_t newhead;
  437. uint8_t avail;
  438. if (UART1_S1 & (UART_S1_RDRF | UART_S1_IDLE)) {
  439. __disable_irq();
  440. avail = UART1_RCFIFO;
  441. if (avail == 0) {
  442. // The only way to clear the IDLE interrupt flag is
  443. // to read the data register. But reading with no
  444. // data causes a FIFO underrun, which causes the
  445. // FIFO to return corrupted data. If anyone from
  446. // Freescale reads this, what a poor design! There
  447. // write should be a write-1-to-clear for IDLE.
  448. c = UART1_D;
  449. // flushing the fifo recovers from the underrun,
  450. // but there's a possible race condition where a
  451. // new character could be received between reading
  452. // RCFIFO == 0 and flushing the FIFO. To minimize
  453. // the chance, interrupts are disabled so a higher
  454. // priority interrupt (hopefully) doesn't delay.
  455. // TODO: change this to disabling the IDLE interrupt
  456. // which won't be simple, since we already manage
  457. // which transmit interrupts are enabled.
  458. UART1_CFIFO = UART_CFIFO_RXFLUSH;
  459. __enable_irq();
  460. } else {
  461. __enable_irq();
  462. head = rx_buffer_head;
  463. tail = rx_buffer_tail;
  464. do {
  465. if (use9Bits && (UART1_C3 & 0x80)) {
  466. n = UART1_D | 0x100;
  467. } else {
  468. n = UART1_D;
  469. }
  470. newhead = head + 1;
  471. if (newhead >= RX_BUFFER_SIZE) newhead = 0;
  472. if (newhead != tail) {
  473. head = newhead;
  474. rx_buffer[head] = n;
  475. }
  476. } while (--avail > 0);
  477. rx_buffer_head = head;
  478. if (rts_pin) {
  479. int avail;
  480. if (head >= tail) avail = head - tail;
  481. else avail = RX_BUFFER_SIZE + head - tail;
  482. if (avail >= RTS_HIGH_WATERMARK) rts_deassert();
  483. }
  484. }
  485. }
  486. c = UART1_C2;
  487. if ((c & UART_C2_TIE) && (UART1_S1 & UART_S1_TDRE)) {
  488. head = tx_buffer_head;
  489. tail = tx_buffer_tail;
  490. do {
  491. if (tail == head) break;
  492. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  493. avail = UART1_S1;
  494. n = tx_buffer[tail];
  495. if (use9Bits) UART1_C3 = (UART1_C3 & ~0x40) | ((n & 0x100) >> 2);
  496. UART1_D = n;
  497. } while (UART1_TCFIFO < 8);
  498. tx_buffer_tail = tail;
  499. if (UART1_S1 & UART_S1_TDRE) UART1_C2 = C2_TX_COMPLETING;
  500. }
  501. #else
  502. if (UART1_S1 & UART_S1_RDRF) {
  503. n = UART1_D;
  504. if (use9Bits && (UART1_C3 & 0x80)) n |= 0x100;
  505. head = rx_buffer_head + 1;
  506. if (head >= RX_BUFFER_SIZE) head = 0;
  507. if (head != rx_buffer_tail) {
  508. rx_buffer[head] = n;
  509. rx_buffer_head = head;
  510. }
  511. }
  512. c = UART1_C2;
  513. if ((c & UART_C2_TIE) && (UART1_S1 & UART_S1_TDRE)) {
  514. head = tx_buffer_head;
  515. tail = tx_buffer_tail;
  516. if (head == tail) {
  517. UART1_C2 = C2_TX_COMPLETING;
  518. } else {
  519. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  520. n = tx_buffer[tail];
  521. if (use9Bits) UART1_C3 = (UART1_C3 & ~0x40) | ((n & 0x100) >> 2);
  522. UART1_D = n;
  523. tx_buffer_tail = tail;
  524. }
  525. }
  526. #endif
  527. if ((c & UART_C2_TCIE) && (UART1_S1 & UART_S1_TC)) {
  528. transmitting = 0;
  529. if (transmit_pin) transmit_deassert();
  530. UART1_C2 = C2_TX_INACTIVE;
  531. }
  532. }