PlatformIO package of the Teensy core framework compatible with GCC 10 & 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.

usb_seremu.c 9.4KB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2019 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 "usb_dev.h"
  31. #include "usb_seremu.h"
  32. #include "core_pins.h" // for yield()
  33. #include <string.h> // for memcpy()
  34. #include "avr/pgmspace.h" // for PROGMEM, DMAMEM, FASTRUN
  35. #include "debug/printf.h"
  36. #include "core_pins.h"
  37. #if defined(SEREMU_INTERFACE) && !defined(CDC_STATUS_INTERFACE) && !defined(CDC_DATA_INTERFACE)
  38. static volatile uint8_t tx_noautoflush=0;
  39. extern volatile uint8_t usb_high_speed;
  40. // TODO: should be 2 different timeouts, high speed (480) vs full speed (12)
  41. #define TRANSMIT_FLUSH_TIMEOUT 75 /* in microseconds */
  42. static void timer_config(void (*callback)(void), uint32_t microseconds);
  43. static void timer_start_oneshot();
  44. static void timer_stop();
  45. static void usb_seremu_flush_callback(void);
  46. #define TX_NUM 12
  47. static transfer_t tx_transfer[TX_NUM] __attribute__ ((used, aligned(32)));
  48. DMAMEM static uint8_t txbuffer[SEREMU_TX_SIZE * TX_NUM] __attribute__ ((aligned(32)));
  49. static uint8_t tx_head=0;
  50. static uint16_t tx_available=0;
  51. #define RX_NUM 8
  52. static transfer_t rx_transfer[RX_NUM] __attribute__ ((used, aligned(32)));
  53. DMAMEM static uint8_t rx_buffer[SEREMU_RX_SIZE * RX_NUM] __attribute__ ((aligned(32)));
  54. static uint16_t rx_index[RX_NUM];
  55. static volatile uint8_t rx_head;
  56. static volatile uint8_t rx_tail;
  57. static uint8_t rx_list[RX_NUM + 1];
  58. static volatile uint32_t rx_available;
  59. static void rx_queue_transfer(int i);
  60. static void rx_event(transfer_t *t);
  61. void usb_seremu_configure(void)
  62. {
  63. printf("usb_seremu_configure\n");
  64. memset(tx_transfer, 0, sizeof(tx_transfer));
  65. tx_head = 0;
  66. tx_available = 0;
  67. memset(rx_transfer, 0, sizeof(rx_transfer));
  68. memset(rx_index, 0, sizeof(rx_index));
  69. rx_head = 0;
  70. rx_tail = 0;
  71. rx_available = 0;
  72. usb_config_rx(SEREMU_RX_ENDPOINT, SEREMU_RX_SIZE, 0, rx_event); // SEREMU_RX_SIZE = 32
  73. usb_config_tx(SEREMU_TX_ENDPOINT, SEREMU_TX_SIZE, 0, NULL); // SEREMU_TX_SIZE = 64
  74. int i;
  75. for (i=0; i < RX_NUM; i++) rx_queue_transfer(i);
  76. timer_config(usb_seremu_flush_callback, TRANSMIT_FLUSH_TIMEOUT);
  77. }
  78. /*************************************************************************/
  79. /** Receive **/
  80. /*************************************************************************/
  81. static void rx_queue_transfer(int i)
  82. {
  83. NVIC_DISABLE_IRQ(IRQ_USB1);
  84. void *buffer = rx_buffer + i * SEREMU_RX_SIZE;
  85. usb_prepare_transfer(rx_transfer + i, buffer, SEREMU_RX_SIZE, i);
  86. arm_dcache_delete(buffer, SEREMU_RX_SIZE);
  87. usb_receive(SEREMU_RX_ENDPOINT, rx_transfer + i);
  88. NVIC_ENABLE_IRQ(IRQ_USB1);
  89. }
  90. // called by USB interrupt when any packet is received
  91. static void rx_event(transfer_t *t)
  92. {
  93. int len = SEREMU_RX_SIZE - ((t->status >> 16) & 0x7FFF);
  94. int i = t->callback_param;
  95. printf("rx event, len=%d, i=%d\n", len, i);
  96. if (len == SEREMU_RX_SIZE && rx_buffer[i * SEREMU_RX_SIZE] != 0) {
  97. // received a packet with data
  98. uint32_t head = rx_head;
  99. rx_index[i] = 0;
  100. if (++head > RX_NUM) head = 0;
  101. rx_list[head] = i;
  102. rx_head = head;
  103. rx_available += len;
  104. // TODO: trigger serialEvent
  105. } else {
  106. // received a short packet - should never happen with HID
  107. rx_queue_transfer(i);
  108. }
  109. }
  110. // get the next character, or -1 if nothing received
  111. int usb_seremu_getchar(void)
  112. {
  113. uint32_t tail = rx_tail;
  114. if (tail == rx_head) return -1;
  115. if (++tail > RX_NUM) tail = 0;
  116. uint32_t i = rx_list[tail];
  117. uint32_t index = rx_index[i];
  118. uint8_t *p = rx_buffer + i * SEREMU_RX_SIZE + index;
  119. int c = *p;
  120. if (++index >= SEREMU_RX_SIZE || *(p+1) == 0) {
  121. rx_tail = tail;
  122. rx_queue_transfer(i);
  123. } else {
  124. rx_index[i] = index;
  125. }
  126. return c;
  127. }
  128. // peek at the next character, or -1 if nothing received
  129. int usb_seremu_peekchar(void)
  130. {
  131. uint32_t tail = rx_tail;
  132. if (tail == rx_head) return -1;
  133. if (++tail > RX_NUM) tail = 0;
  134. uint32_t i = rx_list[tail];
  135. return rx_buffer[i * SEREMU_RX_SIZE + rx_index[i]];
  136. }
  137. // number of bytes available in the receive buffer
  138. int usb_seremu_available(void)
  139. {
  140. uint32_t tail = rx_tail;
  141. if (tail == rx_head) return 0;
  142. // TODO: how much is actually available?
  143. return 1;
  144. }
  145. // discard any buffered input
  146. void usb_seremu_flush_input(void)
  147. {
  148. uint32_t tail = rx_tail;
  149. while (tail != rx_head) {
  150. if (++tail > RX_NUM) tail = 0;
  151. uint32_t i = rx_list[tail];
  152. rx_queue_transfer(i);
  153. rx_tail = tail;
  154. }
  155. }
  156. /*************************************************************************/
  157. /** Transmit **/
  158. /*************************************************************************/
  159. // When the PC isn't listening, how long do we wait before discarding data? If this is
  160. // too short, we risk losing data during the stalls that are common with ordinary desktop
  161. // software. If it's too long, we stall the user's program when no software is running.
  162. #define TX_TIMEOUT_MSEC 50
  163. // When we've suffered the transmit timeout, don't wait again until the computer
  164. // begins accepting data. If no software is running to receive, we'll just discard
  165. // data as rapidly as Serial.print() can generate it, until there's something to
  166. // actually receive it.
  167. static uint8_t transmit_previous_timeout=0;
  168. // transmit a character. 0 returned on success, -1 on error
  169. int usb_seremu_putchar(uint8_t c)
  170. {
  171. return usb_seremu_write(&c, 1);
  172. }
  173. extern volatile uint32_t systick_millis_count;
  174. static void timer_config(void (*callback)(void), uint32_t microseconds);
  175. static void timer_start_oneshot();
  176. static void timer_stop();
  177. static void timer_config(void (*callback)(void), uint32_t microseconds)
  178. {
  179. usb_timer0_callback = callback;
  180. USB1_GPTIMER0CTRL = 0;
  181. USB1_GPTIMER0LD = microseconds - 1;
  182. USB1_USBINTR |= USB_USBINTR_TIE0;
  183. }
  184. static void timer_start_oneshot(void)
  185. {
  186. // restarts timer if already running (retriggerable one-shot)
  187. USB1_GPTIMER0CTRL = USB_GPTIMERCTRL_GPTRUN | USB_GPTIMERCTRL_GPTRST;
  188. }
  189. static void timer_stop(void)
  190. {
  191. USB1_GPTIMER0CTRL = 0;
  192. }
  193. void tx_zero_pad(void)
  194. {
  195. if (!tx_available) return;
  196. uint8_t *txdata = txbuffer + (tx_head * SEREMU_TX_SIZE) + (SEREMU_TX_SIZE - tx_available);
  197. memset(txdata, 0, tx_available);
  198. tx_available = 0;
  199. }
  200. void tx_queue_transfer(void)
  201. {
  202. transfer_t *xfer = tx_transfer + tx_head;
  203. uint8_t *txbuf = txbuffer + (tx_head * SEREMU_TX_SIZE);
  204. usb_prepare_transfer(xfer, txbuf, SEREMU_TX_SIZE, 0);
  205. arm_dcache_flush_delete(txbuf, SEREMU_TX_SIZE);
  206. usb_transmit(SEREMU_TX_ENDPOINT, xfer);
  207. if (++tx_head >= TX_NUM) tx_head = 0;
  208. }
  209. int usb_seremu_write(const void *buffer, uint32_t size)
  210. {
  211. uint32_t sent=0;
  212. const uint8_t *data = (const uint8_t *)buffer;
  213. if (!usb_configuration) return 0;
  214. while (size > 0) {
  215. transfer_t *xfer = tx_transfer + tx_head;
  216. int waiting=0;
  217. uint32_t wait_begin_at=0;
  218. while (!tx_available) {
  219. uint32_t status = usb_transfer_status(xfer);
  220. if (!(status & 0x80)) {
  221. if (status & 0x68) {
  222. // TODO: what if status has errors???
  223. printf("ERROR status = %x, i=%d, ms=%u\n",
  224. status, tx_head, systick_millis_count);
  225. }
  226. tx_available = SEREMU_TX_SIZE;
  227. transmit_previous_timeout = 0;
  228. break;
  229. }
  230. if (!waiting) {
  231. wait_begin_at = systick_millis_count;
  232. waiting = 1;
  233. }
  234. if (transmit_previous_timeout) return sent;
  235. if (systick_millis_count - wait_begin_at > TX_TIMEOUT_MSEC) {
  236. // waited too long, assume the USB host isn't listening
  237. transmit_previous_timeout = 1;
  238. return sent;
  239. }
  240. if (!usb_configuration) return sent;
  241. yield();
  242. }
  243. uint8_t *txdata = txbuffer + (tx_head * SEREMU_TX_SIZE) + (SEREMU_TX_SIZE - tx_available);
  244. if (size >= tx_available) {
  245. memcpy(txdata, data, tx_available);
  246. size -= tx_available;
  247. sent += tx_available;
  248. data += tx_available;
  249. tx_available = 0;
  250. tx_queue_transfer();
  251. timer_stop();
  252. } else {
  253. memcpy(txdata, data, size);
  254. tx_available -= size;
  255. sent += size;
  256. size = 0;
  257. timer_start_oneshot();
  258. }
  259. }
  260. return sent;
  261. }
  262. int usb_seremu_write_buffer_free(void)
  263. {
  264. return 1;
  265. }
  266. void usb_seremu_flush_output(void)
  267. {
  268. if (!usb_configuration) return;
  269. if (tx_available == 0) return;
  270. tx_noautoflush = 1;
  271. tx_zero_pad();
  272. tx_queue_transfer();
  273. tx_noautoflush = 0;
  274. }
  275. static void usb_seremu_flush_callback(void)
  276. {
  277. if (tx_noautoflush) return;
  278. tx_zero_pad();
  279. tx_queue_transfer();
  280. }
  281. #endif // SEREMU_INTERFACE