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.

263 lines
6.8KB

  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. #if F_CPU >= 20000000
  31. //#include "mk20dx128.h"
  32. #include "usb_dev.h"
  33. #include "usb_seremu.h"
  34. #include "core_pins.h" // for yield()
  35. //#include "HardwareSerial.h"
  36. #ifdef SEREMU_INTERFACE // defined by usb_dev.h -> usb_desc.h
  37. volatile uint8_t usb_seremu_transmit_flush_timer=0;
  38. static usb_packet_t *rx_packet=NULL;
  39. static usb_packet_t *tx_packet=NULL;
  40. static volatile uint8_t tx_noautoflush=0;
  41. #define TRANSMIT_FLUSH_TIMEOUT 5 /* in milliseconds */
  42. // get the next character, or -1 if nothing received
  43. int usb_seremu_getchar(void)
  44. {
  45. unsigned int i;
  46. int c;
  47. while (1) {
  48. if (!usb_configuration) return -1;
  49. if (!rx_packet) rx_packet = usb_rx(SEREMU_RX_ENDPOINT);
  50. if (!rx_packet) return -1;
  51. i = rx_packet->index;
  52. c = rx_packet->buf[i++];
  53. if (c) {
  54. if (i >= rx_packet->len) {
  55. usb_free(rx_packet);
  56. rx_packet = NULL;
  57. } else {
  58. rx_packet->index = i;
  59. }
  60. return c;
  61. }
  62. usb_free(rx_packet);
  63. rx_packet = NULL;
  64. }
  65. }
  66. // peek at the next character, or -1 if nothing received
  67. int usb_seremu_peekchar(void)
  68. {
  69. int c;
  70. while (1) {
  71. if (!usb_configuration) return -1;
  72. if (!rx_packet) rx_packet = usb_rx(SEREMU_RX_ENDPOINT);
  73. if (!rx_packet) return -1;
  74. c = rx_packet->buf[rx_packet->index];
  75. if (c) return c;
  76. usb_free(rx_packet);
  77. rx_packet = NULL;
  78. }
  79. }
  80. // number of bytes available in the receive buffer
  81. int usb_seremu_available(void)
  82. {
  83. int i, len, count;
  84. if (!rx_packet) {
  85. if (usb_configuration) rx_packet = usb_rx(SEREMU_RX_ENDPOINT);
  86. if (!rx_packet) return 0;
  87. }
  88. len = rx_packet->len;
  89. i = rx_packet->index;
  90. count = 0;
  91. for (i = rx_packet->index; i < len; i++) {
  92. if (rx_packet->buf[i] == 0) break;
  93. count++;
  94. }
  95. if (count == 0) {
  96. usb_free(rx_packet);
  97. rx_packet = NULL;
  98. }
  99. return count;
  100. }
  101. // discard any buffered input
  102. void usb_seremu_flush_input(void)
  103. {
  104. usb_packet_t *rx;
  105. if (!usb_configuration) return;
  106. if (rx_packet) {
  107. usb_free(rx_packet);
  108. rx_packet = NULL;
  109. }
  110. while (1) {
  111. rx = usb_rx(SEREMU_RX_ENDPOINT);
  112. if (!rx) break;
  113. usb_free(rx);
  114. }
  115. }
  116. // Maximum number of transmit packets to queue so we don't starve other endpoints for memory
  117. #define TX_PACKET_LIMIT 6
  118. // When the PC isn't listening, how long do we wait before discarding data? If this is
  119. // too short, we risk losing data during the stalls that are common with ordinary desktop
  120. // software. If it's too long, we stall the user's program when no software is running.
  121. #define TX_TIMEOUT_MSEC 30
  122. #if F_CPU == 168000000
  123. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1100)
  124. #elif F_CPU == 144000000
  125. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 932)
  126. #elif F_CPU == 120000000
  127. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 764)
  128. #elif F_CPU == 96000000
  129. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 596)
  130. #elif F_CPU == 72000000
  131. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 512)
  132. #elif F_CPU == 48000000
  133. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 428)
  134. #elif F_CPU == 24000000
  135. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 262)
  136. #endif
  137. // When we've suffered the transmit timeout, don't wait again until the computer
  138. // begins accepting data. If no software is running to receive, we'll just discard
  139. // data as rapidly as Serial.print() can generate it, until there's something to
  140. // actually receive it.
  141. static uint8_t transmit_previous_timeout=0;
  142. // transmit a character. 0 returned on success, -1 on error
  143. int usb_seremu_putchar(uint8_t c)
  144. {
  145. return usb_seremu_write(&c, 1);
  146. }
  147. int usb_seremu_write(const void *buffer, uint32_t size)
  148. {
  149. #if 1
  150. uint32_t len;
  151. uint32_t wait_count;
  152. const uint8_t *src = (const uint8_t *)buffer;
  153. uint8_t *dest;
  154. tx_noautoflush = 1;
  155. while (size > 0) {
  156. if (!tx_packet) {
  157. wait_count = 0;
  158. while (1) {
  159. if (!usb_configuration) {
  160. tx_noautoflush = 0;
  161. return -1;
  162. }
  163. if (usb_tx_packet_count(SEREMU_TX_ENDPOINT) < TX_PACKET_LIMIT) {
  164. tx_noautoflush = 1;
  165. tx_packet = usb_malloc();
  166. if (tx_packet) break;
  167. }
  168. if (++wait_count > TX_TIMEOUT || transmit_previous_timeout) {
  169. transmit_previous_timeout = 1;
  170. tx_noautoflush = 0;
  171. return -1;
  172. }
  173. tx_noautoflush = 0;
  174. yield();
  175. tx_noautoflush = 1;
  176. }
  177. }
  178. transmit_previous_timeout = 0;
  179. len = SEREMU_TX_SIZE - tx_packet->index;
  180. if (len > size) len = size;
  181. dest = tx_packet->buf + tx_packet->index;
  182. tx_packet->index += len;
  183. size -= len;
  184. while (len-- > 0) *dest++ = *src++;
  185. if (tx_packet->index < SEREMU_TX_SIZE) {
  186. usb_seremu_transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
  187. } else {
  188. tx_packet->len = SEREMU_TX_SIZE;
  189. usb_seremu_transmit_flush_timer = 0;
  190. usb_tx(SEREMU_TX_ENDPOINT, tx_packet);
  191. tx_packet = NULL;
  192. }
  193. }
  194. tx_noautoflush = 0;
  195. return 0;
  196. #endif
  197. }
  198. void usb_seremu_flush_output(void)
  199. {
  200. int i;
  201. if (!usb_configuration) return;
  202. //serial_print("usb_serial_flush_output\n");
  203. if (tx_packet && tx_packet->index > 0) {
  204. usb_seremu_transmit_flush_timer = 0;
  205. for (i = tx_packet->index; i < SEREMU_TX_SIZE; i++) {
  206. tx_packet->buf[i] = 0;
  207. }
  208. tx_packet->len = SEREMU_TX_SIZE;
  209. usb_tx(SEREMU_TX_ENDPOINT, tx_packet);
  210. tx_packet = NULL;
  211. }
  212. // while (usb_tx_byte_count(SEREMU_TX_ENDPOINT) > 0) ; // wait
  213. }
  214. void usb_seremu_flush_callback(void)
  215. {
  216. int i;
  217. //serial_print("C");
  218. if (tx_noautoflush) return;
  219. //serial_print("usb_flush_callback \n");
  220. for (i = tx_packet->index; i < SEREMU_TX_SIZE; i++) {
  221. tx_packet->buf[i] = 0;
  222. }
  223. tx_packet->len = SEREMU_TX_SIZE;
  224. usb_tx(SEREMU_TX_ENDPOINT, tx_packet);
  225. tx_packet = NULL;
  226. //serial_print("usb_flush_callback end\n");
  227. }
  228. #endif // SEREMU_INTERFACE
  229. #endif // F_CPU >= 20 MHz