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

512 lines
14KB

  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. // alternate version supporting RTS by disable of receive interrupt
  31. // https://forum.pjrc.com/threads/30536-Complete-implementation-of-UART-hardware-flow-control
  32. // https://forum.pjrc.com/threads/29446-Teensy-Hardware-Flow-Control-RTS-CTS/page2
  33. #include "kinetis.h"
  34. #include "core_pins.h"
  35. #include "HardwareSerial.h"
  36. ////////////////////////////////////////////////////////////////
  37. // Tunable parameters (relatively safe to edit these numbers)
  38. ////////////////////////////////////////////////////////////////
  39. #ifndef SERIAL1_TX_BUFFER_SIZE
  40. #define SERIAL1_TX_BUFFER_SIZE 64 // number of outgoing bytes to buffer
  41. #endif
  42. #ifndef SERIAL1_RX_BUFFER_SIZE
  43. #define SERIAL1_RX_BUFFER_SIZE 64 // number of incoming bytes to buffer
  44. #endif
  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[SERIAL1_TX_BUFFER_SIZE];
  57. static volatile BUFTYPE rx_buffer[SERIAL1_RX_BUFFER_SIZE];
  58. static volatile uint8_t transmitting = 0;
  59. #if defined(KINETISK)
  60. static volatile uint8_t *transmit_pin=NULL;
  61. #define transmit_assert() *transmit_pin = 1
  62. #define transmit_deassert() *transmit_pin = 0
  63. #elif defined(KINETISL)
  64. static volatile uint8_t *transmit_pin=NULL;
  65. static uint8_t transmit_mask=0;
  66. #define transmit_assert() *(transmit_pin+4) = transmit_mask;
  67. #define transmit_deassert() *(transmit_pin+8) = transmit_mask;
  68. #endif
  69. #if SERIAL1_TX_BUFFER_SIZE > 255
  70. static volatile uint16_t tx_buffer_head = 0;
  71. static volatile uint16_t tx_buffer_tail = 0;
  72. #else
  73. static volatile uint8_t tx_buffer_head = 0;
  74. static volatile uint8_t tx_buffer_tail = 0;
  75. #endif
  76. #if SERIAL1_RX_BUFFER_SIZE > 255
  77. static volatile uint16_t rx_buffer_head = 0;
  78. static volatile uint16_t rx_buffer_tail = 0;
  79. #else
  80. static volatile uint8_t rx_buffer_head = 0;
  81. static volatile uint8_t rx_buffer_tail = 0;
  82. #endif
  83. // UART0 and UART1 are clocked by F_CPU, UART2 is clocked by F_BUS
  84. // UART0 has 8 byte fifo, UART1 and UART2 have 1 byte buffer
  85. void serial_begin(uint32_t divisor)
  86. {
  87. SIM_SCGC4 |= SIM_SCGC4_UART0; // turn on clock, TODO: use bitband
  88. rx_buffer_head = 0;
  89. rx_buffer_tail = 0;
  90. tx_buffer_head = 0;
  91. tx_buffer_tail = 0;
  92. transmitting = 0;
  93. CORE_PIN0_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3);
  94. CORE_PIN1_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);
  95. #if defined(HAS_KINETISK_UART0)
  96. UART0_BDH = (divisor >> 13) & 0x1F;
  97. UART0_BDL = (divisor >> 5) & 0xFF;
  98. UART0_C4 = divisor & 0x1F;
  99. #ifdef HAS_KINETISK_UART0_FIFO
  100. UART0_C1 = UART_C1_ILT;
  101. UART0_TWFIFO = 2; // tx watermark, causes S1_TDRE to set
  102. UART0_RWFIFO = 4; // rx watermark, causes S1_RDRF to set
  103. UART0_PFIFO = UART_PFIFO_TXFE | UART_PFIFO_RXFE;
  104. #else
  105. UART0_C1 = 0;
  106. UART0_PFIFO = 0;
  107. #endif
  108. #elif defined(HAS_KINETISL_UART0)
  109. UART0_BDH = (divisor >> 8) & 0x1F;
  110. UART0_BDL = divisor & 0xFF;
  111. UART0_C1 = 0;
  112. #endif
  113. UART0_C2 = UART_C2_TE | UART_C2_RE | UART_C2_RIE | UART_C2_ILIE;
  114. NVIC_SET_PRIORITY(IRQ_UART0_STATUS, IRQ_PRIORITY);
  115. NVIC_ENABLE_IRQ(IRQ_UART0_STATUS);
  116. }
  117. void serial_format(uint32_t format)
  118. {
  119. uint8_t c;
  120. c = UART0_C1;
  121. c = (c & ~0x13) | (format & 0x03); // configure parity
  122. if (format & 0x04) c |= 0x10; // 9 bits (might include parity)
  123. UART0_C1 = c;
  124. if ((format & 0x0F) == 0x04) UART0_C3 |= 0x40; // 8N2 is 9 bit with 9th bit always 1
  125. c = UART0_S2 & ~0x10;
  126. if (format & 0x10) c |= 0x10; // rx invert
  127. UART0_S2 = c;
  128. c = UART0_C3 & ~0x10;
  129. if (format & 0x20) c |= 0x10; // tx invert
  130. UART0_C3 = c;
  131. #ifdef SERIAL_9BIT_SUPPORT
  132. c = UART0_C4 & 0x1F;
  133. if (format & 0x08) c |= 0x20; // 9 bit mode with parity (requires 10 bits)
  134. UART0_C4 = c;
  135. use9Bits = format & 0x80;
  136. #endif
  137. }
  138. void serial_end(void)
  139. {
  140. if (!(SIM_SCGC4 & SIM_SCGC4_UART0)) return;
  141. while (transmitting) yield(); // wait for buffered data to send
  142. NVIC_DISABLE_IRQ(IRQ_UART0_STATUS);
  143. UART0_C2 = 0;
  144. CORE_PIN0_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
  145. CORE_PIN1_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
  146. rx_buffer_head = 0;
  147. rx_buffer_tail = 0;
  148. }
  149. void serial_set_transmit_pin(uint8_t pin)
  150. {
  151. while (transmitting) ;
  152. pinMode(pin, OUTPUT);
  153. digitalWrite(pin, LOW);
  154. transmit_pin = portOutputRegister(pin);
  155. #if defined(KINETISL)
  156. transmit_mask = digitalPinToBitMask(pin);
  157. #endif
  158. }
  159. int serial_set_rts(uint8_t pin)
  160. {
  161. if (!(SIM_SCGC4 & SIM_SCGC4_UART0)) return 0;
  162. if (pin == 6) {
  163. CORE_PIN6_CONFIG = PORT_PCR_MUX(3);
  164. } else if (pin == 19) {
  165. CORE_PIN19_CONFIG = PORT_PCR_MUX(3);
  166. } else {
  167. UART0_MODEM &= ~UART_MODEM_RXRTSE;
  168. return 0;
  169. }
  170. UART0_MODEM |= UART_MODEM_RXRTSE;
  171. return 1;
  172. }
  173. int serial_set_cts(uint8_t pin)
  174. {
  175. if (!(SIM_SCGC4 & SIM_SCGC4_UART0)) return 0;
  176. if (pin == 18) {
  177. CORE_PIN18_CONFIG = PORT_PCR_MUX(3); // TODO: weak pullup or pulldown?
  178. } else if (pin == 20) {
  179. CORE_PIN20_CONFIG = PORT_PCR_MUX(3); // TODO: weak pullup or pulldown?
  180. } else {
  181. UART0_MODEM &= ~UART_MODEM_TXCTSE;
  182. return 0;
  183. }
  184. UART0_MODEM |= UART_MODEM_TXCTSE;
  185. return 1;
  186. }
  187. void serial_putchar(uint32_t c)
  188. {
  189. uint32_t head, n;
  190. if (!(SIM_SCGC4 & SIM_SCGC4_UART0)) return;
  191. if (transmit_pin) transmit_assert();
  192. head = tx_buffer_head;
  193. if (++head >= SERIAL1_TX_BUFFER_SIZE) head = 0;
  194. while (tx_buffer_tail == head) {
  195. int priority = nvic_execution_priority();
  196. if (priority <= IRQ_PRIORITY) {
  197. if ((UART0_S1 & UART_S1_TDRE)) {
  198. uint32_t tail = tx_buffer_tail;
  199. if (++tail >= SERIAL1_TX_BUFFER_SIZE) tail = 0;
  200. n = tx_buffer[tail];
  201. if (use9Bits) UART0_C3 = (UART0_C3 & ~0x40) | ((n & 0x100) >> 2);
  202. UART0_D = n;
  203. tx_buffer_tail = tail;
  204. }
  205. } else if (priority >= 256) {
  206. yield();
  207. }
  208. }
  209. tx_buffer[head] = c;
  210. transmitting = 1;
  211. tx_buffer_head = head;
  212. UART0_C2 |= UART_C2_TIE;
  213. UART0_C2 &= ~UART_C2_TCIE;
  214. }
  215. #ifdef HAS_KINETISK_UART0_FIFO
  216. void serial_write(const void *buf, unsigned int count)
  217. {
  218. const uint8_t *p = (const uint8_t *)buf;
  219. const uint8_t *end = p + count;
  220. uint32_t head, n;
  221. if (!(SIM_SCGC4 & SIM_SCGC4_UART0)) return;
  222. if (transmit_pin) transmit_assert();
  223. while (p < end) {
  224. head = tx_buffer_head;
  225. if (++head >= SERIAL1_TX_BUFFER_SIZE) head = 0;
  226. if (tx_buffer_tail == head) {
  227. UART0_C2 |= UART_C2_TIE;
  228. UART0_C2 &= ~UART_C2_TCIE;
  229. do {
  230. int priority = nvic_execution_priority();
  231. if (priority <= IRQ_PRIORITY) {
  232. if ((UART0_S1 & UART_S1_TDRE)) {
  233. uint32_t tail = tx_buffer_tail;
  234. if (++tail >= SERIAL1_TX_BUFFER_SIZE) tail = 0;
  235. n = tx_buffer[tail];
  236. if (use9Bits) UART0_C3 = (UART0_C3 & ~0x40) | ((n & 0x100) >> 2);
  237. UART0_D = n;
  238. tx_buffer_tail = tail;
  239. }
  240. } else if (priority >= 256) {
  241. yield();
  242. }
  243. } while (tx_buffer_tail == head);
  244. }
  245. tx_buffer[head] = *p++;
  246. transmitting = 1;
  247. tx_buffer_head = head;
  248. }
  249. UART0_C2 |= UART_C2_TIE;
  250. UART0_C2 &= ~UART_C2_TCIE;
  251. }
  252. #else
  253. void serial_write(const void *buf, unsigned int count)
  254. {
  255. const uint8_t *p = (const uint8_t *)buf;
  256. while (count-- > 0) serial_putchar(*p++);
  257. }
  258. #endif
  259. void serial_flush(void)
  260. {
  261. while (transmitting) yield(); // wait
  262. }
  263. int serial_write_buffer_free(void)
  264. {
  265. uint32_t head, tail;
  266. head = tx_buffer_head;
  267. tail = tx_buffer_tail;
  268. if (head >= tail) return SERIAL1_TX_BUFFER_SIZE - 1 - head + tail;
  269. return tail - head - 1;
  270. }
  271. int serial_available(void)
  272. {
  273. uint32_t head, tail;
  274. head = rx_buffer_head;
  275. tail = rx_buffer_tail;
  276. if (head >= tail) return head - tail;
  277. return SERIAL1_RX_BUFFER_SIZE + head - tail;
  278. }
  279. int serial_getchar(void)
  280. {
  281. uint32_t head, tail;
  282. int c;
  283. head = rx_buffer_head;
  284. tail = rx_buffer_tail;
  285. if (head == tail) return -1;
  286. if (++tail >= SERIAL1_RX_BUFFER_SIZE) tail = 0;
  287. c = rx_buffer[tail];
  288. rx_buffer_tail = tail;
  289. #ifdef HAS_KINETISK_UART0_FIFO
  290. if ((UART0_C2 & (UART_C2_RIE | UART_C2_ILIE))==0) {//rx interrupt currently disabled
  291. int freespace;
  292. if (head >= tail) //rx head and tail would be unchanged from above if interrupts were disabled
  293. freespace = SERIAL1_RX_BUFFER_SIZE -1 + tail - head;
  294. else
  295. freespace = tail - head - 1;
  296. if (freespace >= UART0_RCFIFO) {
  297. UART0_C2 |= (UART_C2_RIE | UART_C2_ILIE);//enable rx interrupts
  298. }
  299. }
  300. #else
  301. UART0_C2 |= UART_C2_RIE;
  302. #endif
  303. return c;
  304. }
  305. int serial_peek(void)
  306. {
  307. uint32_t head, tail;
  308. head = rx_buffer_head;
  309. tail = rx_buffer_tail;
  310. if (head == tail) return -1;
  311. if (++tail >= SERIAL1_RX_BUFFER_SIZE) tail = 0;
  312. return rx_buffer[tail];
  313. }
  314. void serial_clear(void)
  315. {
  316. #ifdef HAS_KINETISK_UART0_FIFO
  317. if (!(SIM_SCGC4 & SIM_SCGC4_UART0)) return;
  318. UART0_C2 &= ~(UART_C2_RE | UART_C2_RIE | UART_C2_ILIE);
  319. UART0_CFIFO = UART_CFIFO_RXFLUSH;
  320. UART0_C2 |= (UART_C2_RE | UART_C2_RIE | UART_C2_ILIE);
  321. #endif
  322. rx_buffer_head = rx_buffer_tail;
  323. }
  324. // status interrupt combines
  325. // Transmit data below watermark UART_S1_TDRE
  326. // Transmit complete UART_S1_TC
  327. // Idle line UART_S1_IDLE
  328. // Receive data above watermark UART_S1_RDRF
  329. // LIN break detect UART_S2_LBKDIF
  330. // RxD pin active edge UART_S2_RXEDGIF
  331. void uart0_status_isr(void)
  332. {
  333. uint32_t head, tail, n;
  334. uint8_t c;
  335. #ifdef HAS_KINETISK_UART0_FIFO
  336. uint32_t newhead;
  337. if (UART0_S1 & (UART_S1_RDRF | UART_S1_IDLE)) {
  338. if (UART0_RCFIFO == 0) {
  339. // The only way to clear the IDLE interrupt flag is
  340. // to read the data register. But reading with no
  341. // data causes a FIFO underrun, which causes the
  342. // FIFO to return corrupted data. If anyone from
  343. // Freescale reads this, what a poor design! There
  344. // write should be a write-1-to-clear for IDLE.
  345. c = UART0_D;
  346. // flushing the fifo recovers from the underrun,
  347. // but there's a possible race condition where a
  348. // new character could be received between reading
  349. // RCFIFO == 0 and flushing the FIFO. To minimize
  350. // the chance, interrupts are disabled so a higher
  351. // priority interrupt (hopefully) doesn't delay.
  352. // TODO: change this to disabling the IDLE interrupt
  353. // which won't be simple, since we already manage
  354. // which transmit interrupts are enabled.
  355. __disable_irq();
  356. UART0_CFIFO = UART_CFIFO_RXFLUSH;
  357. __enable_irq();
  358. } else {
  359. head = rx_buffer_head;
  360. tail = rx_buffer_tail;
  361. do {
  362. newhead = head + 1;
  363. if (newhead >= SERIAL1_RX_BUFFER_SIZE) newhead = 0;
  364. if (UART0_MODEM & UART_MODEM_RXRTSE) {
  365. if (newhead == tail) {
  366. UART0_C2 &= ~(UART_C2_RIE | UART_C2_ILIE);//disable rx interrupts
  367. break;
  368. }
  369. }
  370. if (UART0_RCFIFO==1) UART0_S1; //as per page 1214 of datasheet regarding resetting of RDRF flag
  371. if (use9Bits && (UART0_C3 & 0x80)) {
  372. n = UART0_D | 0x100;
  373. } else {
  374. n = UART0_D;
  375. }
  376. head = newhead;
  377. rx_buffer[head] = n;
  378. } while (UART0_RCFIFO);
  379. rx_buffer_head = head;
  380. }
  381. }
  382. c = UART0_C2;
  383. if ((c & UART_C2_TIE) && (UART0_S1 & UART_S1_TDRE)) {
  384. head = tx_buffer_head;
  385. tail = tx_buffer_tail;
  386. do {
  387. if (tail == head) break;
  388. if (++tail >= SERIAL1_TX_BUFFER_SIZE) tail = 0;
  389. UART0_S1;
  390. n = tx_buffer[tail];
  391. if (use9Bits) UART0_C3 = (UART0_C3 & ~0x40) | ((n & 0x100) >> 2);
  392. UART0_D = n;
  393. } while (UART0_TCFIFO < 8);
  394. tx_buffer_tail = tail;
  395. if (UART0_S1 & UART_S1_TDRE) {
  396. UART0_C2 |= UART_C2_TCIE;
  397. UART0_C2 &= ~UART_C2_TIE;
  398. }
  399. }
  400. #else
  401. if (UART0_S1 & UART_S1_RDRF) {
  402. do {
  403. head = rx_buffer_head + 1;
  404. if (head >= SERIAL1_RX_BUFFER_SIZE) head = 0;
  405. if (UART0_MODEM & UART_MODEM_RXRTSE) {
  406. if (head == rx_buffer_tail) {
  407. UART0_C2 &= ~(UART_C2_RIE);//disable rx interrupts
  408. break;
  409. }
  410. }
  411. n = UART0_D;
  412. if (use9Bits && (UART0_C3 & 0x80)) n |= 0x100;
  413. rx_buffer[head] = n;
  414. rx_buffer_head = head;
  415. break;
  416. } while (true);
  417. }
  418. c = UART0_C2;
  419. if ((c & UART_C2_TIE) && (UART0_S1 & UART_S1_TDRE)) {
  420. head = tx_buffer_head;
  421. tail = tx_buffer_tail;
  422. if (head == tail) {
  423. UART0_C2 |= UART_C2_TCIE;
  424. UART0_C2 &= ~UART_C2_TIE;
  425. } else {
  426. if (++tail >= SERIAL1_TX_BUFFER_SIZE) tail = 0;
  427. n = tx_buffer[tail];
  428. if (use9Bits) UART0_C3 = (UART0_C3 & ~0x40) | ((n & 0x100) >> 2);
  429. UART0_D = n;
  430. tx_buffer_tail = tail;
  431. }
  432. }
  433. #endif
  434. if ((c & UART_C2_TCIE) && (UART0_S1 & UART_S1_TC)) {
  435. transmitting = 0;
  436. if (transmit_pin) transmit_deassert();
  437. UART0_C2 &= ~(UART_C2_TCIE | UART_C2_TIE);
  438. }
  439. }
  440. void serial_print(const char *p)
  441. {
  442. while (*p) {
  443. char c = *p++;
  444. if (c == '\n') serial_putchar('\r');
  445. serial_putchar(c);
  446. }
  447. }
  448. static void serial_phex1(uint32_t n)
  449. {
  450. n &= 15;
  451. if (n < 10) {
  452. serial_putchar('0' + n);
  453. } else {
  454. serial_putchar('A' - 10 + n);
  455. }
  456. }
  457. void serial_phex(uint32_t n)
  458. {
  459. serial_phex1(n >> 4);
  460. serial_phex1(n);
  461. }
  462. void serial_phex16(uint32_t n)
  463. {
  464. serial_phex(n >> 8);
  465. serial_phex(n);
  466. }
  467. void serial_phex32(uint32_t n)
  468. {
  469. serial_phex(n >> 24);
  470. serial_phex(n >> 16);
  471. serial_phex(n >> 8);
  472. serial_phex(n);
  473. }