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