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.

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