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.

400 lines
11KB

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