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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. UART2_S1;
  186. UART2_D; // clear leftover error status
  187. rx_buffer_head = 0;
  188. rx_buffer_tail = 0;
  189. if (rts_pin) rts_deassert();
  190. }
  191. void serial3_set_transmit_pin(uint8_t pin)
  192. {
  193. while (transmitting) ;
  194. pinMode(pin, OUTPUT);
  195. digitalWrite(pin, LOW);
  196. transmit_pin = portOutputRegister(pin);
  197. #if defined(KINETISL)
  198. transmit_mask = digitalPinToBitMask(pin);
  199. #endif
  200. }
  201. void serial3_set_tx(uint8_t pin, uint8_t opendrain)
  202. {
  203. uint32_t cfg;
  204. if (opendrain) pin |= 128;
  205. if (pin == tx_pin_num) return;
  206. if ((SIM_SCGC4 & SIM_SCGC4_UART2)) {
  207. switch (tx_pin_num & 127) {
  208. case 8: CORE_PIN8_CONFIG = 0; break; // PTD3
  209. #if defined(KINETISL)
  210. case 20: CORE_PIN20_CONFIG = 0; break; // PTD5
  211. #endif
  212. }
  213. if (opendrain) {
  214. cfg = PORT_PCR_DSE | PORT_PCR_ODE;
  215. } else {
  216. cfg = PORT_PCR_DSE | PORT_PCR_SRE;
  217. }
  218. switch (pin & 127) {
  219. case 8: CORE_PIN8_CONFIG = cfg | PORT_PCR_MUX(3); break;
  220. #if defined(KINETISL)
  221. case 20: CORE_PIN20_CONFIG = cfg | PORT_PCR_MUX(3); break;
  222. #endif
  223. }
  224. }
  225. tx_pin_num = pin;
  226. }
  227. void serial3_set_rx(uint8_t pin)
  228. {
  229. #if defined(KINETISL)
  230. if (pin == rx_pin_num) return;
  231. if ((SIM_SCGC4 & SIM_SCGC4_UART2)) {
  232. switch (rx_pin_num) {
  233. case 7: CORE_PIN7_CONFIG = 0; break; // PTD2
  234. case 6: CORE_PIN6_CONFIG = 0; break; // PTD4
  235. }
  236. switch (pin) {
  237. case 7: CORE_PIN7_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  238. case 6: CORE_PIN6_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  239. }
  240. }
  241. rx_pin_num = pin;
  242. #endif
  243. }
  244. int serial3_set_rts(uint8_t pin)
  245. {
  246. if (!(SIM_SCGC4 & SIM_SCGC4_UART2)) return 0;
  247. if (pin < CORE_NUM_DIGITAL) {
  248. rts_pin = portOutputRegister(pin);
  249. #if defined(KINETISL)
  250. rts_mask = digitalPinToBitMask(pin);
  251. #endif
  252. pinMode(pin, OUTPUT);
  253. rts_assert();
  254. } else {
  255. rts_pin = NULL;
  256. return 0;
  257. }
  258. /*
  259. if (pin == 2) {
  260. CORE_PIN2_CONFIG = PORT_PCR_MUX(3);
  261. } else {
  262. UART2_MODEM &= ~UART_MODEM_RXRTSE;
  263. return 0;
  264. }
  265. UART2_MODEM |= UART_MODEM_RXRTSE;
  266. */
  267. return 1;
  268. }
  269. int serial3_set_cts(uint8_t pin)
  270. {
  271. #if defined(KINETISK)
  272. if (!(SIM_SCGC4 & SIM_SCGC4_UART2)) return 0;
  273. if (pin == 14) {
  274. CORE_PIN14_CONFIG = PORT_PCR_MUX(3) | PORT_PCR_PE; // weak pulldown
  275. } else {
  276. UART2_MODEM &= ~UART_MODEM_TXCTSE;
  277. return 0;
  278. }
  279. UART2_MODEM |= UART_MODEM_TXCTSE;
  280. return 1;
  281. #else
  282. return 0;
  283. #endif
  284. }
  285. void serial3_putchar(uint32_t c)
  286. {
  287. uint32_t head, n;
  288. if (!(SIM_SCGC4 & SIM_SCGC4_UART2)) return;
  289. if (transmit_pin) transmit_assert();
  290. head = tx_buffer_head;
  291. if (++head >= SERIAL3_TX_BUFFER_SIZE) head = 0;
  292. while (tx_buffer_tail == head) {
  293. int priority = nvic_execution_priority();
  294. if (priority <= IRQ_PRIORITY) {
  295. if ((UART2_S1 & UART_S1_TDRE)) {
  296. uint32_t tail = tx_buffer_tail;
  297. if (++tail >= SERIAL3_TX_BUFFER_SIZE) tail = 0;
  298. n = tx_buffer[tail];
  299. if (use9Bits) UART2_C3 = (UART2_C3 & ~0x40) | ((n & 0x100) >> 2);
  300. UART2_D = n;
  301. tx_buffer_tail = tail;
  302. }
  303. } else if (priority >= 256) {
  304. yield(); // wait
  305. }
  306. }
  307. tx_buffer[head] = c;
  308. transmitting = 1;
  309. tx_buffer_head = head;
  310. UART2_C2 = C2_TX_ACTIVE;
  311. }
  312. void serial3_write(const void *buf, unsigned int count)
  313. {
  314. const uint8_t *p = (const uint8_t *)buf;
  315. while (count-- > 0) serial3_putchar(*p++);
  316. }
  317. void serial3_flush(void)
  318. {
  319. while (transmitting) yield(); // wait
  320. }
  321. int serial3_write_buffer_free(void)
  322. {
  323. uint32_t head, tail;
  324. head = tx_buffer_head;
  325. tail = tx_buffer_tail;
  326. if (head >= tail) return SERIAL3_TX_BUFFER_SIZE - 1 - head + tail;
  327. return tail - head - 1;
  328. }
  329. int serial3_available(void)
  330. {
  331. uint32_t head, tail;
  332. head = rx_buffer_head;
  333. tail = rx_buffer_tail;
  334. if (head >= tail) return head - tail;
  335. return SERIAL3_RX_BUFFER_SIZE + head - tail;
  336. }
  337. int serial3_getchar(void)
  338. {
  339. uint32_t head, tail;
  340. int c;
  341. head = rx_buffer_head;
  342. tail = rx_buffer_tail;
  343. if (head == tail) return -1;
  344. if (++tail >= SERIAL3_RX_BUFFER_SIZE) tail = 0;
  345. c = rx_buffer[tail];
  346. rx_buffer_tail = tail;
  347. if (rts_pin) {
  348. int avail;
  349. if (head >= tail) avail = head - tail;
  350. else avail = SERIAL3_RX_BUFFER_SIZE + head - tail;
  351. if (avail <= RTS_LOW_WATERMARK) rts_assert();
  352. }
  353. return c;
  354. }
  355. int serial3_peek(void)
  356. {
  357. uint32_t head, tail;
  358. head = rx_buffer_head;
  359. tail = rx_buffer_tail;
  360. if (head == tail) return -1;
  361. if (++tail >= SERIAL3_RX_BUFFER_SIZE) tail = 0;
  362. return rx_buffer[tail];
  363. }
  364. void serial3_clear(void)
  365. {
  366. rx_buffer_head = rx_buffer_tail;
  367. if (rts_pin) rts_assert();
  368. }
  369. // status interrupt combines
  370. // Transmit data below watermark UART_S1_TDRE
  371. // Transmit complete UART_S1_TC
  372. // Idle line UART_S1_IDLE
  373. // Receive data above watermark UART_S1_RDRF
  374. // LIN break detect UART_S2_LBKDIF
  375. // RxD pin active edge UART_S2_RXEDGIF
  376. void uart2_status_isr(void)
  377. {
  378. uint32_t head, tail, n;
  379. uint8_t c;
  380. if (UART2_S1 & UART_S1_RDRF) {
  381. if (use9Bits && (UART2_C3 & 0x80)) {
  382. n = UART2_D | 0x100;
  383. } else {
  384. n = UART2_D;
  385. }
  386. head = rx_buffer_head + 1;
  387. if (head >= SERIAL3_RX_BUFFER_SIZE) head = 0;
  388. if (head != rx_buffer_tail) {
  389. rx_buffer[head] = n;
  390. rx_buffer_head = head;
  391. }
  392. if (rts_pin) {
  393. int avail;
  394. tail = tx_buffer_tail;
  395. if (head >= tail) avail = head - tail;
  396. else avail = SERIAL3_RX_BUFFER_SIZE + head - tail;
  397. if (avail >= RTS_HIGH_WATERMARK) rts_deassert();
  398. }
  399. }
  400. c = UART2_C2;
  401. if ((c & UART_C2_TIE) && (UART2_S1 & UART_S1_TDRE)) {
  402. head = tx_buffer_head;
  403. tail = tx_buffer_tail;
  404. if (head == tail) {
  405. UART2_C2 = C2_TX_COMPLETING;
  406. } else {
  407. if (++tail >= SERIAL3_TX_BUFFER_SIZE) tail = 0;
  408. n = tx_buffer[tail];
  409. if (use9Bits) UART2_C3 = (UART2_C3 & ~0x40) | ((n & 0x100) >> 2);
  410. UART2_D = n;
  411. tx_buffer_tail = tail;
  412. }
  413. }
  414. if ((c & UART_C2_TCIE) && (UART2_S1 & UART_S1_TC)) {
  415. transmitting = 0;
  416. if (transmit_pin) transmit_deassert();
  417. UART2_C2 = C2_TX_INACTIVE;
  418. }
  419. }