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ů.

302 lines
8.4KB

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