Teensy 4.1 core updated for C++20
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

205 rindas
5.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. // UART0 and UART1 are clocked by F_CPU, UART2 is clocked by F_BUS
  34. // UART0 has 8 byte fifo, UART1 and UART2 have 1 byte buffer
  35. #define TX_BUFFER_SIZE 40
  36. static volatile uint8_t tx_buffer[TX_BUFFER_SIZE];
  37. static volatile uint8_t transmitting = 0;
  38. #if TX_BUFFER_SIZE > 255
  39. static volatile uint16_t tx_buffer_head = 0;
  40. static volatile uint16_t tx_buffer_tail = 0;
  41. #else
  42. static volatile uint8_t tx_buffer_head = 0;
  43. static volatile uint8_t tx_buffer_tail = 0;
  44. #endif
  45. #define RX_BUFFER_SIZE 64
  46. static volatile uint8_t rx_buffer[RX_BUFFER_SIZE];
  47. static volatile uint8_t rx_buffer_head = 0;
  48. static volatile uint8_t rx_buffer_tail = 0;
  49. #define C2_ENABLE UART_C2_TE | UART_C2_RE | UART_C2_RIE
  50. #define C2_TX_ACTIVE C2_ENABLE | UART_C2_TIE
  51. #define C2_TX_COMPLETING C2_ENABLE | UART_C2_TCIE
  52. #define C2_TX_INACTIVE C2_ENABLE
  53. void serial3_begin(uint32_t divisor)
  54. {
  55. SIM_SCGC4 |= SIM_SCGC4_UART2; // turn on clock, TODO: use bitband
  56. rx_buffer_head = 0;
  57. rx_buffer_tail = 0;
  58. tx_buffer_head = 0;
  59. tx_buffer_tail = 0;
  60. transmitting = 0;
  61. CORE_PIN7_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3);
  62. CORE_PIN8_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);
  63. UART2_BDH = (divisor >> 13) & 0x1F;
  64. UART2_BDL = (divisor >> 5) & 0xFF;
  65. UART2_C4 = divisor & 0x1F;
  66. UART2_C1 = 0;
  67. UART2_PFIFO = 0;
  68. UART2_C2 = C2_TX_INACTIVE;
  69. NVIC_SET_PRIORITY(IRQ_UART2_STATUS, 64);
  70. NVIC_ENABLE_IRQ(IRQ_UART2_STATUS);
  71. }
  72. void serial3_end(void)
  73. {
  74. if (!(SIM_SCGC4 & SIM_SCGC4_UART2)) return;
  75. while (transmitting) yield(); // wait for buffered data to send
  76. NVIC_DISABLE_IRQ(IRQ_UART2_STATUS);
  77. UART2_C2 = 0;
  78. CORE_PIN7_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
  79. CORE_PIN8_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
  80. rx_buffer_head = 0;
  81. rx_buffer_tail = 0;
  82. }
  83. void serial3_putchar(uint8_t c)
  84. {
  85. uint32_t head;
  86. if (!(SIM_SCGC4 & SIM_SCGC4_UART2)) return;
  87. head = tx_buffer_head;
  88. if (++head >= TX_BUFFER_SIZE) head = 0;
  89. while (tx_buffer_tail == head) {
  90. yield(); // wait
  91. }
  92. tx_buffer[head] = c;
  93. transmitting = 1;
  94. tx_buffer_head = head;
  95. UART2_C2 = C2_TX_ACTIVE;
  96. }
  97. void serial3_write(const void *buf, unsigned int count)
  98. {
  99. const uint8_t *p = (const uint8_t *)buf;
  100. while (count-- > 0) serial3_putchar(*p++);
  101. }
  102. void serial3_flush(void)
  103. {
  104. while (transmitting) yield(); // wait
  105. }
  106. int serial3_available(void)
  107. {
  108. uint32_t head, tail;
  109. head = rx_buffer_head;
  110. tail = rx_buffer_tail;
  111. if (head >= tail) return head - tail;
  112. return RX_BUFFER_SIZE + head - tail;
  113. }
  114. int serial3_getchar(void)
  115. {
  116. uint32_t head, tail;
  117. int c;
  118. head = rx_buffer_head;
  119. tail = rx_buffer_tail;
  120. if (head == tail) return -1;
  121. if (++tail >= RX_BUFFER_SIZE) tail = 0;
  122. c = rx_buffer[tail];
  123. rx_buffer_tail = tail;
  124. return c;
  125. }
  126. int serial3_peek(void)
  127. {
  128. uint32_t head, tail;
  129. head = rx_buffer_head;
  130. tail = rx_buffer_tail;
  131. if (head == tail) return -1;
  132. if (++tail >= RX_BUFFER_SIZE) tail = 0;
  133. return rx_buffer[tail];
  134. }
  135. void serial3_clear(void)
  136. {
  137. rx_buffer_head = rx_buffer_tail;
  138. }
  139. // status interrupt combines
  140. // Transmit data below watermark UART_S1_TDRE
  141. // Transmit complete UART_S1_TC
  142. // Idle line UART_S1_IDLE
  143. // Receive data above watermark UART_S1_RDRF
  144. // LIN break detect UART_S2_LBKDIF
  145. // RxD pin active edge UART_S2_RXEDGIF
  146. void uart2_status_isr(void)
  147. {
  148. uint32_t head, tail;
  149. uint8_t c;
  150. //digitalWriteFast(4, HIGH);
  151. if (UART2_S1 & UART_S1_RDRF) {
  152. //digitalWriteFast(5, HIGH);
  153. c = UART2_D;
  154. head = rx_buffer_head + 1;
  155. if (head >= RX_BUFFER_SIZE) head = 0;
  156. if (head != rx_buffer_tail) {
  157. rx_buffer[head] = c;
  158. rx_buffer_head = head;
  159. }
  160. //digitalWriteFast(5, LOW);
  161. }
  162. c = UART2_C2;
  163. if ((c & UART_C2_TIE) && (UART2_S1 & UART_S1_TDRE)) {
  164. //digitalWriteFast(5, HIGH);
  165. head = tx_buffer_head;
  166. tail = tx_buffer_tail;
  167. if (head == tail) {
  168. UART2_C2 = C2_TX_COMPLETING;
  169. } else {
  170. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  171. UART2_D = tx_buffer[tail];
  172. tx_buffer_tail = tail;
  173. }
  174. //digitalWriteFast(5, LOW);
  175. }
  176. if ((c & UART_C2_TCIE) && (UART2_S1 & UART_S1_TC)) {
  177. transmitting = 0;
  178. UART2_C2 = C2_TX_INACTIVE;
  179. }
  180. //digitalWriteFast(4, LOW);
  181. }