Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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. #if defined(__MK64FX512__) || defined(__MK66FX1M0__) || defined(KINETISL)
  163. // For T3.5/T3.6/TLC See about turning on 2 stop bit mode
  164. if ( format & 0x100) {
  165. uint8_t bdl = UART1_BDL;
  166. UART1_BDH |= UART_BDH_SBNS; // Turn on 2 stop bits - was turned off by set baud
  167. UART1_BDL = bdl; // Says BDH not acted on until BDL is written
  168. }
  169. #endif
  170. }
  171. void serial2_end(void)
  172. {
  173. if (!(SIM_SCGC4 & SIM_SCGC4_UART1)) return;
  174. while (transmitting) yield(); // wait for buffered data to send
  175. NVIC_DISABLE_IRQ(IRQ_UART1_STATUS);
  176. UART1_C2 = 0;
  177. #if defined(KINETISK)
  178. switch (rx_pin_num) {
  179. case 9: CORE_PIN9_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break; // PTC3
  180. #if !(defined(__MK64FX512__) || defined(__MK66FX1M0__)) // not on T3.5 or T3.6
  181. case 26: CORE_PIN26_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break; // PTE1
  182. #endif
  183. }
  184. switch (tx_pin_num & 127) {
  185. case 10: CORE_PIN10_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break; // PTC4
  186. #if !(defined(__MK64FX512__) || defined(__MK66FX1M0__)) // not on T3.5 or T3.6
  187. case 31: CORE_PIN31_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break; // PTE0
  188. #endif
  189. }
  190. #elif defined(KINETISL)
  191. CORE_PIN9_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); // PTC3
  192. CORE_PIN10_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); // PTC4
  193. #endif
  194. rx_buffer_head = 0;
  195. rx_buffer_tail = 0;
  196. if (rts_pin) rts_deassert();
  197. }
  198. void serial2_set_transmit_pin(uint8_t pin)
  199. {
  200. while (transmitting) ;
  201. pinMode(pin, OUTPUT);
  202. digitalWrite(pin, LOW);
  203. transmit_pin = portOutputRegister(pin);
  204. #if defined(KINETISL)
  205. transmit_mask = digitalPinToBitMask(pin);
  206. #endif
  207. }
  208. void serial2_set_tx(uint8_t pin, uint8_t opendrain)
  209. {
  210. #if defined(KINETISK)
  211. uint32_t cfg;
  212. if (opendrain) pin |= 128;
  213. if (pin == tx_pin_num) return;
  214. if ((SIM_SCGC4 & SIM_SCGC4_UART1)) {
  215. switch (tx_pin_num & 127) {
  216. case 10: CORE_PIN10_CONFIG = 0; break; // PTC4
  217. #if !(defined(__MK64FX512__) || defined(__MK66FX1M0__)) // not on T3.5 or T3.6
  218. case 31: CORE_PIN31_CONFIG = 0; break; // PTE0
  219. #endif
  220. }
  221. if (opendrain) {
  222. cfg = PORT_PCR_DSE | PORT_PCR_ODE;
  223. } else {
  224. cfg = PORT_PCR_DSE | PORT_PCR_SRE;
  225. }
  226. switch (pin & 127) {
  227. case 10: CORE_PIN10_CONFIG = cfg | PORT_PCR_MUX(3); break;
  228. #if !(defined(__MK64FX512__) || defined(__MK66FX1M0__)) // not on T3.5 or T3.6
  229. case 31: CORE_PIN31_CONFIG = cfg | PORT_PCR_MUX(3); break;
  230. #endif
  231. }
  232. }
  233. tx_pin_num = pin;
  234. #endif
  235. }
  236. void serial2_set_rx(uint8_t pin)
  237. {
  238. #if defined(KINETISK)
  239. if (pin == rx_pin_num) return;
  240. if ((SIM_SCGC4 & SIM_SCGC4_UART1)) {
  241. switch (rx_pin_num) {
  242. case 9: CORE_PIN9_CONFIG = 0; break; // PTC3
  243. #if !(defined(__MK64FX512__) || defined(__MK66FX1M0__)) // not on T3.5 or T3.6
  244. case 26: CORE_PIN26_CONFIG = 0; break; // PTE1
  245. #endif
  246. }
  247. switch (pin) {
  248. case 9: CORE_PIN9_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  249. #if !(defined(__MK64FX512__) || defined(__MK66FX1M0__)) // not on T3.5 or T3.6
  250. case 26: CORE_PIN26_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  251. #endif
  252. }
  253. }
  254. rx_pin_num = pin;
  255. #endif
  256. }
  257. int serial2_set_rts(uint8_t pin)
  258. {
  259. if (!(SIM_SCGC4 & SIM_SCGC4_UART1)) return 0;
  260. if (pin < CORE_NUM_DIGITAL) {
  261. rts_pin = portOutputRegister(pin);
  262. #if defined(KINETISL)
  263. rts_mask = digitalPinToBitMask(pin);
  264. #endif
  265. pinMode(pin, OUTPUT);
  266. rts_assert();
  267. } else {
  268. rts_pin = NULL;
  269. return 0;
  270. }
  271. /*
  272. if (!(SIM_SCGC4 & SIM_SCGC4_UART1)) return 0;
  273. if (pin == 22) {
  274. CORE_PIN22_CONFIG = PORT_PCR_MUX(3);
  275. } else {
  276. UART1_MODEM &= ~UART_MODEM_RXRTSE;
  277. return 0;
  278. }
  279. UART1_MODEM |= UART_MODEM_RXRTSE;
  280. */
  281. return 1;
  282. }
  283. int serial2_set_cts(uint8_t pin)
  284. {
  285. #if defined(KINETISK)
  286. if (!(SIM_SCGC4 & SIM_SCGC4_UART1)) return 0;
  287. if (pin == 23) {
  288. CORE_PIN23_CONFIG = PORT_PCR_MUX(3) | PORT_PCR_PE; // weak pulldown
  289. } else {
  290. UART1_MODEM &= ~UART_MODEM_TXCTSE;
  291. return 0;
  292. }
  293. UART1_MODEM |= UART_MODEM_TXCTSE;
  294. return 1;
  295. #else
  296. return 0;
  297. #endif
  298. }
  299. void serial2_putchar(uint32_t c)
  300. {
  301. uint32_t head, n;
  302. if (!(SIM_SCGC4 & SIM_SCGC4_UART1)) return;
  303. if (transmit_pin) transmit_assert();
  304. head = tx_buffer_head;
  305. if (++head >= TX_BUFFER_SIZE) head = 0;
  306. while (tx_buffer_tail == head) {
  307. int priority = nvic_execution_priority();
  308. if (priority <= IRQ_PRIORITY) {
  309. if ((UART1_S1 & UART_S1_TDRE)) {
  310. uint32_t tail = tx_buffer_tail;
  311. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  312. n = tx_buffer[tail];
  313. if (use9Bits) UART1_C3 = (UART1_C3 & ~0x40) | ((n & 0x100) >> 2);
  314. UART1_D = n;
  315. tx_buffer_tail = tail;
  316. }
  317. } else if (priority >= 256) {
  318. yield(); // wait
  319. }
  320. }
  321. tx_buffer[head] = c;
  322. transmitting = 1;
  323. tx_buffer_head = head;
  324. UART1_C2 = C2_TX_ACTIVE;
  325. }
  326. #ifdef HAS_KINETISK_UART1_FIFO
  327. void serial2_write(const void *buf, unsigned int count)
  328. {
  329. const uint8_t *p = (const uint8_t *)buf;
  330. const uint8_t *end = p + count;
  331. uint32_t head, n;
  332. if (!(SIM_SCGC4 & SIM_SCGC4_UART1)) return;
  333. if (transmit_pin) transmit_assert();
  334. while (p < end) {
  335. head = tx_buffer_head;
  336. if (++head >= TX_BUFFER_SIZE) head = 0;
  337. if (tx_buffer_tail == head) {
  338. UART1_C2 = C2_TX_ACTIVE;
  339. do {
  340. int priority = nvic_execution_priority();
  341. if (priority <= IRQ_PRIORITY) {
  342. if ((UART1_S1 & UART_S1_TDRE)) {
  343. uint32_t tail = tx_buffer_tail;
  344. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  345. n = tx_buffer[tail];
  346. if (use9Bits) UART1_C3 = (UART1_C3 & ~0x40) | ((n & 0x100) >> 2);
  347. UART1_D = n;
  348. tx_buffer_tail = tail;
  349. }
  350. } else if (priority >= 256) {
  351. yield();
  352. }
  353. } while (tx_buffer_tail == head);
  354. }
  355. tx_buffer[head] = *p++;
  356. transmitting = 1;
  357. tx_buffer_head = head;
  358. }
  359. UART1_C2 = C2_TX_ACTIVE;
  360. }
  361. #else
  362. void serial2_write(const void *buf, unsigned int count)
  363. {
  364. const uint8_t *p = (const uint8_t *)buf;
  365. while (count-- > 0) serial2_putchar(*p++);
  366. }
  367. #endif
  368. void serial2_flush(void)
  369. {
  370. while (transmitting) yield(); // wait
  371. }
  372. int serial2_write_buffer_free(void)
  373. {
  374. uint32_t head, tail;
  375. head = tx_buffer_head;
  376. tail = tx_buffer_tail;
  377. if (head >= tail) return TX_BUFFER_SIZE - 1 - head + tail;
  378. return tail - head - 1;
  379. }
  380. int serial2_available(void)
  381. {
  382. uint32_t head, tail;
  383. head = rx_buffer_head;
  384. tail = rx_buffer_tail;
  385. if (head >= tail) return head - tail;
  386. return RX_BUFFER_SIZE + head - tail;
  387. }
  388. int serial2_getchar(void)
  389. {
  390. uint32_t head, tail;
  391. int c;
  392. head = rx_buffer_head;
  393. tail = rx_buffer_tail;
  394. if (head == tail) return -1;
  395. if (++tail >= RX_BUFFER_SIZE) tail = 0;
  396. c = rx_buffer[tail];
  397. rx_buffer_tail = tail;
  398. if (rts_pin) {
  399. int avail;
  400. if (head >= tail) avail = head - tail;
  401. else avail = RX_BUFFER_SIZE + head - tail;
  402. if (avail <= RTS_LOW_WATERMARK) rts_assert();
  403. }
  404. return c;
  405. }
  406. int serial2_peek(void)
  407. {
  408. uint32_t head, tail;
  409. head = rx_buffer_head;
  410. tail = rx_buffer_tail;
  411. if (head == tail) return -1;
  412. if (++tail >= RX_BUFFER_SIZE) tail = 0;
  413. return rx_buffer[tail];
  414. }
  415. void serial2_clear(void)
  416. {
  417. #ifdef HAS_KINETISK_UART1_FIFO
  418. if (!(SIM_SCGC4 & SIM_SCGC4_UART1)) return;
  419. UART1_C2 &= ~(UART_C2_RE | UART_C2_RIE | UART_C2_ILIE);
  420. UART1_CFIFO = UART_CFIFO_RXFLUSH;
  421. UART1_C2 |= (UART_C2_RE | UART_C2_RIE | UART_C2_ILIE);
  422. #endif
  423. rx_buffer_head = rx_buffer_tail;
  424. if (rts_pin) rts_assert();
  425. }
  426. // status interrupt combines
  427. // Transmit data below watermark UART_S1_TDRE
  428. // Transmit complete UART_S1_TC
  429. // Idle line UART_S1_IDLE
  430. // Receive data above watermark UART_S1_RDRF
  431. // LIN break detect UART_S2_LBKDIF
  432. // RxD pin active edge UART_S2_RXEDGIF
  433. void uart1_status_isr(void)
  434. {
  435. uint32_t head, tail, n;
  436. uint8_t c;
  437. #ifdef HAS_KINETISK_UART1_FIFO
  438. uint32_t newhead;
  439. uint8_t avail;
  440. if (UART1_S1 & (UART_S1_RDRF | UART_S1_IDLE)) {
  441. __disable_irq();
  442. avail = UART1_RCFIFO;
  443. if (avail == 0) {
  444. // The only way to clear the IDLE interrupt flag is
  445. // to read the data register. But reading with no
  446. // data causes a FIFO underrun, which causes the
  447. // FIFO to return corrupted data. If anyone from
  448. // Freescale reads this, what a poor design! There
  449. // write should be a write-1-to-clear for IDLE.
  450. c = UART1_D;
  451. // flushing the fifo recovers from the underrun,
  452. // but there's a possible race condition where a
  453. // new character could be received between reading
  454. // RCFIFO == 0 and flushing the FIFO. To minimize
  455. // the chance, interrupts are disabled so a higher
  456. // priority interrupt (hopefully) doesn't delay.
  457. // TODO: change this to disabling the IDLE interrupt
  458. // which won't be simple, since we already manage
  459. // which transmit interrupts are enabled.
  460. UART1_CFIFO = UART_CFIFO_RXFLUSH;
  461. __enable_irq();
  462. } else {
  463. __enable_irq();
  464. head = rx_buffer_head;
  465. tail = rx_buffer_tail;
  466. do {
  467. if (use9Bits && (UART1_C3 & 0x80)) {
  468. n = UART1_D | 0x100;
  469. } else {
  470. n = UART1_D;
  471. }
  472. newhead = head + 1;
  473. if (newhead >= RX_BUFFER_SIZE) newhead = 0;
  474. if (newhead != tail) {
  475. head = newhead;
  476. rx_buffer[head] = n;
  477. }
  478. } while (--avail > 0);
  479. rx_buffer_head = head;
  480. if (rts_pin) {
  481. int avail;
  482. if (head >= tail) avail = head - tail;
  483. else avail = RX_BUFFER_SIZE + head - tail;
  484. if (avail >= RTS_HIGH_WATERMARK) rts_deassert();
  485. }
  486. }
  487. }
  488. c = UART1_C2;
  489. if ((c & UART_C2_TIE) && (UART1_S1 & UART_S1_TDRE)) {
  490. head = tx_buffer_head;
  491. tail = tx_buffer_tail;
  492. do {
  493. if (tail == head) break;
  494. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  495. avail = UART1_S1;
  496. n = tx_buffer[tail];
  497. if (use9Bits) UART1_C3 = (UART1_C3 & ~0x40) | ((n & 0x100) >> 2);
  498. UART1_D = n;
  499. } while (UART1_TCFIFO < 8);
  500. tx_buffer_tail = tail;
  501. if (UART1_S1 & UART_S1_TDRE) UART1_C2 = C2_TX_COMPLETING;
  502. }
  503. #else
  504. if (UART1_S1 & UART_S1_RDRF) {
  505. n = UART1_D;
  506. if (use9Bits && (UART1_C3 & 0x80)) n |= 0x100;
  507. head = rx_buffer_head + 1;
  508. if (head >= RX_BUFFER_SIZE) head = 0;
  509. if (head != rx_buffer_tail) {
  510. rx_buffer[head] = n;
  511. rx_buffer_head = head;
  512. }
  513. }
  514. c = UART1_C2;
  515. if ((c & UART_C2_TIE) && (UART1_S1 & UART_S1_TDRE)) {
  516. head = tx_buffer_head;
  517. tail = tx_buffer_tail;
  518. if (head == tail) {
  519. UART1_C2 = C2_TX_COMPLETING;
  520. } else {
  521. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  522. n = tx_buffer[tail];
  523. if (use9Bits) UART1_C3 = (UART1_C3 & ~0x40) | ((n & 0x100) >> 2);
  524. UART1_D = n;
  525. tx_buffer_tail = tail;
  526. }
  527. }
  528. #endif
  529. if ((c & UART_C2_TCIE) && (UART1_S1 & UART_S1_TC)) {
  530. transmitting = 0;
  531. if (transmit_pin) transmit_deassert();
  532. UART1_C2 = C2_TX_INACTIVE;
  533. }
  534. }