Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. static volatile uint8_t *transmit_pin=NULL;
  53. #if TX_BUFFER_SIZE > 255
  54. static volatile uint16_t tx_buffer_head = 0;
  55. static volatile uint16_t tx_buffer_tail = 0;
  56. #else
  57. static volatile uint8_t tx_buffer_head = 0;
  58. static volatile uint8_t tx_buffer_tail = 0;
  59. #endif
  60. #if RX_BUFFER_SIZE > 255
  61. static volatile uint16_t rx_buffer_head = 0;
  62. static volatile uint16_t rx_buffer_tail = 0;
  63. #else
  64. static volatile uint8_t rx_buffer_head = 0;
  65. static volatile uint8_t rx_buffer_tail = 0;
  66. #endif
  67. // UART0 and UART1 are clocked by F_CPU, UART2 is clocked by F_BUS
  68. // UART0 has 8 byte fifo, UART1 and UART2 have 1 byte buffer
  69. #define C2_ENABLE UART_C2_TE | UART_C2_RE | UART_C2_RIE
  70. #define C2_TX_ACTIVE C2_ENABLE | UART_C2_TIE
  71. #define C2_TX_COMPLETING C2_ENABLE | UART_C2_TCIE
  72. #define C2_TX_INACTIVE C2_ENABLE
  73. void serial3_begin(uint32_t divisor)
  74. {
  75. SIM_SCGC4 |= SIM_SCGC4_UART2; // turn on clock, TODO: use bitband
  76. rx_buffer_head = 0;
  77. rx_buffer_tail = 0;
  78. tx_buffer_head = 0;
  79. tx_buffer_tail = 0;
  80. transmitting = 0;
  81. CORE_PIN7_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3);
  82. CORE_PIN8_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);
  83. #if defined(KINETISK_UART2)
  84. UART2_BDH = (divisor >> 13) & 0x1F;
  85. UART2_BDL = (divisor >> 5) & 0xFF;
  86. UART2_C4 = divisor & 0x1F;
  87. UART2_C1 = 0;
  88. UART2_PFIFO = 0;
  89. #elif defined(KINETISL_UART2)
  90. UART2_BDH = (divisor >> 8) & 0x1F;
  91. UART2_BDL = divisor & 0xFF;
  92. UART2_C1 = 0;
  93. #endif
  94. UART2_C2 = C2_TX_INACTIVE;
  95. NVIC_SET_PRIORITY(IRQ_UART2_STATUS, IRQ_PRIORITY);
  96. NVIC_ENABLE_IRQ(IRQ_UART2_STATUS);
  97. }
  98. void serial3_format(uint32_t format)
  99. {
  100. uint8_t c;
  101. c = UART2_C1;
  102. c = (c & ~0x13) | (format & 0x03); // configure parity
  103. if (format & 0x04) c |= 0x10; // 9 bits (might include parity)
  104. UART2_C1 = c;
  105. if ((format & 0x0F) == 0x04) UART2_C3 |= 0x40; // 8N2 is 9 bit with 9th bit always 1
  106. c = UART2_S2 & ~0x10;
  107. if (format & 0x10) c |= 0x10; // rx invert
  108. UART2_S2 = c;
  109. c = UART2_C3 & ~0x10;
  110. if (format & 0x20) c |= 0x10; // tx invert
  111. UART2_C3 = c;
  112. #ifdef SERIAL_9BIT_SUPPORT
  113. c = UART2_C4 & 0x1F;
  114. if (format & 0x08) c |= 0x20; // 9 bit mode with parity (requires 10 bits)
  115. UART2_C4 = c;
  116. use9Bits = format & 0x80;
  117. #endif
  118. }
  119. void serial3_end(void)
  120. {
  121. if (!(SIM_SCGC4 & SIM_SCGC4_UART2)) return;
  122. while (transmitting) yield(); // wait for buffered data to send
  123. NVIC_DISABLE_IRQ(IRQ_UART2_STATUS);
  124. UART2_C2 = 0;
  125. CORE_PIN7_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
  126. CORE_PIN8_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
  127. rx_buffer_head = 0;
  128. rx_buffer_tail = 0;
  129. }
  130. void serial3_set_transmit_pin(uint8_t pin)
  131. {
  132. while (transmitting) ;
  133. pinMode(pin, OUTPUT);
  134. digitalWrite(pin, LOW);
  135. transmit_pin = portOutputRegister(pin);
  136. }
  137. void serial3_putchar(uint32_t c)
  138. {
  139. uint32_t head;
  140. if (!(SIM_SCGC4 & SIM_SCGC4_UART2)) return;
  141. if (transmit_pin) *transmit_pin = 1;
  142. head = tx_buffer_head;
  143. if (++head >= TX_BUFFER_SIZE) head = 0;
  144. while (tx_buffer_tail == head) {
  145. int priority = nvic_execution_priority();
  146. if (priority <= IRQ_PRIORITY) {
  147. if ((UART2_S1 & UART_S1_TDRE)) {
  148. uint32_t tail = tx_buffer_tail;
  149. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  150. UART2_D = tx_buffer[tail];
  151. tx_buffer_tail = tail;
  152. }
  153. } else if (priority >= 256) {
  154. yield(); // wait
  155. }
  156. }
  157. tx_buffer[head] = c;
  158. transmitting = 1;
  159. tx_buffer_head = head;
  160. UART2_C2 = C2_TX_ACTIVE;
  161. }
  162. void serial3_write(const void *buf, unsigned int count)
  163. {
  164. const uint8_t *p = (const uint8_t *)buf;
  165. while (count-- > 0) serial3_putchar(*p++);
  166. }
  167. void serial3_flush(void)
  168. {
  169. while (transmitting) yield(); // wait
  170. }
  171. int serial3_write_buffer_free(void)
  172. {
  173. uint32_t head, tail;
  174. head = tx_buffer_head;
  175. tail = tx_buffer_tail;
  176. if (head >= tail) return TX_BUFFER_SIZE - 1 - head + tail;
  177. return tail - head - 1;
  178. }
  179. int serial3_available(void)
  180. {
  181. uint32_t head, tail;
  182. head = rx_buffer_head;
  183. tail = rx_buffer_tail;
  184. if (head >= tail) return head - tail;
  185. return RX_BUFFER_SIZE + head - tail;
  186. }
  187. int serial3_getchar(void)
  188. {
  189. uint32_t head, tail;
  190. int c;
  191. head = rx_buffer_head;
  192. tail = rx_buffer_tail;
  193. if (head == tail) return -1;
  194. if (++tail >= RX_BUFFER_SIZE) tail = 0;
  195. c = rx_buffer[tail];
  196. rx_buffer_tail = tail;
  197. return c;
  198. }
  199. int serial3_peek(void)
  200. {
  201. uint32_t head, tail;
  202. head = rx_buffer_head;
  203. tail = rx_buffer_tail;
  204. if (head == tail) return -1;
  205. if (++tail >= RX_BUFFER_SIZE) tail = 0;
  206. return rx_buffer[tail];
  207. }
  208. void serial3_clear(void)
  209. {
  210. rx_buffer_head = rx_buffer_tail;
  211. }
  212. // status interrupt combines
  213. // Transmit data below watermark UART_S1_TDRE
  214. // Transmit complete UART_S1_TC
  215. // Idle line UART_S1_IDLE
  216. // Receive data above watermark UART_S1_RDRF
  217. // LIN break detect UART_S2_LBKDIF
  218. // RxD pin active edge UART_S2_RXEDGIF
  219. void uart2_status_isr(void)
  220. {
  221. uint32_t head, tail, n;
  222. uint8_t c;
  223. if (UART2_S1 & UART_S1_RDRF) {
  224. n = UART2_D;
  225. if (use9Bits && (UART2_C3 & 0x80)) n |= 0x100;
  226. head = rx_buffer_head + 1;
  227. if (head >= RX_BUFFER_SIZE) head = 0;
  228. if (head != rx_buffer_tail) {
  229. rx_buffer[head] = n;
  230. rx_buffer_head = head;
  231. }
  232. }
  233. c = UART2_C2;
  234. if ((c & UART_C2_TIE) && (UART2_S1 & UART_S1_TDRE)) {
  235. head = tx_buffer_head;
  236. tail = tx_buffer_tail;
  237. if (head == tail) {
  238. UART2_C2 = C2_TX_COMPLETING;
  239. } else {
  240. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  241. n = tx_buffer[tail];
  242. if (use9Bits) UART2_C3 = (UART2_C3 & ~0x40) | ((n & 0x100) >> 2);
  243. UART2_D = n;
  244. tx_buffer_tail = tail;
  245. }
  246. }
  247. if ((c & UART_C2_TCIE) && (UART2_S1 & UART_S1_TC)) {
  248. transmitting = 0;
  249. if (transmit_pin) *transmit_pin = 0;
  250. UART2_C2 = C2_TX_INACTIVE;
  251. }
  252. }