Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

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