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.

пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 9 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. ////////////////////////////////////////////////////////////////
  34. // Tunable parameters (relatively safe to edit these numbers)
  35. ////////////////////////////////////////////////////////////////
  36. #ifndef SERIAL3_TX_BUFFER_SIZE
  37. #define SERIAL3_TX_BUFFER_SIZE 40 // number of outgoing bytes to buffer
  38. #endif
  39. #ifndef SERIAL3_RX_BUFFER_SIZE
  40. #define SERIAL3_RX_BUFFER_SIZE 64 // number of incoming bytes to buffer
  41. #endif
  42. #define RTS_HIGH_WATERMARK (SERIAL3_RX_BUFFER_SIZE-24) // RTS requests sender to pause
  43. #define RTS_LOW_WATERMARK (SERIAL3_RX_BUFFER_SIZE-38) // RTS allows sender to resume
  44. #define IRQ_PRIORITY 64 // 0 = highest priority, 255 = lowest
  45. ////////////////////////////////////////////////////////////////
  46. // changes not recommended below this point....
  47. ////////////////////////////////////////////////////////////////
  48. #ifdef SERIAL_9BIT_SUPPORT
  49. static uint8_t use9Bits = 0;
  50. #define BUFTYPE uint16_t
  51. #else
  52. #define BUFTYPE uint8_t
  53. #define use9Bits 0
  54. #endif
  55. static volatile BUFTYPE tx_buffer[SERIAL3_TX_BUFFER_SIZE];
  56. static volatile BUFTYPE rx_buffer[SERIAL3_RX_BUFFER_SIZE];
  57. static volatile uint8_t transmitting = 0;
  58. #if defined(KINETISK)
  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. #elif defined(KINETISL)
  66. static volatile uint8_t *transmit_pin=NULL;
  67. static uint8_t transmit_mask=0;
  68. #define transmit_assert() *(transmit_pin+4) = transmit_mask;
  69. #define transmit_deassert() *(transmit_pin+8) = transmit_mask;
  70. static volatile uint8_t *rts_pin=NULL;
  71. static uint8_t rts_mask=0;
  72. #define rts_assert() *(rts_pin+8) = rts_mask;
  73. #define rts_deassert() *(rts_pin+4) = rts_mask;
  74. #endif
  75. #if SERIAL3_TX_BUFFER_SIZE > 65535
  76. static volatile uint32_t tx_buffer_head = 0;
  77. static volatile uint32_t tx_buffer_tail = 0;
  78. #elif SERIAL3_TX_BUFFER_SIZE > 255
  79. static volatile uint16_t tx_buffer_head = 0;
  80. static volatile uint16_t tx_buffer_tail = 0;
  81. #else
  82. static volatile uint8_t tx_buffer_head = 0;
  83. static volatile uint8_t tx_buffer_tail = 0;
  84. #endif
  85. #if SERIAL3_RX_BUFFER_SIZE > 65535
  86. static volatile uint32_t rx_buffer_head = 0;
  87. static volatile uint32_t rx_buffer_tail = 0;
  88. #elif SERIAL3_RX_BUFFER_SIZE > 255
  89. static volatile uint16_t rx_buffer_head = 0;
  90. static volatile uint16_t rx_buffer_tail = 0;
  91. #else
  92. static volatile uint8_t rx_buffer_head = 0;
  93. static volatile uint8_t rx_buffer_tail = 0;
  94. #endif
  95. #if defined(KINETISL)
  96. static uint8_t rx_pin_num = 7;
  97. #endif
  98. static uint8_t tx_pin_num = 8;
  99. // UART0 and UART1 are clocked by F_CPU, UART2 is clocked by F_BUS
  100. // UART0 has 8 byte fifo, UART1 and UART2 have 1 byte buffer
  101. #define C2_ENABLE UART_C2_TE | UART_C2_RE | UART_C2_RIE
  102. #define C2_TX_ACTIVE C2_ENABLE | UART_C2_TIE
  103. #define C2_TX_COMPLETING C2_ENABLE | UART_C2_TCIE
  104. #define C2_TX_INACTIVE C2_ENABLE
  105. void serial3_begin(uint32_t divisor)
  106. {
  107. SIM_SCGC4 |= SIM_SCGC4_UART2; // turn on clock, TODO: use bitband
  108. rx_buffer_head = 0;
  109. rx_buffer_tail = 0;
  110. tx_buffer_head = 0;
  111. tx_buffer_tail = 0;
  112. transmitting = 0;
  113. #if defined(KINETISK)
  114. CORE_PIN7_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3);
  115. CORE_PIN8_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);
  116. #elif defined(KINETISL)
  117. switch (rx_pin_num) {
  118. case 7: CORE_PIN7_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  119. case 6: CORE_PIN6_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  120. }
  121. switch (tx_pin_num) {
  122. case 8: CORE_PIN8_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); break;
  123. case 20: CORE_PIN20_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); break;
  124. }
  125. #endif
  126. #if defined(HAS_KINETISK_UART2)
  127. if (divisor < 32) divisor = 32;
  128. UART2_BDH = (divisor >> 13) & 0x1F;
  129. UART2_BDL = (divisor >> 5) & 0xFF;
  130. UART2_C4 = divisor & 0x1F;
  131. UART2_C1 = 0;
  132. UART2_PFIFO = 0;
  133. #elif defined(HAS_KINETISL_UART2)
  134. if (divisor < 1) divisor = 1;
  135. UART2_BDH = (divisor >> 8) & 0x1F;
  136. UART2_BDL = divisor & 0xFF;
  137. UART2_C1 = 0;
  138. #endif
  139. UART2_C2 = C2_TX_INACTIVE;
  140. NVIC_SET_PRIORITY(IRQ_UART2_STATUS, IRQ_PRIORITY);
  141. NVIC_ENABLE_IRQ(IRQ_UART2_STATUS);
  142. }
  143. void serial3_format(uint32_t format)
  144. {
  145. uint8_t c;
  146. c = UART2_C1;
  147. c = (c & ~0x13) | (format & 0x03); // configure parity
  148. if (format & 0x04) c |= 0x10; // 9 bits (might include parity)
  149. UART2_C1 = c;
  150. if ((format & 0x0F) == 0x04) UART2_C3 |= 0x40; // 8N2 is 9 bit with 9th bit always 1
  151. c = UART2_S2 & ~0x10;
  152. if (format & 0x10) c |= 0x10; // rx invert
  153. UART2_S2 = c;
  154. c = UART2_C3 & ~0x10;
  155. if (format & 0x20) c |= 0x10; // tx invert
  156. UART2_C3 = c;
  157. #if defined(SERIAL_9BIT_SUPPORT) && !defined(KINETISL)
  158. c = UART2_C4 & 0x1F;
  159. if (format & 0x08) c |= 0x20; // 9 bit mode with parity (requires 10 bits)
  160. UART2_C4 = c;
  161. use9Bits = format & 0x80;
  162. #endif
  163. #if defined(__MK64FX512__) || defined(__MK66FX1M0__) || defined(KINETISL)
  164. // For T3.5/T3.6/TLC See about turning on 2 stop bit mode
  165. if ( format & 0x100) {
  166. uint8_t bdl = UART2_BDL;
  167. UART2_BDH |= UART_BDH_SBNS; // Turn on 2 stop bits - was turned off by set baud
  168. UART2_BDL = bdl; // Says BDH not acted on until BDL is written
  169. }
  170. #endif
  171. }
  172. void serial3_end(void)
  173. {
  174. if (!(SIM_SCGC4 & SIM_SCGC4_UART2)) return;
  175. while (transmitting) yield(); // wait for buffered data to send
  176. NVIC_DISABLE_IRQ(IRQ_UART2_STATUS);
  177. UART2_C2 = 0;
  178. #if defined(KINETISK)
  179. CORE_PIN7_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
  180. CORE_PIN8_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
  181. #elif defined(KINETISL)
  182. switch (rx_pin_num) {
  183. case 7: CORE_PIN7_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break;
  184. case 6: CORE_PIN6_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break;
  185. }
  186. switch (tx_pin_num & 127) {
  187. case 8: CORE_PIN8_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break;
  188. case 20: CORE_PIN20_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break;
  189. }
  190. #endif
  191. UART2_S1;
  192. UART2_D; // clear leftover error status
  193. rx_buffer_head = 0;
  194. rx_buffer_tail = 0;
  195. if (rts_pin) rts_deassert();
  196. }
  197. void serial3_set_transmit_pin(uint8_t pin)
  198. {
  199. while (transmitting) ;
  200. pinMode(pin, OUTPUT);
  201. digitalWrite(pin, LOW);
  202. transmit_pin = portOutputRegister(pin);
  203. #if defined(KINETISL)
  204. transmit_mask = digitalPinToBitMask(pin);
  205. #endif
  206. }
  207. void serial3_set_tx(uint8_t pin, uint8_t opendrain)
  208. {
  209. uint32_t cfg;
  210. if (opendrain) pin |= 128;
  211. if (pin == tx_pin_num) return;
  212. if ((SIM_SCGC4 & SIM_SCGC4_UART2)) {
  213. switch (tx_pin_num & 127) {
  214. case 8: CORE_PIN8_CONFIG = 0; break; // PTD3
  215. #if defined(KINETISL)
  216. case 20: CORE_PIN20_CONFIG = 0; break; // PTD5
  217. #endif
  218. }
  219. if (opendrain) {
  220. cfg = PORT_PCR_DSE | PORT_PCR_ODE;
  221. } else {
  222. cfg = PORT_PCR_DSE | PORT_PCR_SRE;
  223. }
  224. switch (pin & 127) {
  225. case 8: CORE_PIN8_CONFIG = cfg | PORT_PCR_MUX(3); break;
  226. #if defined(KINETISL)
  227. case 20: CORE_PIN20_CONFIG = cfg | PORT_PCR_MUX(3); break;
  228. #endif
  229. }
  230. }
  231. tx_pin_num = pin;
  232. }
  233. void serial3_set_rx(uint8_t pin)
  234. {
  235. #if defined(KINETISL)
  236. if (pin == rx_pin_num) return;
  237. if ((SIM_SCGC4 & SIM_SCGC4_UART2)) {
  238. switch (rx_pin_num) {
  239. case 7: CORE_PIN7_CONFIG = 0; break; // PTD2
  240. case 6: CORE_PIN6_CONFIG = 0; break; // PTD4
  241. }
  242. switch (pin) {
  243. case 7: CORE_PIN7_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  244. case 6: CORE_PIN6_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  245. }
  246. }
  247. rx_pin_num = pin;
  248. #endif
  249. }
  250. int serial3_set_rts(uint8_t pin)
  251. {
  252. if (!(SIM_SCGC4 & SIM_SCGC4_UART2)) return 0;
  253. if (pin < CORE_NUM_DIGITAL) {
  254. rts_pin = portOutputRegister(pin);
  255. #if defined(KINETISL)
  256. rts_mask = digitalPinToBitMask(pin);
  257. #endif
  258. pinMode(pin, OUTPUT);
  259. rts_assert();
  260. } else {
  261. rts_pin = NULL;
  262. return 0;
  263. }
  264. /*
  265. if (pin == 2) {
  266. CORE_PIN2_CONFIG = PORT_PCR_MUX(3);
  267. } else {
  268. UART2_MODEM &= ~UART_MODEM_RXRTSE;
  269. return 0;
  270. }
  271. UART2_MODEM |= UART_MODEM_RXRTSE;
  272. */
  273. return 1;
  274. }
  275. int serial3_set_cts(uint8_t pin)
  276. {
  277. #if defined(KINETISK)
  278. if (!(SIM_SCGC4 & SIM_SCGC4_UART2)) return 0;
  279. if (pin == 14) {
  280. CORE_PIN14_CONFIG = PORT_PCR_MUX(3) | PORT_PCR_PE; // weak pulldown
  281. } else {
  282. UART2_MODEM &= ~UART_MODEM_TXCTSE;
  283. return 0;
  284. }
  285. UART2_MODEM |= UART_MODEM_TXCTSE;
  286. return 1;
  287. #else
  288. return 0;
  289. #endif
  290. }
  291. void serial3_putchar(uint32_t c)
  292. {
  293. uint32_t head, n;
  294. if (!(SIM_SCGC4 & SIM_SCGC4_UART2)) return;
  295. if (transmit_pin) transmit_assert();
  296. head = tx_buffer_head;
  297. if (++head >= SERIAL3_TX_BUFFER_SIZE) head = 0;
  298. while (tx_buffer_tail == head) {
  299. int priority = nvic_execution_priority();
  300. if (priority <= IRQ_PRIORITY) {
  301. if ((UART2_S1 & UART_S1_TDRE)) {
  302. uint32_t tail = tx_buffer_tail;
  303. if (++tail >= SERIAL3_TX_BUFFER_SIZE) tail = 0;
  304. n = tx_buffer[tail];
  305. if (use9Bits) UART2_C3 = (UART2_C3 & ~0x40) | ((n & 0x100) >> 2);
  306. UART2_D = n;
  307. tx_buffer_tail = tail;
  308. }
  309. } else if (priority >= 256) {
  310. yield(); // wait
  311. }
  312. }
  313. tx_buffer[head] = c;
  314. transmitting = 1;
  315. tx_buffer_head = head;
  316. UART2_C2 = C2_TX_ACTIVE;
  317. }
  318. void serial3_write(const void *buf, unsigned int count)
  319. {
  320. const uint8_t *p = (const uint8_t *)buf;
  321. while (count-- > 0) serial3_putchar(*p++);
  322. }
  323. void serial3_flush(void)
  324. {
  325. while (transmitting) yield(); // wait
  326. }
  327. int serial3_write_buffer_free(void)
  328. {
  329. uint32_t head, tail;
  330. head = tx_buffer_head;
  331. tail = tx_buffer_tail;
  332. if (head >= tail) return SERIAL3_TX_BUFFER_SIZE - 1 - head + tail;
  333. return tail - head - 1;
  334. }
  335. int serial3_available(void)
  336. {
  337. uint32_t head, tail;
  338. head = rx_buffer_head;
  339. tail = rx_buffer_tail;
  340. if (head >= tail) return head - tail;
  341. return SERIAL3_RX_BUFFER_SIZE + head - tail;
  342. }
  343. int serial3_getchar(void)
  344. {
  345. uint32_t head, tail;
  346. int c;
  347. head = rx_buffer_head;
  348. tail = rx_buffer_tail;
  349. if (head == tail) return -1;
  350. if (++tail >= SERIAL3_RX_BUFFER_SIZE) tail = 0;
  351. c = rx_buffer[tail];
  352. rx_buffer_tail = tail;
  353. if (rts_pin) {
  354. int avail;
  355. if (head >= tail) avail = head - tail;
  356. else avail = SERIAL3_RX_BUFFER_SIZE + head - tail;
  357. if (avail <= RTS_LOW_WATERMARK) rts_assert();
  358. }
  359. return c;
  360. }
  361. int serial3_peek(void)
  362. {
  363. uint32_t head, tail;
  364. head = rx_buffer_head;
  365. tail = rx_buffer_tail;
  366. if (head == tail) return -1;
  367. if (++tail >= SERIAL3_RX_BUFFER_SIZE) tail = 0;
  368. return rx_buffer[tail];
  369. }
  370. void serial3_clear(void)
  371. {
  372. rx_buffer_head = rx_buffer_tail;
  373. if (rts_pin) rts_assert();
  374. }
  375. // status interrupt combines
  376. // Transmit data below watermark UART_S1_TDRE
  377. // Transmit complete UART_S1_TC
  378. // Idle line UART_S1_IDLE
  379. // Receive data above watermark UART_S1_RDRF
  380. // LIN break detect UART_S2_LBKDIF
  381. // RxD pin active edge UART_S2_RXEDGIF
  382. void uart2_status_isr(void)
  383. {
  384. uint32_t head, tail, n;
  385. uint8_t c;
  386. if (UART2_S1 & UART_S1_RDRF) {
  387. if (use9Bits && (UART2_C3 & 0x80)) {
  388. n = UART2_D | 0x100;
  389. } else {
  390. n = UART2_D;
  391. }
  392. head = rx_buffer_head + 1;
  393. if (head >= SERIAL3_RX_BUFFER_SIZE) head = 0;
  394. if (head != rx_buffer_tail) {
  395. rx_buffer[head] = n;
  396. rx_buffer_head = head;
  397. }
  398. if (rts_pin) {
  399. int avail;
  400. tail = tx_buffer_tail;
  401. if (head >= tail) avail = head - tail;
  402. else avail = SERIAL3_RX_BUFFER_SIZE + head - tail;
  403. if (avail >= RTS_HIGH_WATERMARK) rts_deassert();
  404. }
  405. }
  406. c = UART2_C2;
  407. if ((c & UART_C2_TIE) && (UART2_S1 & UART_S1_TDRE)) {
  408. head = tx_buffer_head;
  409. tail = tx_buffer_tail;
  410. if (head == tail) {
  411. UART2_C2 = C2_TX_COMPLETING;
  412. } else {
  413. if (++tail >= SERIAL3_TX_BUFFER_SIZE) tail = 0;
  414. n = tx_buffer[tail];
  415. if (use9Bits) UART2_C3 = (UART2_C3 & ~0x40) | ((n & 0x100) >> 2);
  416. UART2_D = n;
  417. tx_buffer_tail = tail;
  418. }
  419. }
  420. if ((c & UART_C2_TCIE) && (UART2_S1 & UART_S1_TC)) {
  421. transmitting = 0;
  422. if (transmit_pin) transmit_deassert();
  423. UART2_C2 = C2_TX_INACTIVE;
  424. }
  425. }