Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 serial3_begin(uint32_t divisor)
  73. {
  74. SIM_SCGC4 |= SIM_SCGC4_UART2; // 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_PIN7_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3);
  81. CORE_PIN8_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);
  82. UART2_BDH = (divisor >> 13) & 0x1F;
  83. UART2_BDL = (divisor >> 5) & 0xFF;
  84. UART2_C4 = divisor & 0x1F;
  85. UART2_C1 = 0;
  86. UART2_PFIFO = 0;
  87. UART2_C2 = C2_TX_INACTIVE;
  88. NVIC_SET_PRIORITY(IRQ_UART2_STATUS, 64);
  89. NVIC_ENABLE_IRQ(IRQ_UART2_STATUS);
  90. }
  91. void serial3_format(uint32_t format)
  92. {
  93. uint8_t c;
  94. c = UART2_C1;
  95. c = (c & ~0x13) | (format & 0x03); // configure parity
  96. if (format & 0x04) c |= 0x10; // 9 bits (might include parity)
  97. UART2_C1 = c;
  98. if ((format & 0x0F) == 0x04) UART2_C3 |= 0x40; // 8N2 is 9 bit with 9th bit always 1
  99. c = UART2_S2 & ~0x10;
  100. if (format & 0x10) c |= 0x10; // rx invert
  101. UART2_S2 = c;
  102. c = UART2_C3 & ~0x10;
  103. if (format & 0x20) c |= 0x10; // tx invert
  104. UART2_C3 = c;
  105. #ifdef SERIAL_9BIT_SUPPORT
  106. c = UART2_C4 & 0x1F;
  107. if (format & 0x08) c |= 0x20; // 9 bit mode with parity (requires 10 bits)
  108. UART2_C4 = c;
  109. use9Bits = format & 0x80;
  110. #endif
  111. }
  112. void serial3_end(void)
  113. {
  114. if (!(SIM_SCGC4 & SIM_SCGC4_UART2)) return;
  115. while (transmitting) yield(); // wait for buffered data to send
  116. NVIC_DISABLE_IRQ(IRQ_UART2_STATUS);
  117. UART2_C2 = 0;
  118. CORE_PIN7_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
  119. CORE_PIN8_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
  120. rx_buffer_head = 0;
  121. rx_buffer_tail = 0;
  122. }
  123. void serial3_putchar(uint32_t c)
  124. {
  125. uint32_t head;
  126. if (!(SIM_SCGC4 & SIM_SCGC4_UART2)) return;
  127. head = tx_buffer_head;
  128. if (++head >= TX_BUFFER_SIZE) head = 0;
  129. while (tx_buffer_tail == head) {
  130. int priority = nvic_execution_priority();
  131. if (priority <= IRQ_PRIORITY) {
  132. if ((UART2_S1 & UART_S1_TDRE)) {
  133. uint32_t tail = tx_buffer_tail;
  134. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  135. UART2_D = tx_buffer[tail];
  136. tx_buffer_tail = tail;
  137. }
  138. } else if (priority >= 256) {
  139. yield(); // wait
  140. }
  141. }
  142. tx_buffer[head] = c;
  143. transmitting = 1;
  144. tx_buffer_head = head;
  145. UART2_C2 = C2_TX_ACTIVE;
  146. }
  147. void serial3_write(const void *buf, unsigned int count)
  148. {
  149. const uint8_t *p = (const uint8_t *)buf;
  150. while (count-- > 0) serial3_putchar(*p++);
  151. }
  152. void serial3_flush(void)
  153. {
  154. while (transmitting) yield(); // wait
  155. }
  156. int serial3_available(void)
  157. {
  158. uint32_t head, tail;
  159. head = rx_buffer_head;
  160. tail = rx_buffer_tail;
  161. if (head >= tail) return head - tail;
  162. return RX_BUFFER_SIZE + head - tail;
  163. }
  164. int serial3_getchar(void)
  165. {
  166. uint32_t head, tail;
  167. int c;
  168. head = rx_buffer_head;
  169. tail = rx_buffer_tail;
  170. if (head == tail) return -1;
  171. if (++tail >= RX_BUFFER_SIZE) tail = 0;
  172. c = rx_buffer[tail];
  173. rx_buffer_tail = tail;
  174. return c;
  175. }
  176. int serial3_peek(void)
  177. {
  178. uint32_t head, tail;
  179. head = rx_buffer_head;
  180. tail = rx_buffer_tail;
  181. if (head == tail) return -1;
  182. if (++tail >= RX_BUFFER_SIZE) tail = 0;
  183. return rx_buffer[tail];
  184. }
  185. void serial3_clear(void)
  186. {
  187. rx_buffer_head = rx_buffer_tail;
  188. }
  189. // status interrupt combines
  190. // Transmit data below watermark UART_S1_TDRE
  191. // Transmit complete UART_S1_TC
  192. // Idle line UART_S1_IDLE
  193. // Receive data above watermark UART_S1_RDRF
  194. // LIN break detect UART_S2_LBKDIF
  195. // RxD pin active edge UART_S2_RXEDGIF
  196. void uart2_status_isr(void)
  197. {
  198. uint32_t head, tail, n;
  199. uint8_t c;
  200. //digitalWriteFast(4, HIGH);
  201. if (UART2_S1 & UART_S1_RDRF) {
  202. //digitalWriteFast(5, HIGH);
  203. n = UART2_D;
  204. if (use9Bits && (UART2_C3 & 0x80)) n |= 0x100;
  205. head = rx_buffer_head + 1;
  206. if (head >= RX_BUFFER_SIZE) head = 0;
  207. if (head != rx_buffer_tail) {
  208. rx_buffer[head] = n;
  209. rx_buffer_head = head;
  210. }
  211. //digitalWriteFast(5, LOW);
  212. }
  213. c = UART2_C2;
  214. if ((c & UART_C2_TIE) && (UART2_S1 & UART_S1_TDRE)) {
  215. //digitalWriteFast(5, HIGH);
  216. head = tx_buffer_head;
  217. tail = tx_buffer_tail;
  218. if (head == tail) {
  219. UART2_C2 = C2_TX_COMPLETING;
  220. } else {
  221. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  222. n = tx_buffer[tail];
  223. if (use9Bits) UART2_C3 = (UART2_C3 & ~0x40) | ((n & 0x100) >> 2);
  224. UART2_D = n;
  225. tx_buffer_tail = tail;
  226. }
  227. //digitalWriteFast(5, LOW);
  228. }
  229. if ((c & UART_C2_TCIE) && (UART2_S1 & UART_S1_TC)) {
  230. transmitting = 0;
  231. UART2_C2 = C2_TX_INACTIVE;
  232. }
  233. //digitalWriteFast(4, LOW);
  234. }