Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

270 lines
7.5KB

  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 "mk20dx128.h"
  31. #include "core_pins.h"
  32. #include "HardwareSerial.h"
  33. ////////////////////////////////////////////////////////////////
  34. // Tunable parameters (relatively safe to edit these numbers)
  35. ////////////////////////////////////////////////////////////////
  36. #define TX_BUFFER_SIZE 40
  37. #define RX_BUFFER_SIZE 64
  38. #define IRQ_PRIORITY 64 // 0 = highest priority, 255 = lowest
  39. ////////////////////////////////////////////////////////////////
  40. // changes not recommended below this point....
  41. ////////////////////////////////////////////////////////////////
  42. #ifdef SERIAL_9BIT_SUPPORT
  43. static uint8_t use9Bits = 0;
  44. #define BUFTYPE uint16_t
  45. #else
  46. #define BUFTYPE uint8_t
  47. #define use9Bits 0
  48. #endif
  49. static volatile BUFTYPE tx_buffer[TX_BUFFER_SIZE];
  50. static volatile BUFTYPE rx_buffer[RX_BUFFER_SIZE];
  51. static volatile uint8_t transmitting = 0;
  52. #if TX_BUFFER_SIZE > 255
  53. static volatile uint16_t tx_buffer_head = 0;
  54. static volatile uint16_t tx_buffer_tail = 0;
  55. #else
  56. static volatile uint8_t tx_buffer_head = 0;
  57. static volatile uint8_t tx_buffer_tail = 0;
  58. #endif
  59. #if RX_BUFFER_SIZE > 255
  60. static volatile uint16_t rx_buffer_head = 0;
  61. static volatile uint16_t rx_buffer_tail = 0;
  62. #else
  63. static volatile uint8_t rx_buffer_head = 0;
  64. static volatile uint8_t rx_buffer_tail = 0;
  65. #endif
  66. // UART0 and UART1 are clocked by F_CPU, UART2 is clocked by F_BUS
  67. // UART0 has 8 byte fifo, UART1 and UART2 have 1 byte buffer
  68. #define C2_ENABLE UART_C2_TE | UART_C2_RE | UART_C2_RIE
  69. #define C2_TX_ACTIVE C2_ENABLE | UART_C2_TIE
  70. #define C2_TX_COMPLETING C2_ENABLE | UART_C2_TCIE
  71. #define C2_TX_INACTIVE C2_ENABLE
  72. void serial2_begin(uint32_t divisor)
  73. {
  74. SIM_SCGC4 |= SIM_SCGC4_UART1; // turn on clock, TODO: use bitband
  75. rx_buffer_head = 0;
  76. rx_buffer_tail = 0;
  77. tx_buffer_head = 0;
  78. tx_buffer_tail = 0;
  79. transmitting = 0;
  80. CORE_PIN9_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3);
  81. CORE_PIN10_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);
  82. UART1_BDH = (divisor >> 13) & 0x1F;
  83. UART1_BDL = (divisor >> 5) & 0xFF;
  84. UART1_C4 = divisor & 0x1F;
  85. UART1_C1 = 0;
  86. UART1_PFIFO = 0;
  87. UART1_C2 = C2_TX_INACTIVE;
  88. NVIC_SET_PRIORITY(IRQ_UART1_STATUS, IRQ_PRIORITY);
  89. NVIC_ENABLE_IRQ(IRQ_UART1_STATUS);
  90. }
  91. void serial2_format(uint32_t format)
  92. {
  93. uint8_t c;
  94. c = UART1_C1;
  95. c = (c & ~0x13) | (format & 0x03); // configure parity
  96. if (format & 0x04) c |= 0x10; // 9 bits (might include parity)
  97. UART1_C1 = c;
  98. if ((format & 0x0F) == 0x04) UART1_C3 |= 0x40; // 8N2 is 9 bit with 9th bit always 1
  99. c = UART1_S2 & ~0x10;
  100. if (format & 0x10) c |= 0x10; // rx invert
  101. UART1_S2 = c;
  102. c = UART1_C3 & ~0x10;
  103. if (format & 0x20) c |= 0x10; // tx invert
  104. UART1_C3 = c;
  105. #ifdef SERIAL_9BIT_SUPPORT
  106. c = UART1_C4 & 0x1F;
  107. if (format & 0x08) c |= 0x20; // 9 bit mode with parity (requires 10 bits)
  108. UART1_C4 = c;
  109. use9Bits = format & 0x80;
  110. #endif
  111. // UART1_C1.0 = parity, 0=even, 1=odd
  112. // UART1_C1.1 = parity, 0=disable, 1=enable
  113. // UART1_C1.4 = mode, 1=9bit, 0=8bit
  114. // UART1_C4.5 = mode, 1=10bit, 0=8bit
  115. // UART1_C3.4 = txinv, 0=normal, 1=inverted
  116. // UART1_S2.4 = rxinv, 0=normal, 1=inverted
  117. }
  118. void serial2_end(void)
  119. {
  120. if (!(SIM_SCGC4 & SIM_SCGC4_UART1)) return;
  121. while (transmitting) yield(); // wait for buffered data to send
  122. NVIC_DISABLE_IRQ(IRQ_UART1_STATUS);
  123. UART1_C2 = 0;
  124. CORE_PIN9_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
  125. CORE_PIN10_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
  126. rx_buffer_head = 0;
  127. rx_buffer_tail = 0;
  128. }
  129. void serial2_putchar(uint32_t c)
  130. {
  131. uint32_t head;
  132. if (!(SIM_SCGC4 & SIM_SCGC4_UART1)) return;
  133. head = tx_buffer_head;
  134. if (++head >= TX_BUFFER_SIZE) head = 0;
  135. while (tx_buffer_tail == head) {
  136. int priority = nvic_execution_priority();
  137. if (priority <= IRQ_PRIORITY) {
  138. if ((UART1_S1 & UART_S1_TDRE)) {
  139. uint32_t tail = tx_buffer_tail;
  140. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  141. UART1_D = tx_buffer[tail];
  142. tx_buffer_tail = tail;
  143. }
  144. } else if (priority >= 256) {
  145. yield(); // wait
  146. }
  147. }
  148. tx_buffer[head] = c;
  149. transmitting = 1;
  150. tx_buffer_head = head;
  151. UART1_C2 = C2_TX_ACTIVE;
  152. }
  153. void serial2_write(const void *buf, unsigned int count)
  154. {
  155. const uint8_t *p = (const uint8_t *)buf;
  156. while (count-- > 0) serial2_putchar(*p++);
  157. }
  158. void serial2_flush(void)
  159. {
  160. while (transmitting) yield(); // wait
  161. }
  162. int serial2_available(void)
  163. {
  164. uint32_t head, tail;
  165. head = rx_buffer_head;
  166. tail = rx_buffer_tail;
  167. if (head >= tail) return head - tail;
  168. return RX_BUFFER_SIZE + head - tail;
  169. }
  170. int serial2_getchar(void)
  171. {
  172. uint32_t head, tail;
  173. int c;
  174. head = rx_buffer_head;
  175. tail = rx_buffer_tail;
  176. if (head == tail) return -1;
  177. if (++tail >= RX_BUFFER_SIZE) tail = 0;
  178. c = rx_buffer[tail];
  179. rx_buffer_tail = tail;
  180. return c;
  181. }
  182. int serial2_peek(void)
  183. {
  184. uint32_t head, tail;
  185. head = rx_buffer_head;
  186. tail = rx_buffer_tail;
  187. if (head == tail) return -1;
  188. if (++tail >= RX_BUFFER_SIZE) tail = 0;
  189. return rx_buffer[tail];
  190. }
  191. void serial2_clear(void)
  192. {
  193. rx_buffer_head = rx_buffer_tail;
  194. }
  195. // status interrupt combines
  196. // Transmit data below watermark UART_S1_TDRE
  197. // Transmit complete UART_S1_TC
  198. // Idle line UART_S1_IDLE
  199. // Receive data above watermark UART_S1_RDRF
  200. // LIN break detect UART_S2_LBKDIF
  201. // RxD pin active edge UART_S2_RXEDGIF
  202. void uart1_status_isr(void)
  203. {
  204. uint32_t head, tail, n;
  205. uint8_t c;
  206. //digitalWriteFast(4, HIGH);
  207. if (UART1_S1 & UART_S1_RDRF) {
  208. //digitalWriteFast(5, HIGH);
  209. n = UART1_D;
  210. if (use9Bits && (UART1_C3 & 0x80)) n |= 0x100;
  211. head = rx_buffer_head + 1;
  212. if (head >= RX_BUFFER_SIZE) head = 0;
  213. if (head != rx_buffer_tail) {
  214. rx_buffer[head] = n;
  215. rx_buffer_head = head;
  216. }
  217. //digitalWriteFast(5, LOW);
  218. }
  219. c = UART1_C2;
  220. if ((c & UART_C2_TIE) && (UART1_S1 & UART_S1_TDRE)) {
  221. //digitalWriteFast(5, HIGH);
  222. head = tx_buffer_head;
  223. tail = tx_buffer_tail;
  224. if (head == tail) {
  225. UART1_C2 = C2_TX_COMPLETING;
  226. } else {
  227. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  228. n = tx_buffer[tail];
  229. if (use9Bits) UART1_C3 = (UART1_C3 & ~0x40) | ((n & 0x100) >> 2);
  230. UART1_D = n;
  231. tx_buffer_tail = tail;
  232. }
  233. //digitalWriteFast(5, LOW);
  234. }
  235. if ((c & UART_C2_TCIE) && (UART1_S1 & UART_S1_TC)) {
  236. transmitting = 0;
  237. UART1_C2 = C2_TX_INACTIVE;
  238. }
  239. //digitalWriteFast(4, LOW);
  240. }