17e8709cf959095d1d089fd1a79fd909f86d2b80
[fw/altos] / src / stmf0 / ao_usb_stm.c
1 /*
2  * Copyright © 2012 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; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include "ao.h"
19 #include "ao_usb.h"
20 #include "ao_product.h"
21 #include "ao_power.h"
22
23 #define USB_DEBUG       0
24 #define USB_STATUS      0
25 #define USB_DEBUG_DATA  0
26 #define USB_ECHO        0
27
28 #ifndef AO_PA11_PA12_RMP
29 #error "must define AO_PA11_PA12_RMP"
30 #endif
31
32 #ifndef AO_POWER_MANAGEMENT
33 #define AO_POWER_MANAGEMENT     0
34 #endif
35
36 #ifndef USE_USB_STDIO
37 #define USE_USB_STDIO   1
38 #endif
39
40 #if USE_USB_STDIO
41 #define AO_USB_OUT_SLEEP_ADDR   (&ao_stdin_ready)
42 #else
43 #define AO_USB_OUT_SLEEP_ADDR   (&ao_usb_out_avail)
44 #endif
45
46 #if USB_DEBUG
47 #define debug(format, args...)  printf(format, ## args);
48 #else
49 #define debug(format, args...)
50 #endif
51
52 #if USB_DEBUG_DATA
53 #define debug_data(format, args...)     printf(format, ## args);
54 #else
55 #define debug_data(format, args...)
56 #endif
57
58 struct ao_usb_setup {
59         uint8_t         dir_type_recip;
60         uint8_t         request;
61         uint16_t        value;
62         uint16_t        index;
63         uint16_t        length;
64 } ao_usb_setup;
65
66 static uint8_t  ao_usb_ep0_state;
67
68 /* Pending EP0 IN data */
69 static const uint8_t    *ao_usb_ep0_in_data;    /* Remaining data */
70 static uint8_t          ao_usb_ep0_in_len;      /* Remaining amount */
71
72 /* Temp buffer for smaller EP0 in data */
73 static uint8_t  ao_usb_ep0_in_buf[2];
74
75 /* Pending EP0 OUT data */
76 static uint8_t *ao_usb_ep0_out_data;
77 static uint8_t  ao_usb_ep0_out_len;
78
79 /*
80  * Objects allocated in special USB memory
81  */
82
83 /* Buffer description tables */
84 static union stm_usb_bdt        *ao_usb_bdt;
85 /* USB address of end of allocated storage */
86 static uint16_t ao_usb_sram_addr;
87
88 /* Pointer to ep0 tx/rx buffers in USB memory */
89 static uint16_t *ao_usb_ep0_tx_buffer;
90 static uint16_t *ao_usb_ep0_rx_buffer;
91
92 /* Pointer to interrupt buffer in USB memory */
93 static uint16_t ao_usb_int_tx_offset;
94
95 /* Pointer to bulk data tx/rx buffers in USB memory */
96 static uint16_t ao_usb_in_tx_offset;
97 static uint16_t *ao_usb_in_tx_buffer;
98 static uint16_t ao_usb_out_rx_offset;
99 static uint16_t *ao_usb_out_rx_buffer;
100
101 /* System ram shadow of USB buffer; writing individual bytes is
102  * too much of a pain (sigh) */
103 static uint8_t  ao_usb_tx_buffer[AO_USB_IN_SIZE];
104 static uint8_t  ao_usb_tx_count;
105
106 static uint8_t  ao_usb_rx_buffer[AO_USB_OUT_SIZE];
107 static uint8_t  ao_usb_rx_count, ao_usb_rx_pos;
108
109 /*
110  * End point register indices
111  */
112
113 #define AO_USB_CONTROL_EPR      0
114 #define AO_USB_INT_EPR          1
115 #define AO_USB_OUT_EPR          2
116 #define AO_USB_IN_EPR           3
117
118 /* Marks when we don't need to send an IN packet.
119  * This happens only when the last IN packet is not full,
120  * otherwise the host will expect to keep seeing packets.
121  * Send a zero-length packet as required
122  */
123 static uint8_t  ao_usb_in_flushed;
124
125 /* Marks when we have delivered an IN packet to the hardware
126  * and it has not been received yet. ao_sleep on this address
127  * to wait for it to be delivered.
128  */
129 static uint8_t  ao_usb_in_pending;
130
131 /* Marks when an OUT packet has been received by the hardware
132  * but not pulled to the shadow buffer.
133  */
134 static uint8_t  ao_usb_out_avail;
135 uint8_t         ao_usb_running;
136 static uint8_t  ao_usb_configuration;
137
138 #define AO_USB_EP0_GOT_SETUP    1
139 #define AO_USB_EP0_GOT_RX_DATA  2
140 #define AO_USB_EP0_GOT_TX_ACK   4
141
142 static uint8_t  ao_usb_ep0_receive;
143 static uint8_t  ao_usb_address;
144 static uint8_t  ao_usb_address_pending;
145
146 static inline uint32_t set_toggle(uint32_t      current_value,
147                                    uint32_t     mask,
148                                    uint32_t     desired_value)
149 {
150         return (current_value ^ desired_value) & mask;
151 }
152
153 static inline uint16_t *ao_usb_packet_buffer_addr(uint16_t sram_addr)
154 {
155         return (uint16_t *) (stm_usb_sram + sram_addr);
156 }
157
158 static inline uint16_t ao_usb_packet_buffer_offset(uint16_t *addr)
159 {
160         return (uint16_t) ((uint8_t *) addr - stm_usb_sram);
161 }
162
163 static inline uint32_t ao_usb_epr_stat_rx(uint32_t epr) {
164         return (epr >> STM_USB_EPR_STAT_RX) & STM_USB_EPR_STAT_RX_MASK;
165 }
166
167 static inline uint32_t ao_usb_epr_stat_tx(uint32_t epr) {
168         return (epr >> STM_USB_EPR_STAT_TX) & STM_USB_EPR_STAT_TX_MASK;
169 }
170
171 static inline uint32_t ao_usb_epr_ctr_rx(uint32_t epr) {
172         return (epr >> STM_USB_EPR_CTR_RX) & 1;
173 }
174
175 static inline uint32_t ao_usb_epr_ctr_tx(uint32_t epr) {
176         return (epr >> STM_USB_EPR_CTR_TX) & 1;
177 }
178
179 static inline uint32_t ao_usb_epr_setup(uint32_t epr) {
180         return (epr >> STM_USB_EPR_SETUP) & 1;
181 }
182
183 static inline uint32_t ao_usb_epr_dtog_rx(uint32_t epr) {
184         return (epr >> STM_USB_EPR_DTOG_RX) & 1;
185 }
186
187 static inline uint32_t ao_usb_epr_dtog_tx(uint32_t epr) {
188         return (epr >> STM_USB_EPR_DTOG_TX) & 1;
189 }
190
191 /*
192  * Set current device address and mark the
193  * interface as active
194  */
195 void
196 ao_usb_set_address(uint8_t address)
197 {
198         debug("ao_usb_set_address %02x\n", address);
199         stm_usb.daddr = (1 << STM_USB_DADDR_EF) | address;
200         ao_usb_address_pending = 0;
201 }
202
203 /*
204  * Write these values to preserve register contents under HW changes
205  */
206
207 #define STM_USB_EPR_INVARIANT   ((1 << STM_USB_EPR_CTR_RX) |            \
208                                  (STM_USB_EPR_DTOG_RX_WRITE_INVARIANT << STM_USB_EPR_DTOG_RX) | \
209                                  (STM_USB_EPR_STAT_RX_WRITE_INVARIANT << STM_USB_EPR_STAT_RX) | \
210                                  (1 << STM_USB_EPR_CTR_TX) |            \
211                                  (STM_USB_EPR_DTOG_TX_WRITE_INVARIANT << STM_USB_EPR_DTOG_TX) | \
212                                  (STM_USB_EPR_STAT_TX_WRITE_INVARIANT << STM_USB_EPR_STAT_TX))
213
214 #define STM_USB_EPR_INVARIANT_MASK      ((1 << STM_USB_EPR_CTR_RX) |    \
215                                          (STM_USB_EPR_DTOG_RX_MASK << STM_USB_EPR_DTOG_RX) | \
216                                          (STM_USB_EPR_STAT_RX_MASK << STM_USB_EPR_STAT_RX) | \
217                                          (1 << STM_USB_EPR_CTR_TX) |    \
218                                          (STM_USB_EPR_DTOG_TX_MASK << STM_USB_EPR_DTOG_TX) | \
219                                          (STM_USB_EPR_STAT_TX_MASK << STM_USB_EPR_STAT_TX))
220
221 /*
222  * These bits are purely under sw control, so preserve them in the
223  * register by re-writing what was read
224  */
225 #define STM_USB_EPR_PRESERVE_MASK       ((STM_USB_EPR_EP_TYPE_MASK << STM_USB_EPR_EP_TYPE) | \
226                                          (1 << STM_USB_EPR_EP_KIND) |   \
227                                          (STM_USB_EPR_EA_MASK << STM_USB_EPR_EA))
228
229 #define TX_DBG 0
230 #define RX_DBG 0
231
232 #if TX_DBG
233 #define _tx_dbg0(msg) _dbg(__LINE__,msg,0)
234 #define _tx_dbg1(msg,value) _dbg(__LINE__,msg,value)
235 #else
236 #define _tx_dbg0(msg)
237 #define _tx_dbg1(msg,value)
238 #endif
239
240 #if RX_DBG
241 #define _rx_dbg0(msg) _dbg(__LINE__,msg,0)
242 #define _rx_dbg1(msg,value) _dbg(__LINE__,msg,value)
243 #else
244 #define _rx_dbg0(msg)
245 #define _rx_dbg1(msg,value)
246 #endif
247
248 #if TX_DBG || RX_DBG
249 static void _dbg(int line, char *msg, uint32_t value);
250 #endif
251
252 /*
253  * Set the state of the specified endpoint register to a new
254  * value. This is tricky because the bits toggle where the new
255  * value is one, and we need to write invariant values in other
256  * spots of the register. This hardware is strange...
257  */
258 static void
259 _ao_usb_set_stat_tx(int ep, uint32_t stat_tx)
260 {
261         uint16_t        epr_write, epr_old;
262
263         _tx_dbg1("set_stat_tx top", stat_tx);
264         epr_old = epr_write = stm_usb.epr[ep].r;
265         epr_write &= STM_USB_EPR_PRESERVE_MASK;
266         epr_write |= STM_USB_EPR_INVARIANT;
267         epr_write |= set_toggle(epr_old,
268                               STM_USB_EPR_STAT_TX_MASK << STM_USB_EPR_STAT_TX,
269                               stat_tx << STM_USB_EPR_STAT_TX);
270         stm_usb.epr[ep].r = epr_write;
271         _tx_dbg1("set_stat_tx bottom", epr_write);
272 }
273
274 static void
275 ao_usb_set_stat_tx(int ep, uint32_t stat_tx)
276 {
277         ao_arch_block_interrupts();
278         _ao_usb_set_stat_tx(ep, stat_tx);
279         ao_arch_release_interrupts();
280 }
281
282 static void
283 _ao_usb_set_stat_rx(int ep, uint32_t stat_rx) {
284         uint16_t        epr_write, epr_old;
285
286         epr_write = epr_old = stm_usb.epr[ep].r;
287         epr_write &= STM_USB_EPR_PRESERVE_MASK;
288         epr_write |= STM_USB_EPR_INVARIANT;
289         epr_write |= set_toggle(epr_old,
290                               STM_USB_EPR_STAT_RX_MASK << STM_USB_EPR_STAT_RX,
291                               stat_rx << STM_USB_EPR_STAT_RX);
292         stm_usb.epr[ep].r = epr_write;
293 }
294
295 static void
296 ao_usb_set_stat_rx(int ep, uint32_t stat_rx) {
297         ao_arch_block_interrupts();
298         _ao_usb_set_stat_rx(ep, stat_rx);
299         ao_arch_release_interrupts();
300 }
301
302 /*
303  * Set just endpoint 0, for use during startup
304  */
305
306 static void
307 ao_usb_init_ep(uint8_t ep, uint32_t addr, uint32_t type, uint32_t stat_rx, uint32_t stat_tx)
308 {
309         uint16_t                epr;
310
311         ao_arch_block_interrupts();
312         epr = stm_usb.epr[ep].r;
313         epr = ((0 << STM_USB_EPR_CTR_RX) |
314                (epr & (1 << STM_USB_EPR_DTOG_RX)) |
315                set_toggle(epr,
316                           (STM_USB_EPR_STAT_RX_MASK << STM_USB_EPR_STAT_RX),
317                           (stat_rx << STM_USB_EPR_STAT_RX)) |
318                (type << STM_USB_EPR_EP_TYPE) |
319                (0 << STM_USB_EPR_EP_KIND) |
320                (0 << STM_USB_EPR_CTR_TX) |
321                (epr & (1 << STM_USB_EPR_DTOG_TX)) |
322                set_toggle(epr,
323                           (STM_USB_EPR_STAT_TX_MASK << STM_USB_EPR_STAT_TX),
324                           (stat_tx << STM_USB_EPR_STAT_TX)) |
325                (addr << STM_USB_EPR_EA));
326         stm_usb.epr[ep].r = epr;
327         ao_arch_release_interrupts();
328         debug ("writing epr[%d] 0x%04x wrote 0x%04x\n",
329                ep, epr, stm_usb.epr[ep].r);
330 }
331
332 static void
333 ao_usb_alloc_buffers(void)
334 {
335         ao_usb_sram_addr = 0;
336
337         ao_usb_bdt = (void *) stm_usb_sram;
338         ao_usb_sram_addr += 8 * STM_USB_BDT_SIZE;
339
340         ao_usb_ep0_tx_buffer = ao_usb_packet_buffer_addr(ao_usb_sram_addr);
341         ao_usb_sram_addr += AO_USB_CONTROL_SIZE;
342
343         ao_usb_ep0_rx_buffer = ao_usb_packet_buffer_addr(ao_usb_sram_addr);
344         ao_usb_sram_addr += AO_USB_CONTROL_SIZE;
345
346         ao_usb_int_tx_offset = ao_usb_sram_addr;
347         ao_usb_sram_addr += AO_USB_INT_SIZE;
348
349         ao_usb_out_rx_buffer = ao_usb_packet_buffer_addr(ao_usb_sram_addr);
350         ao_usb_out_rx_offset = ao_usb_sram_addr;
351         ao_usb_sram_addr += AO_USB_OUT_SIZE;
352
353         ao_usb_in_tx_buffer = ao_usb_packet_buffer_addr(ao_usb_sram_addr);
354         ao_usb_in_tx_offset = ao_usb_sram_addr;
355         ao_usb_sram_addr += AO_USB_IN_SIZE;
356 }
357
358 static void
359 ao_usb_init_btable(void)
360 {
361         /* Set up EP 0 - a Control end point with 32 bytes of in and out buffers */
362
363         ao_usb_bdt[0].single.addr_tx = ao_usb_packet_buffer_offset(ao_usb_ep0_tx_buffer);
364         ao_usb_bdt[0].single.count_tx = 0;
365
366         ao_usb_bdt[0].single.addr_rx = ao_usb_packet_buffer_offset(ao_usb_ep0_rx_buffer);
367         ao_usb_bdt[0].single.count_rx = ((1 << STM_USB_BDT_COUNT_RX_BL_SIZE) |
368                                   (((AO_USB_CONTROL_SIZE / 32) - 1) << STM_USB_BDT_COUNT_RX_NUM_BLOCK));
369 }
370
371 static void
372 ao_usb_set_ep0(void)
373 {
374         int                     e;
375
376         ao_usb_init_btable();
377
378         /* buffer table is at the start of USB memory */
379         stm_usb.btable = 0;
380
381         ao_usb_init_ep(AO_USB_CONTROL_EPR, AO_USB_CONTROL_EP,
382                        STM_USB_EPR_EP_TYPE_CONTROL,
383                        STM_USB_EPR_STAT_RX_VALID,
384                        STM_USB_EPR_STAT_TX_NAK);
385
386         /* Clear all of the other endpoints */
387         for (e = 1; e < 8; e++) {
388                 ao_usb_init_ep(e, 0,
389                                STM_USB_EPR_EP_TYPE_CONTROL,
390                                STM_USB_EPR_STAT_RX_DISABLED,
391                                STM_USB_EPR_STAT_TX_DISABLED);
392         }
393
394         ao_usb_set_address(0);
395
396         ao_usb_running = 0;
397 }
398
399 static void
400 ao_usb_set_configuration(void)
401 {
402         debug ("ao_usb_set_configuration\n");
403
404 #if AO_USB_HAS_INT
405         /* Set up the INT end point */
406         ao_usb_bdt[AO_USB_INT_EPR].single.addr_tx = ao_usb_int_tx_offset;
407         ao_usb_bdt[AO_USB_INT_EPR].single.count_tx = 0;
408
409         ao_usb_init_ep(AO_USB_INT_EPR,
410                        AO_USB_INT_EP,
411                        STM_USB_EPR_EP_TYPE_INTERRUPT,
412                        STM_USB_EPR_STAT_RX_DISABLED,
413                        STM_USB_EPR_STAT_TX_NAK);
414 #endif
415
416 #if AO_USB_HAS_OUT
417         /* Set up the OUT end point */
418         ao_usb_bdt[AO_USB_OUT_EPR].single.addr_rx = ao_usb_out_rx_offset;
419         ao_usb_bdt[AO_USB_OUT_EPR].single.count_rx = ((1 << STM_USB_BDT_COUNT_RX_BL_SIZE) |
420                                                       (((AO_USB_OUT_SIZE / 32) - 1) << STM_USB_BDT_COUNT_RX_NUM_BLOCK));
421
422         ao_usb_init_ep(AO_USB_OUT_EPR,
423                        AO_USB_OUT_EP,
424                        STM_USB_EPR_EP_TYPE_BULK,
425                        STM_USB_EPR_STAT_RX_VALID,
426                        STM_USB_EPR_STAT_TX_DISABLED);
427 #endif
428
429 #if AO_USB_HAS_IN
430         /* Set up the IN end point */
431         ao_usb_bdt[AO_USB_IN_EPR].single.addr_tx = ao_usb_in_tx_offset;
432         ao_usb_bdt[AO_USB_IN_EPR].single.count_tx = 0;
433
434         ao_usb_init_ep(AO_USB_IN_EPR,
435                        AO_USB_IN_EP,
436                        STM_USB_EPR_EP_TYPE_BULK,
437                        STM_USB_EPR_STAT_RX_DISABLED,
438                        STM_USB_EPR_STAT_TX_NAK);
439 #endif
440
441         ao_usb_running = 1;
442 #if AO_USB_DIRECTIO
443         ao_wakeup(&ao_usb_running);
444 #endif
445 }
446
447 static uint16_t control_count;
448 static uint16_t int_count;
449 static uint16_t in_count;
450 static uint16_t out_count;
451 static uint16_t reset_count;
452
453 /* The USB memory must be accessed in 16-bit units
454  */
455
456 static void
457 ao_usb_copy_tx(const uint8_t *src, uint16_t *base, uint16_t bytes)
458 {
459         while (bytes >= 2) {
460                 *base++ = src[0] | (src[1] << 8);
461                 src += 2;
462                 bytes -= 2;
463         }
464         if (bytes)
465                 *base = *src;
466 }
467
468 static void
469 ao_usb_copy_rx(uint8_t *dst, uint16_t *base, uint16_t bytes)
470 {
471         while (bytes >= 2) {
472                 uint16_t s = *base++;
473                 dst[0] = s;
474                 dst[1] = s >> 8;
475                 dst += 2;
476                 bytes -= 2;
477         }
478         if (bytes)
479                 *dst = *base;
480 }
481
482 /* Send an IN data packet */
483 static void
484 ao_usb_ep0_flush(void)
485 {
486         uint8_t this_len;
487
488         /* Check to see if the endpoint is still busy */
489         if (ao_usb_epr_stat_tx(stm_usb.epr[0].r) == STM_USB_EPR_STAT_TX_VALID) {
490                 debug("EP0 not accepting IN data\n");
491                 return;
492         }
493
494         this_len = ao_usb_ep0_in_len;
495         if (this_len > AO_USB_CONTROL_SIZE)
496                 this_len = AO_USB_CONTROL_SIZE;
497
498         if (this_len < AO_USB_CONTROL_SIZE)
499                 ao_usb_ep0_state = AO_USB_EP0_IDLE;
500
501         ao_usb_ep0_in_len -= this_len;
502
503         debug_data ("Flush EP0 len %d:", this_len);
504         ao_usb_copy_tx(ao_usb_ep0_in_data, ao_usb_ep0_tx_buffer, this_len);
505         debug_data ("\n");
506         ao_usb_ep0_in_data += this_len;
507
508         /* Mark the endpoint as TX valid to send the packet */
509         ao_usb_bdt[AO_USB_CONTROL_EPR].single.count_tx = this_len;
510         ao_usb_set_stat_tx(AO_USB_CONTROL_EPR, STM_USB_EPR_STAT_TX_VALID);
511         debug ("queue tx. epr 0 now %08x\n", stm_usb.epr[AO_USB_CONTROL_EPR]);
512 }
513
514 /* Read data from the ep0 OUT fifo */
515 static void
516 ao_usb_ep0_fill(void)
517 {
518         uint16_t        len = ao_usb_bdt[0].single.count_rx & STM_USB_BDT_COUNT_RX_COUNT_RX_MASK;
519
520         if (len > ao_usb_ep0_out_len)
521                 len = ao_usb_ep0_out_len;
522         ao_usb_ep0_out_len -= len;
523
524         /* Pull all of the data out of the packet */
525         debug_data ("Fill EP0 len %d:", len);
526         ao_usb_copy_rx(ao_usb_ep0_out_data, ao_usb_ep0_rx_buffer, len);
527         debug_data ("\n");
528         ao_usb_ep0_out_data += len;
529
530         /* ACK the packet */
531         ao_usb_set_stat_rx(0, STM_USB_EPR_STAT_RX_VALID);
532 }
533
534 static void
535 ao_usb_ep0_in_reset(void)
536 {
537         ao_usb_ep0_in_data = ao_usb_ep0_in_buf;
538         ao_usb_ep0_in_len = 0;
539 }
540
541 static void
542 ao_usb_ep0_in_queue_byte(uint8_t a)
543 {
544         if (ao_usb_ep0_in_len < sizeof (ao_usb_ep0_in_buf))
545                 ao_usb_ep0_in_buf[ao_usb_ep0_in_len++] = a;
546 }
547
548 static void
549 ao_usb_ep0_in_set(const uint8_t *data, uint8_t len)
550 {
551         ao_usb_ep0_in_data = data;
552         ao_usb_ep0_in_len = len;
553 }
554
555 static void
556 ao_usb_ep0_out_set(uint8_t *data, uint8_t len)
557 {
558         ao_usb_ep0_out_data = data;
559         ao_usb_ep0_out_len = len;
560 }
561
562 static void
563 ao_usb_ep0_in_start(uint16_t max)
564 {
565         /* Don't send more than asked for */
566         if (ao_usb_ep0_in_len > max)
567                 ao_usb_ep0_in_len = max;
568         ao_usb_ep0_flush();
569 }
570
571 static struct ao_usb_line_coding ao_usb_line_coding = {115200, 0, 0, 8};
572
573 #if AO_USB_DEVICE_ID_SERIAL
574 static uint8_t ao_usb_serial[2 + 48];
575
576 /* Convert a 32-bit value to 8 hexidecimal UCS2 characters */
577 static void
578 hex_to_ucs2(uint32_t in, uint8_t *out)
579 {
580         int     i;
581
582         for (i = 28; i >= 0; i -= 4) {
583                 uint8_t bits = (in >> i) & 0xf;
584                 *out++ = (bits < 10) ? ('0' + bits) : ('a' + bits);
585                 *out++ = 0;
586         }
587 }
588
589 /* Encode the device ID (96 bits) in hexidecimal to use as a device
590  * serial number
591  */
592 static void
593 ao_usb_serial_init(void)
594 {
595         ao_usb_serial[0] = 50;  /* length */
596         ao_usb_serial[1] = AO_USB_DESC_STRING;
597         hex_to_ucs2(stm_device_id.u_id0, ao_usb_serial + 2 + 0);
598         hex_to_ucs2(stm_device_id.u_id1, ao_usb_serial + 2 + 16);
599         hex_to_ucs2(stm_device_id.u_id2, ao_usb_serial + 2 + 32);
600 }
601 #endif
602
603 /* Walk through the list of descriptors and find a match
604  */
605 static void
606 ao_usb_get_descriptor(uint16_t value)
607 {
608         const uint8_t           *descriptor;
609         uint8_t         type = value >> 8;
610         uint8_t         index = value;
611
612         descriptor = ao_usb_descriptors;
613         while (descriptor[0] != 0) {
614                 if (descriptor[1] == type && index-- == 0) {
615                         uint8_t len;
616                         if (type == AO_USB_DESC_CONFIGURATION)
617                                 len = descriptor[2];
618                         else
619                                 len = descriptor[0];
620 #if AO_USB_DEVICE_ID_SERIAL
621                         /* Slightly hacky - the serial number is string 3 */
622                         if (type == AO_USB_DESC_STRING && (value & 0xff) == 3) {
623                                 descriptor = ao_usb_serial;
624                                 len = sizeof (ao_usb_serial);
625                         }
626 #endif
627                         ao_usb_ep0_in_set(descriptor, len);
628                         break;
629                 }
630                 descriptor += descriptor[0];
631         }
632 }
633
634 static void
635 ao_usb_ep0_setup(void)
636 {
637         /* Pull the setup packet out of the fifo */
638         ao_usb_ep0_out_set((uint8_t *) &ao_usb_setup, 8);
639         ao_usb_ep0_fill();
640         if (ao_usb_ep0_out_len != 0) {
641                 debug ("invalid setup packet length\n");
642                 return;
643         }
644
645         if ((ao_usb_setup.dir_type_recip & AO_USB_DIR_IN) || ao_usb_setup.length == 0)
646                 ao_usb_ep0_state = AO_USB_EP0_DATA_IN;
647         else
648                 ao_usb_ep0_state = AO_USB_EP0_DATA_OUT;
649
650         ao_usb_ep0_in_reset();
651
652         switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_TYPE_MASK) {
653         case AO_USB_TYPE_STANDARD:
654                 debug ("Standard setup packet\n");
655                 switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_RECIP_MASK) {
656                 case AO_USB_RECIP_DEVICE:
657                         debug ("Device setup packet\n");
658                         switch(ao_usb_setup.request) {
659                         case AO_USB_REQ_GET_STATUS:
660                                 debug ("get status\n");
661                                 ao_usb_ep0_in_queue_byte(0);
662                                 ao_usb_ep0_in_queue_byte(0);
663                                 break;
664                         case AO_USB_REQ_SET_ADDRESS:
665                                 debug ("set address %d\n", ao_usb_setup.value);
666                                 ao_usb_address = ao_usb_setup.value;
667                                 ao_usb_address_pending = 1;
668                                 break;
669                         case AO_USB_REQ_GET_DESCRIPTOR:
670                                 debug ("get descriptor %d\n", ao_usb_setup.value);
671                                 ao_usb_get_descriptor(ao_usb_setup.value);
672                                 break;
673                         case AO_USB_REQ_GET_CONFIGURATION:
674                                 debug ("get configuration %d\n", ao_usb_configuration);
675                                 ao_usb_ep0_in_queue_byte(ao_usb_configuration);
676                                 break;
677                         case AO_USB_REQ_SET_CONFIGURATION:
678                                 ao_usb_configuration = ao_usb_setup.value;
679                                 debug ("set configuration %d\n", ao_usb_configuration);
680                                 ao_usb_set_configuration();
681                                 break;
682                         }
683                         break;
684                 case AO_USB_RECIP_INTERFACE:
685                         debug ("Interface setup packet\n");
686                         switch(ao_usb_setup.request) {
687                         case AO_USB_REQ_GET_STATUS:
688                                 ao_usb_ep0_in_queue_byte(0);
689                                 ao_usb_ep0_in_queue_byte(0);
690                                 break;
691                         case AO_USB_REQ_GET_INTERFACE:
692                                 ao_usb_ep0_in_queue_byte(0);
693                                 break;
694                         case AO_USB_REQ_SET_INTERFACE:
695                                 break;
696                         }
697                         break;
698                 case AO_USB_RECIP_ENDPOINT:
699                         debug ("Endpoint setup packet\n");
700                         switch(ao_usb_setup.request) {
701                         case AO_USB_REQ_GET_STATUS:
702                                 ao_usb_ep0_in_queue_byte(0);
703                                 ao_usb_ep0_in_queue_byte(0);
704                                 break;
705                         }
706                         break;
707                 }
708                 break;
709         case AO_USB_TYPE_CLASS:
710                 debug ("Class setup packet\n");
711                 switch (ao_usb_setup.request) {
712                 case AO_USB_SET_LINE_CODING:
713                         debug ("set line coding\n");
714                         ao_usb_ep0_out_set((uint8_t *) &ao_usb_line_coding, 7);
715                         break;
716                 case AO_USB_GET_LINE_CODING:
717                         debug ("get line coding\n");
718                         ao_usb_ep0_in_set((const uint8_t *) &ao_usb_line_coding, 7);
719                         break;
720                 case AO_USB_SET_CONTROL_LINE_STATE:
721                         break;
722                 }
723                 break;
724         }
725
726         /* If we're not waiting to receive data from the host,
727          * queue an IN response
728          */
729         if (ao_usb_ep0_state == AO_USB_EP0_DATA_IN)
730                 ao_usb_ep0_in_start(ao_usb_setup.length);
731 }
732
733 static void
734 ao_usb_ep0_handle(uint8_t receive)
735 {
736         ao_usb_ep0_receive = 0;
737         if (receive & AO_USB_EP0_GOT_SETUP) {
738                 debug ("\tsetup\n");
739                 ao_usb_ep0_setup();
740         }
741         if (receive & AO_USB_EP0_GOT_RX_DATA) {
742                 debug ("\tgot rx data\n");
743                 if (ao_usb_ep0_state == AO_USB_EP0_DATA_OUT) {
744                         ao_usb_ep0_fill();
745                         if (ao_usb_ep0_out_len == 0) {
746                                 ao_usb_ep0_state = AO_USB_EP0_DATA_IN;
747                                 ao_usb_ep0_in_start(0);
748                         }
749                 }
750         }
751         if (receive & AO_USB_EP0_GOT_TX_ACK) {
752                 debug ("\tgot tx ack\n");
753
754 #if HAS_FLIGHT && AO_USB_FORCE_IDLE
755                 ao_flight_force_idle = 1;
756 #endif
757                 /* Wait until the IN packet is received from addr 0
758                  * before assigning our local address
759                  */
760                 if (ao_usb_address_pending)
761                         ao_usb_set_address(ao_usb_address);
762                 if (ao_usb_ep0_state == AO_USB_EP0_DATA_IN)
763                         ao_usb_ep0_flush();
764         }
765 }
766
767 #if AO_POWER_MANAGEMENT
768 void
769 ao_usb_suspend(void)
770 {
771         stm_usb.cntr |= (1 << STM_USB_CNTR_FSUSP);
772         ao_power_suspend();
773         stm_usb.cntr |= (1 << STM_USB_CNTR_LP_MODE);
774         ao_clock_suspend();
775 }
776
777 void
778 ao_usb_wakeup(void)
779 {
780         ao_clock_resume();
781         stm_usb.cntr &= ~(1 << STM_USB_CNTR_FSUSP);
782         ao_power_resume();
783 }
784 #endif
785
786 void
787 stm_usb_isr(void)
788 {
789         uint32_t        istr = stm_usb.istr;
790
791         stm_usb.istr = ~istr;
792         if (istr & (1 << STM_USB_ISTR_CTR)) {
793                 uint8_t         ep = istr & STM_USB_ISTR_EP_ID_MASK;
794                 uint16_t        epr, epr_write;
795
796                 /* Preserve the SW write bits, don't mess with most HW writable bits,
797                  * clear the CTR_RX and CTR_TX bits
798                  */
799                 epr = stm_usb.epr[ep].r;
800                 epr_write = epr;
801                 epr_write &= STM_USB_EPR_PRESERVE_MASK;
802                 epr_write |= STM_USB_EPR_INVARIANT;
803                 epr_write &= ~(1 << STM_USB_EPR_CTR_RX);
804                 epr_write &= ~(1 << STM_USB_EPR_CTR_TX);
805                 stm_usb.epr[ep].r = epr_write;
806
807                 switch (ep) {
808                 case 0:
809                         ++control_count;
810                         if (ao_usb_epr_ctr_rx(epr)) {
811                                 if (ao_usb_epr_setup(epr))
812                                         ao_usb_ep0_receive |= AO_USB_EP0_GOT_SETUP;
813                                 else
814                                         ao_usb_ep0_receive |= AO_USB_EP0_GOT_RX_DATA;
815                         }
816                         if (ao_usb_epr_ctr_tx(epr))
817                                 ao_usb_ep0_receive |= AO_USB_EP0_GOT_TX_ACK;
818                         ao_usb_ep0_handle(ao_usb_ep0_receive);
819                         break;
820                 case AO_USB_OUT_EPR:
821                         ++out_count;
822                         if (ao_usb_epr_ctr_rx(epr)) {
823                                 _rx_dbg1("RX ISR", epr);
824                                 ao_usb_out_avail = 1;
825                                 _rx_dbg0("out avail set");
826                                 ao_wakeup(AO_USB_OUT_SLEEP_ADDR);
827                                 _rx_dbg0("stdin awoken");
828                         }
829                         break;
830                 case AO_USB_IN_EPR:
831                         ++in_count;
832                         _tx_dbg1("TX ISR", epr);
833                         if (ao_usb_epr_ctr_tx(epr)) {
834                                 ao_usb_in_pending = 0;
835                                 ao_wakeup(&ao_usb_in_pending);
836                         }
837                         break;
838                 case AO_USB_INT_EPR:
839                         ++int_count;
840                         if (ao_usb_epr_ctr_tx(epr))
841                                 _ao_usb_set_stat_tx(AO_USB_INT_EPR, STM_USB_EPR_STAT_TX_NAK);
842                         break;
843                 }
844                 return;
845         }
846
847         if (istr & (1 << STM_USB_ISTR_RESET)) {
848                 ++reset_count;
849                 debug ("\treset\n");
850                 ao_usb_set_ep0();
851         }
852 #if AO_POWER_MANAGEMENT
853         if (istr & (1 << STM_USB_ISTR_SUSP)) {
854                 debug ("\tsuspend\n");
855                 ao_usb_suspend();
856         }
857         if (istr & (1 << STM_USB_ISTR_WKUP)) {
858                 debug ("\twakeup\n");
859                 ao_usb_wakeup();
860         }
861 #endif
862 }
863
864 /* Queue the current IN buffer for transmission */
865 static void
866 _ao_usb_in_send(void)
867 {
868         _tx_dbg0("in_send start");
869         debug ("send %d\n", ao_usb_tx_count);
870         while (ao_usb_in_pending)
871                 ao_sleep(&ao_usb_in_pending);
872         ao_usb_in_pending = 1;
873         if (ao_usb_tx_count != AO_USB_IN_SIZE)
874                 ao_usb_in_flushed = 1;
875         ao_usb_copy_tx(ao_usb_tx_buffer, ao_usb_in_tx_buffer, ao_usb_tx_count);
876         ao_usb_bdt[AO_USB_IN_EPR].single.addr_tx = ao_usb_in_tx_offset;
877         ao_usb_bdt[AO_USB_IN_EPR].single.count_tx = ao_usb_tx_count;
878         ao_usb_tx_count = 0;
879         _ao_usb_set_stat_tx(AO_USB_IN_EPR, STM_USB_EPR_STAT_TX_VALID);
880         _tx_dbg0("in_send end");
881 }
882
883 /* Wait for a free IN buffer. Interrupts are blocked */
884 static void
885 _ao_usb_in_wait(void)
886 {
887         for (;;) {
888                 /* Check if the current buffer is writable */
889                 if (ao_usb_tx_count < AO_USB_IN_SIZE)
890                         break;
891
892                 _tx_dbg0("in_wait top");
893                 /* Wait for an IN buffer to be ready */
894                 while (ao_usb_in_pending)
895                         ao_sleep(&ao_usb_in_pending);
896                 _tx_dbg0("in_wait bottom");
897         }
898 }
899
900 void
901 ao_usb_flush(void)
902 {
903         if (!ao_usb_running)
904                 return;
905
906         /* Anytime we've sent a character since
907          * the last time we flushed, we'll need
908          * to send a packet -- the only other time
909          * we would send a packet is when that
910          * packet was full, in which case we now
911          * want to send an empty packet
912          */
913         ao_arch_block_interrupts();
914         while (!ao_usb_in_flushed) {
915                 _tx_dbg0("flush top");
916                 _ao_usb_in_send();
917                 _tx_dbg0("flush end");
918         }
919         ao_arch_release_interrupts();
920 }
921
922 void
923 ao_usb_putchar(char c)
924 {
925         if (!ao_usb_running)
926                 return;
927
928         ao_arch_block_interrupts();
929         _ao_usb_in_wait();
930
931         ao_usb_in_flushed = 0;
932         ao_usb_tx_buffer[ao_usb_tx_count++] = (uint8_t) c;
933
934         /* Send the packet when full */
935         if (ao_usb_tx_count == AO_USB_IN_SIZE) {
936                 _tx_dbg0("putchar full");
937                 _ao_usb_in_send();
938                 _tx_dbg0("putchar flushed");
939         }
940         ao_arch_release_interrupts();
941 }
942
943 static void
944 _ao_usb_out_recv(void)
945 {
946         _rx_dbg0("out_recv top");
947         ao_usb_out_avail = 0;
948
949         ao_usb_rx_count = ao_usb_bdt[AO_USB_OUT_EPR].single.count_rx & STM_USB_BDT_COUNT_RX_COUNT_RX_MASK;
950
951         _rx_dbg1("out_recv count", ao_usb_rx_count);
952         debug ("recv %d\n", ao_usb_rx_count);
953         debug_data("Fill OUT len %d:", ao_usb_rx_count);
954         ao_usb_copy_rx(ao_usb_rx_buffer, ao_usb_out_rx_buffer, ao_usb_rx_count);
955         debug_data("\n");
956         ao_usb_rx_pos = 0;
957
958         /* ACK the packet */
959         _ao_usb_set_stat_rx(AO_USB_OUT_EPR, STM_USB_EPR_STAT_RX_VALID);
960 }
961
962 int
963 _ao_usb_pollchar(void)
964 {
965         uint8_t c;
966
967         if (!ao_usb_running)
968                 return AO_READ_AGAIN;
969
970         for (;;) {
971                 if (ao_usb_rx_pos != ao_usb_rx_count)
972                         break;
973
974                 _rx_dbg0("poll check");
975                 /* Check to see if a packet has arrived */
976                 if (!ao_usb_out_avail) {
977                         _rx_dbg0("poll none");
978                         return AO_READ_AGAIN;
979                 }
980                 _ao_usb_out_recv();
981         }
982
983         /* Pull a character out of the fifo */
984         c = ao_usb_rx_buffer[ao_usb_rx_pos++];
985         return c;
986 }
987
988 char
989 ao_usb_getchar(void)
990 {
991         int     c;
992
993         ao_arch_block_interrupts();
994         while ((c = _ao_usb_pollchar()) == AO_READ_AGAIN)
995                 ao_sleep(AO_USB_OUT_SLEEP_ADDR);
996         ao_arch_release_interrupts();
997         return c;
998 }
999
1000 #if AO_USB_DIRECTIO
1001 uint16_t *
1002 ao_usb_alloc(void)
1003 {
1004         uint16_t        *buffer;
1005
1006         buffer = ao_usb_packet_buffer_addr(ao_usb_sram_addr);
1007         ao_usb_sram_addr += AO_USB_IN_SIZE;
1008         return buffer;
1009 }
1010
1011 void
1012 ao_usb_free(uint16_t *addr)
1013 {
1014         uint16_t        offset = ao_usb_packet_buffer_offset(addr);
1015         if (offset < ao_usb_sram_addr)
1016                 ao_usb_sram_addr = offset;
1017 }
1018
1019 void
1020 ao_usb_write(uint16_t *buffer, uint16_t len)
1021 {
1022         ao_arch_block_interrupts();
1023
1024         /* Wait for everything to be ready at the same time */
1025         for (;;) {
1026                 /* Make sure USB is connected */
1027                 if (!ao_usb_running) {
1028                         ao_sleep(&ao_usb_running);
1029                         continue;
1030                 }
1031
1032                 /* Flush any pending regular I/O */
1033                 if (ao_usb_tx_count) {
1034                         _ao_usb_in_send();
1035                         continue;
1036                 }
1037
1038                 /* Wait for an idle IN buffer */
1039                 if (ao_usb_in_pending) {
1040                         ao_sleep(&ao_usb_in_pending);
1041                         continue;
1042                 }
1043                 break;
1044         }
1045
1046         ao_usb_in_pending = 1;
1047         ao_usb_in_flushed = (len != AO_USB_IN_SIZE);
1048         ao_usb_bdt[AO_USB_IN_EPR].single.addr_tx = ao_usb_packet_buffer_offset(buffer);
1049         ao_usb_bdt[AO_USB_IN_EPR].single.count_tx = len;
1050         _ao_usb_set_stat_tx(AO_USB_IN_EPR, STM_USB_EPR_STAT_TX_VALID);
1051         ao_arch_release_interrupts();
1052 }
1053 #endif
1054
1055 void
1056 ao_usb_disable(void)
1057 {
1058         ao_arch_block_interrupts();
1059         stm_usb.cntr = (1 << STM_USB_CNTR_FRES);
1060         stm_usb.istr = 0;
1061
1062         /* Disable USB pull-up */
1063         stm_usb.bcdr &= ~(1 << STM_USB_BCDR_DPPU);
1064
1065         /* Switch off the device */
1066         stm_usb.cntr = (1 << STM_USB_CNTR_PDWN) | (1 << STM_USB_CNTR_FRES);
1067
1068         /* Disable the interface */
1069         stm_rcc.apb1enr &= ~(1 << STM_RCC_APB1ENR_USBEN);
1070         ao_arch_release_interrupts();
1071 }
1072
1073 void
1074 ao_usb_enable(void)
1075 {
1076         int     t;
1077
1078         /* Select HSI48 as USB clock source */
1079         stm_rcc.cfgr3 &= ~(1 << STM_RCC_CFGR3_USBSW);
1080
1081         /* Enable USB device */
1082         stm_rcc.apb1enr |= (1 << STM_RCC_APB1ENR_USBEN);
1083
1084         /* Clear reset condition */
1085         stm_rcc.apb1rstr &= ~(1 << STM_RCC_APB1RSTR_USBRST);
1086
1087         /* Disable USB pull-up */
1088         stm_usb.bcdr &= ~(1 << STM_USB_BCDR_DPPU);
1089
1090         /* Do not touch the GPIOA configuration; USB takes priority
1091          * over GPIO on pins A11 and A12, but if you select alternate
1092          * input 10 (the documented correct selection), then USB is
1093          * pulled low and doesn't work at all
1094          */
1095
1096         ao_arch_block_interrupts();
1097
1098         /* Route interrupts */
1099         stm_nvic_set_enable(STM_ISR_USB_POS);
1100         stm_nvic_set_priority(STM_ISR_USB_POS, 3);
1101
1102         ao_usb_configuration = 0;
1103
1104         /* Set up buffer descriptors */
1105         ao_usb_init_btable();
1106
1107         /* Reset the USB controller */
1108         stm_usb.cntr = (1 << STM_USB_CNTR_FRES);
1109
1110         /* Clear the reset bit */
1111         stm_usb.cntr = 0;
1112
1113         /* Clear any spurious interrupts */
1114         stm_usb.istr = 0;
1115
1116         ao_usb_set_ep0();
1117
1118         debug ("ao_usb_enable\n");
1119
1120         /* Enable interrupts */
1121         stm_usb.cntr = ((1 << STM_USB_CNTR_CTRM) |
1122                         (0 << STM_USB_CNTR_PMAOVRM) |
1123                         (0 << STM_USB_CNTR_ERRM) |
1124                         (AO_POWER_MANAGEMENT << STM_USB_CNTR_WKUPM) |
1125                         (AO_POWER_MANAGEMENT << STM_USB_CNTR_SUSPM) |
1126                         (1 << STM_USB_CNTR_RESETM) |
1127                         (0 << STM_USB_CNTR_SOFM) |
1128                         (0 << STM_USB_CNTR_ESOFM) |
1129                         (0 << STM_USB_CNTR_RESUME) |
1130                         (0 << STM_USB_CNTR_FSUSP) |
1131                         (0 << STM_USB_CNTR_LP_MODE) |
1132                         (0 << STM_USB_CNTR_PDWN) |
1133                         (0 << STM_USB_CNTR_FRES));
1134
1135         ao_arch_release_interrupts();
1136
1137         for (t = 0; t < 1000; t++)
1138                 ao_arch_nop();
1139
1140         /* Enable USB pull-up */
1141         stm_usb.bcdr |= (1 << STM_USB_BCDR_DPPU);
1142 }
1143
1144 #if USB_ECHO
1145 struct ao_task ao_usb_echo_task;
1146
1147 static void
1148 ao_usb_echo(void)
1149 {
1150         char    c;
1151
1152         for (;;) {
1153                 c = ao_usb_getchar();
1154                 ao_usb_putchar(c);
1155                 ao_usb_flush();
1156         }
1157 }
1158 #endif
1159
1160 #if USB_STATUS
1161 static void
1162 ao_usb_irq(void)
1163 {
1164         printf ("control: %d out: %d in: %d int: %d reset: %d\n",
1165                 control_count, out_count, in_count, int_count, reset_count);
1166 }
1167
1168 __code struct ao_cmds ao_usb_cmds[] = {
1169         { ao_usb_irq, "I\0Show USB interrupt counts" },
1170         { 0, NULL }
1171 };
1172 #endif
1173
1174 void
1175 ao_usb_init(void)
1176 {
1177         /* Turn on syscfg */
1178         stm_rcc.apb2enr |= (1 << STM_RCC_APB2ENR_SYSCFGCOMPEN);
1179
1180         /* Set PA11/PA12 remapping bit */
1181         stm_syscfg.cfgr1 |= (AO_PA11_PA12_RMP << STM_SYSCFG_CFGR1_PA11_PA12_RMP);
1182
1183         ao_usb_enable();
1184
1185 #if AO_USB_DEVICE_ID_SERIAL
1186         ao_usb_serial_init();
1187 #endif
1188
1189         debug ("ao_usb_init\n");
1190         ao_usb_ep0_state = AO_USB_EP0_IDLE;
1191
1192         ao_usb_alloc_buffers();
1193
1194 #if USB_ECHO
1195         ao_add_task(&ao_usb_echo_task, ao_usb_echo, "usb echo");
1196 #endif
1197 #if USB_STATUS
1198         ao_cmd_register(&ao_usb_cmds[0]);
1199 #endif
1200 #if !USB_ECHO
1201 #if USE_USB_STDIO
1202         ao_add_stdio(_ao_usb_pollchar, ao_usb_putchar, ao_usb_flush);
1203 #endif
1204 #endif
1205 }
1206
1207 #if TX_DBG || RX_DBG
1208
1209 struct ao_usb_dbg {
1210         int             line;
1211         char            *msg;
1212         uint32_t        value;
1213         uint32_t        primask;
1214 #if TX_DBG
1215         uint16_t        in_count;
1216         uint32_t        in_epr;
1217         uint32_t        in_pending;
1218         uint32_t        tx_count;
1219         uint32_t        in_flushed;
1220 #endif
1221 #if RX_DBG
1222         uint8_t         rx_count;
1223         uint8_t         rx_pos;
1224         uint8_t         out_avail;
1225         uint32_t        out_epr;
1226 #endif
1227 };
1228
1229 #define NUM_USB_DBG     128
1230
1231 static struct ao_usb_dbg dbg[128];
1232 static int dbg_i;
1233
1234 static void _dbg(int line, char *msg, uint32_t value)
1235 {
1236         uint32_t        primask;
1237         dbg[dbg_i].line = line;
1238         dbg[dbg_i].msg = msg;
1239         dbg[dbg_i].value = value;
1240         asm("mrs %0,primask" : "=&r" (primask));
1241         dbg[dbg_i].primask = primask;
1242 #if TX_DBG
1243         dbg[dbg_i].in_count = in_count;
1244         dbg[dbg_i].in_epr = stm_usb.epr[AO_USB_IN_EPR];
1245         dbg[dbg_i].in_pending = ao_usb_in_pending;
1246         dbg[dbg_i].tx_count = ao_usb_tx_count;
1247         dbg[dbg_i].in_flushed = ao_usb_in_flushed;
1248 #endif
1249 #if RX_DBG
1250         dbg[dbg_i].rx_count = ao_usb_rx_count;
1251         dbg[dbg_i].rx_pos = ao_usb_rx_pos;
1252         dbg[dbg_i].out_avail = ao_usb_out_avail;
1253         dbg[dbg_i].out_epr = stm_usb.epr[AO_USB_OUT_EPR];
1254 #endif
1255         if (++dbg_i == NUM_USB_DBG)
1256                 dbg_i = 0;
1257 }
1258 #endif