Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

373 lines
10KB

  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2017 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. #ifdef HAS_KINETISK_UART4
  34. ////////////////////////////////////////////////////////////////
  35. // Tunable parameters (relatively safe to edit these numbers)
  36. ////////////////////////////////////////////////////////////////
  37. #ifndef SERIAL5_TX_BUFFER_SIZE
  38. #define SERIAL5_TX_BUFFER_SIZE 40 // number of outgoing bytes to buffer
  39. #endif
  40. #ifndef SERIAL5_RX_BUFFER_SIZE
  41. #define SERIAL5_RX_BUFFER_SIZE 64 // number of incoming bytes to buffer
  42. #endif
  43. #define RTS_HIGH_WATERMARK (SERIAL5_RX_BUFFER_SIZE-24) // RTS requests sender to pause
  44. #define RTS_LOW_WATERMARK (SERIAL5_RX_BUFFER_SIZE-38) // RTS allows sender to resume
  45. #define IRQ_PRIORITY 64 // 0 = highest priority, 255 = lowest
  46. ////////////////////////////////////////////////////////////////
  47. // changes not recommended below this point....
  48. ////////////////////////////////////////////////////////////////
  49. #ifdef SERIAL_9BIT_SUPPORT
  50. static uint8_t use9Bits = 0;
  51. #define BUFTYPE uint16_t
  52. #else
  53. #define BUFTYPE uint8_t
  54. #define use9Bits 0
  55. #endif
  56. static volatile BUFTYPE tx_buffer[SERIAL5_TX_BUFFER_SIZE];
  57. static volatile BUFTYPE rx_buffer[SERIAL5_RX_BUFFER_SIZE];
  58. static volatile uint8_t transmitting = 0;
  59. static volatile uint8_t *transmit_pin=NULL;
  60. #define transmit_assert() *transmit_pin = 1
  61. #define transmit_deassert() *transmit_pin = 0
  62. static volatile uint8_t *rts_pin=NULL;
  63. #define rts_assert() *rts_pin = 0
  64. #define rts_deassert() *rts_pin = 1
  65. #if SERIAL5_TX_BUFFER_SIZE > 255
  66. static volatile uint16_t tx_buffer_head = 0;
  67. static volatile uint16_t tx_buffer_tail = 0;
  68. #else
  69. static volatile uint8_t tx_buffer_head = 0;
  70. static volatile uint8_t tx_buffer_tail = 0;
  71. #endif
  72. #if SERIAL5_RX_BUFFER_SIZE > 255
  73. static volatile uint16_t rx_buffer_head = 0;
  74. static volatile uint16_t rx_buffer_tail = 0;
  75. #else
  76. static volatile uint8_t rx_buffer_head = 0;
  77. static volatile uint8_t rx_buffer_tail = 0;
  78. #endif
  79. static uint8_t tx_pin_num = 33;
  80. // UART0 and UART1 are clocked by F_CPU, UART2 is clocked by F_BUS
  81. // UART0 has 8 byte fifo, UART1 and UART2 have 1 byte buffer
  82. #define C2_ENABLE UART_C2_TE | UART_C2_RE | UART_C2_RIE
  83. #define C2_TX_ACTIVE C2_ENABLE | UART_C2_TIE
  84. #define C2_TX_COMPLETING C2_ENABLE | UART_C2_TCIE
  85. #define C2_TX_INACTIVE C2_ENABLE
  86. void serial5_begin(uint32_t divisor)
  87. {
  88. SIM_SCGC1 |= SIM_SCGC1_UART4; // turn on clock, TODO: use bitband
  89. rx_buffer_head = 0;
  90. rx_buffer_tail = 0;
  91. tx_buffer_head = 0;
  92. tx_buffer_tail = 0;
  93. transmitting = 0;
  94. CORE_PIN34_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3);
  95. CORE_PIN33_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);
  96. UART4_BDH = (divisor >> 13) & 0x1F;
  97. UART4_BDL = (divisor >> 5) & 0xFF;
  98. UART4_C4 = divisor & 0x1F;
  99. UART4_C1 = 0;
  100. UART4_PFIFO = 0;
  101. UART4_C2 = C2_TX_INACTIVE;
  102. NVIC_SET_PRIORITY(IRQ_UART4_STATUS, IRQ_PRIORITY);
  103. NVIC_ENABLE_IRQ(IRQ_UART4_STATUS);
  104. }
  105. void serial5_format(uint32_t format)
  106. {
  107. uint8_t c;
  108. c = UART4_C1;
  109. c = (c & ~0x13) | (format & 0x03); // configure parity
  110. if (format & 0x04) c |= 0x10; // 9 bits (might include parity)
  111. UART4_C1 = c;
  112. if ((format & 0x0F) == 0x04) UART4_C3 |= 0x40; // 8N2 is 9 bit with 9th bit always 1
  113. c = UART4_S2 & ~0x10;
  114. if (format & 0x10) c |= 0x10; // rx invert
  115. UART4_S2 = c;
  116. c = UART4_C3 & ~0x10;
  117. if (format & 0x20) c |= 0x10; // tx invert
  118. UART4_C3 = c;
  119. #ifdef SERIAL_9BIT_SUPPORT
  120. c = UART4_C4 & 0x1F;
  121. if (format & 0x08) c |= 0x20; // 9 bit mode with parity (requires 10 bits)
  122. UART4_C4 = c;
  123. use9Bits = format & 0x80;
  124. #endif
  125. // For T3.5/T3.6 See about turning on 2 stop bit mode
  126. if ( format & 0x100) {
  127. uint8_t bdl = UART4_BDL;
  128. UART4_BDH |= UART_BDH_SBNS; // Turn on 2 stop bits - was turned off by set baud
  129. UART4_BDL = bdl; // Says BDH not acted on until BDL is written
  130. }
  131. }
  132. void serial5_end(void)
  133. {
  134. if (!(SIM_SCGC1 & SIM_SCGC1_UART4)) return;
  135. while (transmitting) yield(); // wait for buffered data to send
  136. NVIC_DISABLE_IRQ(IRQ_UART4_STATUS);
  137. UART4_C2 = 0;
  138. CORE_PIN34_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
  139. CORE_PIN33_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
  140. rx_buffer_head = 0;
  141. rx_buffer_tail = 0;
  142. if (rts_pin) rts_deassert();
  143. }
  144. void serial5_set_transmit_pin(uint8_t pin)
  145. {
  146. while (transmitting) ;
  147. pinMode(pin, OUTPUT);
  148. digitalWrite(pin, LOW);
  149. transmit_pin = portOutputRegister(pin);
  150. }
  151. void serial5_set_tx(uint8_t pin, uint8_t opendrain)
  152. {
  153. uint32_t cfg;
  154. if (opendrain) pin |= 128;
  155. if (pin == tx_pin_num) return;
  156. if ((SIM_SCGC1 & SIM_SCGC1_UART4)) {
  157. switch (tx_pin_num & 127) {
  158. case 33: CORE_PIN33_CONFIG = 0; break; // PTE24
  159. }
  160. if (opendrain) {
  161. cfg = PORT_PCR_DSE | PORT_PCR_ODE;
  162. } else {
  163. cfg = PORT_PCR_DSE | PORT_PCR_SRE;
  164. }
  165. switch (pin & 127) {
  166. case 33: CORE_PIN33_CONFIG = cfg | PORT_PCR_MUX(3); break;
  167. }
  168. }
  169. tx_pin_num = pin;
  170. }
  171. void serial5_set_rx(uint8_t pin)
  172. {
  173. }
  174. int serial5_set_rts(uint8_t pin)
  175. {
  176. if (!(SIM_SCGC1 & SIM_SCGC1_UART4)) return 0;
  177. if (pin < CORE_NUM_DIGITAL) {
  178. rts_pin = portOutputRegister(pin);
  179. pinMode(pin, OUTPUT);
  180. rts_assert();
  181. } else {
  182. rts_pin = NULL;
  183. return 0;
  184. }
  185. return 1;
  186. }
  187. int serial5_set_cts(uint8_t pin)
  188. {
  189. if (!(SIM_SCGC1 & SIM_SCGC1_UART4)) return 0;
  190. if (pin == 24) {
  191. CORE_PIN24_CONFIG = PORT_PCR_MUX(3) | PORT_PCR_PE; // weak pulldown
  192. } else {
  193. UART4_MODEM &= ~UART_MODEM_TXCTSE;
  194. return 0;
  195. }
  196. UART4_MODEM |= UART_MODEM_TXCTSE;
  197. return 1;
  198. }
  199. void serial5_putchar(uint32_t c)
  200. {
  201. uint32_t head, n;
  202. if (!(SIM_SCGC1 & SIM_SCGC1_UART4)) return;
  203. if (transmit_pin) transmit_assert();
  204. head = tx_buffer_head;
  205. if (++head >= SERIAL5_TX_BUFFER_SIZE) head = 0;
  206. while (tx_buffer_tail == head) {
  207. int priority = nvic_execution_priority();
  208. if (priority <= IRQ_PRIORITY) {
  209. if ((UART4_S1 & UART_S1_TDRE)) {
  210. uint32_t tail = tx_buffer_tail;
  211. if (++tail >= SERIAL5_TX_BUFFER_SIZE) tail = 0;
  212. n = tx_buffer[tail];
  213. if (use9Bits) UART4_C3 = (UART4_C3 & ~0x40) | ((n & 0x100) >> 2);
  214. UART4_D = n;
  215. tx_buffer_tail = tail;
  216. }
  217. } else if (priority >= 256) {
  218. yield(); // wait
  219. }
  220. }
  221. tx_buffer[head] = c;
  222. transmitting = 1;
  223. tx_buffer_head = head;
  224. UART4_C2 = C2_TX_ACTIVE;
  225. }
  226. void serial5_write(const void *buf, unsigned int count)
  227. {
  228. const uint8_t *p = (const uint8_t *)buf;
  229. while (count-- > 0) serial5_putchar(*p++);
  230. }
  231. void serial5_flush(void)
  232. {
  233. while (transmitting) yield(); // wait
  234. }
  235. int serial5_write_buffer_free(void)
  236. {
  237. uint32_t head, tail;
  238. head = tx_buffer_head;
  239. tail = tx_buffer_tail;
  240. if (head >= tail) return SERIAL5_TX_BUFFER_SIZE - 1 - head + tail;
  241. return tail - head - 1;
  242. }
  243. int serial5_available(void)
  244. {
  245. uint32_t head, tail;
  246. head = rx_buffer_head;
  247. tail = rx_buffer_tail;
  248. if (head >= tail) return head - tail;
  249. return SERIAL5_RX_BUFFER_SIZE + head - tail;
  250. }
  251. int serial5_getchar(void)
  252. {
  253. uint32_t head, tail;
  254. int c;
  255. head = rx_buffer_head;
  256. tail = rx_buffer_tail;
  257. if (head == tail) return -1;
  258. if (++tail >= SERIAL5_RX_BUFFER_SIZE) tail = 0;
  259. c = rx_buffer[tail];
  260. rx_buffer_tail = tail;
  261. if (rts_pin) {
  262. int avail;
  263. if (head >= tail) avail = head - tail;
  264. else avail = SERIAL5_RX_BUFFER_SIZE + head - tail;
  265. if (avail <= RTS_LOW_WATERMARK) rts_assert();
  266. }
  267. return c;
  268. }
  269. int serial5_peek(void)
  270. {
  271. uint32_t head, tail;
  272. head = rx_buffer_head;
  273. tail = rx_buffer_tail;
  274. if (head == tail) return -1;
  275. if (++tail >= SERIAL5_RX_BUFFER_SIZE) tail = 0;
  276. return rx_buffer[tail];
  277. }
  278. void serial5_clear(void)
  279. {
  280. rx_buffer_head = rx_buffer_tail;
  281. if (rts_pin) rts_assert();
  282. }
  283. // status interrupt combines
  284. // Transmit data below watermark UART_S1_TDRE
  285. // Transmit complete UART_S1_TC
  286. // Idle line UART_S1_IDLE
  287. // Receive data above watermark UART_S1_RDRF
  288. // LIN break detect UART_S2_LBKDIF
  289. // RxD pin active edge UART_S2_RXEDGIF
  290. void uart4_status_isr(void)
  291. {
  292. uint32_t head, tail, n;
  293. uint8_t c;
  294. if (UART4_S1 & UART_S1_RDRF) {
  295. if (use9Bits && (UART4_C3 & 0x80)) {
  296. n = UART4_D | 0x100;
  297. } else {
  298. n = UART4_D;
  299. }
  300. head = rx_buffer_head + 1;
  301. if (head >= SERIAL5_RX_BUFFER_SIZE) head = 0;
  302. if (head != rx_buffer_tail) {
  303. rx_buffer[head] = n;
  304. rx_buffer_head = head;
  305. }
  306. if (rts_pin) {
  307. int avail;
  308. tail = tx_buffer_tail;
  309. if (head >= tail) avail = head - tail;
  310. else avail = SERIAL5_RX_BUFFER_SIZE + head - tail;
  311. if (avail >= RTS_HIGH_WATERMARK) rts_deassert();
  312. }
  313. }
  314. c = UART4_C2;
  315. if ((c & UART_C2_TIE) && (UART4_S1 & UART_S1_TDRE)) {
  316. head = tx_buffer_head;
  317. tail = tx_buffer_tail;
  318. if (head == tail) {
  319. UART4_C2 = C2_TX_COMPLETING;
  320. } else {
  321. if (++tail >= SERIAL5_TX_BUFFER_SIZE) tail = 0;
  322. n = tx_buffer[tail];
  323. if (use9Bits) UART4_C3 = (UART4_C3 & ~0x40) | ((n & 0x100) >> 2);
  324. UART4_D = n;
  325. tx_buffer_tail = tail;
  326. }
  327. }
  328. if ((c & UART_C2_TCIE) && (UART4_S1 & UART_S1_TC)) {
  329. transmitting = 0;
  330. if (transmit_pin) transmit_deassert();
  331. UART4_C2 = C2_TX_INACTIVE;
  332. }
  333. }
  334. #endif // HAS_KINETISK_UART4