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.

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