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.

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