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.

430 lines
12KB

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