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.

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