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.

456 lines
13KB

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