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

328 lines
8.9KB

  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. int serial3_set_rts(uint8_t pin)
  150. {
  151. if (!(SIM_SCGC4 & SIM_SCGC4_UART2)) return 0;
  152. if (pin == 2) {
  153. CORE_PIN2_CONFIG = PORT_PCR_MUX(3);
  154. } else {
  155. UART2_MODEM &= ~UART_MODEM_RXRTSE;
  156. return 0;
  157. }
  158. UART2_MODEM |= UART_MODEM_RXRTSE;
  159. return 1;
  160. }
  161. int serial3_set_cts(uint8_t pin)
  162. {
  163. if (!(SIM_SCGC4 & SIM_SCGC4_UART2)) return 0;
  164. if (pin == 14) {
  165. CORE_PIN14_CONFIG = PORT_PCR_MUX(3); // TODO: weak pullup or pulldown?
  166. } else {
  167. UART2_MODEM &= ~UART_MODEM_TXCTSE;
  168. return 0;
  169. }
  170. UART2_MODEM |= UART_MODEM_TXCTSE;
  171. return 1;
  172. }
  173. void serial3_putchar(uint32_t c)
  174. {
  175. uint32_t head, n;
  176. if (!(SIM_SCGC4 & SIM_SCGC4_UART2)) return;
  177. if (transmit_pin) transmit_assert();
  178. head = tx_buffer_head;
  179. if (++head >= TX_BUFFER_SIZE) head = 0;
  180. while (tx_buffer_tail == head) {
  181. int priority = nvic_execution_priority();
  182. if (priority <= IRQ_PRIORITY) {
  183. if ((UART2_S1 & UART_S1_TDRE)) {
  184. uint32_t tail = tx_buffer_tail;
  185. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  186. n = tx_buffer[tail];
  187. if (use9Bits) UART2_C3 = (UART2_C3 & ~0x40) | ((n & 0x100) >> 2);
  188. UART2_D = n;
  189. tx_buffer_tail = tail;
  190. }
  191. } else if (priority >= 256) {
  192. yield(); // wait
  193. }
  194. }
  195. tx_buffer[head] = c;
  196. transmitting = 1;
  197. tx_buffer_head = head;
  198. UART2_C2 = C2_TX_ACTIVE;
  199. }
  200. void serial3_write(const void *buf, unsigned int count)
  201. {
  202. const uint8_t *p = (const uint8_t *)buf;
  203. while (count-- > 0) serial3_putchar(*p++);
  204. }
  205. void serial3_flush(void)
  206. {
  207. while (transmitting) yield(); // wait
  208. }
  209. int serial3_write_buffer_free(void)
  210. {
  211. uint32_t head, tail;
  212. head = tx_buffer_head;
  213. tail = tx_buffer_tail;
  214. if (head >= tail) return TX_BUFFER_SIZE - 1 - head + tail;
  215. return tail - head - 1;
  216. }
  217. int serial3_available(void)
  218. {
  219. uint32_t head, tail;
  220. head = rx_buffer_head;
  221. tail = rx_buffer_tail;
  222. if (head >= tail) return head - tail;
  223. return RX_BUFFER_SIZE + head - tail;
  224. }
  225. int serial3_getchar(void)
  226. {
  227. uint32_t head, tail;
  228. int c;
  229. head = rx_buffer_head;
  230. tail = rx_buffer_tail;
  231. if (head == tail) return -1;
  232. if (++tail >= RX_BUFFER_SIZE) tail = 0;
  233. c = rx_buffer[tail];
  234. rx_buffer_tail = tail;
  235. return c;
  236. }
  237. int serial3_peek(void)
  238. {
  239. uint32_t head, tail;
  240. head = rx_buffer_head;
  241. tail = rx_buffer_tail;
  242. if (head == tail) return -1;
  243. if (++tail >= RX_BUFFER_SIZE) tail = 0;
  244. return rx_buffer[tail];
  245. }
  246. void serial3_clear(void)
  247. {
  248. rx_buffer_head = rx_buffer_tail;
  249. }
  250. // status interrupt combines
  251. // Transmit data below watermark UART_S1_TDRE
  252. // Transmit complete UART_S1_TC
  253. // Idle line UART_S1_IDLE
  254. // Receive data above watermark UART_S1_RDRF
  255. // LIN break detect UART_S2_LBKDIF
  256. // RxD pin active edge UART_S2_RXEDGIF
  257. void uart2_status_isr(void)
  258. {
  259. uint32_t head, tail, n;
  260. uint8_t c;
  261. if (UART2_S1 & UART_S1_RDRF) {
  262. if (use9Bits && (UART2_C3 & 0x80)) {
  263. n = UART2_D | 0x100;
  264. } else {
  265. n = UART2_D;
  266. }
  267. head = rx_buffer_head + 1;
  268. if (head >= RX_BUFFER_SIZE) head = 0;
  269. if (head != rx_buffer_tail) {
  270. rx_buffer[head] = n;
  271. rx_buffer_head = head;
  272. }
  273. }
  274. c = UART2_C2;
  275. if ((c & UART_C2_TIE) && (UART2_S1 & UART_S1_TDRE)) {
  276. head = tx_buffer_head;
  277. tail = tx_buffer_tail;
  278. if (head == tail) {
  279. UART2_C2 = C2_TX_COMPLETING;
  280. } else {
  281. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  282. n = tx_buffer[tail];
  283. if (use9Bits) UART2_C3 = (UART2_C3 & ~0x40) | ((n & 0x100) >> 2);
  284. UART2_D = n;
  285. tx_buffer_tail = tail;
  286. }
  287. }
  288. if ((c & UART_C2_TCIE) && (UART2_S1 & UART_S1_TC)) {
  289. transmitting = 0;
  290. if (transmit_pin) transmit_deassert();
  291. UART2_C2 = C2_TX_INACTIVE;
  292. }
  293. }