您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

7 年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. #ifdef HAS_KINETISK_UART3
  34. ////////////////////////////////////////////////////////////////
  35. // Tunable parameters (relatively safe to edit these numbers)
  36. ////////////////////////////////////////////////////////////////
  37. #ifndef SERIAL4_TX_BUFFER_SIZE
  38. #define SERIAL4_TX_BUFFER_SIZE 40 // number of outgoing bytes to buffer
  39. #endif
  40. #ifndef SERIAL4_RX_BUFFER_SIZE
  41. #define SERIAL4_RX_BUFFER_SIZE 64 // number of incoming bytes to buffer
  42. #endif
  43. #define RTS_HIGH_WATERMARK (SERIAL4_RX_BUFFER_SIZE-24) // RTS requests sender to pause
  44. #define RTS_LOW_WATERMARK (SERIAL4_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[SERIAL4_TX_BUFFER_SIZE];
  57. static volatile BUFTYPE rx_buffer[SERIAL4_RX_BUFFER_SIZE];
  58. static volatile uint8_t transmitting = 0;
  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. #if SERIAL4_TX_BUFFER_SIZE > 65535
  66. static volatile uint32_t tx_buffer_head = 0;
  67. static volatile uint32_t tx_buffer_tail = 0;
  68. #elif SERIAL4_TX_BUFFER_SIZE > 255
  69. static volatile uint16_t tx_buffer_head = 0;
  70. static volatile uint16_t tx_buffer_tail = 0;
  71. #else
  72. static volatile uint8_t tx_buffer_head = 0;
  73. static volatile uint8_t tx_buffer_tail = 0;
  74. #endif
  75. #if SERIAL4_RX_BUFFER_SIZE > 65535
  76. static volatile uint32_t rx_buffer_head = 0;
  77. static volatile uint32_t rx_buffer_tail = 0;
  78. #elif SERIAL4_RX_BUFFER_SIZE > 255
  79. static volatile uint16_t rx_buffer_head = 0;
  80. static volatile uint16_t rx_buffer_tail = 0;
  81. #else
  82. static volatile uint8_t rx_buffer_head = 0;
  83. static volatile uint8_t rx_buffer_tail = 0;
  84. #endif
  85. static uint8_t rx_pin_num = 31;
  86. static uint8_t tx_pin_num = 32;
  87. // UART0 and UART1 are clocked by F_CPU, UART2 is clocked by F_BUS
  88. // UART0 has 8 byte fifo, UART1 and UART2 have 1 byte buffer
  89. #define C2_ENABLE UART_C2_TE | UART_C2_RE | UART_C2_RIE
  90. #define C2_TX_ACTIVE C2_ENABLE | UART_C2_TIE
  91. #define C2_TX_COMPLETING C2_ENABLE | UART_C2_TCIE
  92. #define C2_TX_INACTIVE C2_ENABLE
  93. void serial4_begin(uint32_t divisor)
  94. {
  95. SIM_SCGC4 |= SIM_SCGC4_UART3; // turn on clock, TODO: use bitband
  96. rx_buffer_head = 0;
  97. rx_buffer_tail = 0;
  98. tx_buffer_head = 0;
  99. tx_buffer_tail = 0;
  100. transmitting = 0;
  101. switch (rx_pin_num) {
  102. case 31: CORE_PIN31_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  103. case 63: CORE_PIN63_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  104. }
  105. switch (tx_pin_num) {
  106. case 32: CORE_PIN32_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); break;
  107. case 62: CORE_PIN62_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); break;
  108. }
  109. if (divisor < 32) divisor = 32;
  110. UART3_BDH = (divisor >> 13) & 0x1F;
  111. UART3_BDL = (divisor >> 5) & 0xFF;
  112. UART3_C4 = divisor & 0x1F;
  113. UART3_C1 = 0;
  114. UART3_PFIFO = 0;
  115. UART3_C2 = C2_TX_INACTIVE;
  116. NVIC_SET_PRIORITY(IRQ_UART3_STATUS, IRQ_PRIORITY);
  117. NVIC_ENABLE_IRQ(IRQ_UART3_STATUS);
  118. }
  119. void serial4_format(uint32_t format)
  120. {
  121. uint8_t c;
  122. c = UART3_C1;
  123. c = (c & ~0x13) | (format & 0x03); // configure parity
  124. if (format & 0x04) c |= 0x10; // 9 bits (might include parity)
  125. UART3_C1 = c;
  126. if ((format & 0x0F) == 0x04) UART3_C3 |= 0x40; // 8N2 is 9 bit with 9th bit always 1
  127. c = UART3_S2 & ~0x10;
  128. if (format & 0x10) c |= 0x10; // rx invert
  129. UART3_S2 = c;
  130. c = UART3_C3 & ~0x10;
  131. if (format & 0x20) c |= 0x10; // tx invert
  132. UART3_C3 = c;
  133. #ifdef SERIAL_9BIT_SUPPORT
  134. c = UART3_C4 & 0x1F;
  135. if (format & 0x08) c |= 0x20; // 9 bit mode with parity (requires 10 bits)
  136. UART3_C4 = c;
  137. use9Bits = format & 0x80;
  138. #endif
  139. #if defined(__MK64FX512__) || defined(__MK66FX1M0__) || defined(KINETISL)
  140. // For T3.5/T3.6/TLC See about turning on 2 stop bit mode
  141. if ( format & 0x100) {
  142. uint8_t bdl = UART3_BDL;
  143. UART3_BDH |= UART_BDH_SBNS; // Turn on 2 stop bits - was turned off by set baud
  144. UART3_BDL = bdl; // Says BDH not acted on until BDL is written
  145. }
  146. #endif
  147. }
  148. void serial4_end(void)
  149. {
  150. if (!(SIM_SCGC4 & SIM_SCGC4_UART3)) return;
  151. while (transmitting) yield(); // wait for buffered data to send
  152. NVIC_DISABLE_IRQ(IRQ_UART3_STATUS);
  153. UART3_C2 = 0;
  154. switch (rx_pin_num) {
  155. case 31: CORE_PIN31_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break; // PTC3
  156. case 63: CORE_PIN63_CONFIG = 0; break;
  157. }
  158. switch (tx_pin_num & 127) {
  159. case 32: CORE_PIN32_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break; // PTC4
  160. case 62: CORE_PIN62_CONFIG = 0; break;
  161. }
  162. UART3_S1;
  163. UART3_D; // clear leftover error status
  164. rx_buffer_head = 0;
  165. rx_buffer_tail = 0;
  166. if (rts_pin) rts_deassert();
  167. }
  168. void serial4_set_transmit_pin(uint8_t pin)
  169. {
  170. while (transmitting) ;
  171. pinMode(pin, OUTPUT);
  172. digitalWrite(pin, LOW);
  173. transmit_pin = portOutputRegister(pin);
  174. }
  175. void serial4_set_tx(uint8_t pin, uint8_t opendrain)
  176. {
  177. uint32_t cfg;
  178. if (opendrain) pin |= 128;
  179. if (pin == tx_pin_num) return;
  180. if ((SIM_SCGC4 & SIM_SCGC4_UART3)) {
  181. switch (tx_pin_num & 127) {
  182. case 32: CORE_PIN32_CONFIG = 0; break; // PTB11
  183. case 62: CORE_PIN62_CONFIG = 0; break;
  184. }
  185. if (opendrain) {
  186. cfg = PORT_PCR_DSE | PORT_PCR_ODE;
  187. } else {
  188. cfg = PORT_PCR_DSE | PORT_PCR_SRE;
  189. }
  190. switch (pin & 127) {
  191. case 32: CORE_PIN32_CONFIG = cfg | PORT_PCR_MUX(3); break;
  192. case 62: CORE_PIN62_CONFIG = cfg | PORT_PCR_MUX(3); break;
  193. }
  194. }
  195. tx_pin_num = pin;
  196. }
  197. void serial4_set_rx(uint8_t pin)
  198. {
  199. if (pin == rx_pin_num) return;
  200. if ((SIM_SCGC4 & SIM_SCGC4_UART3)) {
  201. switch (rx_pin_num) {
  202. case 31: CORE_PIN31_CONFIG = 0; break; // PTC3
  203. case 63: CORE_PIN63_CONFIG = 0; break;
  204. }
  205. switch (pin) {
  206. case 31: CORE_PIN31_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  207. case 63: CORE_PIN63_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  208. }
  209. }
  210. rx_pin_num = pin;
  211. }
  212. int serial4_set_rts(uint8_t pin)
  213. {
  214. if (!(SIM_SCGC4 & SIM_SCGC4_UART3)) return 0;
  215. if (pin < CORE_NUM_DIGITAL) {
  216. rts_pin = portOutputRegister(pin);
  217. pinMode(pin, OUTPUT);
  218. rts_assert();
  219. } else {
  220. rts_pin = NULL;
  221. return 0;
  222. }
  223. return 1;
  224. }
  225. int serial4_set_cts(uint8_t pin)
  226. {
  227. return 0;
  228. }
  229. void serial4_putchar(uint32_t c)
  230. {
  231. uint32_t head, n;
  232. if (!(SIM_SCGC4 & SIM_SCGC4_UART3)) return;
  233. if (transmit_pin) transmit_assert();
  234. head = tx_buffer_head;
  235. if (++head >= SERIAL4_TX_BUFFER_SIZE) head = 0;
  236. while (tx_buffer_tail == head) {
  237. int priority = nvic_execution_priority();
  238. if (priority <= IRQ_PRIORITY) {
  239. if ((UART3_S1 & UART_S1_TDRE)) {
  240. uint32_t tail = tx_buffer_tail;
  241. if (++tail >= SERIAL4_TX_BUFFER_SIZE) tail = 0;
  242. n = tx_buffer[tail];
  243. if (use9Bits) UART3_C3 = (UART3_C3 & ~0x40) | ((n & 0x100) >> 2);
  244. UART3_D = n;
  245. tx_buffer_tail = tail;
  246. }
  247. } else if (priority >= 256) {
  248. yield(); // wait
  249. }
  250. }
  251. tx_buffer[head] = c;
  252. transmitting = 1;
  253. tx_buffer_head = head;
  254. UART3_C2 = C2_TX_ACTIVE;
  255. }
  256. void serial4_write(const void *buf, unsigned int count)
  257. {
  258. const uint8_t *p = (const uint8_t *)buf;
  259. while (count-- > 0) serial4_putchar(*p++);
  260. }
  261. void serial4_flush(void)
  262. {
  263. while (transmitting) yield(); // wait
  264. }
  265. int serial4_write_buffer_free(void)
  266. {
  267. uint32_t head, tail;
  268. head = tx_buffer_head;
  269. tail = tx_buffer_tail;
  270. if (head >= tail) return SERIAL4_TX_BUFFER_SIZE - 1 - head + tail;
  271. return tail - head - 1;
  272. }
  273. int serial4_available(void)
  274. {
  275. uint32_t head, tail;
  276. head = rx_buffer_head;
  277. tail = rx_buffer_tail;
  278. if (head >= tail) return head - tail;
  279. return SERIAL4_RX_BUFFER_SIZE + head - tail;
  280. }
  281. int serial4_getchar(void)
  282. {
  283. uint32_t head, tail;
  284. int c;
  285. head = rx_buffer_head;
  286. tail = rx_buffer_tail;
  287. if (head == tail) return -1;
  288. if (++tail >= SERIAL4_RX_BUFFER_SIZE) tail = 0;
  289. c = rx_buffer[tail];
  290. rx_buffer_tail = tail;
  291. if (rts_pin) {
  292. int avail;
  293. if (head >= tail) avail = head - tail;
  294. else avail = SERIAL4_RX_BUFFER_SIZE + head - tail;
  295. if (avail <= RTS_LOW_WATERMARK) rts_assert();
  296. }
  297. return c;
  298. }
  299. int serial4_peek(void)
  300. {
  301. uint32_t head, tail;
  302. head = rx_buffer_head;
  303. tail = rx_buffer_tail;
  304. if (head == tail) return -1;
  305. if (++tail >= SERIAL4_RX_BUFFER_SIZE) tail = 0;
  306. return rx_buffer[tail];
  307. }
  308. void serial4_clear(void)
  309. {
  310. rx_buffer_head = rx_buffer_tail;
  311. if (rts_pin) rts_assert();
  312. }
  313. // status interrupt combines
  314. // Transmit data below watermark UART_S1_TDRE
  315. // Transmit complete UART_S1_TC
  316. // Idle line UART_S1_IDLE
  317. // Receive data above watermark UART_S1_RDRF
  318. // LIN break detect UART_S2_LBKDIF
  319. // RxD pin active edge UART_S2_RXEDGIF
  320. void uart3_status_isr(void)
  321. {
  322. uint32_t head, tail, n;
  323. uint8_t c;
  324. if (UART3_S1 & UART_S1_RDRF) {
  325. if (use9Bits && (UART3_C3 & 0x80)) {
  326. n = UART3_D | 0x100;
  327. } else {
  328. n = UART3_D;
  329. }
  330. head = rx_buffer_head + 1;
  331. if (head >= SERIAL4_RX_BUFFER_SIZE) head = 0;
  332. if (head != rx_buffer_tail) {
  333. rx_buffer[head] = n;
  334. rx_buffer_head = head;
  335. }
  336. if (rts_pin) {
  337. int avail;
  338. tail = tx_buffer_tail;
  339. if (head >= tail) avail = head - tail;
  340. else avail = SERIAL4_RX_BUFFER_SIZE + head - tail;
  341. if (avail >= RTS_HIGH_WATERMARK) rts_deassert();
  342. }
  343. }
  344. c = UART3_C2;
  345. if ((c & UART_C2_TIE) && (UART3_S1 & UART_S1_TDRE)) {
  346. head = tx_buffer_head;
  347. tail = tx_buffer_tail;
  348. if (head == tail) {
  349. UART3_C2 = C2_TX_COMPLETING;
  350. } else {
  351. if (++tail >= SERIAL4_TX_BUFFER_SIZE) tail = 0;
  352. n = tx_buffer[tail];
  353. if (use9Bits) UART3_C3 = (UART3_C3 & ~0x40) | ((n & 0x100) >> 2);
  354. UART3_D = n;
  355. tx_buffer_tail = tail;
  356. }
  357. }
  358. if ((c & UART_C2_TCIE) && (UART3_S1 & UART_S1_TC)) {
  359. transmitting = 0;
  360. if (transmit_pin) transmit_deassert();
  361. UART3_C2 = C2_TX_INACTIVE;
  362. }
  363. }
  364. #endif // HAS_KINETISK_UART3