Teensy 4.1 core updated for C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

443 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. ////////////////////////////////////////////////////////////////
  34. // Tunable parameters (relatively safe to edit these numbers)
  35. ////////////////////////////////////////////////////////////////
  36. #ifndef SERIAL3_TX_BUFFER_SIZE
  37. #define SERIAL3_TX_BUFFER_SIZE 40 // number of outgoing bytes to buffer
  38. #endif
  39. #ifndef SERIAL3_RX_BUFFER_SIZE
  40. #define SERIAL3_RX_BUFFER_SIZE 64 // number of incoming bytes to buffer
  41. #endif
  42. #define RTS_HIGH_WATERMARK 40 // RTS requests sender to pause
  43. #define RTS_LOW_WATERMARK 26 // RTS allows sender to resume
  44. #define IRQ_PRIORITY 64 // 0 = highest priority, 255 = lowest
  45. ////////////////////////////////////////////////////////////////
  46. // changes not recommended below this point....
  47. ////////////////////////////////////////////////////////////////
  48. #ifdef SERIAL_9BIT_SUPPORT
  49. static uint8_t use9Bits = 0;
  50. #define BUFTYPE uint16_t
  51. #else
  52. #define BUFTYPE uint8_t
  53. #define use9Bits 0
  54. #endif
  55. static volatile BUFTYPE tx_buffer[SERIAL3_TX_BUFFER_SIZE];
  56. static volatile BUFTYPE rx_buffer[SERIAL3_RX_BUFFER_SIZE];
  57. static volatile uint8_t transmitting = 0;
  58. #if defined(KINETISK)
  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. #elif defined(KINETISL)
  66. static volatile uint8_t *transmit_pin=NULL;
  67. static uint8_t transmit_mask=0;
  68. #define transmit_assert() *(transmit_pin+4) = transmit_mask;
  69. #define transmit_deassert() *(transmit_pin+8) = transmit_mask;
  70. static volatile uint8_t *rts_pin=NULL;
  71. static uint8_t rts_mask=0;
  72. #define rts_assert() *(rts_pin+8) = rts_mask;
  73. #define rts_deassert() *(rts_pin+4) = rts_mask;
  74. #endif
  75. #if SERIAL3_TX_BUFFER_SIZE > 255
  76. static volatile uint16_t tx_buffer_head = 0;
  77. static volatile uint16_t tx_buffer_tail = 0;
  78. #else
  79. static volatile uint8_t tx_buffer_head = 0;
  80. static volatile uint8_t tx_buffer_tail = 0;
  81. #endif
  82. #if SERIAL3_RX_BUFFER_SIZE > 255
  83. static volatile uint16_t rx_buffer_head = 0;
  84. static volatile uint16_t rx_buffer_tail = 0;
  85. #else
  86. static volatile uint8_t rx_buffer_head = 0;
  87. static volatile uint8_t rx_buffer_tail = 0;
  88. #endif
  89. #if defined(KINETISL)
  90. static uint8_t rx_pin_num = 7;
  91. #endif
  92. static uint8_t tx_pin_num = 8;
  93. // UART0 and UART1 are clocked by F_CPU, UART2 is clocked by F_BUS
  94. // UART0 has 8 byte fifo, UART1 and UART2 have 1 byte buffer
  95. #define C2_ENABLE UART_C2_TE | UART_C2_RE | UART_C2_RIE
  96. #define C2_TX_ACTIVE C2_ENABLE | UART_C2_TIE
  97. #define C2_TX_COMPLETING C2_ENABLE | UART_C2_TCIE
  98. #define C2_TX_INACTIVE C2_ENABLE
  99. void serial3_begin(uint32_t divisor)
  100. {
  101. SIM_SCGC4 |= SIM_SCGC4_UART2; // turn on clock, TODO: use bitband
  102. rx_buffer_head = 0;
  103. rx_buffer_tail = 0;
  104. tx_buffer_head = 0;
  105. tx_buffer_tail = 0;
  106. transmitting = 0;
  107. #if defined(KINETISK)
  108. CORE_PIN7_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3);
  109. CORE_PIN8_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);
  110. #elif defined(KINETISL)
  111. switch (rx_pin_num) {
  112. case 7: CORE_PIN7_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  113. case 6: CORE_PIN6_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  114. }
  115. switch (tx_pin_num) {
  116. case 8: CORE_PIN8_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); break;
  117. case 20: CORE_PIN20_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); break;
  118. }
  119. #endif
  120. #if defined(HAS_KINETISK_UART2)
  121. UART2_BDH = (divisor >> 13) & 0x1F;
  122. UART2_BDL = (divisor >> 5) & 0xFF;
  123. UART2_C4 = divisor & 0x1F;
  124. UART2_C1 = 0;
  125. UART2_PFIFO = 0;
  126. #elif defined(HAS_KINETISL_UART2)
  127. UART2_BDH = (divisor >> 8) & 0x1F;
  128. UART2_BDL = divisor & 0xFF;
  129. UART2_C1 = 0;
  130. #endif
  131. UART2_C2 = C2_TX_INACTIVE;
  132. NVIC_SET_PRIORITY(IRQ_UART2_STATUS, IRQ_PRIORITY);
  133. NVIC_ENABLE_IRQ(IRQ_UART2_STATUS);
  134. }
  135. void serial3_format(uint32_t format)
  136. {
  137. uint8_t c;
  138. c = UART2_C1;
  139. c = (c & ~0x13) | (format & 0x03); // configure parity
  140. if (format & 0x04) c |= 0x10; // 9 bits (might include parity)
  141. UART2_C1 = c;
  142. if ((format & 0x0F) == 0x04) UART2_C3 |= 0x40; // 8N2 is 9 bit with 9th bit always 1
  143. c = UART2_S2 & ~0x10;
  144. if (format & 0x10) c |= 0x10; // rx invert
  145. UART2_S2 = c;
  146. c = UART2_C3 & ~0x10;
  147. if (format & 0x20) c |= 0x10; // tx invert
  148. UART2_C3 = c;
  149. #ifdef SERIAL_9BIT_SUPPORT
  150. c = UART2_C4 & 0x1F;
  151. if (format & 0x08) c |= 0x20; // 9 bit mode with parity (requires 10 bits)
  152. UART2_C4 = c;
  153. use9Bits = format & 0x80;
  154. #endif
  155. #if defined(__MK64FX512__) || defined(__MK66FX1M0__) || defined(KINETISL)
  156. // For T3.5/T3.6/TLC See about turning on 2 stop bit mode
  157. if ( format & 0x100) {
  158. uint8_t bdl = UART2_BDL;
  159. UART2_BDH |= UART_BDH_SBNS; // Turn on 2 stop bits - was turned off by set baud
  160. UART2_BDL = bdl; // Says BDH not acted on until BDL is written
  161. }
  162. #endif
  163. }
  164. void serial3_end(void)
  165. {
  166. if (!(SIM_SCGC4 & SIM_SCGC4_UART2)) return;
  167. while (transmitting) yield(); // wait for buffered data to send
  168. NVIC_DISABLE_IRQ(IRQ_UART2_STATUS);
  169. UART2_C2 = 0;
  170. CORE_PIN7_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
  171. CORE_PIN8_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
  172. rx_buffer_head = 0;
  173. rx_buffer_tail = 0;
  174. if (rts_pin) rts_deassert();
  175. }
  176. void serial3_set_transmit_pin(uint8_t pin)
  177. {
  178. while (transmitting) ;
  179. pinMode(pin, OUTPUT);
  180. digitalWrite(pin, LOW);
  181. transmit_pin = portOutputRegister(pin);
  182. #if defined(KINETISL)
  183. transmit_mask = digitalPinToBitMask(pin);
  184. #endif
  185. }
  186. void serial3_set_tx(uint8_t pin, uint8_t opendrain)
  187. {
  188. uint32_t cfg;
  189. if (opendrain) pin |= 128;
  190. if (pin == tx_pin_num) return;
  191. if ((SIM_SCGC4 & SIM_SCGC4_UART2)) {
  192. switch (tx_pin_num & 127) {
  193. case 8: CORE_PIN8_CONFIG = 0; break; // PTD3
  194. #if defined(KINETISL)
  195. case 20: CORE_PIN20_CONFIG = 0; break; // PTD5
  196. #endif
  197. }
  198. if (opendrain) {
  199. cfg = PORT_PCR_DSE | PORT_PCR_ODE;
  200. } else {
  201. cfg = PORT_PCR_DSE | PORT_PCR_SRE;
  202. }
  203. switch (pin & 127) {
  204. case 8: CORE_PIN8_CONFIG = cfg | PORT_PCR_MUX(3); break;
  205. #if defined(KINETISL)
  206. case 20: CORE_PIN20_CONFIG = cfg | PORT_PCR_MUX(3); break;
  207. #endif
  208. }
  209. }
  210. tx_pin_num = pin;
  211. }
  212. void serial3_set_rx(uint8_t pin)
  213. {
  214. #if defined(KINETISL)
  215. if (pin == rx_pin_num) return;
  216. if ((SIM_SCGC4 & SIM_SCGC4_UART2)) {
  217. switch (rx_pin_num) {
  218. case 7: CORE_PIN7_CONFIG = 0; break; // PTD2
  219. case 6: CORE_PIN6_CONFIG = 0; break; // PTD4
  220. }
  221. switch (pin) {
  222. case 7: CORE_PIN7_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  223. case 6: CORE_PIN6_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  224. }
  225. }
  226. rx_pin_num = pin;
  227. #endif
  228. }
  229. int serial3_set_rts(uint8_t pin)
  230. {
  231. if (!(SIM_SCGC4 & SIM_SCGC4_UART2)) return 0;
  232. if (pin < CORE_NUM_DIGITAL) {
  233. rts_pin = portOutputRegister(pin);
  234. #if defined(KINETISL)
  235. rts_mask = digitalPinToBitMask(pin);
  236. #endif
  237. pinMode(pin, OUTPUT);
  238. rts_assert();
  239. } else {
  240. rts_pin = NULL;
  241. return 0;
  242. }
  243. /*
  244. if (pin == 2) {
  245. CORE_PIN2_CONFIG = PORT_PCR_MUX(3);
  246. } else {
  247. UART2_MODEM &= ~UART_MODEM_RXRTSE;
  248. return 0;
  249. }
  250. UART2_MODEM |= UART_MODEM_RXRTSE;
  251. */
  252. return 1;
  253. }
  254. int serial3_set_cts(uint8_t pin)
  255. {
  256. #if defined(KINETISK)
  257. if (!(SIM_SCGC4 & SIM_SCGC4_UART2)) return 0;
  258. if (pin == 14) {
  259. CORE_PIN14_CONFIG = PORT_PCR_MUX(3) | PORT_PCR_PE; // weak pulldown
  260. } else {
  261. UART2_MODEM &= ~UART_MODEM_TXCTSE;
  262. return 0;
  263. }
  264. UART2_MODEM |= UART_MODEM_TXCTSE;
  265. return 1;
  266. #else
  267. return 0;
  268. #endif
  269. }
  270. void serial3_putchar(uint32_t c)
  271. {
  272. uint32_t head, n;
  273. if (!(SIM_SCGC4 & SIM_SCGC4_UART2)) return;
  274. if (transmit_pin) transmit_assert();
  275. head = tx_buffer_head;
  276. if (++head >= SERIAL3_TX_BUFFER_SIZE) head = 0;
  277. while (tx_buffer_tail == head) {
  278. int priority = nvic_execution_priority();
  279. if (priority <= IRQ_PRIORITY) {
  280. if ((UART2_S1 & UART_S1_TDRE)) {
  281. uint32_t tail = tx_buffer_tail;
  282. if (++tail >= SERIAL3_TX_BUFFER_SIZE) tail = 0;
  283. n = tx_buffer[tail];
  284. if (use9Bits) UART2_C3 = (UART2_C3 & ~0x40) | ((n & 0x100) >> 2);
  285. UART2_D = n;
  286. tx_buffer_tail = tail;
  287. }
  288. } else if (priority >= 256) {
  289. yield(); // wait
  290. }
  291. }
  292. tx_buffer[head] = c;
  293. transmitting = 1;
  294. tx_buffer_head = head;
  295. UART2_C2 = C2_TX_ACTIVE;
  296. }
  297. void serial3_write(const void *buf, unsigned int count)
  298. {
  299. const uint8_t *p = (const uint8_t *)buf;
  300. while (count-- > 0) serial3_putchar(*p++);
  301. }
  302. void serial3_flush(void)
  303. {
  304. while (transmitting) yield(); // wait
  305. }
  306. int serial3_write_buffer_free(void)
  307. {
  308. uint32_t head, tail;
  309. head = tx_buffer_head;
  310. tail = tx_buffer_tail;
  311. if (head >= tail) return SERIAL3_TX_BUFFER_SIZE - 1 - head + tail;
  312. return tail - head - 1;
  313. }
  314. int serial3_available(void)
  315. {
  316. uint32_t head, tail;
  317. head = rx_buffer_head;
  318. tail = rx_buffer_tail;
  319. if (head >= tail) return head - tail;
  320. return SERIAL3_RX_BUFFER_SIZE + head - tail;
  321. }
  322. int serial3_getchar(void)
  323. {
  324. uint32_t head, tail;
  325. int c;
  326. head = rx_buffer_head;
  327. tail = rx_buffer_tail;
  328. if (head == tail) return -1;
  329. if (++tail >= SERIAL3_RX_BUFFER_SIZE) tail = 0;
  330. c = rx_buffer[tail];
  331. rx_buffer_tail = tail;
  332. if (rts_pin) {
  333. int avail;
  334. if (head >= tail) avail = head - tail;
  335. else avail = SERIAL3_RX_BUFFER_SIZE + head - tail;
  336. if (avail <= RTS_LOW_WATERMARK) rts_assert();
  337. }
  338. return c;
  339. }
  340. int serial3_peek(void)
  341. {
  342. uint32_t head, tail;
  343. head = rx_buffer_head;
  344. tail = rx_buffer_tail;
  345. if (head == tail) return -1;
  346. if (++tail >= SERIAL3_RX_BUFFER_SIZE) tail = 0;
  347. return rx_buffer[tail];
  348. }
  349. void serial3_clear(void)
  350. {
  351. rx_buffer_head = rx_buffer_tail;
  352. if (rts_pin) rts_assert();
  353. }
  354. // status interrupt combines
  355. // Transmit data below watermark UART_S1_TDRE
  356. // Transmit complete UART_S1_TC
  357. // Idle line UART_S1_IDLE
  358. // Receive data above watermark UART_S1_RDRF
  359. // LIN break detect UART_S2_LBKDIF
  360. // RxD pin active edge UART_S2_RXEDGIF
  361. void uart2_status_isr(void)
  362. {
  363. uint32_t head, tail, n;
  364. uint8_t c;
  365. if (UART2_S1 & UART_S1_RDRF) {
  366. if (use9Bits && (UART2_C3 & 0x80)) {
  367. n = UART2_D | 0x100;
  368. } else {
  369. n = UART2_D;
  370. }
  371. head = rx_buffer_head + 1;
  372. if (head >= SERIAL3_RX_BUFFER_SIZE) head = 0;
  373. if (head != rx_buffer_tail) {
  374. rx_buffer[head] = n;
  375. rx_buffer_head = head;
  376. }
  377. if (rts_pin) {
  378. int avail;
  379. tail = tx_buffer_tail;
  380. if (head >= tail) avail = head - tail;
  381. else avail = SERIAL3_RX_BUFFER_SIZE + head - tail;
  382. if (avail >= RTS_HIGH_WATERMARK) rts_deassert();
  383. }
  384. }
  385. c = UART2_C2;
  386. if ((c & UART_C2_TIE) && (UART2_S1 & UART_S1_TDRE)) {
  387. head = tx_buffer_head;
  388. tail = tx_buffer_tail;
  389. if (head == tail) {
  390. UART2_C2 = C2_TX_COMPLETING;
  391. } else {
  392. if (++tail >= SERIAL3_TX_BUFFER_SIZE) tail = 0;
  393. n = tx_buffer[tail];
  394. if (use9Bits) UART2_C3 = (UART2_C3 & ~0x40) | ((n & 0x100) >> 2);
  395. UART2_D = n;
  396. tx_buffer_tail = tail;
  397. }
  398. }
  399. if ((c & UART_C2_TCIE) && (UART2_S1 & UART_S1_TC)) {
  400. transmitting = 0;
  401. if (transmit_pin) transmit_deassert();
  402. UART2_C2 = C2_TX_INACTIVE;
  403. }
  404. }