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.

395 lines
11KB

  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2017 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. #ifndef SERIAL4_TX_BUFFER_SIZE
  38. #define SERIAL4_TX_BUFFER_SIZE 40 // number of outgoing bytes to buffer
  39. #endif
  40. #ifndef SERIAL4_RX_BUFFER_SIZE
  41. #define SERIAL4_RX_BUFFER_SIZE 64 // number of incoming bytes to buffer
  42. #endif
  43. #define RTS_HIGH_WATERMARK (SERIAL4_RX_BUFFER_SIZE-24) // RTS requests sender to pause
  44. #define RTS_LOW_WATERMARK (SERIAL4_RX_BUFFER_SIZE-38) // RTS allows sender to resume
  45. #define IRQ_PRIORITY 64 // 0 = highest priority, 255 = lowest
  46. ////////////////////////////////////////////////////////////////
  47. // changes not recommended below this point....
  48. ////////////////////////////////////////////////////////////////
  49. #ifdef SERIAL_9BIT_SUPPORT
  50. static uint8_t use9Bits = 0;
  51. #define BUFTYPE uint16_t
  52. #else
  53. #define BUFTYPE uint8_t
  54. #define use9Bits 0
  55. #endif
  56. static volatile BUFTYPE tx_buffer[SERIAL4_TX_BUFFER_SIZE];
  57. static volatile BUFTYPE rx_buffer[SERIAL4_RX_BUFFER_SIZE];
  58. static volatile uint8_t transmitting = 0;
  59. static volatile uint8_t *transmit_pin=NULL;
  60. #define transmit_assert() *transmit_pin = 1
  61. #define transmit_deassert() *transmit_pin = 0
  62. static volatile uint8_t *rts_pin=NULL;
  63. #define rts_assert() *rts_pin = 0
  64. #define rts_deassert() *rts_pin = 1
  65. #if SERIAL4_TX_BUFFER_SIZE > 255
  66. static volatile uint16_t tx_buffer_head = 0;
  67. static volatile uint16_t tx_buffer_tail = 0;
  68. #else
  69. static volatile uint8_t tx_buffer_head = 0;
  70. static volatile uint8_t tx_buffer_tail = 0;
  71. #endif
  72. #if SERIAL4_RX_BUFFER_SIZE > 255
  73. static volatile uint16_t rx_buffer_head = 0;
  74. static volatile uint16_t rx_buffer_tail = 0;
  75. #else
  76. static volatile uint8_t rx_buffer_head = 0;
  77. static volatile uint8_t rx_buffer_tail = 0;
  78. #endif
  79. static uint8_t rx_pin_num = 31;
  80. static uint8_t tx_pin_num = 32;
  81. // UART0 and UART1 are clocked by F_CPU, UART2 is clocked by F_BUS
  82. // UART0 has 8 byte fifo, UART1 and UART2 have 1 byte buffer
  83. #define C2_ENABLE UART_C2_TE | UART_C2_RE | UART_C2_RIE
  84. #define C2_TX_ACTIVE C2_ENABLE | UART_C2_TIE
  85. #define C2_TX_COMPLETING C2_ENABLE | UART_C2_TCIE
  86. #define C2_TX_INACTIVE C2_ENABLE
  87. void serial4_begin(uint32_t divisor)
  88. {
  89. SIM_SCGC4 |= SIM_SCGC4_UART3; // turn on clock, TODO: use bitband
  90. rx_buffer_head = 0;
  91. rx_buffer_tail = 0;
  92. tx_buffer_head = 0;
  93. tx_buffer_tail = 0;
  94. transmitting = 0;
  95. switch (rx_pin_num) {
  96. case 31: CORE_PIN31_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  97. case 63: CORE_PIN63_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  98. }
  99. switch (tx_pin_num) {
  100. case 32: CORE_PIN32_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); break;
  101. case 62: CORE_PIN62_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); break;
  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. switch (rx_pin_num) {
  148. case 31: CORE_PIN31_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break; // PTC3
  149. case 63: CORE_PIN63_CONFIG = 0; break;
  150. }
  151. switch (tx_pin_num & 127) {
  152. case 32: CORE_PIN32_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break; // PTC4
  153. case 62: CORE_PIN62_CONFIG = 0; break;
  154. }
  155. rx_buffer_head = 0;
  156. rx_buffer_tail = 0;
  157. if (rts_pin) rts_deassert();
  158. }
  159. void serial4_set_transmit_pin(uint8_t pin)
  160. {
  161. while (transmitting) ;
  162. pinMode(pin, OUTPUT);
  163. digitalWrite(pin, LOW);
  164. transmit_pin = portOutputRegister(pin);
  165. }
  166. void serial4_set_tx(uint8_t pin, uint8_t opendrain)
  167. {
  168. uint32_t cfg;
  169. if (opendrain) pin |= 128;
  170. if (pin == tx_pin_num) return;
  171. if ((SIM_SCGC4 & SIM_SCGC4_UART3)) {
  172. switch (tx_pin_num & 127) {
  173. case 32: CORE_PIN32_CONFIG = 0; break; // PTB11
  174. case 62: CORE_PIN62_CONFIG = 0; break;
  175. }
  176. if (opendrain) {
  177. cfg = PORT_PCR_DSE | PORT_PCR_ODE;
  178. } else {
  179. cfg = PORT_PCR_DSE | PORT_PCR_SRE;
  180. }
  181. switch (pin & 127) {
  182. case 32: CORE_PIN32_CONFIG = cfg | PORT_PCR_MUX(3); break;
  183. case 62: CORE_PIN62_CONFIG = cfg | PORT_PCR_MUX(3);; break;
  184. }
  185. }
  186. tx_pin_num = pin;
  187. }
  188. void serial4_set_rx(uint8_t pin)
  189. {
  190. if (pin == rx_pin_num) return;
  191. if ((SIM_SCGC4 & SIM_SCGC4_UART3)) {
  192. switch (rx_pin_num) {
  193. case 31: CORE_PIN31_CONFIG = 0; break; // PTC3
  194. case 63: CORE_PIN63_CONFIG = 0; break;
  195. }
  196. switch (pin) {
  197. case 31: CORE_PIN31_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  198. case 63: CORE_PIN63_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  199. }
  200. }
  201. rx_pin_num = pin;
  202. }
  203. int serial4_set_rts(uint8_t pin)
  204. {
  205. if (!(SIM_SCGC4 & SIM_SCGC4_UART3)) return 0;
  206. if (pin < CORE_NUM_DIGITAL) {
  207. rts_pin = portOutputRegister(pin);
  208. pinMode(pin, OUTPUT);
  209. rts_assert();
  210. } else {
  211. rts_pin = NULL;
  212. return 0;
  213. }
  214. return 1;
  215. }
  216. int serial4_set_cts(uint8_t pin)
  217. {
  218. return 0;
  219. }
  220. void serial4_putchar(uint32_t c)
  221. {
  222. uint32_t head, n;
  223. if (!(SIM_SCGC4 & SIM_SCGC4_UART3)) return;
  224. if (transmit_pin) transmit_assert();
  225. head = tx_buffer_head;
  226. if (++head >= SERIAL4_TX_BUFFER_SIZE) head = 0;
  227. while (tx_buffer_tail == head) {
  228. int priority = nvic_execution_priority();
  229. if (priority <= IRQ_PRIORITY) {
  230. if ((UART3_S1 & UART_S1_TDRE)) {
  231. uint32_t tail = tx_buffer_tail;
  232. if (++tail >= SERIAL4_TX_BUFFER_SIZE) tail = 0;
  233. n = tx_buffer[tail];
  234. if (use9Bits) UART3_C3 = (UART3_C3 & ~0x40) | ((n & 0x100) >> 2);
  235. UART3_D = n;
  236. tx_buffer_tail = tail;
  237. }
  238. } else if (priority >= 256) {
  239. yield(); // wait
  240. }
  241. }
  242. tx_buffer[head] = c;
  243. transmitting = 1;
  244. tx_buffer_head = head;
  245. UART3_C2 = C2_TX_ACTIVE;
  246. }
  247. void serial4_write(const void *buf, unsigned int count)
  248. {
  249. const uint8_t *p = (const uint8_t *)buf;
  250. while (count-- > 0) serial4_putchar(*p++);
  251. }
  252. void serial4_flush(void)
  253. {
  254. while (transmitting) yield(); // wait
  255. }
  256. int serial4_write_buffer_free(void)
  257. {
  258. uint32_t head, tail;
  259. head = tx_buffer_head;
  260. tail = tx_buffer_tail;
  261. if (head >= tail) return SERIAL4_TX_BUFFER_SIZE - 1 - head + tail;
  262. return tail - head - 1;
  263. }
  264. int serial4_available(void)
  265. {
  266. uint32_t head, tail;
  267. head = rx_buffer_head;
  268. tail = rx_buffer_tail;
  269. if (head >= tail) return head - tail;
  270. return SERIAL4_RX_BUFFER_SIZE + head - tail;
  271. }
  272. int serial4_getchar(void)
  273. {
  274. uint32_t head, tail;
  275. int c;
  276. head = rx_buffer_head;
  277. tail = rx_buffer_tail;
  278. if (head == tail) return -1;
  279. if (++tail >= SERIAL4_RX_BUFFER_SIZE) tail = 0;
  280. c = rx_buffer[tail];
  281. rx_buffer_tail = tail;
  282. if (rts_pin) {
  283. int avail;
  284. if (head >= tail) avail = head - tail;
  285. else avail = SERIAL4_RX_BUFFER_SIZE + head - tail;
  286. if (avail <= RTS_LOW_WATERMARK) rts_assert();
  287. }
  288. return c;
  289. }
  290. int serial4_peek(void)
  291. {
  292. uint32_t head, tail;
  293. head = rx_buffer_head;
  294. tail = rx_buffer_tail;
  295. if (head == tail) return -1;
  296. if (++tail >= SERIAL4_RX_BUFFER_SIZE) tail = 0;
  297. return rx_buffer[tail];
  298. }
  299. void serial4_clear(void)
  300. {
  301. rx_buffer_head = rx_buffer_tail;
  302. if (rts_pin) rts_assert();
  303. }
  304. // status interrupt combines
  305. // Transmit data below watermark UART_S1_TDRE
  306. // Transmit complete UART_S1_TC
  307. // Idle line UART_S1_IDLE
  308. // Receive data above watermark UART_S1_RDRF
  309. // LIN break detect UART_S2_LBKDIF
  310. // RxD pin active edge UART_S2_RXEDGIF
  311. void uart3_status_isr(void)
  312. {
  313. uint32_t head, tail, n;
  314. uint8_t c;
  315. if (UART3_S1 & UART_S1_RDRF) {
  316. if (use9Bits && (UART3_C3 & 0x80)) {
  317. n = UART3_D | 0x100;
  318. } else {
  319. n = UART3_D;
  320. }
  321. head = rx_buffer_head + 1;
  322. if (head >= SERIAL4_RX_BUFFER_SIZE) head = 0;
  323. if (head != rx_buffer_tail) {
  324. rx_buffer[head] = n;
  325. rx_buffer_head = head;
  326. }
  327. if (rts_pin) {
  328. int avail;
  329. tail = tx_buffer_tail;
  330. if (head >= tail) avail = head - tail;
  331. else avail = SERIAL4_RX_BUFFER_SIZE + head - tail;
  332. if (avail >= RTS_HIGH_WATERMARK) rts_deassert();
  333. }
  334. }
  335. c = UART3_C2;
  336. if ((c & UART_C2_TIE) && (UART3_S1 & UART_S1_TDRE)) {
  337. head = tx_buffer_head;
  338. tail = tx_buffer_tail;
  339. if (head == tail) {
  340. UART3_C2 = C2_TX_COMPLETING;
  341. } else {
  342. if (++tail >= SERIAL4_TX_BUFFER_SIZE) tail = 0;
  343. n = tx_buffer[tail];
  344. if (use9Bits) UART3_C3 = (UART3_C3 & ~0x40) | ((n & 0x100) >> 2);
  345. UART3_D = n;
  346. tx_buffer_tail = tail;
  347. }
  348. }
  349. if ((c & UART_C2_TCIE) && (UART3_S1 & UART_S1_TC)) {
  350. transmitting = 0;
  351. if (transmit_pin) transmit_deassert();
  352. UART3_C2 = C2_TX_INACTIVE;
  353. }
  354. }
  355. #endif // HAS_KINETISK_UART3