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 година
пре 8 година
пре 11 година
пре 8 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 9 година
пре 9 година
пре 11 година
пре 11 година
пре 11 година
пре 9 година
пре 9 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 9 година
пре 9 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 9 година
пре 11 година
пре 11 година
пре 8 година
пре 11 година
пре 11 година
пре 11 година
пре 9 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 9 година
пре 9 година
пре 11 година
пре 11 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  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 SERIAL1_TX_BUFFER_SIZE
  37. #define SERIAL1_TX_BUFFER_SIZE 64 // number of outgoing bytes to buffer
  38. #endif
  39. #ifndef SERIAL1_RX_BUFFER_SIZE
  40. #define SERIAL1_RX_BUFFER_SIZE 64 // number of incoming bytes to buffer
  41. #endif
  42. #define RTS_HIGH_WATERMARK (SERIAL1_RX_BUFFER_SIZE-24) // RTS requests sender to pause
  43. #define RTS_LOW_WATERMARK (SERIAL1_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[SERIAL1_TX_BUFFER_SIZE];
  56. static volatile BUFTYPE rx_buffer[SERIAL1_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 SERIAL1_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 SERIAL1_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. static uint8_t rx_pin_num = 0;
  90. static uint8_t tx_pin_num = 1;
  91. // UART0 and UART1 are clocked by F_CPU, UART2 is clocked by F_BUS
  92. // UART0 has 8 byte fifo, UART1 and UART2 have 1 byte buffer
  93. #ifdef HAS_KINETISK_UART0_FIFO
  94. #define C2_ENABLE UART_C2_TE | UART_C2_RE | UART_C2_RIE | UART_C2_ILIE
  95. #else
  96. #define C2_ENABLE UART_C2_TE | UART_C2_RE | UART_C2_RIE
  97. #endif
  98. #define C2_TX_ACTIVE C2_ENABLE | UART_C2_TIE
  99. #define C2_TX_COMPLETING C2_ENABLE | UART_C2_TCIE
  100. #define C2_TX_INACTIVE C2_ENABLE
  101. void serial_begin(uint32_t divisor)
  102. {
  103. SIM_SCGC4 |= SIM_SCGC4_UART0; // turn on clock, TODO: use bitband
  104. rx_buffer_head = 0;
  105. rx_buffer_tail = 0;
  106. tx_buffer_head = 0;
  107. tx_buffer_tail = 0;
  108. transmitting = 0;
  109. switch (rx_pin_num) {
  110. case 0: CORE_PIN0_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  111. case 21: CORE_PIN21_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  112. #if defined(KINETISL)
  113. case 3: CORE_PIN3_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(2); break;
  114. case 25: CORE_PIN25_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(4); break;
  115. #endif
  116. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  117. case 27: CORE_PIN27_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 1: CORE_PIN1_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); break;
  122. case 5: CORE_PIN5_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); break;
  123. #if defined(KINETISL)
  124. case 4: CORE_PIN4_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(2); break;
  125. case 24: CORE_PIN24_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(4); break;
  126. #endif
  127. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  128. case 26: CORE_PIN26_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); break;
  129. #endif
  130. }
  131. #if defined(HAS_KINETISK_UART0)
  132. if (divisor < 32) divisor = 32;
  133. UART0_BDH = (divisor >> 13) & 0x1F;
  134. UART0_BDL = (divisor >> 5) & 0xFF;
  135. UART0_C4 = divisor & 0x1F;
  136. #ifdef HAS_KINETISK_UART0_FIFO
  137. UART0_C1 = UART_C1_ILT;
  138. UART0_TWFIFO = 2; // tx watermark, causes S1_TDRE to set
  139. UART0_RWFIFO = 4; // rx watermark, causes S1_RDRF to set
  140. UART0_PFIFO = UART_PFIFO_TXFE | UART_PFIFO_RXFE;
  141. #else
  142. UART0_C1 = 0;
  143. UART0_PFIFO = 0;
  144. #endif
  145. #elif defined(HAS_KINETISL_UART0)
  146. if (divisor < 1) divisor = 1;
  147. UART0_BDH = (divisor >> 8) & 0x1F;
  148. UART0_BDL = divisor & 0xFF;
  149. UART0_C1 = 0;
  150. #endif
  151. UART0_C2 = C2_TX_INACTIVE;
  152. NVIC_SET_PRIORITY(IRQ_UART0_STATUS, IRQ_PRIORITY);
  153. NVIC_ENABLE_IRQ(IRQ_UART0_STATUS);
  154. }
  155. void serial_format(uint32_t format)
  156. {
  157. uint8_t c;
  158. c = UART0_C1;
  159. c = (c & ~0x13) | (format & 0x03); // configure parity
  160. if (format & 0x04) c |= 0x10; // 9 bits (might include parity)
  161. UART0_C1 = c;
  162. if ((format & 0x0F) == 0x04) UART0_C3 |= 0x40; // 8N2 is 9 bit with 9th bit always 1
  163. c = UART0_S2 & ~0x10;
  164. if (format & 0x10) c |= 0x10; // rx invert
  165. UART0_S2 = c;
  166. c = UART0_C3 & ~0x10;
  167. if (format & 0x20) c |= 0x10; // tx invert
  168. UART0_C3 = c;
  169. #ifdef SERIAL_9BIT_SUPPORT
  170. c = UART0_C4 & 0x1F;
  171. if (format & 0x08) c |= 0x20; // 9 bit mode with parity (requires 10 bits)
  172. UART0_C4 = c;
  173. use9Bits = format & 0x80;
  174. #endif
  175. #if defined(__MK64FX512__) || defined(__MK66FX1M0__) || defined(KINETISL)
  176. // For T3.5/T3.6/TLC See about turning on 2 stop bit mode
  177. if ( format & 0x100) {
  178. uint8_t bdl = UART0_BDL;
  179. UART0_BDH |= UART_BDH_SBNS; // Turn on 2 stop bits - was turned off by set baud
  180. UART0_BDL = bdl; // Says BDH not acted on until BDL is written
  181. }
  182. #endif
  183. }
  184. void serial_end(void)
  185. {
  186. if (!(SIM_SCGC4 & SIM_SCGC4_UART0)) return;
  187. while (transmitting) yield(); // wait for buffered data to send
  188. NVIC_DISABLE_IRQ(IRQ_UART0_STATUS);
  189. UART0_C2 = 0;
  190. switch (rx_pin_num) {
  191. case 0: CORE_PIN0_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break;
  192. case 21: CORE_PIN21_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break;
  193. #if defined(KINETISL)
  194. case 3: CORE_PIN3_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break;
  195. case 25: CORE_PIN25_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break;
  196. #endif
  197. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  198. case 27: CORE_PIN27_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break;
  199. #endif
  200. }
  201. switch (tx_pin_num & 127) {
  202. case 1: CORE_PIN1_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break;
  203. case 5: CORE_PIN5_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break;
  204. #if defined(KINETISL)
  205. case 4: CORE_PIN4_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break;
  206. case 24: CORE_PIN24_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break;
  207. #endif
  208. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  209. case 26: CORE_PIN26_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break;
  210. #endif
  211. }
  212. UART0_S1;
  213. UART0_D; // clear leftover error status
  214. rx_buffer_head = 0;
  215. rx_buffer_tail = 0;
  216. if (rts_pin) rts_deassert();
  217. }
  218. void serial_set_transmit_pin(uint8_t pin)
  219. {
  220. while (transmitting) ;
  221. pinMode(pin, OUTPUT);
  222. digitalWrite(pin, LOW);
  223. transmit_pin = portOutputRegister(pin);
  224. #if defined(KINETISL)
  225. transmit_mask = digitalPinToBitMask(pin);
  226. #endif
  227. }
  228. void serial_set_tx(uint8_t pin, uint8_t opendrain)
  229. {
  230. uint32_t cfg;
  231. if (opendrain) pin |= 128;
  232. if (pin == tx_pin_num) return;
  233. if ((SIM_SCGC4 & SIM_SCGC4_UART0)) {
  234. switch (tx_pin_num & 127) {
  235. case 1: CORE_PIN1_CONFIG = 0; break; // PTB17
  236. case 5: CORE_PIN5_CONFIG = 0; break; // PTD7
  237. #if defined(KINETISL)
  238. case 4: CORE_PIN4_CONFIG = 0; break; // PTA2
  239. case 24: CORE_PIN24_CONFIG = 0; break; // PTE20
  240. #endif
  241. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  242. case 26: CORE_PIN26_CONFIG = 0; break; //PTA14
  243. #endif
  244. }
  245. if (opendrain) {
  246. cfg = PORT_PCR_DSE | PORT_PCR_ODE;
  247. } else {
  248. cfg = PORT_PCR_DSE | PORT_PCR_SRE;
  249. }
  250. switch (pin & 127) {
  251. case 1: CORE_PIN1_CONFIG = cfg | PORT_PCR_MUX(3); break;
  252. case 5: CORE_PIN5_CONFIG = cfg | PORT_PCR_MUX(3); break;
  253. #if defined(KINETISL)
  254. case 4: CORE_PIN4_CONFIG = cfg | PORT_PCR_MUX(2); break;
  255. case 24: CORE_PIN24_CONFIG = cfg | PORT_PCR_MUX(4); break;
  256. #endif
  257. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  258. case 26: CORE_PIN26_CONFIG = cfg | PORT_PCR_MUX(3); break;
  259. #endif
  260. }
  261. }
  262. tx_pin_num = pin;
  263. }
  264. void serial_set_rx(uint8_t pin)
  265. {
  266. if (pin == rx_pin_num) return;
  267. if ((SIM_SCGC4 & SIM_SCGC4_UART0)) {
  268. switch (rx_pin_num) {
  269. case 0: CORE_PIN0_CONFIG = 0; break; // PTB16
  270. case 21: CORE_PIN21_CONFIG = 0; break; // PTD6
  271. #if defined(KINETISL)
  272. case 3: CORE_PIN3_CONFIG = 0; break; // PTA1
  273. case 25: CORE_PIN25_CONFIG = 0; break; // PTE21
  274. #endif
  275. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  276. case 27: CORE_PIN27_CONFIG = 0; break; // PTA15
  277. #endif
  278. }
  279. switch (pin) {
  280. case 0: CORE_PIN0_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  281. case 21: CORE_PIN21_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  282. #if defined(KINETISL)
  283. case 3: CORE_PIN3_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(2); break;
  284. case 25: CORE_PIN25_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(4); break;
  285. #endif
  286. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  287. case 27: CORE_PIN27_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  288. #endif
  289. }
  290. }
  291. rx_pin_num = pin;
  292. }
  293. int serial_set_rts(uint8_t pin)
  294. {
  295. if (!(SIM_SCGC4 & SIM_SCGC4_UART0)) return 0;
  296. if (pin < CORE_NUM_DIGITAL) {
  297. rts_pin = portOutputRegister(pin);
  298. #if defined(KINETISL)
  299. rts_mask = digitalPinToBitMask(pin);
  300. #endif
  301. pinMode(pin, OUTPUT);
  302. rts_assert();
  303. } else {
  304. rts_pin = NULL;
  305. return 0;
  306. }
  307. /*
  308. if (pin == 6) {
  309. CORE_PIN6_CONFIG = PORT_PCR_MUX(3);
  310. } else if (pin == 19) {
  311. CORE_PIN19_CONFIG = PORT_PCR_MUX(3);
  312. } else {
  313. UART0_MODEM &= ~UART_MODEM_RXRTSE;
  314. return 0;
  315. }
  316. UART0_MODEM |= UART_MODEM_RXRTSE;
  317. */
  318. return 1;
  319. }
  320. int serial_set_cts(uint8_t pin)
  321. {
  322. #if defined(KINETISK)
  323. if (!(SIM_SCGC4 & SIM_SCGC4_UART0)) return 0;
  324. if (pin == 18) {
  325. CORE_PIN18_CONFIG = PORT_PCR_MUX(3) | PORT_PCR_PE; // weak pulldown
  326. } else if (pin == 20) {
  327. CORE_PIN20_CONFIG = PORT_PCR_MUX(3) | PORT_PCR_PE; // weak pulldown
  328. } else {
  329. UART0_MODEM &= ~UART_MODEM_TXCTSE;
  330. return 0;
  331. }
  332. UART0_MODEM |= UART_MODEM_TXCTSE;
  333. return 1;
  334. #else
  335. return 0;
  336. #endif
  337. }
  338. void serial_putchar(uint32_t c)
  339. {
  340. uint32_t head, n;
  341. if (!(SIM_SCGC4 & SIM_SCGC4_UART0)) return;
  342. if (transmit_pin) transmit_assert();
  343. head = tx_buffer_head;
  344. if (++head >= SERIAL1_TX_BUFFER_SIZE) head = 0;
  345. while (tx_buffer_tail == head) {
  346. int priority = nvic_execution_priority();
  347. if (priority <= IRQ_PRIORITY) {
  348. if ((UART0_S1 & UART_S1_TDRE)) {
  349. uint32_t tail = tx_buffer_tail;
  350. if (++tail >= SERIAL1_TX_BUFFER_SIZE) tail = 0;
  351. n = tx_buffer[tail];
  352. if (use9Bits) UART0_C3 = (UART0_C3 & ~0x40) | ((n & 0x100) >> 2);
  353. UART0_D = n;
  354. tx_buffer_tail = tail;
  355. }
  356. } else if (priority >= 256) {
  357. yield();
  358. }
  359. }
  360. tx_buffer[head] = c;
  361. transmitting = 1;
  362. tx_buffer_head = head;
  363. UART0_C2 = C2_TX_ACTIVE;
  364. }
  365. #ifdef HAS_KINETISK_UART0_FIFO
  366. void serial_write(const void *buf, unsigned int count)
  367. {
  368. const uint8_t *p = (const uint8_t *)buf;
  369. const uint8_t *end = p + count;
  370. uint32_t head, n;
  371. if (!(SIM_SCGC4 & SIM_SCGC4_UART0)) return;
  372. if (transmit_pin) transmit_assert();
  373. while (p < end) {
  374. head = tx_buffer_head;
  375. if (++head >= SERIAL1_TX_BUFFER_SIZE) head = 0;
  376. if (tx_buffer_tail == head) {
  377. UART0_C2 = C2_TX_ACTIVE;
  378. do {
  379. int priority = nvic_execution_priority();
  380. if (priority <= IRQ_PRIORITY) {
  381. if ((UART0_S1 & UART_S1_TDRE)) {
  382. uint32_t tail = tx_buffer_tail;
  383. if (++tail >= SERIAL1_TX_BUFFER_SIZE) tail = 0;
  384. n = tx_buffer[tail];
  385. if (use9Bits) UART0_C3 = (UART0_C3 & ~0x40) | ((n & 0x100) >> 2);
  386. UART0_D = n;
  387. tx_buffer_tail = tail;
  388. }
  389. } else if (priority >= 256) {
  390. yield();
  391. }
  392. } while (tx_buffer_tail == head);
  393. }
  394. tx_buffer[head] = *p++;
  395. transmitting = 1;
  396. tx_buffer_head = head;
  397. }
  398. UART0_C2 = C2_TX_ACTIVE;
  399. }
  400. #else
  401. void serial_write(const void *buf, unsigned int count)
  402. {
  403. const uint8_t *p = (const uint8_t *)buf;
  404. while (count-- > 0) serial_putchar(*p++);
  405. }
  406. #endif
  407. void serial_flush(void)
  408. {
  409. while (transmitting) yield(); // wait
  410. }
  411. int serial_write_buffer_free(void)
  412. {
  413. uint32_t head, tail;
  414. head = tx_buffer_head;
  415. tail = tx_buffer_tail;
  416. if (head >= tail) return SERIAL1_TX_BUFFER_SIZE - 1 - head + tail;
  417. return tail - head - 1;
  418. }
  419. int serial_available(void)
  420. {
  421. uint32_t head, tail;
  422. head = rx_buffer_head;
  423. tail = rx_buffer_tail;
  424. if (head >= tail) return head - tail;
  425. return SERIAL1_RX_BUFFER_SIZE + head - tail;
  426. }
  427. int serial_getchar(void)
  428. {
  429. uint32_t head, tail;
  430. int c;
  431. head = rx_buffer_head;
  432. tail = rx_buffer_tail;
  433. if (head == tail) return -1;
  434. if (++tail >= SERIAL1_RX_BUFFER_SIZE) tail = 0;
  435. c = rx_buffer[tail];
  436. rx_buffer_tail = tail;
  437. if (rts_pin) {
  438. int avail;
  439. if (head >= tail) avail = head - tail;
  440. else avail = SERIAL1_RX_BUFFER_SIZE + head - tail;
  441. if (avail <= RTS_LOW_WATERMARK) rts_assert();
  442. }
  443. return c;
  444. }
  445. int serial_peek(void)
  446. {
  447. uint32_t head, tail;
  448. head = rx_buffer_head;
  449. tail = rx_buffer_tail;
  450. if (head == tail) return -1;
  451. if (++tail >= SERIAL1_RX_BUFFER_SIZE) tail = 0;
  452. return rx_buffer[tail];
  453. }
  454. void serial_clear(void)
  455. {
  456. #ifdef HAS_KINETISK_UART0_FIFO
  457. if (!(SIM_SCGC4 & SIM_SCGC4_UART0)) return;
  458. UART0_C2 &= ~(UART_C2_RE | UART_C2_RIE | UART_C2_ILIE);
  459. UART0_CFIFO = UART_CFIFO_RXFLUSH;
  460. UART0_C2 |= (UART_C2_RE | UART_C2_RIE | UART_C2_ILIE);
  461. #endif
  462. rx_buffer_head = rx_buffer_tail;
  463. if (rts_pin) rts_assert();
  464. }
  465. // status interrupt combines
  466. // Transmit data below watermark UART_S1_TDRE
  467. // Transmit complete UART_S1_TC
  468. // Idle line UART_S1_IDLE
  469. // Receive data above watermark UART_S1_RDRF
  470. // LIN break detect UART_S2_LBKDIF
  471. // RxD pin active edge UART_S2_RXEDGIF
  472. void uart0_status_isr(void)
  473. {
  474. uint32_t head, tail, n;
  475. uint8_t c;
  476. #ifdef HAS_KINETISK_UART0_FIFO
  477. uint32_t newhead;
  478. uint8_t avail;
  479. if (UART0_S1 & (UART_S1_RDRF | UART_S1_IDLE)) {
  480. __disable_irq();
  481. avail = UART0_RCFIFO;
  482. if (avail == 0) {
  483. // The only way to clear the IDLE interrupt flag is
  484. // to read the data register. But reading with no
  485. // data causes a FIFO underrun, which causes the
  486. // FIFO to return corrupted data. If anyone from
  487. // Freescale reads this, what a poor design! There
  488. // write should be a write-1-to-clear for IDLE.
  489. c = UART0_D;
  490. // flushing the fifo recovers from the underrun,
  491. // but there's a possible race condition where a
  492. // new character could be received between reading
  493. // RCFIFO == 0 and flushing the FIFO. To minimize
  494. // the chance, interrupts are disabled so a higher
  495. // priority interrupt (hopefully) doesn't delay.
  496. // TODO: change this to disabling the IDLE interrupt
  497. // which won't be simple, since we already manage
  498. // which transmit interrupts are enabled.
  499. UART0_CFIFO = UART_CFIFO_RXFLUSH;
  500. __enable_irq();
  501. } else {
  502. __enable_irq();
  503. head = rx_buffer_head;
  504. tail = rx_buffer_tail;
  505. do {
  506. if (use9Bits && (UART0_C3 & 0x80)) {
  507. n = UART0_D | 0x100;
  508. } else {
  509. n = UART0_D;
  510. }
  511. newhead = head + 1;
  512. if (newhead >= SERIAL1_RX_BUFFER_SIZE) newhead = 0;
  513. if (newhead != tail) {
  514. head = newhead;
  515. rx_buffer[head] = n;
  516. }
  517. } while (--avail > 0);
  518. rx_buffer_head = head;
  519. if (rts_pin) {
  520. int avail;
  521. if (head >= tail) avail = head - tail;
  522. else avail = SERIAL1_RX_BUFFER_SIZE + head - tail;
  523. if (avail >= RTS_HIGH_WATERMARK) rts_deassert();
  524. }
  525. }
  526. }
  527. c = UART0_C2;
  528. if ((c & UART_C2_TIE) && (UART0_S1 & UART_S1_TDRE)) {
  529. head = tx_buffer_head;
  530. tail = tx_buffer_tail;
  531. do {
  532. if (tail == head) break;
  533. if (++tail >= SERIAL1_TX_BUFFER_SIZE) tail = 0;
  534. avail = UART0_S1;
  535. n = tx_buffer[tail];
  536. if (use9Bits) UART0_C3 = (UART0_C3 & ~0x40) | ((n & 0x100) >> 2);
  537. UART0_D = n;
  538. } while (UART0_TCFIFO < 8);
  539. tx_buffer_tail = tail;
  540. if (UART0_S1 & UART_S1_TDRE) UART0_C2 = C2_TX_COMPLETING;
  541. }
  542. #else
  543. if (UART0_S1 & UART_S1_RDRF) {
  544. if (use9Bits && (UART0_C3 & 0x80)) {
  545. n = UART0_D | 0x100;
  546. } else {
  547. n = UART0_D;
  548. }
  549. head = rx_buffer_head + 1;
  550. if (head >= SERIAL1_RX_BUFFER_SIZE) head = 0;
  551. if (head != rx_buffer_tail) {
  552. rx_buffer[head] = n;
  553. rx_buffer_head = head;
  554. }
  555. }
  556. c = UART0_C2;
  557. if ((c & UART_C2_TIE) && (UART0_S1 & UART_S1_TDRE)) {
  558. head = tx_buffer_head;
  559. tail = tx_buffer_tail;
  560. if (head == tail) {
  561. UART0_C2 = C2_TX_COMPLETING;
  562. } else {
  563. if (++tail >= SERIAL1_TX_BUFFER_SIZE) tail = 0;
  564. n = tx_buffer[tail];
  565. if (use9Bits) UART0_C3 = (UART0_C3 & ~0x40) | ((n & 0x100) >> 2);
  566. UART0_D = n;
  567. tx_buffer_tail = tail;
  568. }
  569. }
  570. #endif
  571. if ((c & UART_C2_TCIE) && (UART0_S1 & UART_S1_TC)) {
  572. transmitting = 0;
  573. if (transmit_pin) transmit_deassert();
  574. UART0_C2 = C2_TX_INACTIVE;
  575. }
  576. }
  577. void serial_print(const char *p)
  578. {
  579. while (*p) {
  580. char c = *p++;
  581. if (c == '\n') serial_putchar('\r');
  582. serial_putchar(c);
  583. }
  584. }
  585. static void serial_phex1(uint32_t n)
  586. {
  587. n &= 15;
  588. if (n < 10) {
  589. serial_putchar('0' + n);
  590. } else {
  591. serial_putchar('A' - 10 + n);
  592. }
  593. }
  594. void serial_phex(uint32_t n)
  595. {
  596. serial_phex1(n >> 4);
  597. serial_phex1(n);
  598. }
  599. void serial_phex16(uint32_t n)
  600. {
  601. serial_phex(n >> 8);
  602. serial_phex(n);
  603. }
  604. void serial_phex32(uint32_t n)
  605. {
  606. serial_phex(n >> 24);
  607. serial_phex(n >> 16);
  608. serial_phex(n >> 8);
  609. serial_phex(n);
  610. }