Teensy 4.1 core updated for C++20
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

343 Zeilen
9.1KB

  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. #ifdef HAS_KINETISK_UART4
  34. ////////////////////////////////////////////////////////////////
  35. // Tunable parameters (relatively safe to edit these numbers)
  36. ////////////////////////////////////////////////////////////////
  37. #define TX_BUFFER_SIZE 40 // number of outgoing bytes to buffer
  38. #define RX_BUFFER_SIZE 64 // number of incoming bytes to buffer
  39. #define RTS_HIGH_WATERMARK 40 // RTS requests sender to pause
  40. #define RTS_LOW_WATERMARK 26 // RTS allows sender to resume
  41. #define IRQ_PRIORITY 64 // 0 = highest priority, 255 = lowest
  42. ////////////////////////////////////////////////////////////////
  43. // changes not recommended below this point....
  44. ////////////////////////////////////////////////////////////////
  45. #ifdef SERIAL_9BIT_SUPPORT
  46. static uint8_t use9Bits = 0;
  47. #define BUFTYPE uint16_t
  48. #else
  49. #define BUFTYPE uint8_t
  50. #define use9Bits 0
  51. #endif
  52. static volatile BUFTYPE tx_buffer[TX_BUFFER_SIZE];
  53. static volatile BUFTYPE rx_buffer[RX_BUFFER_SIZE];
  54. static volatile uint8_t transmitting = 0;
  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. #if TX_BUFFER_SIZE > 255
  62. static volatile uint16_t tx_buffer_head = 0;
  63. static volatile uint16_t tx_buffer_tail = 0;
  64. #else
  65. static volatile uint8_t tx_buffer_head = 0;
  66. static volatile uint8_t tx_buffer_tail = 0;
  67. #endif
  68. #if RX_BUFFER_SIZE > 255
  69. static volatile uint16_t rx_buffer_head = 0;
  70. static volatile uint16_t rx_buffer_tail = 0;
  71. #else
  72. static volatile uint8_t rx_buffer_head = 0;
  73. static volatile uint8_t rx_buffer_tail = 0;
  74. #endif
  75. // UART0 and UART1 are clocked by F_CPU, UART2 is clocked by F_BUS
  76. // UART0 has 8 byte fifo, UART1 and UART2 have 1 byte buffer
  77. #define C2_ENABLE UART_C2_TE | UART_C2_RE | UART_C2_RIE
  78. #define C2_TX_ACTIVE C2_ENABLE | UART_C2_TIE
  79. #define C2_TX_COMPLETING C2_ENABLE | UART_C2_TCIE
  80. #define C2_TX_INACTIVE C2_ENABLE
  81. void serial5_begin(uint32_t divisor)
  82. {
  83. SIM_SCGC1 |= SIM_SCGC1_UART4; // turn on clock, TODO: use bitband
  84. rx_buffer_head = 0;
  85. rx_buffer_tail = 0;
  86. tx_buffer_head = 0;
  87. tx_buffer_tail = 0;
  88. transmitting = 0;
  89. CORE_PIN34_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3);
  90. CORE_PIN33_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);
  91. UART4_BDH = (divisor >> 13) & 0x1F;
  92. UART4_BDL = (divisor >> 5) & 0xFF;
  93. UART4_C4 = divisor & 0x1F;
  94. UART4_C1 = 0;
  95. UART4_PFIFO = 0;
  96. UART4_C2 = C2_TX_INACTIVE;
  97. NVIC_SET_PRIORITY(IRQ_UART4_STATUS, IRQ_PRIORITY);
  98. NVIC_ENABLE_IRQ(IRQ_UART4_STATUS);
  99. }
  100. void serial5_format(uint32_t format)
  101. {
  102. uint8_t c;
  103. c = UART4_C1;
  104. c = (c & ~0x13) | (format & 0x03); // configure parity
  105. if (format & 0x04) c |= 0x10; // 9 bits (might include parity)
  106. UART4_C1 = c;
  107. if ((format & 0x0F) == 0x04) UART4_C3 |= 0x40; // 8N2 is 9 bit with 9th bit always 1
  108. c = UART4_S2 & ~0x10;
  109. if (format & 0x10) c |= 0x10; // rx invert
  110. UART4_S2 = c;
  111. c = UART4_C3 & ~0x10;
  112. if (format & 0x20) c |= 0x10; // tx invert
  113. UART4_C3 = c;
  114. #ifdef SERIAL_9BIT_SUPPORT
  115. c = UART4_C4 & 0x1F;
  116. if (format & 0x08) c |= 0x20; // 9 bit mode with parity (requires 10 bits)
  117. UART4_C4 = c;
  118. use9Bits = format & 0x80;
  119. #endif
  120. }
  121. void serial5_end(void)
  122. {
  123. if (!(SIM_SCGC1 & SIM_SCGC1_UART4)) return;
  124. while (transmitting) yield(); // wait for buffered data to send
  125. NVIC_DISABLE_IRQ(IRQ_UART4_STATUS);
  126. UART4_C2 = 0;
  127. CORE_PIN34_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
  128. CORE_PIN33_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
  129. rx_buffer_head = 0;
  130. rx_buffer_tail = 0;
  131. if (rts_pin) rts_deassert();
  132. }
  133. void serial5_set_transmit_pin(uint8_t pin)
  134. {
  135. while (transmitting) ;
  136. pinMode(pin, OUTPUT);
  137. digitalWrite(pin, LOW);
  138. transmit_pin = portOutputRegister(pin);
  139. }
  140. void serial5_set_tx(uint8_t pin, uint8_t opendrain)
  141. {
  142. }
  143. void serial5_set_rx(uint8_t pin)
  144. {
  145. }
  146. int serial5_set_rts(uint8_t pin)
  147. {
  148. if (!(SIM_SCGC1 & SIM_SCGC1_UART4)) return 0;
  149. if (pin < CORE_NUM_DIGITAL) {
  150. rts_pin = portOutputRegister(pin);
  151. pinMode(pin, OUTPUT);
  152. rts_assert();
  153. } else {
  154. rts_pin = NULL;
  155. return 0;
  156. }
  157. return 1;
  158. }
  159. int serial5_set_cts(uint8_t pin)
  160. {
  161. if (!(SIM_SCGC1 & SIM_SCGC1_UART4)) return 0;
  162. if (pin == 24) {
  163. CORE_PIN24_CONFIG = PORT_PCR_MUX(3) | PORT_PCR_PE; // weak pulldown
  164. } else {
  165. UART4_MODEM &= ~UART_MODEM_TXCTSE;
  166. return 0;
  167. }
  168. UART4_MODEM |= UART_MODEM_TXCTSE;
  169. return 1;
  170. }
  171. void serial5_putchar(uint32_t c)
  172. {
  173. uint32_t head, n;
  174. if (!(SIM_SCGC1 & SIM_SCGC1_UART4)) return;
  175. if (transmit_pin) transmit_assert();
  176. head = tx_buffer_head;
  177. if (++head >= TX_BUFFER_SIZE) head = 0;
  178. while (tx_buffer_tail == head) {
  179. int priority = nvic_execution_priority();
  180. if (priority <= IRQ_PRIORITY) {
  181. if ((UART4_S1 & UART_S1_TDRE)) {
  182. uint32_t tail = tx_buffer_tail;
  183. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  184. n = tx_buffer[tail];
  185. if (use9Bits) UART4_C3 = (UART4_C3 & ~0x40) | ((n & 0x100) >> 2);
  186. UART4_D = n;
  187. tx_buffer_tail = tail;
  188. }
  189. } else if (priority >= 256) {
  190. yield(); // wait
  191. }
  192. }
  193. tx_buffer[head] = c;
  194. transmitting = 1;
  195. tx_buffer_head = head;
  196. UART4_C2 = C2_TX_ACTIVE;
  197. }
  198. void serial5_write(const void *buf, unsigned int count)
  199. {
  200. const uint8_t *p = (const uint8_t *)buf;
  201. while (count-- > 0) serial5_putchar(*p++);
  202. }
  203. void serial5_flush(void)
  204. {
  205. while (transmitting) yield(); // wait
  206. }
  207. int serial5_write_buffer_free(void)
  208. {
  209. uint32_t head, tail;
  210. head = tx_buffer_head;
  211. tail = tx_buffer_tail;
  212. if (head >= tail) return TX_BUFFER_SIZE - 1 - head + tail;
  213. return tail - head - 1;
  214. }
  215. int serial5_available(void)
  216. {
  217. uint32_t head, tail;
  218. head = rx_buffer_head;
  219. tail = rx_buffer_tail;
  220. if (head >= tail) return head - tail;
  221. return RX_BUFFER_SIZE + head - tail;
  222. }
  223. int serial5_getchar(void)
  224. {
  225. uint32_t head, tail;
  226. int c;
  227. head = rx_buffer_head;
  228. tail = rx_buffer_tail;
  229. if (head == tail) return -1;
  230. if (++tail >= RX_BUFFER_SIZE) tail = 0;
  231. c = rx_buffer[tail];
  232. rx_buffer_tail = tail;
  233. if (rts_pin) {
  234. int avail;
  235. if (head >= tail) avail = head - tail;
  236. else avail = RX_BUFFER_SIZE + head - tail;
  237. if (avail <= RTS_LOW_WATERMARK) rts_assert();
  238. }
  239. return c;
  240. }
  241. int serial5_peek(void)
  242. {
  243. uint32_t head, tail;
  244. head = rx_buffer_head;
  245. tail = rx_buffer_tail;
  246. if (head == tail) return -1;
  247. if (++tail >= RX_BUFFER_SIZE) tail = 0;
  248. return rx_buffer[tail];
  249. }
  250. void serial5_clear(void)
  251. {
  252. rx_buffer_head = rx_buffer_tail;
  253. if (rts_pin) rts_assert();
  254. }
  255. // status interrupt combines
  256. // Transmit data below watermark UART_S1_TDRE
  257. // Transmit complete UART_S1_TC
  258. // Idle line UART_S1_IDLE
  259. // Receive data above watermark UART_S1_RDRF
  260. // LIN break detect UART_S2_LBKDIF
  261. // RxD pin active edge UART_S2_RXEDGIF
  262. void uart4_status_isr(void)
  263. {
  264. uint32_t head, tail, n;
  265. uint8_t c;
  266. if (UART4_S1 & UART_S1_RDRF) {
  267. if (use9Bits && (UART4_C3 & 0x80)) {
  268. n = UART4_D | 0x100;
  269. } else {
  270. n = UART4_D;
  271. }
  272. head = rx_buffer_head + 1;
  273. if (head >= RX_BUFFER_SIZE) head = 0;
  274. if (head != rx_buffer_tail) {
  275. rx_buffer[head] = n;
  276. rx_buffer_head = head;
  277. }
  278. if (rts_pin) {
  279. int avail;
  280. tail = tx_buffer_tail;
  281. if (head >= tail) avail = head - tail;
  282. else avail = RX_BUFFER_SIZE + head - tail;
  283. if (avail >= RTS_HIGH_WATERMARK) rts_deassert();
  284. }
  285. }
  286. c = UART4_C2;
  287. if ((c & UART_C2_TIE) && (UART4_S1 & UART_S1_TDRE)) {
  288. head = tx_buffer_head;
  289. tail = tx_buffer_tail;
  290. if (head == tail) {
  291. UART4_C2 = C2_TX_COMPLETING;
  292. } else {
  293. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  294. n = tx_buffer[tail];
  295. if (use9Bits) UART4_C3 = (UART4_C3 & ~0x40) | ((n & 0x100) >> 2);
  296. UART4_D = n;
  297. tx_buffer_tail = tail;
  298. }
  299. }
  300. if ((c & UART_C2_TCIE) && (UART4_S1 & UART_S1_TC)) {
  301. transmitting = 0;
  302. if (transmit_pin) transmit_deassert();
  303. UART4_C2 = C2_TX_INACTIVE;
  304. }
  305. }
  306. #endif // HAS_KINETISK_UART4