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.

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