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.

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