Teensy 4.1 core updated for C++20
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.

486 lines
13KB

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