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

365 行
9.9KB

  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 // number of outgoing bytes to buffer
  37. #define RX_BUFFER_SIZE 64 // number of incoming bytes to buffer
  38. #define RTS_HIGH_WATERMARK 40 // RTS requests sender to pause
  39. #define RTS_LOW_WATERMARK 26 // RTS allows sender to resume
  40. #define IRQ_PRIORITY 64 // 0 = highest priority, 255 = lowest
  41. ////////////////////////////////////////////////////////////////
  42. // changes not recommended below this point....
  43. ////////////////////////////////////////////////////////////////
  44. #ifdef SERIAL_9BIT_SUPPORT
  45. static uint8_t use9Bits = 0;
  46. #define BUFTYPE uint16_t
  47. #else
  48. #define BUFTYPE uint8_t
  49. #define use9Bits 0
  50. #endif
  51. static volatile BUFTYPE tx_buffer[TX_BUFFER_SIZE];
  52. static volatile BUFTYPE rx_buffer[RX_BUFFER_SIZE];
  53. static volatile uint8_t transmitting = 0;
  54. #if defined(KINETISK)
  55. static volatile uint8_t *transmit_pin=NULL;
  56. #define transmit_assert() *transmit_pin = 1
  57. #define transmit_deassert() *transmit_pin = 0
  58. static volatile uint8_t *rts_pin=NULL;
  59. #define rts_assert() *rts_pin = 0
  60. #define rts_deassert() *rts_pin = 1
  61. #elif defined(KINETISL)
  62. static volatile uint8_t *transmit_pin=NULL;
  63. static uint8_t transmit_mask=0;
  64. #define transmit_assert() *(transmit_pin+4) = transmit_mask;
  65. #define transmit_deassert() *(transmit_pin+8) = transmit_mask;
  66. static volatile uint8_t *rts_pin=NULL;
  67. static uint8_t rts_mask=0;
  68. #define rts_assert() *(rts_pin+8) = rts_mask;
  69. #define rts_deassert() *(rts_pin+4) = rts_mask;
  70. #endif
  71. #if TX_BUFFER_SIZE > 255
  72. static volatile uint16_t tx_buffer_head = 0;
  73. static volatile uint16_t tx_buffer_tail = 0;
  74. #else
  75. static volatile uint8_t tx_buffer_head = 0;
  76. static volatile uint8_t tx_buffer_tail = 0;
  77. #endif
  78. #if RX_BUFFER_SIZE > 255
  79. static volatile uint16_t rx_buffer_head = 0;
  80. static volatile uint16_t rx_buffer_tail = 0;
  81. #else
  82. static volatile uint8_t rx_buffer_head = 0;
  83. static volatile uint8_t rx_buffer_tail = 0;
  84. #endif
  85. // UART0 and UART1 are clocked by F_CPU, UART2 is clocked by F_BUS
  86. // UART0 has 8 byte fifo, UART1 and UART2 have 1 byte buffer
  87. #define C2_ENABLE UART_C2_TE | UART_C2_RE | UART_C2_RIE
  88. #define C2_TX_ACTIVE C2_ENABLE | UART_C2_TIE
  89. #define C2_TX_COMPLETING C2_ENABLE | UART_C2_TCIE
  90. #define C2_TX_INACTIVE C2_ENABLE
  91. void serial3_begin(uint32_t divisor)
  92. {
  93. SIM_SCGC4 |= SIM_SCGC4_UART2; // turn on clock, TODO: use bitband
  94. rx_buffer_head = 0;
  95. rx_buffer_tail = 0;
  96. tx_buffer_head = 0;
  97. tx_buffer_tail = 0;
  98. transmitting = 0;
  99. CORE_PIN7_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3);
  100. CORE_PIN8_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);
  101. #if defined(HAS_KINETISK_UART2)
  102. UART2_BDH = (divisor >> 13) & 0x1F;
  103. UART2_BDL = (divisor >> 5) & 0xFF;
  104. UART2_C4 = divisor & 0x1F;
  105. UART2_C1 = 0;
  106. UART2_PFIFO = 0;
  107. #elif defined(HAS_KINETISL_UART2)
  108. UART2_BDH = (divisor >> 8) & 0x1F;
  109. UART2_BDL = divisor & 0xFF;
  110. UART2_C1 = 0;
  111. #endif
  112. UART2_C2 = C2_TX_INACTIVE;
  113. NVIC_SET_PRIORITY(IRQ_UART2_STATUS, IRQ_PRIORITY);
  114. NVIC_ENABLE_IRQ(IRQ_UART2_STATUS);
  115. }
  116. void serial3_format(uint32_t format)
  117. {
  118. uint8_t c;
  119. c = UART2_C1;
  120. c = (c & ~0x13) | (format & 0x03); // configure parity
  121. if (format & 0x04) c |= 0x10; // 9 bits (might include parity)
  122. UART2_C1 = c;
  123. if ((format & 0x0F) == 0x04) UART2_C3 |= 0x40; // 8N2 is 9 bit with 9th bit always 1
  124. c = UART2_S2 & ~0x10;
  125. if (format & 0x10) c |= 0x10; // rx invert
  126. UART2_S2 = c;
  127. c = UART2_C3 & ~0x10;
  128. if (format & 0x20) c |= 0x10; // tx invert
  129. UART2_C3 = c;
  130. #ifdef SERIAL_9BIT_SUPPORT
  131. c = UART2_C4 & 0x1F;
  132. if (format & 0x08) c |= 0x20; // 9 bit mode with parity (requires 10 bits)
  133. UART2_C4 = c;
  134. use9Bits = format & 0x80;
  135. #endif
  136. }
  137. void serial3_end(void)
  138. {
  139. if (!(SIM_SCGC4 & SIM_SCGC4_UART2)) return;
  140. while (transmitting) yield(); // wait for buffered data to send
  141. NVIC_DISABLE_IRQ(IRQ_UART2_STATUS);
  142. UART2_C2 = 0;
  143. CORE_PIN7_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
  144. CORE_PIN8_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
  145. rx_buffer_head = 0;
  146. rx_buffer_tail = 0;
  147. if (rts_pin) rts_deassert();
  148. }
  149. void serial3_set_transmit_pin(uint8_t pin)
  150. {
  151. while (transmitting) ;
  152. pinMode(pin, OUTPUT);
  153. digitalWrite(pin, LOW);
  154. transmit_pin = portOutputRegister(pin);
  155. #if defined(KINETISL)
  156. transmit_mask = digitalPinToBitMask(pin);
  157. #endif
  158. }
  159. int serial3_set_rts(uint8_t pin)
  160. {
  161. if (!(SIM_SCGC4 & SIM_SCGC4_UART2)) return 0;
  162. if (pin < CORE_NUM_DIGITAL) {
  163. rts_pin = portOutputRegister(pin);
  164. #if defined(KINETISL)
  165. rts_mask = digitalPinToBitMask(pin);
  166. #endif
  167. pinMode(pin, OUTPUT);
  168. rts_assert();
  169. } else {
  170. rts_pin = NULL;
  171. return 0;
  172. }
  173. /*
  174. if (pin == 2) {
  175. CORE_PIN2_CONFIG = PORT_PCR_MUX(3);
  176. } else {
  177. UART2_MODEM &= ~UART_MODEM_RXRTSE;
  178. return 0;
  179. }
  180. UART2_MODEM |= UART_MODEM_RXRTSE;
  181. */
  182. return 1;
  183. }
  184. int serial3_set_cts(uint8_t pin)
  185. {
  186. if (!(SIM_SCGC4 & SIM_SCGC4_UART2)) return 0;
  187. if (pin == 14) {
  188. CORE_PIN14_CONFIG = PORT_PCR_MUX(3) | PORT_PCR_PE; // weak pulldown
  189. } else {
  190. UART2_MODEM &= ~UART_MODEM_TXCTSE;
  191. return 0;
  192. }
  193. UART2_MODEM |= UART_MODEM_TXCTSE;
  194. return 1;
  195. }
  196. void serial3_putchar(uint32_t c)
  197. {
  198. uint32_t head, n;
  199. if (!(SIM_SCGC4 & SIM_SCGC4_UART2)) return;
  200. if (transmit_pin) transmit_assert();
  201. head = tx_buffer_head;
  202. if (++head >= TX_BUFFER_SIZE) head = 0;
  203. while (tx_buffer_tail == head) {
  204. int priority = nvic_execution_priority();
  205. if (priority <= IRQ_PRIORITY) {
  206. if ((UART2_S1 & UART_S1_TDRE)) {
  207. uint32_t tail = tx_buffer_tail;
  208. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  209. n = tx_buffer[tail];
  210. if (use9Bits) UART2_C3 = (UART2_C3 & ~0x40) | ((n & 0x100) >> 2);
  211. UART2_D = n;
  212. tx_buffer_tail = tail;
  213. }
  214. } else if (priority >= 256) {
  215. yield(); // wait
  216. }
  217. }
  218. tx_buffer[head] = c;
  219. transmitting = 1;
  220. tx_buffer_head = head;
  221. UART2_C2 = C2_TX_ACTIVE;
  222. }
  223. void serial3_write(const void *buf, unsigned int count)
  224. {
  225. const uint8_t *p = (const uint8_t *)buf;
  226. while (count-- > 0) serial3_putchar(*p++);
  227. }
  228. void serial3_flush(void)
  229. {
  230. while (transmitting) yield(); // wait
  231. }
  232. int serial3_write_buffer_free(void)
  233. {
  234. uint32_t head, tail;
  235. head = tx_buffer_head;
  236. tail = tx_buffer_tail;
  237. if (head >= tail) return TX_BUFFER_SIZE - 1 - head + tail;
  238. return tail - head - 1;
  239. }
  240. int serial3_available(void)
  241. {
  242. uint32_t head, tail;
  243. head = rx_buffer_head;
  244. tail = rx_buffer_tail;
  245. if (head >= tail) return head - tail;
  246. return RX_BUFFER_SIZE + head - tail;
  247. }
  248. int serial3_getchar(void)
  249. {
  250. uint32_t head, tail;
  251. int c;
  252. head = rx_buffer_head;
  253. tail = rx_buffer_tail;
  254. if (head == tail) return -1;
  255. if (++tail >= RX_BUFFER_SIZE) tail = 0;
  256. c = rx_buffer[tail];
  257. rx_buffer_tail = tail;
  258. if (rts_pin) {
  259. int avail;
  260. if (head >= tail) avail = head - tail;
  261. else avail = RX_BUFFER_SIZE + head - tail;
  262. if (avail <= RTS_LOW_WATERMARK) rts_assert();
  263. }
  264. return c;
  265. }
  266. int serial3_peek(void)
  267. {
  268. uint32_t head, tail;
  269. head = rx_buffer_head;
  270. tail = rx_buffer_tail;
  271. if (head == tail) return -1;
  272. if (++tail >= RX_BUFFER_SIZE) tail = 0;
  273. return rx_buffer[tail];
  274. }
  275. void serial3_clear(void)
  276. {
  277. rx_buffer_head = rx_buffer_tail;
  278. if (rts_pin) rts_assert();
  279. }
  280. // status interrupt combines
  281. // Transmit data below watermark UART_S1_TDRE
  282. // Transmit complete UART_S1_TC
  283. // Idle line UART_S1_IDLE
  284. // Receive data above watermark UART_S1_RDRF
  285. // LIN break detect UART_S2_LBKDIF
  286. // RxD pin active edge UART_S2_RXEDGIF
  287. void uart2_status_isr(void)
  288. {
  289. uint32_t head, tail, n;
  290. uint8_t c;
  291. if (UART2_S1 & UART_S1_RDRF) {
  292. if (use9Bits && (UART2_C3 & 0x80)) {
  293. n = UART2_D | 0x100;
  294. } else {
  295. n = UART2_D;
  296. }
  297. head = rx_buffer_head + 1;
  298. if (head >= RX_BUFFER_SIZE) head = 0;
  299. if (head != rx_buffer_tail) {
  300. rx_buffer[head] = n;
  301. rx_buffer_head = head;
  302. }
  303. if (rts_pin) {
  304. int avail;
  305. tail = tx_buffer_tail;
  306. if (head >= tail) avail = head - tail;
  307. else avail = RX_BUFFER_SIZE + head - tail;
  308. if (avail >= RTS_HIGH_WATERMARK) rts_deassert();
  309. }
  310. }
  311. c = UART2_C2;
  312. if ((c & UART_C2_TIE) && (UART2_S1 & UART_S1_TDRE)) {
  313. head = tx_buffer_head;
  314. tail = tx_buffer_tail;
  315. if (head == tail) {
  316. UART2_C2 = C2_TX_COMPLETING;
  317. } else {
  318. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  319. n = tx_buffer[tail];
  320. if (use9Bits) UART2_C3 = (UART2_C3 & ~0x40) | ((n & 0x100) >> 2);
  321. UART2_D = n;
  322. tx_buffer_tail = tail;
  323. }
  324. }
  325. if ((c & UART_C2_TCIE) && (UART2_S1 & UART_S1_TC)) {
  326. transmitting = 0;
  327. if (transmit_pin) transmit_deassert();
  328. UART2_C2 = C2_TX_INACTIVE;
  329. }
  330. }