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.

280 satır
7.7KB

  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. ////////////////////////////////////////////////////////////////
  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_write_buffer_free(void)
  163. {
  164. uint32_t head, tail;
  165. head = tx_buffer_head;
  166. tail = tx_buffer_tail;
  167. if (head >= tail) return TX_BUFFER_SIZE - 1 - head + tail;
  168. return tail - head - 1;
  169. }
  170. int serial2_available(void)
  171. {
  172. uint32_t head, tail;
  173. head = rx_buffer_head;
  174. tail = rx_buffer_tail;
  175. if (head >= tail) return head - tail;
  176. return RX_BUFFER_SIZE + head - tail;
  177. }
  178. int serial2_getchar(void)
  179. {
  180. uint32_t head, tail;
  181. int c;
  182. head = rx_buffer_head;
  183. tail = rx_buffer_tail;
  184. if (head == tail) return -1;
  185. if (++tail >= RX_BUFFER_SIZE) tail = 0;
  186. c = rx_buffer[tail];
  187. rx_buffer_tail = tail;
  188. return c;
  189. }
  190. int serial2_peek(void)
  191. {
  192. uint32_t head, tail;
  193. head = rx_buffer_head;
  194. tail = rx_buffer_tail;
  195. if (head == tail) return -1;
  196. if (++tail >= RX_BUFFER_SIZE) tail = 0;
  197. return rx_buffer[tail];
  198. }
  199. void serial2_clear(void)
  200. {
  201. rx_buffer_head = rx_buffer_tail;
  202. }
  203. // status interrupt combines
  204. // Transmit data below watermark UART_S1_TDRE
  205. // Transmit complete UART_S1_TC
  206. // Idle line UART_S1_IDLE
  207. // Receive data above watermark UART_S1_RDRF
  208. // LIN break detect UART_S2_LBKDIF
  209. // RxD pin active edge UART_S2_RXEDGIF
  210. void uart1_status_isr(void)
  211. {
  212. uint32_t head, tail, n;
  213. uint8_t c;
  214. //digitalWriteFast(4, HIGH);
  215. if (UART1_S1 & UART_S1_RDRF) {
  216. //digitalWriteFast(5, HIGH);
  217. n = UART1_D;
  218. if (use9Bits && (UART1_C3 & 0x80)) n |= 0x100;
  219. head = rx_buffer_head + 1;
  220. if (head >= RX_BUFFER_SIZE) head = 0;
  221. if (head != rx_buffer_tail) {
  222. rx_buffer[head] = n;
  223. rx_buffer_head = head;
  224. }
  225. //digitalWriteFast(5, LOW);
  226. }
  227. c = UART1_C2;
  228. if ((c & UART_C2_TIE) && (UART1_S1 & UART_S1_TDRE)) {
  229. //digitalWriteFast(5, HIGH);
  230. head = tx_buffer_head;
  231. tail = tx_buffer_tail;
  232. if (head == tail) {
  233. UART1_C2 = C2_TX_COMPLETING;
  234. } else {
  235. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  236. n = tx_buffer[tail];
  237. if (use9Bits) UART1_C3 = (UART1_C3 & ~0x40) | ((n & 0x100) >> 2);
  238. UART1_D = n;
  239. tx_buffer_tail = tail;
  240. }
  241. //digitalWriteFast(5, LOW);
  242. }
  243. if ((c & UART_C2_TCIE) && (UART1_S1 & UART_S1_TC)) {
  244. transmitting = 0;
  245. UART1_C2 = C2_TX_INACTIVE;
  246. }
  247. //digitalWriteFast(4, LOW);
  248. }