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.

325 lines
9.6KB

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