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.

290 rindas
8.0KB

  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 40
  37. #define RX_BUFFER_SIZE 64
  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
  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 serial3_begin(uint32_t divisor)
  74. {
  75. SIM_SCGC4 |= SIM_SCGC4_UART2; // 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_PIN7_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3);
  82. CORE_PIN8_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);
  83. #if defined(HAS_KINETISK_UART2)
  84. UART2_BDH = (divisor >> 13) & 0x1F;
  85. UART2_BDL = (divisor >> 5) & 0xFF;
  86. UART2_C4 = divisor & 0x1F;
  87. UART2_C1 = 0;
  88. UART2_PFIFO = 0;
  89. #elif defined(HAS_KINETISL_UART2)
  90. UART2_BDH = (divisor >> 8) & 0x1F;
  91. UART2_BDL = divisor & 0xFF;
  92. UART2_C1 = 0;
  93. #endif
  94. UART2_C2 = C2_TX_INACTIVE;
  95. NVIC_SET_PRIORITY(IRQ_UART2_STATUS, IRQ_PRIORITY);
  96. NVIC_ENABLE_IRQ(IRQ_UART2_STATUS);
  97. }
  98. void serial3_format(uint32_t format)
  99. {
  100. uint8_t c;
  101. c = UART2_C1;
  102. c = (c & ~0x13) | (format & 0x03); // configure parity
  103. if (format & 0x04) c |= 0x10; // 9 bits (might include parity)
  104. UART2_C1 = c;
  105. if ((format & 0x0F) == 0x04) UART2_C3 |= 0x40; // 8N2 is 9 bit with 9th bit always 1
  106. c = UART2_S2 & ~0x10;
  107. if (format & 0x10) c |= 0x10; // rx invert
  108. UART2_S2 = c;
  109. c = UART2_C3 & ~0x10;
  110. if (format & 0x20) c |= 0x10; // tx invert
  111. UART2_C3 = c;
  112. #ifdef SERIAL_9BIT_SUPPORT
  113. c = UART2_C4 & 0x1F;
  114. if (format & 0x08) c |= 0x20; // 9 bit mode with parity (requires 10 bits)
  115. UART2_C4 = c;
  116. use9Bits = format & 0x80;
  117. #endif
  118. }
  119. void serial3_end(void)
  120. {
  121. if (!(SIM_SCGC4 & SIM_SCGC4_UART2)) return;
  122. while (transmitting) yield(); // wait for buffered data to send
  123. NVIC_DISABLE_IRQ(IRQ_UART2_STATUS);
  124. UART2_C2 = 0;
  125. CORE_PIN7_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
  126. CORE_PIN8_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
  127. rx_buffer_head = 0;
  128. rx_buffer_tail = 0;
  129. }
  130. void serial3_set_transmit_pin(uint8_t pin)
  131. {
  132. while (transmitting) ;
  133. pinMode(pin, OUTPUT);
  134. digitalWrite(pin, LOW);
  135. transmit_pin = portOutputRegister(pin);
  136. }
  137. void serial3_putchar(uint32_t c)
  138. {
  139. uint32_t head, n;
  140. if (!(SIM_SCGC4 & SIM_SCGC4_UART2)) return;
  141. if (transmit_pin) *transmit_pin = 1;
  142. head = tx_buffer_head;
  143. if (++head >= TX_BUFFER_SIZE) head = 0;
  144. while (tx_buffer_tail == head) {
  145. int priority = nvic_execution_priority();
  146. if (priority <= IRQ_PRIORITY) {
  147. if ((UART2_S1 & UART_S1_TDRE)) {
  148. uint32_t tail = tx_buffer_tail;
  149. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  150. n = tx_buffer[tail];
  151. if (use9Bits) UART2_C3 = (UART2_C3 & ~0x40) | ((n & 0x100) >> 2);
  152. UART2_D = n;
  153. tx_buffer_tail = tail;
  154. }
  155. } else if (priority >= 256) {
  156. yield(); // wait
  157. }
  158. }
  159. tx_buffer[head] = c;
  160. transmitting = 1;
  161. tx_buffer_head = head;
  162. UART2_C2 = C2_TX_ACTIVE;
  163. }
  164. void serial3_write(const void *buf, unsigned int count)
  165. {
  166. const uint8_t *p = (const uint8_t *)buf;
  167. while (count-- > 0) serial3_putchar(*p++);
  168. }
  169. void serial3_flush(void)
  170. {
  171. while (transmitting) yield(); // wait
  172. }
  173. int serial3_write_buffer_free(void)
  174. {
  175. uint32_t head, tail;
  176. head = tx_buffer_head;
  177. tail = tx_buffer_tail;
  178. if (head >= tail) return TX_BUFFER_SIZE - 1 - head + tail;
  179. return tail - head - 1;
  180. }
  181. int serial3_available(void)
  182. {
  183. uint32_t head, tail;
  184. head = rx_buffer_head;
  185. tail = rx_buffer_tail;
  186. if (head >= tail) return head - tail;
  187. return RX_BUFFER_SIZE + head - tail;
  188. }
  189. int serial3_getchar(void)
  190. {
  191. uint32_t head, tail;
  192. int c;
  193. head = rx_buffer_head;
  194. tail = rx_buffer_tail;
  195. if (head == tail) return -1;
  196. if (++tail >= RX_BUFFER_SIZE) tail = 0;
  197. c = rx_buffer[tail];
  198. rx_buffer_tail = tail;
  199. return c;
  200. }
  201. int serial3_peek(void)
  202. {
  203. uint32_t head, tail;
  204. head = rx_buffer_head;
  205. tail = rx_buffer_tail;
  206. if (head == tail) return -1;
  207. if (++tail >= RX_BUFFER_SIZE) tail = 0;
  208. return rx_buffer[tail];
  209. }
  210. void serial3_clear(void)
  211. {
  212. rx_buffer_head = rx_buffer_tail;
  213. }
  214. // status interrupt combines
  215. // Transmit data below watermark UART_S1_TDRE
  216. // Transmit complete UART_S1_TC
  217. // Idle line UART_S1_IDLE
  218. // Receive data above watermark UART_S1_RDRF
  219. // LIN break detect UART_S2_LBKDIF
  220. // RxD pin active edge UART_S2_RXEDGIF
  221. void uart2_status_isr(void)
  222. {
  223. uint32_t head, tail, n;
  224. uint8_t c;
  225. if (UART2_S1 & UART_S1_RDRF) {
  226. if (use9Bits && (UART2_C3 & 0x80)) {
  227. n = UART2_D | 0x100;
  228. } else {
  229. n = UART2_D;
  230. }
  231. head = rx_buffer_head + 1;
  232. if (head >= RX_BUFFER_SIZE) head = 0;
  233. if (head != rx_buffer_tail) {
  234. rx_buffer[head] = n;
  235. rx_buffer_head = head;
  236. }
  237. }
  238. c = UART2_C2;
  239. if ((c & UART_C2_TIE) && (UART2_S1 & UART_S1_TDRE)) {
  240. head = tx_buffer_head;
  241. tail = tx_buffer_tail;
  242. if (head == tail) {
  243. UART2_C2 = C2_TX_COMPLETING;
  244. } else {
  245. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  246. n = tx_buffer[tail];
  247. if (use9Bits) UART2_C3 = (UART2_C3 & ~0x40) | ((n & 0x100) >> 2);
  248. UART2_D = n;
  249. tx_buffer_tail = tail;
  250. }
  251. }
  252. if ((c & UART_C2_TCIE) && (UART2_S1 & UART_S1_TC)) {
  253. transmitting = 0;
  254. if (transmit_pin) *transmit_pin = 0;
  255. UART2_C2 = C2_TX_INACTIVE;
  256. }
  257. }