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.

преди 7 години
преди 7 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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_UART5
  34. ////////////////////////////////////////////////////////////////
  35. // Tunable parameters (relatively safe to edit these numbers)
  36. ////////////////////////////////////////////////////////////////
  37. #ifndef SERIAL6_TX_BUFFER_SIZE
  38. #define SERIAL6_TX_BUFFER_SIZE 40 // number of outgoing bytes to buffer
  39. #endif
  40. #ifndef SERIAL6_RX_BUFFER_SIZE
  41. #define SERIAL6_RX_BUFFER_SIZE 64 // number of incoming bytes to buffer
  42. #endif
  43. #define RTS_HIGH_WATERMARK (SERIAL6_RX_BUFFER_SIZE-24) // RTS requests sender to pause
  44. #define RTS_LOW_WATERMARK (SERIAL6_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[SERIAL6_TX_BUFFER_SIZE];
  57. static volatile BUFTYPE rx_buffer[SERIAL6_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 SERIAL6_TX_BUFFER_SIZE > 255
  66. static volatile uint16_t tx_buffer_head = 0;
  67. static volatile uint16_t tx_buffer_tail = 0;
  68. #else
  69. static volatile uint8_t tx_buffer_head = 0;
  70. static volatile uint8_t tx_buffer_tail = 0;
  71. #endif
  72. #if SERIAL6_RX_BUFFER_SIZE > 255
  73. static volatile uint16_t rx_buffer_head = 0;
  74. static volatile uint16_t rx_buffer_tail = 0;
  75. #else
  76. static volatile uint8_t rx_buffer_head = 0;
  77. static volatile uint8_t rx_buffer_tail = 0;
  78. #endif
  79. static uint8_t tx_pin_num = 48;
  80. // UART0 and UART1 are clocked by F_CPU, UART2 is clocked by F_BUS
  81. // UART0 has 8 byte fifo, UART1 and UART2 have 1 byte buffer
  82. #define C2_ENABLE UART_C2_TE | UART_C2_RE | UART_C2_RIE
  83. #define C2_TX_ACTIVE C2_ENABLE | UART_C2_TIE
  84. #define C2_TX_COMPLETING C2_ENABLE | UART_C2_TCIE
  85. #define C2_TX_INACTIVE C2_ENABLE
  86. void serial6_begin(uint32_t divisor)
  87. {
  88. SIM_SCGC1 |= SIM_SCGC1_UART5; // turn on clock, TODO: use bitband
  89. rx_buffer_head = 0;
  90. rx_buffer_tail = 0;
  91. tx_buffer_head = 0;
  92. tx_buffer_tail = 0;
  93. transmitting = 0;
  94. CORE_PIN47_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3);
  95. CORE_PIN48_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);
  96. if (divisor < 32) divisor = 32;
  97. UART5_BDH = (divisor >> 13) & 0x1F;
  98. UART5_BDL = (divisor >> 5) & 0xFF;
  99. UART5_C4 = divisor & 0x1F;
  100. UART5_C1 = 0;
  101. UART5_PFIFO = 0;
  102. UART5_C2 = C2_TX_INACTIVE;
  103. NVIC_SET_PRIORITY(IRQ_UART5_STATUS, IRQ_PRIORITY);
  104. NVIC_ENABLE_IRQ(IRQ_UART5_STATUS);
  105. }
  106. void serial6_format(uint32_t format)
  107. {
  108. uint8_t c;
  109. c = UART5_C1;
  110. c = (c & ~0x13) | (format & 0x03); // configure parity
  111. if (format & 0x04) c |= 0x10; // 9 bits (might include parity)
  112. UART5_C1 = c;
  113. if ((format & 0x0F) == 0x04) UART5_C3 |= 0x40; // 8N2 is 9 bit with 9th bit always 1
  114. c = UART5_S2 & ~0x10;
  115. if (format & 0x10) c |= 0x10; // rx invert
  116. UART5_S2 = c;
  117. c = UART5_C3 & ~0x10;
  118. if (format & 0x20) c |= 0x10; // tx invert
  119. UART5_C3 = c;
  120. #ifdef SERIAL_9BIT_SUPPORT
  121. c = UART5_C4 & 0x1F;
  122. if (format & 0x08) c |= 0x20; // 9 bit mode with parity (requires 10 bits)
  123. UART5_C4 = c;
  124. use9Bits = format & 0x80;
  125. #endif
  126. // For T3.5 See about turning on 2 stop bit mode
  127. if ( format & 0x100) {
  128. uint8_t bdl = UART5_BDL;
  129. UART5_BDH |= UART_BDH_SBNS; // Turn on 2 stop bits - was turned off by set baud
  130. UART5_BDL = bdl; // Says BDH not acted on until BDL is written
  131. }
  132. }
  133. void serial6_end(void)
  134. {
  135. if (!(SIM_SCGC1 & SIM_SCGC1_UART5)) return;
  136. while (transmitting) yield(); // wait for buffered data to send
  137. NVIC_DISABLE_IRQ(IRQ_UART5_STATUS);
  138. UART5_C2 = 0;
  139. CORE_PIN47_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
  140. CORE_PIN48_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
  141. rx_buffer_head = 0;
  142. rx_buffer_tail = 0;
  143. if (rts_pin) rts_deassert();
  144. }
  145. void serial6_set_transmit_pin(uint8_t pin)
  146. {
  147. while (transmitting) ;
  148. pinMode(pin, OUTPUT);
  149. digitalWrite(pin, LOW);
  150. transmit_pin = portOutputRegister(pin);
  151. }
  152. void serial6_set_tx(uint8_t pin, uint8_t opendrain)
  153. {
  154. uint32_t cfg;
  155. if (opendrain) pin |= 128;
  156. if (pin == tx_pin_num) return;
  157. if ((SIM_SCGC1 & SIM_SCGC1_UART5)) {
  158. switch (tx_pin_num & 127) {
  159. case 48: CORE_PIN48_CONFIG = 0; break; // PTE24
  160. }
  161. if (opendrain) {
  162. cfg = PORT_PCR_DSE | PORT_PCR_ODE;
  163. } else {
  164. cfg = PORT_PCR_DSE | PORT_PCR_SRE;
  165. }
  166. switch (pin & 127) {
  167. case 48: CORE_PIN48_CONFIG = cfg | PORT_PCR_MUX(3); break;
  168. }
  169. }
  170. tx_pin_num = pin;
  171. }
  172. void serial6_set_rx(uint8_t pin)
  173. {
  174. }
  175. int serial6_set_rts(uint8_t pin)
  176. {
  177. if (!(SIM_SCGC1 & SIM_SCGC1_UART5)) return 0;
  178. if (pin < CORE_NUM_DIGITAL) {
  179. rts_pin = portOutputRegister(pin);
  180. pinMode(pin, OUTPUT);
  181. rts_assert();
  182. } else {
  183. rts_pin = NULL;
  184. return 0;
  185. }
  186. return 1;
  187. }
  188. int serial6_set_cts(uint8_t pin)
  189. {
  190. if (!(SIM_SCGC1 & SIM_SCGC1_UART5)) return 0;
  191. if (pin == 56) {
  192. CORE_PIN56_CONFIG = PORT_PCR_MUX(3) | PORT_PCR_PE; // weak pulldown
  193. } else {
  194. UART5_MODEM &= ~UART_MODEM_TXCTSE;
  195. return 0;
  196. }
  197. UART5_MODEM |= UART_MODEM_TXCTSE;
  198. return 1;
  199. }
  200. void serial6_putchar(uint32_t c)
  201. {
  202. uint32_t head, n;
  203. if (!(SIM_SCGC1 & SIM_SCGC1_UART5)) return;
  204. if (transmit_pin) transmit_assert();
  205. head = tx_buffer_head;
  206. if (++head >= SERIAL6_TX_BUFFER_SIZE) head = 0;
  207. while (tx_buffer_tail == head) {
  208. int priority = nvic_execution_priority();
  209. if (priority <= IRQ_PRIORITY) {
  210. if ((UART5_S1 & UART_S1_TDRE)) {
  211. uint32_t tail = tx_buffer_tail;
  212. if (++tail >= SERIAL6_TX_BUFFER_SIZE) tail = 0;
  213. n = tx_buffer[tail];
  214. if (use9Bits) UART5_C3 = (UART5_C3 & ~0x40) | ((n & 0x100) >> 2);
  215. UART5_D = n;
  216. tx_buffer_tail = tail;
  217. }
  218. } else if (priority >= 256) {
  219. yield(); // wait
  220. }
  221. }
  222. tx_buffer[head] = c;
  223. transmitting = 1;
  224. tx_buffer_head = head;
  225. UART5_C2 = C2_TX_ACTIVE;
  226. }
  227. void serial6_write(const void *buf, unsigned int count)
  228. {
  229. const uint8_t *p = (const uint8_t *)buf;
  230. while (count-- > 0) serial6_putchar(*p++);
  231. }
  232. void serial6_flush(void)
  233. {
  234. while (transmitting) yield(); // wait
  235. }
  236. int serial6_write_buffer_free(void)
  237. {
  238. uint32_t head, tail;
  239. head = tx_buffer_head;
  240. tail = tx_buffer_tail;
  241. if (head >= tail) return SERIAL6_TX_BUFFER_SIZE - 1 - head + tail;
  242. return tail - head - 1;
  243. }
  244. int serial6_available(void)
  245. {
  246. uint32_t head, tail;
  247. head = rx_buffer_head;
  248. tail = rx_buffer_tail;
  249. if (head >= tail) return head - tail;
  250. return SERIAL6_RX_BUFFER_SIZE + head - tail;
  251. }
  252. int serial6_getchar(void)
  253. {
  254. uint32_t head, tail;
  255. int c;
  256. head = rx_buffer_head;
  257. tail = rx_buffer_tail;
  258. if (head == tail) return -1;
  259. if (++tail >= SERIAL6_RX_BUFFER_SIZE) tail = 0;
  260. c = rx_buffer[tail];
  261. rx_buffer_tail = tail;
  262. if (rts_pin) {
  263. int avail;
  264. if (head >= tail) avail = head - tail;
  265. else avail = SERIAL6_RX_BUFFER_SIZE + head - tail;
  266. if (avail <= RTS_LOW_WATERMARK) rts_assert();
  267. }
  268. return c;
  269. }
  270. int serial6_peek(void)
  271. {
  272. uint32_t head, tail;
  273. head = rx_buffer_head;
  274. tail = rx_buffer_tail;
  275. if (head == tail) return -1;
  276. if (++tail >= SERIAL6_RX_BUFFER_SIZE) tail = 0;
  277. return rx_buffer[tail];
  278. }
  279. void serial6_clear(void)
  280. {
  281. rx_buffer_head = rx_buffer_tail;
  282. if (rts_pin) rts_assert();
  283. }
  284. // status interrupt combines
  285. // Transmit data below watermark UART_S1_TDRE
  286. // Transmit complete UART_S1_TC
  287. // Idle line UART_S1_IDLE
  288. // Receive data above watermark UART_S1_RDRF
  289. // LIN break detect UART_S2_LBKDIF
  290. // RxD pin active edge UART_S2_RXEDGIF
  291. void uart5_status_isr(void)
  292. {
  293. uint32_t head, tail, n;
  294. uint8_t c;
  295. if (UART5_S1 & UART_S1_RDRF) {
  296. if (use9Bits && (UART5_C3 & 0x80)) {
  297. n = UART5_D | 0x100;
  298. } else {
  299. n = UART5_D;
  300. }
  301. head = rx_buffer_head + 1;
  302. if (head >= SERIAL6_RX_BUFFER_SIZE) head = 0;
  303. if (head != rx_buffer_tail) {
  304. rx_buffer[head] = n;
  305. rx_buffer_head = head;
  306. }
  307. if (rts_pin) {
  308. int avail;
  309. tail = tx_buffer_tail;
  310. if (head >= tail) avail = head - tail;
  311. else avail = SERIAL6_RX_BUFFER_SIZE + head - tail;
  312. if (avail >= RTS_HIGH_WATERMARK) rts_deassert();
  313. }
  314. }
  315. c = UART5_C2;
  316. if ((c & UART_C2_TIE) && (UART5_S1 & UART_S1_TDRE)) {
  317. head = tx_buffer_head;
  318. tail = tx_buffer_tail;
  319. if (head == tail) {
  320. UART5_C2 = C2_TX_COMPLETING;
  321. } else {
  322. if (++tail >= SERIAL6_TX_BUFFER_SIZE) tail = 0;
  323. n = tx_buffer[tail];
  324. if (use9Bits) UART5_C3 = (UART5_C3 & ~0x40) | ((n & 0x100) >> 2);
  325. UART5_D = n;
  326. tx_buffer_tail = tail;
  327. }
  328. }
  329. if ((c & UART_C2_TCIE) && (UART5_S1 & UART_S1_TC)) {
  330. transmitting = 0;
  331. if (transmit_pin) transmit_deassert();
  332. UART5_C2 = C2_TX_INACTIVE;
  333. }
  334. }
  335. #endif // HAS_KINETISK_UART5