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.

serial1.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 "mk20dx128.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 64 // number of outgoing bytes to buffer
  37. #define RX_BUFFER_SIZE 64 // number of incoming bytes to buffer
  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. #define C2_ENABLE UART_C2_TE | UART_C2_RE | UART_C2_RIE | UART_C2_ILIE
  70. #define C2_TX_ACTIVE C2_ENABLE | UART_C2_TIE
  71. #define C2_TX_COMPLETING C2_ENABLE | UART_C2_TCIE
  72. #define C2_TX_INACTIVE C2_ENABLE
  73. void serial_begin(uint32_t divisor)
  74. {
  75. SIM_SCGC4 |= SIM_SCGC4_UART0; // turn on clock, TODO: use bitband
  76. rx_buffer_head = 0;
  77. rx_buffer_tail = 0;
  78. tx_buffer_head = 0;
  79. tx_buffer_tail = 0;
  80. transmitting = 0;
  81. CORE_PIN0_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3);
  82. CORE_PIN1_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);
  83. UART0_BDH = (divisor >> 13) & 0x1F;
  84. UART0_BDL = (divisor >> 5) & 0xFF;
  85. UART0_C4 = divisor & 0x1F;
  86. //UART0_C1 = 0;
  87. UART0_C1 = UART_C1_ILT;
  88. UART0_TWFIFO = 2; // tx watermark, causes S1_TDRE to set
  89. UART0_RWFIFO = 4; // rx watermark, causes S1_RDRF to set
  90. UART0_PFIFO = UART_PFIFO_TXFE | UART_PFIFO_RXFE;
  91. UART0_C2 = C2_TX_INACTIVE;
  92. NVIC_SET_PRIORITY(IRQ_UART0_STATUS, IRQ_PRIORITY);
  93. NVIC_ENABLE_IRQ(IRQ_UART0_STATUS);
  94. }
  95. void serial_format(uint32_t format)
  96. {
  97. uint8_t c;
  98. c = UART0_C1;
  99. c = (c & ~0x13) | (format & 0x03); // configure parity
  100. if (format & 0x04) c |= 0x10; // 9 bits (might include parity)
  101. UART0_C1 = c;
  102. if ((format & 0x0F) == 0x04) UART0_C3 |= 0x40; // 8N2 is 9 bit with 9th bit always 1
  103. c = UART0_S2 & ~0x10;
  104. if (format & 0x10) c |= 0x10; // rx invert
  105. UART0_S2 = c;
  106. c = UART0_C3 & ~0x10;
  107. if (format & 0x20) c |= 0x10; // tx invert
  108. UART0_C3 = c;
  109. #ifdef SERIAL_9BIT_SUPPORT
  110. c = UART0_C4 & 0x1F;
  111. if (format & 0x08) c |= 0x20; // 9 bit mode with parity (requires 10 bits)
  112. UART0_C4 = c;
  113. use9Bits = format & 0x80;
  114. #endif
  115. }
  116. void serial_end(void)
  117. {
  118. if (!(SIM_SCGC4 & SIM_SCGC4_UART0)) return;
  119. while (transmitting) yield(); // wait for buffered data to send
  120. NVIC_DISABLE_IRQ(IRQ_UART0_STATUS);
  121. UART0_C2 = 0;
  122. CORE_PIN0_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
  123. CORE_PIN1_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
  124. rx_buffer_head = 0;
  125. rx_buffer_tail = 0;
  126. }
  127. void serial_set_transmit_pin(uint8_t pin)
  128. {
  129. while (transmitting) ;
  130. pinMode(pin, OUTPUT);
  131. digitalWrite(pin, LOW);
  132. transmit_pin = portOutputRegister(pin);
  133. }
  134. void serial_putchar(uint32_t c)
  135. {
  136. uint32_t head, n;
  137. if (!(SIM_SCGC4 & SIM_SCGC4_UART0)) return;
  138. if (transmit_pin) *transmit_pin = 1;
  139. head = tx_buffer_head;
  140. if (++head >= TX_BUFFER_SIZE) head = 0;
  141. while (tx_buffer_tail == head) {
  142. int priority = nvic_execution_priority();
  143. if (priority <= IRQ_PRIORITY) {
  144. if ((UART0_S1 & UART_S1_TDRE)) {
  145. uint32_t tail = tx_buffer_tail;
  146. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  147. n = tx_buffer[tail];
  148. if (use9Bits) UART0_C3 = (UART0_C3 & ~0x40) | ((n & 0x100) >> 2);
  149. UART0_D = n;
  150. tx_buffer_tail = tail;
  151. }
  152. } else if (priority >= 256) {
  153. yield();
  154. }
  155. }
  156. tx_buffer[head] = c;
  157. transmitting = 1;
  158. tx_buffer_head = head;
  159. UART0_C2 = C2_TX_ACTIVE;
  160. }
  161. void serial_write(const void *buf, unsigned int count)
  162. {
  163. const uint8_t *p = (const uint8_t *)buf;
  164. const uint8_t *end = p + count;
  165. uint32_t head, n;
  166. if (!(SIM_SCGC4 & SIM_SCGC4_UART0)) return;
  167. if (transmit_pin) *transmit_pin = 1;
  168. while (p < end) {
  169. head = tx_buffer_head;
  170. if (++head >= TX_BUFFER_SIZE) head = 0;
  171. if (tx_buffer_tail == head) {
  172. UART0_C2 = C2_TX_ACTIVE;
  173. do {
  174. int priority = nvic_execution_priority();
  175. if (priority <= IRQ_PRIORITY) {
  176. if ((UART0_S1 & UART_S1_TDRE)) {
  177. uint32_t tail = tx_buffer_tail;
  178. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  179. n = tx_buffer[tail];
  180. if (use9Bits) UART0_C3 = (UART0_C3 & ~0x40) | ((n & 0x100) >> 2);
  181. UART0_D = n;
  182. tx_buffer_tail = tail;
  183. }
  184. } else if (priority >= 256) {
  185. yield();
  186. }
  187. } while (tx_buffer_tail == head);
  188. }
  189. tx_buffer[head] = *p++;
  190. transmitting = 1;
  191. tx_buffer_head = head;
  192. }
  193. UART0_C2 = C2_TX_ACTIVE;
  194. }
  195. void serial_flush(void)
  196. {
  197. while (transmitting) yield(); // wait
  198. }
  199. int serial_available(void)
  200. {
  201. uint32_t head, tail;
  202. head = rx_buffer_head;
  203. tail = rx_buffer_tail;
  204. if (head >= tail) return head - tail;
  205. return RX_BUFFER_SIZE + head - tail;
  206. }
  207. int serial_getchar(void)
  208. {
  209. uint32_t head, tail;
  210. int c;
  211. head = rx_buffer_head;
  212. tail = rx_buffer_tail;
  213. if (head == tail) return -1;
  214. if (++tail >= RX_BUFFER_SIZE) tail = 0;
  215. c = rx_buffer[tail];
  216. rx_buffer_tail = tail;
  217. return c;
  218. }
  219. int serial_peek(void)
  220. {
  221. uint32_t head, tail;
  222. head = rx_buffer_head;
  223. tail = rx_buffer_tail;
  224. if (head == tail) return -1;
  225. if (++tail >= RX_BUFFER_SIZE) tail = 0;
  226. return rx_buffer[tail];
  227. }
  228. void serial_clear(void)
  229. {
  230. if (!(SIM_SCGC4 & SIM_SCGC4_UART0)) return;
  231. UART0_C2 &= ~(UART_C2_RE | UART_C2_RIE | UART_C2_ILIE);
  232. UART0_CFIFO = UART_CFIFO_RXFLUSH;
  233. UART0_C2 |= (UART_C2_RE | UART_C2_RIE | UART_C2_ILIE);
  234. rx_buffer_head = rx_buffer_tail;
  235. }
  236. // status interrupt combines
  237. // Transmit data below watermark UART_S1_TDRE
  238. // Transmit complete UART_S1_TC
  239. // Idle line UART_S1_IDLE
  240. // Receive data above watermark UART_S1_RDRF
  241. // LIN break detect UART_S2_LBKDIF
  242. // RxD pin active edge UART_S2_RXEDGIF
  243. void uart0_status_isr(void)
  244. {
  245. uint32_t head, newhead, tail, n;
  246. uint8_t avail, c;
  247. if (UART0_S1 & (UART_S1_RDRF | UART_S1_IDLE)) {
  248. __disable_irq();
  249. avail = UART0_RCFIFO;
  250. if (avail == 0) {
  251. // The only way to clear the IDLE interrupt flag is
  252. // to read the data register. But reading with no
  253. // data causes a FIFO underrun, which causes the
  254. // FIFO to return corrupted data. If anyone from
  255. // Freescale reads this, what a poor design! There
  256. // write should be a write-1-to-clear for IDLE.
  257. c = UART0_D;
  258. // flushing the fifo recovers from the underrun,
  259. // but there's a possible race condition where a
  260. // new character could be received between reading
  261. // RCFIFO == 0 and flushing the FIFO. To minimize
  262. // the chance, interrupts are disabled so a higher
  263. // priority interrupt (hopefully) doesn't delay.
  264. // TODO: change this to disabling the IDLE interrupt
  265. // which won't be simple, since we already manage
  266. // which transmit interrupts are enabled.
  267. UART0_CFIFO = UART_CFIFO_RXFLUSH;
  268. __enable_irq();
  269. } else {
  270. __enable_irq();
  271. head = rx_buffer_head;
  272. tail = rx_buffer_tail;
  273. do {
  274. n = UART0_D;
  275. if (use9Bits && (UART0_C3 & 0x80)) n |= 0x100;
  276. newhead = head + 1;
  277. if (newhead >= RX_BUFFER_SIZE) newhead = 0;
  278. if (newhead != tail) {
  279. head = newhead;
  280. rx_buffer[head] = n;
  281. }
  282. } while (--avail > 0);
  283. rx_buffer_head = head;
  284. }
  285. }
  286. c = UART0_C2;
  287. if ((c & UART_C2_TIE) && (UART0_S1 & UART_S1_TDRE)) {
  288. head = tx_buffer_head;
  289. tail = tx_buffer_tail;
  290. do {
  291. if (tail == head) break;
  292. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  293. avail = UART0_S1;
  294. n = tx_buffer[tail];
  295. if (use9Bits) UART0_C3 = (UART0_C3 & ~0x40) | ((n & 0x100) >> 2);
  296. UART0_D = n;
  297. } while (UART0_TCFIFO < 8);
  298. tx_buffer_tail = tail;
  299. if (UART0_S1 & UART_S1_TDRE) UART0_C2 = C2_TX_COMPLETING;
  300. }
  301. if ((c & UART_C2_TCIE) && (UART0_S1 & UART_S1_TC)) {
  302. transmitting = 0;
  303. if (transmit_pin) *transmit_pin = 0;
  304. UART0_C2 = C2_TX_INACTIVE;
  305. }
  306. }
  307. void serial_print(const char *p)
  308. {
  309. while (*p) {
  310. char c = *p++;
  311. if (c == '\n') serial_putchar('\r');
  312. serial_putchar(c);
  313. }
  314. }
  315. static void serial_phex1(uint32_t n)
  316. {
  317. n &= 15;
  318. if (n < 10) {
  319. serial_putchar('0' + n);
  320. } else {
  321. serial_putchar('A' - 10 + n);
  322. }
  323. }
  324. void serial_phex(uint32_t n)
  325. {
  326. serial_phex1(n >> 4);
  327. serial_phex1(n);
  328. }
  329. void serial_phex16(uint32_t n)
  330. {
  331. serial_phex(n >> 8);
  332. serial_phex(n);
  333. }
  334. void serial_phex32(uint32_t n)
  335. {
  336. serial_phex(n >> 24);
  337. serial_phex(n >> 16);
  338. serial_phex(n >> 8);
  339. serial_phex(n);
  340. }