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.

488 lines
14KB

  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. #define TX_BUFFER_SIZE 40 // number of outgoing bytes to buffer
  37. #define RX_BUFFER_SIZE 64 // number of incoming bytes to buffer
  38. #define RTS_HIGH_WATERMARK 40 // RTS requests sender to pause
  39. #define RTS_LOW_WATERMARK 26 // RTS allows sender to resume
  40. #define IRQ_PRIORITY 64 // 0 = highest priority, 255 = lowest
  41. ////////////////////////////////////////////////////////////////
  42. // changes not recommended below this point....
  43. ////////////////////////////////////////////////////////////////
  44. #ifdef SERIAL_9BIT_SUPPORT
  45. static uint8_t use9Bits = 0;
  46. #define BUFTYPE uint16_t
  47. #else
  48. #define BUFTYPE uint8_t
  49. #define use9Bits 0
  50. #endif
  51. static volatile BUFTYPE tx_buffer[TX_BUFFER_SIZE];
  52. static volatile BUFTYPE rx_buffer[RX_BUFFER_SIZE];
  53. static volatile uint8_t transmitting = 0;
  54. #if defined(KINETISK)
  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. #elif defined(KINETISL)
  62. static volatile uint8_t *transmit_pin=NULL;
  63. static uint8_t transmit_mask=0;
  64. #define transmit_assert() *(transmit_pin+4) = transmit_mask;
  65. #define transmit_deassert() *(transmit_pin+8) = transmit_mask;
  66. static volatile uint8_t *rts_pin=NULL;
  67. static uint8_t rts_mask=0;
  68. #define rts_assert() *(rts_pin+8) = rts_mask;
  69. #define rts_deassert() *(rts_pin+4) = rts_mask;
  70. #endif
  71. #if TX_BUFFER_SIZE > 255
  72. static volatile uint16_t tx_buffer_head = 0;
  73. static volatile uint16_t tx_buffer_tail = 0;
  74. #else
  75. static volatile uint8_t tx_buffer_head = 0;
  76. static volatile uint8_t tx_buffer_tail = 0;
  77. #endif
  78. #if 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. // UART0 and UART1 are clocked by F_CPU, UART2 is clocked by F_BUS
  86. // UART0 has 8 byte fifo, UART1 and UART2 have 1 byte buffer
  87. #ifdef HAS_KINETISK_UART1_FIFO
  88. #define C2_ENABLE UART_C2_TE | UART_C2_RE | UART_C2_RIE | UART_C2_ILIE
  89. #else
  90. #define C2_ENABLE UART_C2_TE | UART_C2_RE | UART_C2_RIE
  91. #endif
  92. #define C2_TX_ACTIVE C2_ENABLE | UART_C2_TIE
  93. #define C2_TX_COMPLETING C2_ENABLE | UART_C2_TCIE
  94. #define C2_TX_INACTIVE C2_ENABLE
  95. void serial2_begin(uint32_t divisor)
  96. {
  97. SIM_SCGC4 |= SIM_SCGC4_UART1; // turn on clock, TODO: use bitband
  98. rx_buffer_head = 0;
  99. rx_buffer_tail = 0;
  100. tx_buffer_head = 0;
  101. tx_buffer_tail = 0;
  102. transmitting = 0;
  103. CORE_PIN9_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3);
  104. CORE_PIN10_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);
  105. #if defined(HAS_KINETISK_UART1)
  106. UART1_BDH = (divisor >> 13) & 0x1F;
  107. UART1_BDL = (divisor >> 5) & 0xFF;
  108. UART1_C4 = divisor & 0x1F;
  109. #ifdef HAS_KINETISK_UART1_FIFO
  110. UART1_C1 = UART_C1_ILT;
  111. UART1_TWFIFO = 2; // tx watermark, causes S1_TDRE to set
  112. UART1_RWFIFO = 4; // rx watermark, causes S1_RDRF to set
  113. UART1_PFIFO = UART_PFIFO_TXFE | UART_PFIFO_RXFE;
  114. #else
  115. UART1_C1 = 0;
  116. UART1_PFIFO = 0;
  117. #endif
  118. #elif defined(HAS_KINETISL_UART1)
  119. UART1_BDH = (divisor >> 8) & 0x1F;
  120. UART1_BDL = divisor & 0xFF;
  121. UART1_C1 = 0;
  122. #endif
  123. UART1_C2 = C2_TX_INACTIVE;
  124. NVIC_SET_PRIORITY(IRQ_UART1_STATUS, IRQ_PRIORITY);
  125. NVIC_ENABLE_IRQ(IRQ_UART1_STATUS);
  126. }
  127. void serial2_format(uint32_t format)
  128. {
  129. uint8_t c;
  130. c = UART1_C1;
  131. c = (c & ~0x13) | (format & 0x03); // configure parity
  132. if (format & 0x04) c |= 0x10; // 9 bits (might include parity)
  133. UART1_C1 = c;
  134. if ((format & 0x0F) == 0x04) UART1_C3 |= 0x40; // 8N2 is 9 bit with 9th bit always 1
  135. c = UART1_S2 & ~0x10;
  136. if (format & 0x10) c |= 0x10; // rx invert
  137. UART1_S2 = c;
  138. c = UART1_C3 & ~0x10;
  139. if (format & 0x20) c |= 0x10; // tx invert
  140. UART1_C3 = c;
  141. #ifdef SERIAL_9BIT_SUPPORT
  142. c = UART1_C4 & 0x1F;
  143. if (format & 0x08) c |= 0x20; // 9 bit mode with parity (requires 10 bits)
  144. UART1_C4 = c;
  145. use9Bits = format & 0x80;
  146. #endif
  147. // UART1_C1.0 = parity, 0=even, 1=odd
  148. // UART1_C1.1 = parity, 0=disable, 1=enable
  149. // UART1_C1.4 = mode, 1=9bit, 0=8bit
  150. // UART1_C4.5 = mode, 1=10bit, 0=8bit
  151. // UART1_C3.4 = txinv, 0=normal, 1=inverted
  152. // UART1_S2.4 = rxinv, 0=normal, 1=inverted
  153. }
  154. void serial2_end(void)
  155. {
  156. if (!(SIM_SCGC4 & SIM_SCGC4_UART1)) return;
  157. while (transmitting) yield(); // wait for buffered data to send
  158. NVIC_DISABLE_IRQ(IRQ_UART1_STATUS);
  159. UART1_C2 = 0;
  160. CORE_PIN9_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
  161. CORE_PIN10_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
  162. rx_buffer_head = 0;
  163. rx_buffer_tail = 0;
  164. if (rts_pin) rts_deassert();
  165. }
  166. void serial2_set_transmit_pin(uint8_t pin)
  167. {
  168. while (transmitting) ;
  169. pinMode(pin, OUTPUT);
  170. digitalWrite(pin, LOW);
  171. transmit_pin = portOutputRegister(pin);
  172. #if defined(KINETISL)
  173. transmit_mask = digitalPinToBitMask(pin);
  174. #endif
  175. }
  176. int serial2_set_rts(uint8_t pin)
  177. {
  178. if (!(SIM_SCGC4 & SIM_SCGC4_UART1)) return 0;
  179. if (pin < CORE_NUM_DIGITAL) {
  180. rts_pin = portOutputRegister(pin);
  181. #if defined(KINETISL)
  182. rts_mask = digitalPinToBitMask(pin);
  183. #endif
  184. pinMode(pin, OUTPUT);
  185. rts_assert();
  186. } else {
  187. rts_pin = NULL;
  188. return 0;
  189. }
  190. /*
  191. if (!(SIM_SCGC4 & SIM_SCGC4_UART1)) return 0;
  192. if (pin == 22) {
  193. CORE_PIN22_CONFIG = PORT_PCR_MUX(3);
  194. } else {
  195. UART1_MODEM &= ~UART_MODEM_RXRTSE;
  196. return 0;
  197. }
  198. UART1_MODEM |= UART_MODEM_RXRTSE;
  199. */
  200. return 1;
  201. }
  202. int serial2_set_cts(uint8_t pin)
  203. {
  204. #if defined(KINETISK)
  205. if (!(SIM_SCGC4 & SIM_SCGC4_UART1)) return 0;
  206. if (pin == 23) {
  207. CORE_PIN23_CONFIG = PORT_PCR_MUX(3) | PORT_PCR_PE; // weak pulldown
  208. } else {
  209. UART1_MODEM &= ~UART_MODEM_TXCTSE;
  210. return 0;
  211. }
  212. UART1_MODEM |= UART_MODEM_TXCTSE;
  213. return 1;
  214. #else
  215. return 0;
  216. #endif
  217. }
  218. void serial2_putchar(uint32_t c)
  219. {
  220. uint32_t head, n;
  221. if (!(SIM_SCGC4 & SIM_SCGC4_UART1)) return;
  222. if (transmit_pin) transmit_assert();
  223. head = tx_buffer_head;
  224. if (++head >= TX_BUFFER_SIZE) head = 0;
  225. while (tx_buffer_tail == head) {
  226. int priority = nvic_execution_priority();
  227. if (priority <= IRQ_PRIORITY) {
  228. if ((UART1_S1 & UART_S1_TDRE)) {
  229. uint32_t tail = tx_buffer_tail;
  230. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  231. n = tx_buffer[tail];
  232. if (use9Bits) UART1_C3 = (UART1_C3 & ~0x40) | ((n & 0x100) >> 2);
  233. UART1_D = n;
  234. tx_buffer_tail = tail;
  235. }
  236. } else if (priority >= 256) {
  237. yield(); // wait
  238. }
  239. }
  240. tx_buffer[head] = c;
  241. transmitting = 1;
  242. tx_buffer_head = head;
  243. UART1_C2 = C2_TX_ACTIVE;
  244. }
  245. #ifdef HAS_KINETISK_UART1_FIFO
  246. void serial2_write(const void *buf, unsigned int count)
  247. {
  248. const uint8_t *p = (const uint8_t *)buf;
  249. const uint8_t *end = p + count;
  250. uint32_t head, n;
  251. if (!(SIM_SCGC4 & SIM_SCGC4_UART1)) return;
  252. if (transmit_pin) transmit_assert();
  253. while (p < end) {
  254. head = tx_buffer_head;
  255. if (++head >= TX_BUFFER_SIZE) head = 0;
  256. if (tx_buffer_tail == head) {
  257. UART1_C2 = C2_TX_ACTIVE;
  258. do {
  259. int priority = nvic_execution_priority();
  260. if (priority <= IRQ_PRIORITY) {
  261. if ((UART1_S1 & UART_S1_TDRE)) {
  262. uint32_t tail = tx_buffer_tail;
  263. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  264. n = tx_buffer[tail];
  265. if (use9Bits) UART1_C3 = (UART1_C3 & ~0x40) | ((n & 0x100) >> 2);
  266. UART1_D = n;
  267. tx_buffer_tail = tail;
  268. }
  269. } else if (priority >= 256) {
  270. yield();
  271. }
  272. } while (tx_buffer_tail == head);
  273. }
  274. tx_buffer[head] = *p++;
  275. transmitting = 1;
  276. tx_buffer_head = head;
  277. }
  278. UART1_C2 = C2_TX_ACTIVE;
  279. }
  280. #else
  281. void serial2_write(const void *buf, unsigned int count)
  282. {
  283. const uint8_t *p = (const uint8_t *)buf;
  284. while (count-- > 0) serial2_putchar(*p++);
  285. }
  286. #endif
  287. void serial2_flush(void)
  288. {
  289. while (transmitting) yield(); // wait
  290. }
  291. int serial2_write_buffer_free(void)
  292. {
  293. uint32_t head, tail;
  294. head = tx_buffer_head;
  295. tail = tx_buffer_tail;
  296. if (head >= tail) return TX_BUFFER_SIZE - 1 - head + tail;
  297. return tail - head - 1;
  298. }
  299. int serial2_available(void)
  300. {
  301. uint32_t head, tail;
  302. head = rx_buffer_head;
  303. tail = rx_buffer_tail;
  304. if (head >= tail) return head - tail;
  305. return RX_BUFFER_SIZE + head - tail;
  306. }
  307. int serial2_getchar(void)
  308. {
  309. uint32_t head, tail;
  310. int c;
  311. head = rx_buffer_head;
  312. tail = rx_buffer_tail;
  313. if (head == tail) return -1;
  314. if (++tail >= RX_BUFFER_SIZE) tail = 0;
  315. c = rx_buffer[tail];
  316. rx_buffer_tail = tail;
  317. if (rts_pin) {
  318. int avail;
  319. if (head >= tail) avail = head - tail;
  320. else avail = RX_BUFFER_SIZE + head - tail;
  321. if (avail <= RTS_LOW_WATERMARK) rts_assert();
  322. }
  323. return c;
  324. }
  325. int serial2_peek(void)
  326. {
  327. uint32_t head, tail;
  328. head = rx_buffer_head;
  329. tail = rx_buffer_tail;
  330. if (head == tail) return -1;
  331. if (++tail >= RX_BUFFER_SIZE) tail = 0;
  332. return rx_buffer[tail];
  333. }
  334. void serial2_clear(void)
  335. {
  336. #ifdef HAS_KINETISK_UART1_FIFO
  337. if (!(SIM_SCGC4 & SIM_SCGC4_UART1)) return;
  338. UART1_C2 &= ~(UART_C2_RE | UART_C2_RIE | UART_C2_ILIE);
  339. UART1_CFIFO = UART_CFIFO_RXFLUSH;
  340. UART1_C2 |= (UART_C2_RE | UART_C2_RIE | UART_C2_ILIE);
  341. #endif
  342. rx_buffer_head = rx_buffer_tail;
  343. if (rts_pin) rts_assert();
  344. }
  345. // status interrupt combines
  346. // Transmit data below watermark UART_S1_TDRE
  347. // Transmit complete UART_S1_TC
  348. // Idle line UART_S1_IDLE
  349. // Receive data above watermark UART_S1_RDRF
  350. // LIN break detect UART_S2_LBKDIF
  351. // RxD pin active edge UART_S2_RXEDGIF
  352. void uart1_status_isr(void)
  353. {
  354. uint32_t head, tail, n;
  355. uint8_t c;
  356. #ifdef HAS_KINETISK_UART1_FIFO
  357. uint32_t newhead;
  358. uint8_t avail;
  359. if (UART1_S1 & (UART_S1_RDRF | UART_S1_IDLE)) {
  360. __disable_irq();
  361. avail = UART1_RCFIFO;
  362. if (avail == 0) {
  363. // The only way to clear the IDLE interrupt flag is
  364. // to read the data register. But reading with no
  365. // data causes a FIFO underrun, which causes the
  366. // FIFO to return corrupted data. If anyone from
  367. // Freescale reads this, what a poor design! There
  368. // write should be a write-1-to-clear for IDLE.
  369. c = UART1_D;
  370. // flushing the fifo recovers from the underrun,
  371. // but there's a possible race condition where a
  372. // new character could be received between reading
  373. // RCFIFO == 0 and flushing the FIFO. To minimize
  374. // the chance, interrupts are disabled so a higher
  375. // priority interrupt (hopefully) doesn't delay.
  376. // TODO: change this to disabling the IDLE interrupt
  377. // which won't be simple, since we already manage
  378. // which transmit interrupts are enabled.
  379. UART1_CFIFO = UART_CFIFO_RXFLUSH;
  380. __enable_irq();
  381. } else {
  382. __enable_irq();
  383. head = rx_buffer_head;
  384. tail = rx_buffer_tail;
  385. do {
  386. if (use9Bits && (UART1_C3 & 0x80)) {
  387. n = UART1_D | 0x100;
  388. } else {
  389. n = UART1_D;
  390. }
  391. newhead = head + 1;
  392. if (newhead >= RX_BUFFER_SIZE) newhead = 0;
  393. if (newhead != tail) {
  394. head = newhead;
  395. rx_buffer[head] = n;
  396. }
  397. } while (--avail > 0);
  398. rx_buffer_head = head;
  399. if (rts_pin) {
  400. int avail;
  401. if (head >= tail) avail = head - tail;
  402. else avail = RX_BUFFER_SIZE + head - tail;
  403. if (avail >= RTS_HIGH_WATERMARK) rts_deassert();
  404. }
  405. }
  406. }
  407. c = UART1_C2;
  408. if ((c & UART_C2_TIE) && (UART1_S1 & UART_S1_TDRE)) {
  409. head = tx_buffer_head;
  410. tail = tx_buffer_tail;
  411. do {
  412. if (tail == head) break;
  413. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  414. avail = UART1_S1;
  415. n = tx_buffer[tail];
  416. if (use9Bits) UART1_C3 = (UART1_C3 & ~0x40) | ((n & 0x100) >> 2);
  417. UART1_D = n;
  418. } while (UART1_TCFIFO < 8);
  419. tx_buffer_tail = tail;
  420. if (UART1_S1 & UART_S1_TDRE) UART1_C2 = C2_TX_COMPLETING;
  421. }
  422. #else
  423. if (UART1_S1 & UART_S1_RDRF) {
  424. n = UART1_D;
  425. if (use9Bits && (UART1_C3 & 0x80)) n |= 0x100;
  426. head = rx_buffer_head + 1;
  427. if (head >= RX_BUFFER_SIZE) head = 0;
  428. if (head != rx_buffer_tail) {
  429. rx_buffer[head] = n;
  430. rx_buffer_head = head;
  431. }
  432. }
  433. c = UART1_C2;
  434. if ((c & UART_C2_TIE) && (UART1_S1 & UART_S1_TDRE)) {
  435. head = tx_buffer_head;
  436. tail = tx_buffer_tail;
  437. if (head == tail) {
  438. UART1_C2 = C2_TX_COMPLETING;
  439. } else {
  440. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  441. n = tx_buffer[tail];
  442. if (use9Bits) UART1_C3 = (UART1_C3 & ~0x40) | ((n & 0x100) >> 2);
  443. UART1_D = n;
  444. tx_buffer_tail = tail;
  445. }
  446. }
  447. #endif
  448. if ((c & UART_C2_TCIE) && (UART1_S1 & UART_S1_TC)) {
  449. transmitting = 0;
  450. if (transmit_pin) transmit_deassert();
  451. UART1_C2 = C2_TX_INACTIVE;
  452. }
  453. }