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.

598 lines
18KB

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