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.

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