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.

349 line
9.6KB

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