Teensy 4.1 core updated for C++20
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

409 lines
12KB

  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_UART3
  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. static uint8_t rx_pin_num = 31;
  76. static uint8_t tx_pin_num = 32;
  77. // UART0 and UART1 are clocked by F_CPU, UART2 is clocked by F_BUS
  78. // UART0 has 8 byte fifo, UART1 and UART2 have 1 byte buffer
  79. #define C2_ENABLE UART_C2_TE | UART_C2_RE | UART_C2_RIE
  80. #define C2_TX_ACTIVE C2_ENABLE | UART_C2_TIE
  81. #define C2_TX_COMPLETING C2_ENABLE | UART_C2_TCIE
  82. #define C2_TX_INACTIVE C2_ENABLE
  83. void serial4_begin(uint32_t divisor)
  84. {
  85. SIM_SCGC4 |= SIM_SCGC4_UART3; // turn on clock, TODO: use bitband
  86. rx_buffer_head = 0;
  87. rx_buffer_tail = 0;
  88. tx_buffer_head = 0;
  89. tx_buffer_tail = 0;
  90. transmitting = 0;
  91. switch (rx_pin_num) {
  92. case 31: CORE_PIN31_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  93. #if defined USE_SDCARD_PINS && (defined(__MK64FX512__) || defined(__MK66FX1M0__)) // on T3.5 or T3.6
  94. case 63: CORE_PIN63_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  95. #endif
  96. }
  97. switch (tx_pin_num) {
  98. case 32: CORE_PIN32_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); break;
  99. #if defined USE_SDCARD_PINS && (defined(__MK64FX512__) || defined(__MK66FX1M0__)) // on T3.5 or T3.6
  100. case 62: CORE_PIN62_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); break;
  101. #endif
  102. }
  103. UART3_BDH = (divisor >> 13) & 0x1F;
  104. UART3_BDL = (divisor >> 5) & 0xFF;
  105. UART3_C4 = divisor & 0x1F;
  106. UART3_C1 = 0;
  107. UART3_PFIFO = 0;
  108. UART3_C2 = C2_TX_INACTIVE;
  109. NVIC_SET_PRIORITY(IRQ_UART3_STATUS, IRQ_PRIORITY);
  110. NVIC_ENABLE_IRQ(IRQ_UART3_STATUS);
  111. }
  112. void serial4_format(uint32_t format)
  113. {
  114. uint8_t c;
  115. c = UART3_C1;
  116. c = (c & ~0x13) | (format & 0x03); // configure parity
  117. if (format & 0x04) c |= 0x10; // 9 bits (might include parity)
  118. UART3_C1 = c;
  119. if ((format & 0x0F) == 0x04) UART3_C3 |= 0x40; // 8N2 is 9 bit with 9th bit always 1
  120. c = UART3_S2 & ~0x10;
  121. if (format & 0x10) c |= 0x10; // rx invert
  122. UART3_S2 = c;
  123. c = UART3_C3 & ~0x10;
  124. if (format & 0x20) c |= 0x10; // tx invert
  125. UART3_C3 = c;
  126. #ifdef SERIAL_9BIT_SUPPORT
  127. c = UART3_C4 & 0x1F;
  128. if (format & 0x08) c |= 0x20; // 9 bit mode with parity (requires 10 bits)
  129. UART3_C4 = c;
  130. use9Bits = format & 0x80;
  131. #endif
  132. #if defined(__MK64FX512__) || defined(__MK66FX1M0__) || defined(KINETISL)
  133. // For T3.5/T3.6/TLC See about turning on 2 stop bit mode
  134. if ( format & 0x100) {
  135. uint8_t bdl = UART3_BDL;
  136. UART3_BDH |= UART_BDH_SBNS; // Turn on 2 stop bits - was turned off by set baud
  137. UART3_BDL = bdl; // Says BDH not acted on until BDL is written
  138. }
  139. #endif
  140. }
  141. void serial4_end(void)
  142. {
  143. if (!(SIM_SCGC4 & SIM_SCGC4_UART3)) return;
  144. while (transmitting) yield(); // wait for buffered data to send
  145. NVIC_DISABLE_IRQ(IRQ_UART3_STATUS);
  146. UART3_C2 = 0;
  147. CORE_PIN31_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
  148. CORE_PIN32_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
  149. switch (rx_pin_num) {
  150. case 31: CORE_PIN31_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break; // PTC3
  151. #if defined USE_SDCARD_PINS && (defined(__MK64FX512__) || defined(__MK66FX1M0__)) // on T3.5 or T3.6
  152. case 63: CORE_PIN63_CONFIG = 0; break;
  153. #endif
  154. }
  155. switch (tx_pin_num & 127) {
  156. case 32: CORE_PIN32_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break; // PTC4
  157. #if defined USE_SDCARD_PINS && (defined(__MK64FX512__) || defined(__MK66FX1M0__)) // on T3.5 or T3.6
  158. case 62: CORE_PIN62_CONFIG = 0; break;
  159. #endif
  160. }
  161. rx_buffer_head = 0;
  162. rx_buffer_tail = 0;
  163. if (rts_pin) rts_deassert();
  164. }
  165. void serial4_set_transmit_pin(uint8_t pin)
  166. {
  167. while (transmitting) ;
  168. pinMode(pin, OUTPUT);
  169. digitalWrite(pin, LOW);
  170. transmit_pin = portOutputRegister(pin);
  171. }
  172. void serial4_set_tx(uint8_t pin, uint8_t opendrain)
  173. {
  174. uint32_t cfg;
  175. if (opendrain) pin |= 128;
  176. if (pin == tx_pin_num) return;
  177. if ((SIM_SCGC4 & SIM_SCGC4_UART3)) {
  178. switch (tx_pin_num & 127) {
  179. case 32: CORE_PIN32_CONFIG = 0; break; // PTB11
  180. #if defined USE_SDCARD_PINS && (defined(__MK64FX512__) || defined(__MK66FX1M0__)) // on T3.5 or T3.6
  181. case 62: CORE_PIN62_CONFIG = 0; break;
  182. #endif
  183. }
  184. if (opendrain) {
  185. cfg = PORT_PCR_DSE | PORT_PCR_ODE;
  186. } else {
  187. cfg = PORT_PCR_DSE | PORT_PCR_SRE;
  188. }
  189. switch (pin & 127) {
  190. case 32: CORE_PIN32_CONFIG = cfg | PORT_PCR_MUX(3); break;
  191. #if defined USE_SDCARD_PINS && (defined(__MK64FX512__) || defined(__MK66FX1M0__)) // on T3.5 or T3.6
  192. case 62: CORE_PIN62_CONFIG = cfg | PORT_PCR_MUX(3);; break;
  193. #endif
  194. }
  195. }
  196. tx_pin_num = pin;
  197. }
  198. void serial4_set_rx(uint8_t pin)
  199. {
  200. if (pin == rx_pin_num) return;
  201. if ((SIM_SCGC4 & SIM_SCGC4_UART3)) {
  202. switch (rx_pin_num) {
  203. case 31: CORE_PIN31_CONFIG = 0; break; // PTC3
  204. #if defined USE_SDCARD_PINS && (defined(__MK64FX512__) || defined(__MK66FX1M0__)) // on T3.5 or T3.6
  205. case 63: CORE_PIN63_CONFIG = 0; break;
  206. #endif
  207. }
  208. switch (pin) {
  209. case 31: CORE_PIN31_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  210. #if defined USE_SDCARD_PINS && (defined(__MK64FX512__) || defined(__MK66FX1M0__)) // on T3.5 or T3.6
  211. case 63: CORE_PIN63_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  212. #endif
  213. }
  214. }
  215. rx_pin_num = pin;
  216. }
  217. int serial4_set_rts(uint8_t pin)
  218. {
  219. if (!(SIM_SCGC4 & SIM_SCGC4_UART3)) return 0;
  220. if (pin < CORE_NUM_DIGITAL) {
  221. rts_pin = portOutputRegister(pin);
  222. pinMode(pin, OUTPUT);
  223. rts_assert();
  224. } else {
  225. rts_pin = NULL;
  226. return 0;
  227. }
  228. return 1;
  229. }
  230. int serial4_set_cts(uint8_t pin)
  231. {
  232. return 0;
  233. }
  234. void serial4_putchar(uint32_t c)
  235. {
  236. uint32_t head, n;
  237. if (!(SIM_SCGC4 & SIM_SCGC4_UART3)) return;
  238. if (transmit_pin) transmit_assert();
  239. head = tx_buffer_head;
  240. if (++head >= TX_BUFFER_SIZE) head = 0;
  241. while (tx_buffer_tail == head) {
  242. int priority = nvic_execution_priority();
  243. if (priority <= IRQ_PRIORITY) {
  244. if ((UART3_S1 & UART_S1_TDRE)) {
  245. uint32_t tail = tx_buffer_tail;
  246. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  247. n = tx_buffer[tail];
  248. if (use9Bits) UART3_C3 = (UART3_C3 & ~0x40) | ((n & 0x100) >> 2);
  249. UART3_D = n;
  250. tx_buffer_tail = tail;
  251. }
  252. } else if (priority >= 256) {
  253. yield(); // wait
  254. }
  255. }
  256. tx_buffer[head] = c;
  257. transmitting = 1;
  258. tx_buffer_head = head;
  259. UART3_C2 = C2_TX_ACTIVE;
  260. }
  261. void serial4_write(const void *buf, unsigned int count)
  262. {
  263. const uint8_t *p = (const uint8_t *)buf;
  264. while (count-- > 0) serial4_putchar(*p++);
  265. }
  266. void serial4_flush(void)
  267. {
  268. while (transmitting) yield(); // wait
  269. }
  270. int serial4_write_buffer_free(void)
  271. {
  272. uint32_t head, tail;
  273. head = tx_buffer_head;
  274. tail = tx_buffer_tail;
  275. if (head >= tail) return TX_BUFFER_SIZE - 1 - head + tail;
  276. return tail - head - 1;
  277. }
  278. int serial4_available(void)
  279. {
  280. uint32_t head, tail;
  281. head = rx_buffer_head;
  282. tail = rx_buffer_tail;
  283. if (head >= tail) return head - tail;
  284. return RX_BUFFER_SIZE + head - tail;
  285. }
  286. int serial4_getchar(void)
  287. {
  288. uint32_t head, tail;
  289. int c;
  290. head = rx_buffer_head;
  291. tail = rx_buffer_tail;
  292. if (head == tail) return -1;
  293. if (++tail >= RX_BUFFER_SIZE) tail = 0;
  294. c = rx_buffer[tail];
  295. rx_buffer_tail = tail;
  296. if (rts_pin) {
  297. int avail;
  298. if (head >= tail) avail = head - tail;
  299. else avail = RX_BUFFER_SIZE + head - tail;
  300. if (avail <= RTS_LOW_WATERMARK) rts_assert();
  301. }
  302. return c;
  303. }
  304. int serial4_peek(void)
  305. {
  306. uint32_t head, tail;
  307. head = rx_buffer_head;
  308. tail = rx_buffer_tail;
  309. if (head == tail) return -1;
  310. if (++tail >= RX_BUFFER_SIZE) tail = 0;
  311. return rx_buffer[tail];
  312. }
  313. void serial4_clear(void)
  314. {
  315. rx_buffer_head = rx_buffer_tail;
  316. if (rts_pin) rts_assert();
  317. }
  318. // status interrupt combines
  319. // Transmit data below watermark UART_S1_TDRE
  320. // Transmit complete UART_S1_TC
  321. // Idle line UART_S1_IDLE
  322. // Receive data above watermark UART_S1_RDRF
  323. // LIN break detect UART_S2_LBKDIF
  324. // RxD pin active edge UART_S2_RXEDGIF
  325. void uart3_status_isr(void)
  326. {
  327. uint32_t head, tail, n;
  328. uint8_t c;
  329. if (UART3_S1 & UART_S1_RDRF) {
  330. if (use9Bits && (UART3_C3 & 0x80)) {
  331. n = UART3_D | 0x100;
  332. } else {
  333. n = UART3_D;
  334. }
  335. head = rx_buffer_head + 1;
  336. if (head >= RX_BUFFER_SIZE) head = 0;
  337. if (head != rx_buffer_tail) {
  338. rx_buffer[head] = n;
  339. rx_buffer_head = head;
  340. }
  341. if (rts_pin) {
  342. int avail;
  343. tail = tx_buffer_tail;
  344. if (head >= tail) avail = head - tail;
  345. else avail = RX_BUFFER_SIZE + head - tail;
  346. if (avail >= RTS_HIGH_WATERMARK) rts_deassert();
  347. }
  348. }
  349. c = UART3_C2;
  350. if ((c & UART_C2_TIE) && (UART3_S1 & UART_S1_TDRE)) {
  351. head = tx_buffer_head;
  352. tail = tx_buffer_tail;
  353. if (head == tail) {
  354. UART3_C2 = C2_TX_COMPLETING;
  355. } else {
  356. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  357. n = tx_buffer[tail];
  358. if (use9Bits) UART3_C3 = (UART3_C3 & ~0x40) | ((n & 0x100) >> 2);
  359. UART3_D = n;
  360. tx_buffer_tail = tail;
  361. }
  362. }
  363. if ((c & UART_C2_TCIE) && (UART3_S1 & UART_S1_TC)) {
  364. transmitting = 0;
  365. if (transmit_pin) transmit_deassert();
  366. UART3_C2 = C2_TX_INACTIVE;
  367. }
  368. }
  369. #endif // HAS_KINETISK_UART3