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.

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