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.

383 lines
11KB

  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 "mk20dx128.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 TX_BUFFER_SIZE > 255
  53. static volatile uint16_t tx_buffer_head = 0;
  54. static volatile uint16_t tx_buffer_tail = 0;
  55. #else
  56. static volatile uint8_t tx_buffer_head = 0;
  57. static volatile uint8_t tx_buffer_tail = 0;
  58. #endif
  59. #if RX_BUFFER_SIZE > 255
  60. static volatile uint16_t rx_buffer_head = 0;
  61. static volatile uint16_t rx_buffer_tail = 0;
  62. #else
  63. static volatile uint8_t rx_buffer_head = 0;
  64. static volatile uint8_t rx_buffer_tail = 0;
  65. #endif
  66. // UART0 and UART1 are clocked by F_CPU, UART2 is clocked by F_BUS
  67. // UART0 has 8 byte fifo, UART1 and UART2 have 1 byte buffer
  68. #define C2_ENABLE UART_C2_TE | UART_C2_RE | UART_C2_RIE | UART_C2_ILIE
  69. #define C2_TX_ACTIVE C2_ENABLE | UART_C2_TIE
  70. #define C2_TX_COMPLETING C2_ENABLE | UART_C2_TCIE
  71. #define C2_TX_INACTIVE C2_ENABLE
  72. void serial_begin(uint32_t divisor)
  73. {
  74. SIM_SCGC4 |= SIM_SCGC4_UART0; // turn on clock, TODO: use bitband
  75. rx_buffer_head = 0;
  76. rx_buffer_tail = 0;
  77. tx_buffer_head = 0;
  78. tx_buffer_tail = 0;
  79. transmitting = 0;
  80. CORE_PIN0_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3);
  81. CORE_PIN1_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);
  82. UART0_BDH = (divisor >> 13) & 0x1F;
  83. UART0_BDL = (divisor >> 5) & 0xFF;
  84. UART0_C4 = divisor & 0x1F;
  85. //UART0_C1 = 0;
  86. UART0_C1 = UART_C1_ILT;
  87. UART0_TWFIFO = 2; // tx watermark, causes S1_TDRE to set
  88. UART0_RWFIFO = 4; // rx watermark, causes S1_RDRF to set
  89. UART0_PFIFO = UART_PFIFO_TXFE | UART_PFIFO_RXFE;
  90. UART0_C2 = C2_TX_INACTIVE;
  91. NVIC_SET_PRIORITY(IRQ_UART0_STATUS, IRQ_PRIORITY);
  92. NVIC_ENABLE_IRQ(IRQ_UART0_STATUS);
  93. }
  94. void serial_format(uint32_t format)
  95. {
  96. uint8_t c;
  97. c = UART0_C1;
  98. c = (c & ~0x13) | (format & 0x03); // configure parity
  99. if (format & 0x04) c |= 0x10; // 9 bits (might include parity)
  100. UART0_C1 = c;
  101. if ((format & 0x0F) == 0x04) UART0_C3 |= 0x40; // 8N2 is 9 bit with 9th bit always 1
  102. c = UART0_S2 & ~0x10;
  103. if (format & 0x10) c |= 0x10; // rx invert
  104. UART0_S2 = c;
  105. c = UART0_C3 & ~0x10;
  106. if (format & 0x20) c |= 0x10; // tx invert
  107. UART0_C3 = c;
  108. #ifdef SERIAL_9BIT_SUPPORT
  109. c = UART0_C4 & 0x1F;
  110. if (format & 0x08) c |= 0x20; // 9 bit mode with parity (requires 10 bits)
  111. UART0_C4 = c;
  112. use9Bits = format & 0x80;
  113. #endif
  114. }
  115. void serial_end(void)
  116. {
  117. if (!(SIM_SCGC4 & SIM_SCGC4_UART0)) return;
  118. while (transmitting) yield(); // wait for buffered data to send
  119. NVIC_DISABLE_IRQ(IRQ_UART0_STATUS);
  120. UART0_C2 = 0;
  121. CORE_PIN0_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
  122. CORE_PIN1_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
  123. rx_buffer_head = 0;
  124. rx_buffer_tail = 0;
  125. }
  126. static int get_nvic_execution_priority(void)
  127. {
  128. int priority=256;
  129. uint32_t primask, faultmask, basepri, ipsr;
  130. // full algorithm in ARM DDI0403D, page B1-639
  131. // this isn't quite complete, but hopefully good enough
  132. asm volatile("mrs %0, faultmask\n" : "=r" (faultmask)::);
  133. if (faultmask) return -1;
  134. asm volatile("mrs %0, primask\n" : "=r" (primask)::);
  135. if (primask) return 0;
  136. asm volatile("mrs %0, ipsr\n" : "=r" (ipsr)::);
  137. if (ipsr) {
  138. if (ipsr < 16) priority = 0; // could be non-zero
  139. else priority = NVIC_GET_PRIORITY(ipsr - 16);
  140. }
  141. asm volatile("mrs %0, basepri\n" : "=r" (basepri)::);
  142. if (basepri > 0 && basepri < priority) priority = basepri;
  143. return priority;
  144. }
  145. void serial_putchar(uint32_t c)
  146. {
  147. uint32_t head;
  148. if (!(SIM_SCGC4 & SIM_SCGC4_UART0)) return;
  149. head = tx_buffer_head;
  150. if (++head >= TX_BUFFER_SIZE) head = 0;
  151. while (tx_buffer_tail == head) {
  152. if (get_nvic_execution_priority() <= IRQ_PRIORITY) {
  153. if ((UART0_S1 & UART_S1_TDRE)) {
  154. uint32_t tail = tx_buffer_tail;
  155. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  156. UART0_D = tx_buffer[tail];
  157. tx_buffer_tail = tail;
  158. }
  159. } else {
  160. yield();
  161. }
  162. }
  163. tx_buffer[head] = c;
  164. transmitting = 1;
  165. tx_buffer_head = head;
  166. UART0_C2 = C2_TX_ACTIVE;
  167. }
  168. void serial_write(const void *buf, unsigned int count)
  169. {
  170. const uint8_t *p = (const uint8_t *)buf;
  171. const uint8_t *end = p + count;
  172. uint32_t head;
  173. if (!(SIM_SCGC4 & SIM_SCGC4_UART0)) return;
  174. while (p < end) {
  175. head = tx_buffer_head;
  176. if (++head >= TX_BUFFER_SIZE) head = 0;
  177. if (tx_buffer_tail == head) {
  178. UART0_C2 = C2_TX_ACTIVE;
  179. do {
  180. if (get_nvic_execution_priority() <= IRQ_PRIORITY) {
  181. if ((UART0_S1 & UART_S1_TDRE)) {
  182. uint32_t tail = tx_buffer_tail;
  183. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  184. UART0_D = tx_buffer[tail];
  185. tx_buffer_tail = tail;
  186. }
  187. } else {
  188. yield();
  189. }
  190. } while (tx_buffer_tail == head);
  191. }
  192. tx_buffer[head] = *p++;
  193. transmitting = 1;
  194. tx_buffer_head = head;
  195. }
  196. UART0_C2 = C2_TX_ACTIVE;
  197. }
  198. void serial_flush(void)
  199. {
  200. while (transmitting) yield(); // wait
  201. }
  202. int serial_available(void)
  203. {
  204. uint32_t head, tail;
  205. head = rx_buffer_head;
  206. tail = rx_buffer_tail;
  207. if (head >= tail) return head - tail;
  208. return RX_BUFFER_SIZE + head - tail;
  209. }
  210. int serial_getchar(void)
  211. {
  212. uint32_t head, tail;
  213. int c;
  214. head = rx_buffer_head;
  215. tail = rx_buffer_tail;
  216. if (head == tail) return -1;
  217. if (++tail >= RX_BUFFER_SIZE) tail = 0;
  218. c = rx_buffer[tail];
  219. rx_buffer_tail = tail;
  220. return c;
  221. }
  222. int serial_peek(void)
  223. {
  224. uint32_t head, tail;
  225. head = rx_buffer_head;
  226. tail = rx_buffer_tail;
  227. if (head == tail) return -1;
  228. if (++tail >= RX_BUFFER_SIZE) tail = 0;
  229. return rx_buffer[tail];
  230. }
  231. void serial_clear(void)
  232. {
  233. if (!(SIM_SCGC4 & SIM_SCGC4_UART0)) return;
  234. UART0_C2 &= ~(UART_C2_RE | UART_C2_RIE | UART_C2_ILIE);
  235. UART0_CFIFO = UART_CFIFO_RXFLUSH;
  236. UART0_C2 |= (UART_C2_RE | UART_C2_RIE | UART_C2_ILIE);
  237. rx_buffer_head = rx_buffer_tail;
  238. }
  239. // status interrupt combines
  240. // Transmit data below watermark UART_S1_TDRE
  241. // Transmit complete UART_S1_TC
  242. // Idle line UART_S1_IDLE
  243. // Receive data above watermark UART_S1_RDRF
  244. // LIN break detect UART_S2_LBKDIF
  245. // RxD pin active edge UART_S2_RXEDGIF
  246. void uart0_status_isr(void)
  247. {
  248. uint32_t head, newhead, tail, n;
  249. uint8_t avail, c;
  250. if (UART0_S1 & (UART_S1_RDRF | UART_S1_IDLE)) {
  251. __disable_irq();
  252. avail = UART0_RCFIFO;
  253. if (avail == 0) {
  254. // The only way to clear the IDLE interrupt flag is
  255. // to read the data register. But reading with no
  256. // data causes a FIFO underrun, which causes the
  257. // FIFO to return corrupted data. If anyone from
  258. // Freescale reads this, what a poor design! There
  259. // write should be a write-1-to-clear for IDLE.
  260. c = UART0_D;
  261. // flushing the fifo recovers from the underrun,
  262. // but there's a possible race condition where a
  263. // new character could be received between reading
  264. // RCFIFO == 0 and flushing the FIFO. To minimize
  265. // the chance, interrupts are disabled so a higher
  266. // priority interrupt (hopefully) doesn't delay.
  267. // TODO: change this to disabling the IDLE interrupt
  268. // which won't be simple, since we already manage
  269. // which transmit interrupts are enabled.
  270. UART0_CFIFO = UART_CFIFO_RXFLUSH;
  271. __enable_irq();
  272. } else {
  273. __enable_irq();
  274. head = rx_buffer_head;
  275. tail = rx_buffer_tail;
  276. do {
  277. n = UART0_D;
  278. if (use9Bits && (UART0_C3 & 0x80)) n |= 0x100;
  279. newhead = head + 1;
  280. if (newhead >= RX_BUFFER_SIZE) newhead = 0;
  281. if (newhead != tail) {
  282. head = newhead;
  283. rx_buffer[head] = n;
  284. }
  285. } while (--avail > 0);
  286. rx_buffer_head = head;
  287. }
  288. }
  289. c = UART0_C2;
  290. if ((c & UART_C2_TIE) && (UART0_S1 & UART_S1_TDRE)) {
  291. head = tx_buffer_head;
  292. tail = tx_buffer_tail;
  293. do {
  294. if (tail == head) break;
  295. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  296. avail = UART0_S1;
  297. n = tx_buffer[tail];
  298. if (use9Bits) UART0_C3 = (UART0_C3 & ~0x40) | ((n & 0x100) >> 2);
  299. UART0_D = n;
  300. } while (UART0_TCFIFO < 8);
  301. tx_buffer_tail = tail;
  302. if (UART0_S1 & UART_S1_TDRE) UART0_C2 = C2_TX_COMPLETING;
  303. }
  304. if ((c & UART_C2_TCIE) && (UART0_S1 & UART_S1_TC)) {
  305. transmitting = 0;
  306. UART0_C2 = C2_TX_INACTIVE;
  307. }
  308. }
  309. void serial_print(const char *p)
  310. {
  311. while (*p) {
  312. char c = *p++;
  313. if (c == '\n') serial_putchar('\r');
  314. serial_putchar(c);
  315. }
  316. }
  317. static void serial_phex1(uint32_t n)
  318. {
  319. n &= 15;
  320. if (n < 10) {
  321. serial_putchar('0' + n);
  322. } else {
  323. serial_putchar('A' - 10 + n);
  324. }
  325. }
  326. void serial_phex(uint32_t n)
  327. {
  328. serial_phex1(n >> 4);
  329. serial_phex1(n);
  330. }
  331. void serial_phex16(uint32_t n)
  332. {
  333. serial_phex(n >> 8);
  334. serial_phex(n);
  335. }
  336. void serial_phex32(uint32_t n)
  337. {
  338. serial_phex(n >> 24);
  339. serial_phex(n >> 16);
  340. serial_phex(n >> 8);
  341. serial_phex(n);
  342. }