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.

519 line
15KB

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