Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

356 lines
9.4KB

  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_UART3
  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 = 32;
  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 serial4_begin(uint32_t divisor)
  83. {
  84. SIM_SCGC4 |= SIM_SCGC4_UART3; // 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_PIN31_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3);
  91. CORE_PIN32_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);
  92. UART3_BDH = (divisor >> 13) & 0x1F;
  93. UART3_BDL = (divisor >> 5) & 0xFF;
  94. UART3_C4 = divisor & 0x1F;
  95. UART3_C1 = 0;
  96. UART3_PFIFO = 0;
  97. UART3_C2 = C2_TX_INACTIVE;
  98. NVIC_SET_PRIORITY(IRQ_UART3_STATUS, IRQ_PRIORITY);
  99. NVIC_ENABLE_IRQ(IRQ_UART3_STATUS);
  100. }
  101. void serial4_format(uint32_t format)
  102. {
  103. uint8_t c;
  104. c = UART3_C1;
  105. c = (c & ~0x13) | (format & 0x03); // configure parity
  106. if (format & 0x04) c |= 0x10; // 9 bits (might include parity)
  107. UART3_C1 = c;
  108. if ((format & 0x0F) == 0x04) UART3_C3 |= 0x40; // 8N2 is 9 bit with 9th bit always 1
  109. c = UART3_S2 & ~0x10;
  110. if (format & 0x10) c |= 0x10; // rx invert
  111. UART3_S2 = c;
  112. c = UART3_C3 & ~0x10;
  113. if (format & 0x20) c |= 0x10; // tx invert
  114. UART3_C3 = c;
  115. #ifdef SERIAL_9BIT_SUPPORT
  116. c = UART3_C4 & 0x1F;
  117. if (format & 0x08) c |= 0x20; // 9 bit mode with parity (requires 10 bits)
  118. UART3_C4 = c;
  119. use9Bits = format & 0x80;
  120. #endif
  121. }
  122. void serial4_end(void)
  123. {
  124. if (!(SIM_SCGC4 & SIM_SCGC4_UART3)) return;
  125. while (transmitting) yield(); // wait for buffered data to send
  126. NVIC_DISABLE_IRQ(IRQ_UART3_STATUS);
  127. UART3_C2 = 0;
  128. CORE_PIN31_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
  129. CORE_PIN32_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
  130. rx_buffer_head = 0;
  131. rx_buffer_tail = 0;
  132. if (rts_pin) rts_deassert();
  133. }
  134. void serial4_set_transmit_pin(uint8_t pin)
  135. {
  136. while (transmitting) ;
  137. pinMode(pin, OUTPUT);
  138. digitalWrite(pin, LOW);
  139. transmit_pin = portOutputRegister(pin);
  140. }
  141. void serial4_set_tx(uint8_t pin, uint8_t opendrain)
  142. {
  143. uint32_t cfg;
  144. if (opendrain) pin |= 128;
  145. if (pin == tx_pin_num) return;
  146. if ((SIM_SCGC4 & SIM_SCGC4_UART2)) {
  147. switch (tx_pin_num & 127) {
  148. case 32: CORE_PIN32_CONFIG = 0; break; // PTB11
  149. }
  150. if (opendrain) {
  151. cfg = PORT_PCR_DSE | PORT_PCR_ODE;
  152. } else {
  153. cfg = PORT_PCR_DSE | PORT_PCR_SRE;
  154. }
  155. switch (pin & 127) {
  156. case 32: CORE_PIN32_CONFIG = cfg | PORT_PCR_MUX(3); break;
  157. }
  158. }
  159. tx_pin_num = pin;
  160. }
  161. void serial4_set_rx(uint8_t pin)
  162. {
  163. }
  164. int serial4_set_rts(uint8_t pin)
  165. {
  166. if (!(SIM_SCGC4 & SIM_SCGC4_UART3)) return 0;
  167. if (pin < CORE_NUM_DIGITAL) {
  168. rts_pin = portOutputRegister(pin);
  169. pinMode(pin, OUTPUT);
  170. rts_assert();
  171. } else {
  172. rts_pin = NULL;
  173. return 0;
  174. }
  175. return 1;
  176. }
  177. int serial4_set_cts(uint8_t pin)
  178. {
  179. return 0;
  180. }
  181. void serial4_putchar(uint32_t c)
  182. {
  183. uint32_t head, n;
  184. if (!(SIM_SCGC4 & SIM_SCGC4_UART3)) return;
  185. if (transmit_pin) transmit_assert();
  186. head = tx_buffer_head;
  187. if (++head >= TX_BUFFER_SIZE) head = 0;
  188. while (tx_buffer_tail == head) {
  189. int priority = nvic_execution_priority();
  190. if (priority <= IRQ_PRIORITY) {
  191. if ((UART3_S1 & UART_S1_TDRE)) {
  192. uint32_t tail = tx_buffer_tail;
  193. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  194. n = tx_buffer[tail];
  195. if (use9Bits) UART3_C3 = (UART3_C3 & ~0x40) | ((n & 0x100) >> 2);
  196. UART3_D = n;
  197. tx_buffer_tail = tail;
  198. }
  199. } else if (priority >= 256) {
  200. yield(); // wait
  201. }
  202. }
  203. tx_buffer[head] = c;
  204. transmitting = 1;
  205. tx_buffer_head = head;
  206. UART3_C2 = C2_TX_ACTIVE;
  207. }
  208. void serial4_write(const void *buf, unsigned int count)
  209. {
  210. const uint8_t *p = (const uint8_t *)buf;
  211. while (count-- > 0) serial4_putchar(*p++);
  212. }
  213. void serial4_flush(void)
  214. {
  215. while (transmitting) yield(); // wait
  216. }
  217. int serial4_write_buffer_free(void)
  218. {
  219. uint32_t head, tail;
  220. head = tx_buffer_head;
  221. tail = tx_buffer_tail;
  222. if (head >= tail) return TX_BUFFER_SIZE - 1 - head + tail;
  223. return tail - head - 1;
  224. }
  225. int serial4_available(void)
  226. {
  227. uint32_t head, tail;
  228. head = rx_buffer_head;
  229. tail = rx_buffer_tail;
  230. if (head >= tail) return head - tail;
  231. return RX_BUFFER_SIZE + head - tail;
  232. }
  233. int serial4_getchar(void)
  234. {
  235. uint32_t head, tail;
  236. int c;
  237. head = rx_buffer_head;
  238. tail = rx_buffer_tail;
  239. if (head == tail) return -1;
  240. if (++tail >= RX_BUFFER_SIZE) tail = 0;
  241. c = rx_buffer[tail];
  242. rx_buffer_tail = tail;
  243. if (rts_pin) {
  244. int avail;
  245. if (head >= tail) avail = head - tail;
  246. else avail = RX_BUFFER_SIZE + head - tail;
  247. if (avail <= RTS_LOW_WATERMARK) rts_assert();
  248. }
  249. return c;
  250. }
  251. int serial4_peek(void)
  252. {
  253. uint32_t head, tail;
  254. head = rx_buffer_head;
  255. tail = rx_buffer_tail;
  256. if (head == tail) return -1;
  257. if (++tail >= RX_BUFFER_SIZE) tail = 0;
  258. return rx_buffer[tail];
  259. }
  260. void serial4_clear(void)
  261. {
  262. rx_buffer_head = rx_buffer_tail;
  263. if (rts_pin) rts_assert();
  264. }
  265. // status interrupt combines
  266. // Transmit data below watermark UART_S1_TDRE
  267. // Transmit complete UART_S1_TC
  268. // Idle line UART_S1_IDLE
  269. // Receive data above watermark UART_S1_RDRF
  270. // LIN break detect UART_S2_LBKDIF
  271. // RxD pin active edge UART_S2_RXEDGIF
  272. void uart3_status_isr(void)
  273. {
  274. uint32_t head, tail, n;
  275. uint8_t c;
  276. if (UART3_S1 & UART_S1_RDRF) {
  277. if (use9Bits && (UART3_C3 & 0x80)) {
  278. n = UART3_D | 0x100;
  279. } else {
  280. n = UART3_D;
  281. }
  282. head = rx_buffer_head + 1;
  283. if (head >= RX_BUFFER_SIZE) head = 0;
  284. if (head != rx_buffer_tail) {
  285. rx_buffer[head] = n;
  286. rx_buffer_head = head;
  287. }
  288. if (rts_pin) {
  289. int avail;
  290. tail = tx_buffer_tail;
  291. if (head >= tail) avail = head - tail;
  292. else avail = RX_BUFFER_SIZE + head - tail;
  293. if (avail >= RTS_HIGH_WATERMARK) rts_deassert();
  294. }
  295. }
  296. c = UART3_C2;
  297. if ((c & UART_C2_TIE) && (UART3_S1 & UART_S1_TDRE)) {
  298. head = tx_buffer_head;
  299. tail = tx_buffer_tail;
  300. if (head == tail) {
  301. UART3_C2 = C2_TX_COMPLETING;
  302. } else {
  303. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  304. n = tx_buffer[tail];
  305. if (use9Bits) UART3_C3 = (UART3_C3 & ~0x40) | ((n & 0x100) >> 2);
  306. UART3_D = n;
  307. tx_buffer_tail = tail;
  308. }
  309. }
  310. if ((c & UART_C2_TCIE) && (UART3_S1 & UART_S1_TC)) {
  311. transmitting = 0;
  312. if (transmit_pin) transmit_deassert();
  313. UART3_C2 = C2_TX_INACTIVE;
  314. }
  315. }
  316. #endif // HAS_KINETISK_UART3