Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

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