Merge branch 'master' of ssh://git.gag.com/scm/git/fw/altos
[fw/altos] / src / stm32f4 / ao_usb_gen.c
1 /*
2  * Copyright © 2018 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include "ao_usb_gen.h"
20
21 static uint8_t  ao_usb_ep0_state;
22
23 /* Pending EP0 IN data */
24 static const uint8_t    *ao_usb_ep0_in_data;    /* Remaining data */
25 static uint8_t          ao_usb_ep0_in_len;      /* Remaining amount */
26
27 /* Temp buffer for smaller EP0 in data */
28 static uint8_t  ao_usb_ep0_in_buf[2];
29
30 /* Pending EP0 OUT data */
31 static uint8_t *ao_usb_ep0_out_data;
32 static uint8_t  ao_usb_ep0_out_len;
33
34 /* System ram shadow of USB buffer; writing individual bytes is
35  * too much of a pain (sigh) */
36 static uint8_t  ao_usb_tx_buffer[AO_USB_IN_SIZE];
37 static uint8_t  ao_usb_tx_count;
38
39 static uint8_t  ao_usb_rx_buffer[AO_USB_OUT_SIZE];
40 static uint8_t  ao_usb_rx_count, ao_usb_rx_pos;
41
42 /*
43  * End point register indices
44  */
45
46 #define AO_USB_CONTROL_EPR      0
47 #define AO_USB_INT_EPR          1
48 #define AO_USB_OUT_EPR          2
49 #define AO_USB_IN_EPR           3
50
51 /* Marks when we don't need to send an IN packet.
52  * This happens only when the last IN packet is not full,
53  * otherwise the host will expect to keep seeing packets.
54  * Send a zero-length packet as required
55  */
56 static uint8_t  ao_usb_in_flushed;
57
58 /* Marks when we have delivered an IN packet to the hardware
59  * and it has not been received yet. ao_sleep on this address
60  * to wait for it to be delivered.
61  */
62 static uint8_t  ao_usb_in_pending;
63
64 /* Marks when an OUT packet has been received by the hardware
65  * but not pulled to the shadow buffer.
66  */
67 static uint8_t  ao_usb_out_avail;
68 uint8_t         ao_usb_running;
69 static uint8_t  ao_usb_configuration;
70
71 static uint8_t  ao_usb_address;
72 static uint8_t  ao_usb_address_pending;
73
74 /*
75  * Set current device address and mark the
76  * interface as active
77  */
78 static void
79 ao_usb_set_address(uint8_t address)
80 {
81         ao_usb_dev_set_address(address);
82         ao_usb_address_pending = 0;
83 }
84
85 #define TX_DBG 0
86 #define RX_DBG 0
87
88 #if TX_DBG
89 #define _tx_dbg0(msg) _dbg(__LINE__,msg,0)
90 #define _tx_dbg1(msg,value) _dbg(__LINE__,msg,value)
91 #else
92 #define _tx_dbg0(msg)
93 #define _tx_dbg1(msg,value)
94 #endif
95
96 #if RX_DBG
97 #define _rx_dbg0(msg) _dbg(__LINE__,msg,0)
98 #define _rx_dbg1(msg,value) _dbg(__LINE__,msg,value)
99 #else
100 #define _rx_dbg0(msg)
101 #define _rx_dbg1(msg,value)
102 #endif
103
104 #if TX_DBG || RX_DBG
105 static void _dbg(int line, char *msg, uint32_t value);
106 #endif
107
108 /*
109  * Set just endpoint 0, for use during startup
110  */
111
112 static void
113 ao_usb_set_ep0(void)
114 {
115         ao_usb_dev_ep0_init();
116
117         ao_usb_set_address(0);
118
119         ao_usb_running = 0;
120
121         /* Reset our internal state
122          */
123
124         ao_usb_ep0_state = AO_USB_EP0_IDLE;
125
126         ao_usb_ep0_in_data = NULL;
127         ao_usb_ep0_in_len = 0;
128
129         ao_usb_ep0_out_data = 0;
130         ao_usb_ep0_out_len = 0;
131 }
132
133 static void
134 ao_usb_set_configuration(void)
135 {
136 #if 0
137         /* Set up the INT end point */
138         ao_usb_bdt[AO_USB_INT_EPR].single.addr_tx = ao_usb_sram_addr;
139         ao_usb_bdt[AO_USB_INT_EPR].single.count_tx = 0;
140         ao_usb_in_tx_buffer = ao_usb_packet_buffer_addr(ao_usb_sram_addr);
141         ao_usb_sram_addr += AO_USB_INT_SIZE;
142
143         ao_usb_init_ep(AO_USB_INT_EPR,
144                        AO_USB_INT_EP);
145
146         /* Set up the OUT end point */
147         ao_usb_bdt[AO_USB_OUT_EPR].single.addr_rx = ao_usb_sram_addr;
148         ao_usb_bdt[AO_USB_OUT_EPR].single.count_rx = ((1 << STM_USB_BDT_COUNT_RX_BL_SIZE) |
149                                                       (((AO_USB_OUT_SIZE / 32) - 1) << STM_USB_BDT_COUNT_RX_NUM_BLOCK));
150         ao_usb_out_rx_buffer = ao_usb_packet_buffer_addr(ao_usb_sram_addr);
151         ao_usb_sram_addr += AO_USB_OUT_SIZE;
152
153         ao_usb_init_ep(AO_USB_OUT_EPR,
154                        AO_USB_OUT_EP,
155                        STM_USB_EPR_EP_TYPE_BULK,
156                        STM_USB_EPR_STAT_RX_VALID,
157                        STM_USB_EPR_STAT_TX_DISABLED);
158
159         /* Set up the IN end point */
160         ao_usb_bdt[AO_USB_IN_EPR].single.addr_tx = ao_usb_sram_addr;
161         ao_usb_bdt[AO_USB_IN_EPR].single.count_tx = 0;
162         ao_usb_in_tx_buffer = ao_usb_packet_buffer_addr(ao_usb_sram_addr);
163         ao_usb_sram_addr += AO_USB_IN_SIZE;
164
165         ao_usb_init_ep(AO_USB_IN_EPR,
166                        AO_USB_IN_EP,
167                        STM_USB_EPR_EP_TYPE_BULK,
168                        STM_USB_EPR_STAT_RX_DISABLED,
169                        STM_USB_EPR_STAT_TX_NAK);
170 #endif
171
172         ao_usb_in_flushed = 0;
173         ao_usb_in_pending = 0;
174         ao_wakeup(&ao_usb_in_pending);
175
176         ao_usb_out_avail = 0;
177         ao_usb_configuration = 0;
178
179         ao_usb_running = 1;
180         ao_wakeup(&ao_usb_running);
181 }
182
183 static uint16_t control_count;
184 static uint16_t in_count;
185 static uint16_t out_count;
186 #if USB_DEBUG
187 static uint16_t int_count;
188 static uint16_t reset_count;
189 #endif
190
191 /* Send an IN data packet */
192 static void
193 ao_usb_ep0_flush(void)
194 {
195         uint8_t this_len;
196
197         /* Check to see if the endpoint is still busy */
198         if (ao_usb_dev_ep0_in_busy()) {
199                 return;
200         }
201
202         this_len = ao_usb_ep0_in_len;
203         if (this_len > AO_USB_CONTROL_SIZE)
204                 this_len = AO_USB_CONTROL_SIZE;
205
206         if (this_len < AO_USB_CONTROL_SIZE)
207                 ao_usb_ep0_state = AO_USB_EP0_IDLE;
208
209         ao_usb_ep0_in_len -= this_len;
210
211         ao_usb_dev_ep0_in(ao_usb_ep0_in_data, this_len);
212         ao_usb_ep0_in_data += this_len;
213 }
214
215 /* Read data from the ep0 OUT fifo */
216 static void
217 ao_usb_ep0_fill(void)
218 {
219         uint16_t        len;
220
221         len = ao_usb_dev_ep0_out(ao_usb_ep0_out_data, ao_usb_ep0_out_len);
222         ao_usb_ep0_out_len -= len;
223         ao_usb_ep0_out_data += len;
224 }
225
226 static void
227 ao_usb_ep0_in_reset(void)
228 {
229         ao_usb_ep0_in_data = ao_usb_ep0_in_buf;
230         ao_usb_ep0_in_len = 0;
231 }
232
233 static void
234 ao_usb_ep0_in_queue_byte(uint8_t a)
235 {
236         if (ao_usb_ep0_in_len < sizeof (ao_usb_ep0_in_buf))
237                 ao_usb_ep0_in_buf[ao_usb_ep0_in_len++] = a;
238 }
239
240 static void
241 ao_usb_ep0_in_set(const uint8_t *data, uint8_t len)
242 {
243         ao_usb_ep0_in_data = data;
244         ao_usb_ep0_in_len = len;
245 }
246
247 static void
248 ao_usb_ep0_out_set(uint8_t *data, uint8_t len)
249 {
250         ao_usb_ep0_out_data = data;
251         ao_usb_ep0_out_len = len;
252 }
253
254 static void
255 ao_usb_ep0_in_start(uint16_t max)
256 {
257         /* Don't send more than asked for */
258         if (ao_usb_ep0_in_len > max)
259                 ao_usb_ep0_in_len = max;
260
261         ao_usb_dev_ep0_in(ao_usb_ep0_in_data, ao_usb_ep0_in_len);
262 }
263
264 struct ao_usb_line_coding ao_usb_line_coding = {115200, 0, 0, 8};
265
266 /* Walk through the list of descriptors and find a match
267  */
268 static void
269 ao_usb_get_descriptor(uint16_t value, uint16_t length)
270 {
271         const uint8_t           *descriptor;
272         uint8_t         type = value >> 8;
273         uint8_t         index = value;
274
275         descriptor = ao_usb_descriptors;
276         while (descriptor[0] != 0) {
277                 if (descriptor[1] == type && index-- == 0) {
278                         uint8_t len;
279                         if (type == AO_USB_DESC_CONFIGURATION)
280                                 len = descriptor[2];
281                         else
282                                 len = descriptor[0];
283                         if (len > length)
284                                 len = length;
285                         ao_usb_ep0_in_set(descriptor, len);
286                         break;
287                 }
288                 descriptor += descriptor[0];
289         }
290 }
291
292 static void
293 ao_usb_ep0_setup(void)
294 {
295         uint16_t        setup_len;
296
297         /* Pull the setup packet out of the fifo */
298         setup_len = ao_usb_dev_ep0_out(&ao_usb_setup, 8);
299         if (setup_len != 8) {
300                 return;
301         }
302
303         if ((ao_usb_setup.dir_type_recip & AO_USB_DIR_IN) || ao_usb_setup.length == 0)
304                 ao_usb_ep0_state = AO_USB_EP0_DATA_IN;
305         else
306                 ao_usb_ep0_state = AO_USB_EP0_DATA_OUT;
307
308         ao_usb_ep0_in_reset();
309
310         switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_TYPE_MASK) {
311         case AO_USB_TYPE_STANDARD:
312                 switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_RECIP_MASK) {
313                 case AO_USB_RECIP_DEVICE:
314                         switch(ao_usb_setup.request) {
315                         case AO_USB_REQ_GET_STATUS:
316                                 ao_usb_ep0_in_queue_byte(0);
317                                 ao_usb_ep0_in_queue_byte(0);
318                                 break;
319                         case AO_USB_REQ_SET_ADDRESS:
320                                 ao_usb_address = ao_usb_setup.value;
321                                 ao_usb_address_pending = 1;
322                                 break;
323                         case AO_USB_REQ_GET_DESCRIPTOR:
324                                 ao_usb_get_descriptor(ao_usb_setup.value, ao_usb_setup.length);
325                                 break;
326                         case AO_USB_REQ_GET_CONFIGURATION:
327                                 ao_usb_ep0_in_queue_byte(ao_usb_configuration);
328                                 break;
329                         case AO_USB_REQ_SET_CONFIGURATION:
330                                 ao_usb_configuration = ao_usb_setup.value;
331                                 ao_usb_set_configuration();
332                                 break;
333                         }
334                         break;
335                 case AO_USB_RECIP_INTERFACE:
336                         switch(ao_usb_setup.request) {
337                         case AO_USB_REQ_GET_STATUS:
338                                 ao_usb_ep0_in_queue_byte(0);
339                                 ao_usb_ep0_in_queue_byte(0);
340                                 break;
341                         case AO_USB_REQ_GET_INTERFACE:
342                                 ao_usb_ep0_in_queue_byte(0);
343                                 break;
344                         case AO_USB_REQ_SET_INTERFACE:
345                                 break;
346                         }
347                         break;
348                 case AO_USB_RECIP_ENDPOINT:
349                         switch(ao_usb_setup.request) {
350                         case AO_USB_REQ_GET_STATUS:
351                                 ao_usb_ep0_in_queue_byte(0);
352                                 ao_usb_ep0_in_queue_byte(0);
353                                 break;
354                         }
355                         break;
356                 }
357                 break;
358         case AO_USB_TYPE_CLASS:
359                 switch (ao_usb_setup.request) {
360                 case AO_USB_SET_LINE_CODING:
361                         ao_usb_ep0_out_set((uint8_t *) &ao_usb_line_coding, 7);
362                         break;
363                 case AO_USB_GET_LINE_CODING:
364                         ao_usb_ep0_in_set((const uint8_t *) &ao_usb_line_coding, 7);
365                         break;
366                 case AO_USB_SET_CONTROL_LINE_STATE:
367                         break;
368                 }
369                 break;
370         }
371
372         /* If we're not waiting to receive data from the host,
373          * queue an IN response
374          */
375         if (ao_usb_ep0_state == AO_USB_EP0_DATA_IN)
376                 ao_usb_ep0_in_start(ao_usb_setup.length);
377 }
378
379 static void
380 ao_usb_ep0_handle(uint8_t receive)
381 {
382         if (receive & AO_USB_EP0_GOT_RESET) {
383                 ao_usb_set_ep0();
384                 return;
385         }
386         if (receive & AO_USB_EP0_GOT_SETUP) {
387                 ao_usb_ep0_setup();
388         }
389         if (receive & AO_USB_EP0_GOT_RX_DATA) {
390                 if (ao_usb_ep0_state == AO_USB_EP0_DATA_OUT) {
391                         ao_usb_ep0_fill();
392                         if (ao_usb_ep0_out_len == 0) {
393                                 ao_usb_ep0_state = AO_USB_EP0_DATA_IN;
394                                 ao_usb_ep0_in_start(0);
395                         }
396                 }
397         }
398         if (receive & AO_USB_EP0_GOT_TX_ACK) {
399 #if HAS_FLIGHT && AO_USB_FORCE_IDLE
400                 ao_flight_force_idle = 1;
401 #endif
402                 /* Wait until the IN packet is received from addr 0
403                  * before assigning our local address
404                  */
405                 if (ao_usb_address_pending)
406                         ao_usb_set_address(ao_usb_address);
407                 if (ao_usb_ep0_state == AO_USB_EP0_DATA_IN)
408                         ao_usb_ep0_flush();
409         }
410 }
411
412 void
413 ao_usb_ep0_interrupt(uint8_t mask)
414 {
415         if (mask) {
416                 ++control_count;
417                 ao_usb_ep0_handle(mask);
418         }
419 }
420
421 void
422 ao_usb_in_interrupt(uint32_t mask)
423 {
424         if (mask & (1 << AO_USB_IN_EPR)) {
425                 ++in_count;
426                 _tx_dbg1("TX ISR", epr);
427                 ao_usb_in_pending = 0;
428                 ao_wakeup(&ao_usb_in_pending);
429         }
430 }
431
432 void
433 ao_usb_out_interrupt(uint32_t mask)
434 {
435         if (mask & (1 << AO_USB_OUT_EPR)) {
436                 ++out_count;
437                 _rx_dbg1("RX ISR", epr);
438                 ao_usb_out_avail = 1;
439                 _rx_dbg0("out avail set");
440                 ao_wakeup(AO_USB_OUT_SLEEP_ADDR);
441                 _rx_dbg0("stdin awoken");
442         }
443 }
444
445 void
446 ao_usb_int_interrupt(uint32_t mask)
447 {
448         (void) mask;
449 }
450
451 void
452 stm_usb_fs_wkup(void)
453 {
454         /* USB wakeup, just clear the bit for now */
455 //      stm_usb.istr &= ~(1 << STM_USB_ISTR_WKUP);
456 }
457
458 /* Queue the current IN buffer for transmission */
459 static void
460 _ao_usb_in_send(void)
461 {
462         _tx_dbg0("in_send start");
463
464         while (ao_usb_in_pending)
465                 ao_sleep(&ao_usb_in_pending);
466
467         ao_usb_in_pending = 1;
468         if (ao_usb_tx_count != AO_USB_IN_SIZE)
469                 ao_usb_in_flushed = 1;
470
471         ao_usb_dev_ep_in(AO_USB_IN_EPR, ao_usb_tx_buffer, ao_usb_tx_count);
472         ao_usb_tx_count = 0;
473
474         _tx_dbg0("in_send end");
475 }
476
477 /* Wait for a free IN buffer. Interrupts are blocked */
478 static void
479 _ao_usb_in_wait(void)
480 {
481         for (;;) {
482                 /* Check if the current buffer is writable */
483                 if (ao_usb_tx_count < AO_USB_IN_SIZE)
484                         break;
485
486                 _tx_dbg0("in_wait top");
487                 /* Wait for an IN buffer to be ready */
488                 while (ao_usb_in_pending)
489                         ao_sleep(&ao_usb_in_pending);
490                 _tx_dbg0("in_wait bottom");
491         }
492 }
493
494 void
495 ao_usb_flush(void)
496 {
497         if (!ao_usb_running)
498                 return;
499
500         /* Anytime we've sent a character since
501          * the last time we flushed, we'll need
502          * to send a packet -- the only other time
503          * we would send a packet is when that
504          * packet was full, in which case we now
505          * want to send an empty packet
506          */
507         ao_arch_block_interrupts();
508         while (!ao_usb_in_flushed) {
509                 _tx_dbg0("flush top");
510                 _ao_usb_in_send();
511                 _tx_dbg0("flush end");
512         }
513         ao_arch_release_interrupts();
514 }
515
516 void
517 ao_usb_putchar(char c)
518 {
519         if (!ao_usb_running)
520                 return;
521
522         ao_arch_block_interrupts();
523         _ao_usb_in_wait();
524
525         ao_usb_in_flushed = 0;
526         ao_usb_tx_buffer[ao_usb_tx_count++] = (uint8_t) c;
527
528         /* Send the packet when full */
529         if (ao_usb_tx_count == AO_USB_IN_SIZE) {
530                 _tx_dbg0("putchar full");
531                 _ao_usb_in_send();
532                 _tx_dbg0("putchar flushed");
533         }
534         ao_arch_release_interrupts();
535 }
536
537 static void
538 _ao_usb_out_recv(void)
539 {
540         _rx_dbg0("out_recv top");
541         ao_usb_out_avail = 0;
542
543         ao_usb_rx_count = ao_usb_dev_ep_out(AO_USB_OUT_EPR, ao_usb_rx_buffer, sizeof (ao_usb_rx_buffer));
544
545         _rx_dbg1("out_recv count", ao_usb_rx_count);
546
547         ao_usb_rx_pos = 0;
548 }
549
550 int
551 _ao_usb_pollchar(void)
552 {
553         uint8_t c;
554
555         if (!ao_usb_running)
556                 return AO_READ_AGAIN;
557
558         for (;;) {
559                 if (ao_usb_rx_pos != ao_usb_rx_count)
560                         break;
561
562                 _rx_dbg0("poll check");
563                 /* Check to see if a packet has arrived */
564                 if (!ao_usb_out_avail) {
565                         _rx_dbg0("poll none");
566                         return AO_READ_AGAIN;
567                 }
568                 _ao_usb_out_recv();
569         }
570
571         /* Pull a character out of the fifo */
572         c = ao_usb_rx_buffer[ao_usb_rx_pos++];
573         return c;
574 }
575
576 char
577 ao_usb_getchar(void)
578 {
579         int     c;
580
581         ao_arch_block_interrupts();
582         while ((c = _ao_usb_pollchar()) == AO_READ_AGAIN)
583                 ao_sleep(AO_USB_OUT_SLEEP_ADDR);
584         ao_arch_release_interrupts();
585         return c;
586 }
587
588 #ifndef HAS_USB_DISABLE
589 #define HAS_USB_DISABLE 1
590 #endif
591
592 #if HAS_USB_DISABLE
593 void
594 ao_usb_disable(void)
595 {
596         ao_usb_dev_disable();
597 }
598 #endif
599
600 void
601 ao_usb_enable(void)
602 {
603         ao_usb_dev_enable();
604
605         ao_usb_configuration = 0;
606 }
607
608 #if USB_ECHO
609 struct ao_task ao_usb_echo_task;
610
611 static void
612 ao_usb_echo(void)
613 {
614         char    c;
615
616         for (;;) {
617                 c = ao_usb_getchar();
618                 ao_usb_putchar(c);
619                 ao_usb_flush();
620         }
621 }
622 #endif
623
624 #if USB_DEBUG
625 static void
626 ao_usb_irq(void)
627 {
628         printf ("control: %d out: %d in: %d int: %d reset: %d\n",
629                 control_count, out_count, in_count, int_count, reset_count);
630 }
631
632 const struct ao_cmds ao_usb_cmds[] = {
633         { ao_usb_irq, "I\0Show USB interrupt counts" },
634         { 0, NULL }
635 };
636 #endif
637
638 void
639 ao_usb_init(void)
640 {
641         ao_usb_enable();
642
643         ao_usb_ep0_state = AO_USB_EP0_IDLE;
644 #if USB_ECHO
645         ao_add_task(&ao_usb_echo_task, ao_usb_echo, "usb echo");
646 #endif
647 #if USB_DEBUG
648         ao_cmd_register(&ao_usb_cmds[0]);
649 #endif
650 #if !USB_ECHO
651 #if USE_USB_STDIO
652         ao_add_stdio(_ao_usb_pollchar, ao_usb_putchar, ao_usb_flush);
653 #endif
654 #endif
655 }
656
657 #if TX_DBG || RX_DBG
658
659 struct ao_usb_dbg {
660         int             line;
661         char            *msg;
662         uint32_t        value;
663         uint32_t        prival;
664 #if TX_DBG
665         uint16_t        in_count;
666         uint32_t        in_epr;
667         uint32_t        in_pending;
668         uint32_t        tx_count;
669         uint32_t        in_flushed;
670 #endif
671 #if RX_DBG
672         uint8_t         rx_count;
673         uint8_t         rx_pos;
674         uint8_t         out_avail;
675         uint32_t        out_epr;
676 #endif
677 };
678
679 #define NUM_USB_DBG     16
680
681 static struct ao_usb_dbg dbg[NUM_USB_DBG];
682 static int dbg_i;
683
684 static void _dbg(int line, char *msg, uint32_t value)
685 {
686         uint32_t        prival;
687         dbg[dbg_i].line = line;
688         dbg[dbg_i].msg = msg;
689         dbg[dbg_i].value = value;
690 #if AO_NONMASK_INTERRUPT
691         asm("mrs %0,basepri" : "=&r" (prival));
692 #else
693         asm("mrs %0,primask" : "=&r" (prival));
694 #endif
695         dbg[dbg_i].prival = prival;
696 #if TX_DBG
697         dbg[dbg_i].in_count = in_count;
698         dbg[dbg_i].in_epr = stm_usb.epr[AO_USB_IN_EPR];
699         dbg[dbg_i].in_pending = ao_usb_in_pending;
700         dbg[dbg_i].tx_count = ao_usb_tx_count;
701         dbg[dbg_i].in_flushed = ao_usb_in_flushed;
702 #endif
703 #if RX_DBG
704         dbg[dbg_i].rx_count = ao_usb_rx_count;
705         dbg[dbg_i].rx_pos = ao_usb_rx_pos;
706         dbg[dbg_i].out_avail = ao_usb_out_avail;
707         dbg[dbg_i].out_epr = stm_usb.epr[AO_USB_OUT_EPR];
708 #endif
709         if (++dbg_i == NUM_USB_DBG)
710                 dbg_i = 0;
711 }
712 #endif