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.

519 lines
15KB

  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. ////////////////////////////////////////////////////////////////
  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 (SERIAL3_RX_BUFFER_SIZE-24) // RTS requests sender to pause
  43. #define RTS_LOW_WATERMARK (SERIAL3_RX_BUFFER_SIZE-38) // 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 > 65535
  76. static volatile uint32_t tx_buffer_head = 0;
  77. static volatile uint32_t tx_buffer_tail = 0;
  78. #elif SERIAL3_TX_BUFFER_SIZE > 255
  79. static volatile uint16_t tx_buffer_head = 0;
  80. static volatile uint16_t tx_buffer_tail = 0;
  81. #else
  82. static volatile uint8_t tx_buffer_head = 0;
  83. static volatile uint8_t tx_buffer_tail = 0;
  84. #endif
  85. #if SERIAL3_RX_BUFFER_SIZE > 65535
  86. static volatile uint32_t rx_buffer_head = 0;
  87. static volatile uint32_t rx_buffer_tail = 0;
  88. #elif SERIAL3_RX_BUFFER_SIZE > 255
  89. static volatile uint16_t rx_buffer_head = 0;
  90. static volatile uint16_t rx_buffer_tail = 0;
  91. #else
  92. static volatile uint8_t rx_buffer_head = 0;
  93. static volatile uint8_t rx_buffer_tail = 0;
  94. #endif
  95. #if defined(KINETISL)
  96. static uint8_t rx_pin_num = 7;
  97. #endif
  98. static uint8_t tx_pin_num = 8;
  99. #if defined(KINETISL)
  100. static uint8_t half_duplex_mode = 0;
  101. #endif
  102. // UART0 and UART1 are clocked by F_CPU, UART2 is clocked by F_BUS
  103. // UART0 has 8 byte fifo, UART1 and UART2 have 1 byte buffer
  104. #define C2_ENABLE UART_C2_TE | UART_C2_RE | UART_C2_RIE
  105. #define C2_TX_ACTIVE C2_ENABLE | UART_C2_TIE
  106. #define C2_TX_COMPLETING C2_ENABLE | UART_C2_TCIE
  107. #define C2_TX_INACTIVE C2_ENABLE
  108. // BITBAND Support
  109. #define GPIO_BITBAND_ADDR(reg, bit) (((uint32_t)&(reg) - 0x40000000) * 32 + (bit) * 4 + 0x42000000)
  110. #define GPIO_BITBAND_PTR(reg, bit) ((uint32_t *)GPIO_BITBAND_ADDR((reg), (bit)))
  111. #define C3_TXDIR_BIT 5
  112. void serial3_begin(uint32_t divisor)
  113. {
  114. SIM_SCGC4 |= SIM_SCGC4_UART2; // turn on clock, TODO: use bitband
  115. rx_buffer_head = 0;
  116. rx_buffer_tail = 0;
  117. tx_buffer_head = 0;
  118. tx_buffer_tail = 0;
  119. transmitting = 0;
  120. #if defined(KINETISK)
  121. CORE_PIN7_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3);
  122. CORE_PIN8_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);
  123. #elif defined(KINETISL)
  124. switch (rx_pin_num) {
  125. case 7: CORE_PIN7_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  126. case 6: CORE_PIN6_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  127. }
  128. switch (tx_pin_num) {
  129. case 8: CORE_PIN8_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); break;
  130. case 20: CORE_PIN20_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); break;
  131. }
  132. #endif
  133. #if defined(HAS_KINETISK_UART2)
  134. if (divisor < 32) divisor = 32;
  135. UART2_BDH = (divisor >> 13) & 0x1F;
  136. UART2_BDL = (divisor >> 5) & 0xFF;
  137. UART2_C4 = divisor & 0x1F;
  138. UART2_C1 = 0;
  139. UART2_PFIFO = 0;
  140. #elif defined(HAS_KINETISL_UART2)
  141. if (divisor < 1) divisor = 1;
  142. UART2_BDH = (divisor >> 8) & 0x1F;
  143. UART2_BDL = divisor & 0xFF;
  144. UART2_C1 = 0;
  145. #endif
  146. UART2_C2 = C2_TX_INACTIVE;
  147. NVIC_SET_PRIORITY(IRQ_UART2_STATUS, IRQ_PRIORITY);
  148. NVIC_ENABLE_IRQ(IRQ_UART2_STATUS);
  149. }
  150. void serial3_format(uint32_t format)
  151. {
  152. uint8_t c;
  153. c = UART2_C1;
  154. c = (c & ~0x13) | (format & 0x03); // configure parity
  155. if (format & 0x04) c |= 0x10; // 9 bits (might include parity)
  156. UART2_C1 = c;
  157. if ((format & 0x0F) == 0x04) UART2_C3 |= 0x40; // 8N2 is 9 bit with 9th bit always 1
  158. c = UART2_S2 & ~0x10;
  159. if (format & 0x10) c |= 0x10; // rx invert
  160. UART2_S2 = c;
  161. c = UART2_C3 & ~0x10;
  162. if (format & 0x20) c |= 0x10; // tx invert
  163. UART2_C3 = c;
  164. #if defined(SERIAL_9BIT_SUPPORT) && !defined(KINETISL)
  165. c = UART2_C4 & 0x1F;
  166. if (format & 0x08) c |= 0x20; // 9 bit mode with parity (requires 10 bits)
  167. UART2_C4 = c;
  168. use9Bits = format & 0x80;
  169. #endif
  170. #if defined(__MK64FX512__) || defined(__MK66FX1M0__) || defined(KINETISL)
  171. // For T3.5/T3.6/TLC See about turning on 2 stop bit mode
  172. if ( format & 0x100) {
  173. uint8_t bdl = UART2_BDL;
  174. UART2_BDH |= UART_BDH_SBNS; // Turn on 2 stop bits - was turned off by set baud
  175. UART2_BDL = bdl; // Says BDH not acted on until BDL is written
  176. }
  177. #endif
  178. // process request for half duplex.
  179. if ((format & SERIAL_HALF_DUPLEX) != 0) {
  180. c = UART2_C1;
  181. c |= UART_C1_LOOPS | UART_C1_RSRC;
  182. UART2_C1 = c;
  183. // Lets try to make use of bitband address to set the direction for ue...
  184. #if defined(KINETISL)
  185. switch (tx_pin_num) {
  186. case 8: CORE_PIN8_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  187. case 20: CORE_PIN20_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  188. }
  189. half_duplex_mode = 1;
  190. #else
  191. volatile uint32_t *reg = portConfigRegister(tx_pin_num);
  192. *reg = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3) | PORT_PCR_PE | PORT_PCR_PS; // pullup on output pin;
  193. transmit_pin = (uint8_t*)GPIO_BITBAND_PTR(UART2_C3, C3_TXDIR_BIT);
  194. #endif
  195. } else {
  196. #if defined(KINETISL)
  197. half_duplex_mode = 0;
  198. #else
  199. if (transmit_pin == (uint8_t*)GPIO_BITBAND_PTR(UART2_C3, C3_TXDIR_BIT)) transmit_pin = NULL;
  200. #endif
  201. }
  202. }
  203. void serial3_end(void)
  204. {
  205. if (!(SIM_SCGC4 & SIM_SCGC4_UART2)) return;
  206. while (transmitting) yield(); // wait for buffered data to send
  207. NVIC_DISABLE_IRQ(IRQ_UART2_STATUS);
  208. UART2_C2 = 0;
  209. #if defined(KINETISK)
  210. CORE_PIN7_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
  211. CORE_PIN8_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
  212. #elif defined(KINETISL)
  213. switch (rx_pin_num) {
  214. case 7: CORE_PIN7_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break;
  215. case 6: CORE_PIN6_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break;
  216. }
  217. switch (tx_pin_num & 127) {
  218. case 8: CORE_PIN8_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break;
  219. case 20: CORE_PIN20_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break;
  220. }
  221. #endif
  222. UART2_S1;
  223. UART2_D; // clear leftover error status
  224. rx_buffer_head = 0;
  225. rx_buffer_tail = 0;
  226. if (rts_pin) rts_deassert();
  227. }
  228. void serial3_set_transmit_pin(uint8_t pin)
  229. {
  230. while (transmitting) ;
  231. pinMode(pin, OUTPUT);
  232. digitalWrite(pin, LOW);
  233. transmit_pin = portOutputRegister(pin);
  234. #if defined(KINETISL)
  235. transmit_mask = digitalPinToBitMask(pin);
  236. #endif
  237. }
  238. void serial3_set_tx(uint8_t pin, uint8_t opendrain)
  239. {
  240. uint32_t cfg;
  241. if (opendrain) pin |= 128;
  242. if (pin == tx_pin_num) return;
  243. if ((SIM_SCGC4 & SIM_SCGC4_UART2)) {
  244. switch (tx_pin_num & 127) {
  245. case 8: CORE_PIN8_CONFIG = 0; break; // PTD3
  246. #if defined(KINETISL)
  247. case 20: CORE_PIN20_CONFIG = 0; break; // PTD5
  248. #endif
  249. }
  250. if (opendrain) {
  251. cfg = PORT_PCR_DSE | PORT_PCR_ODE;
  252. } else {
  253. cfg = PORT_PCR_DSE | PORT_PCR_SRE;
  254. }
  255. switch (pin & 127) {
  256. case 8: CORE_PIN8_CONFIG = cfg | PORT_PCR_MUX(3); break;
  257. #if defined(KINETISL)
  258. case 20: CORE_PIN20_CONFIG = cfg | PORT_PCR_MUX(3); break;
  259. #endif
  260. }
  261. }
  262. tx_pin_num = pin;
  263. }
  264. void serial3_set_rx(uint8_t pin)
  265. {
  266. #if defined(KINETISL)
  267. if (pin == rx_pin_num) return;
  268. if ((SIM_SCGC4 & SIM_SCGC4_UART2)) {
  269. switch (rx_pin_num) {
  270. case 7: CORE_PIN7_CONFIG = 0; break; // PTD2
  271. case 6: CORE_PIN6_CONFIG = 0; break; // PTD4
  272. }
  273. switch (pin) {
  274. case 7: CORE_PIN7_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  275. case 6: CORE_PIN6_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  276. }
  277. }
  278. rx_pin_num = pin;
  279. #endif
  280. }
  281. int serial3_set_rts(uint8_t pin)
  282. {
  283. if (!(SIM_SCGC4 & SIM_SCGC4_UART2)) return 0;
  284. if (pin < CORE_NUM_DIGITAL) {
  285. rts_pin = portOutputRegister(pin);
  286. #if defined(KINETISL)
  287. rts_mask = digitalPinToBitMask(pin);
  288. #endif
  289. pinMode(pin, OUTPUT);
  290. rts_assert();
  291. } else {
  292. rts_pin = NULL;
  293. return 0;
  294. }
  295. /*
  296. if (pin == 2) {
  297. CORE_PIN2_CONFIG = PORT_PCR_MUX(3);
  298. } else {
  299. UART2_MODEM &= ~UART_MODEM_RXRTSE;
  300. return 0;
  301. }
  302. UART2_MODEM |= UART_MODEM_RXRTSE;
  303. */
  304. return 1;
  305. }
  306. int serial3_set_cts(uint8_t pin)
  307. {
  308. #if defined(KINETISK)
  309. if (!(SIM_SCGC4 & SIM_SCGC4_UART2)) return 0;
  310. if (pin == 14) {
  311. CORE_PIN14_CONFIG = PORT_PCR_MUX(3) | PORT_PCR_PE; // weak pulldown
  312. } else {
  313. UART2_MODEM &= ~UART_MODEM_TXCTSE;
  314. return 0;
  315. }
  316. UART2_MODEM |= UART_MODEM_TXCTSE;
  317. return 1;
  318. #else
  319. return 0;
  320. #endif
  321. }
  322. void serial3_putchar(uint32_t c)
  323. {
  324. uint32_t head, n;
  325. if (!(SIM_SCGC4 & SIM_SCGC4_UART2)) return;
  326. if (transmit_pin) transmit_assert();
  327. #if defined(KINETISL)
  328. if (half_duplex_mode) {
  329. __disable_irq();
  330. volatile uint32_t reg = UART2_C3;
  331. reg |= UART_C3_TXDIR;
  332. UART2_C3 = reg;
  333. __enable_irq();
  334. }
  335. #endif
  336. head = tx_buffer_head;
  337. if (++head >= SERIAL3_TX_BUFFER_SIZE) head = 0;
  338. while (tx_buffer_tail == head) {
  339. int priority = nvic_execution_priority();
  340. if (priority <= IRQ_PRIORITY) {
  341. if ((UART2_S1 & UART_S1_TDRE)) {
  342. uint32_t tail = tx_buffer_tail;
  343. if (++tail >= SERIAL3_TX_BUFFER_SIZE) tail = 0;
  344. n = tx_buffer[tail];
  345. if (use9Bits) UART2_C3 = (UART2_C3 & ~0x40) | ((n & 0x100) >> 2);
  346. UART2_D = n;
  347. tx_buffer_tail = tail;
  348. }
  349. } else if (priority >= 256) {
  350. yield(); // wait
  351. }
  352. }
  353. tx_buffer[head] = c;
  354. transmitting = 1;
  355. tx_buffer_head = head;
  356. UART2_C2 = C2_TX_ACTIVE;
  357. }
  358. void serial3_write(const void *buf, unsigned int count)
  359. {
  360. const uint8_t *p = (const uint8_t *)buf;
  361. while (count-- > 0) serial3_putchar(*p++);
  362. }
  363. void serial3_flush(void)
  364. {
  365. while (transmitting) yield(); // wait
  366. }
  367. int serial3_write_buffer_free(void)
  368. {
  369. uint32_t head, tail;
  370. head = tx_buffer_head;
  371. tail = tx_buffer_tail;
  372. if (head >= tail) return SERIAL3_TX_BUFFER_SIZE - 1 - head + tail;
  373. return tail - head - 1;
  374. }
  375. int serial3_available(void)
  376. {
  377. uint32_t head, tail;
  378. head = rx_buffer_head;
  379. tail = rx_buffer_tail;
  380. if (head >= tail) return head - tail;
  381. return SERIAL3_RX_BUFFER_SIZE + head - tail;
  382. }
  383. int serial3_getchar(void)
  384. {
  385. uint32_t head, tail;
  386. int c;
  387. head = rx_buffer_head;
  388. tail = rx_buffer_tail;
  389. if (head == tail) return -1;
  390. if (++tail >= SERIAL3_RX_BUFFER_SIZE) tail = 0;
  391. c = rx_buffer[tail];
  392. rx_buffer_tail = tail;
  393. if (rts_pin) {
  394. int avail;
  395. if (head >= tail) avail = head - tail;
  396. else avail = SERIAL3_RX_BUFFER_SIZE + head - tail;
  397. if (avail <= RTS_LOW_WATERMARK) rts_assert();
  398. }
  399. return c;
  400. }
  401. int serial3_peek(void)
  402. {
  403. uint32_t head, tail;
  404. head = rx_buffer_head;
  405. tail = rx_buffer_tail;
  406. if (head == tail) return -1;
  407. if (++tail >= SERIAL3_RX_BUFFER_SIZE) tail = 0;
  408. return rx_buffer[tail];
  409. }
  410. void serial3_clear(void)
  411. {
  412. rx_buffer_head = rx_buffer_tail;
  413. if (rts_pin) rts_assert();
  414. }
  415. // status interrupt combines
  416. // Transmit data below watermark UART_S1_TDRE
  417. // Transmit complete UART_S1_TC
  418. // Idle line UART_S1_IDLE
  419. // Receive data above watermark UART_S1_RDRF
  420. // LIN break detect UART_S2_LBKDIF
  421. // RxD pin active edge UART_S2_RXEDGIF
  422. void uart2_status_isr(void)
  423. {
  424. uint32_t head, tail, n;
  425. uint8_t c;
  426. if (UART2_S1 & UART_S1_RDRF) {
  427. if (use9Bits && (UART2_C3 & 0x80)) {
  428. n = UART2_D | 0x100;
  429. } else {
  430. n = UART2_D;
  431. }
  432. head = rx_buffer_head + 1;
  433. if (head >= SERIAL3_RX_BUFFER_SIZE) head = 0;
  434. if (head != rx_buffer_tail) {
  435. rx_buffer[head] = n;
  436. rx_buffer_head = head;
  437. }
  438. if (rts_pin) {
  439. int avail;
  440. tail = tx_buffer_tail;
  441. if (head >= tail) avail = head - tail;
  442. else avail = SERIAL3_RX_BUFFER_SIZE + head - tail;
  443. if (avail >= RTS_HIGH_WATERMARK) rts_deassert();
  444. }
  445. }
  446. c = UART2_C2;
  447. if ((c & UART_C2_TIE) && (UART2_S1 & UART_S1_TDRE)) {
  448. head = tx_buffer_head;
  449. tail = tx_buffer_tail;
  450. if (head == tail) {
  451. UART2_C2 = C2_TX_COMPLETING;
  452. } else {
  453. if (++tail >= SERIAL3_TX_BUFFER_SIZE) tail = 0;
  454. n = tx_buffer[tail];
  455. if (use9Bits) UART2_C3 = (UART2_C3 & ~0x40) | ((n & 0x100) >> 2);
  456. UART2_D = n;
  457. tx_buffer_tail = tail;
  458. }
  459. }
  460. if ((c & UART_C2_TCIE) && (UART2_S1 & UART_S1_TC)) {
  461. transmitting = 0;
  462. if (transmit_pin) transmit_deassert();
  463. #if defined(KINETISL)
  464. if (transmit_pin) transmit_deassert();
  465. if (half_duplex_mode) {
  466. __disable_irq();
  467. volatile uint32_t reg = UART2_C3;
  468. reg &= ~UART_C3_TXDIR;
  469. UART2_C3 = reg;
  470. __enable_irq();
  471. }
  472. #endif
  473. UART2_C2 = C2_TX_INACTIVE;
  474. }
  475. }