Teensy 4.1 core updated for C++20
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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