2 * Copyright © 2012 Keith Packard <keithp@keithp.com>
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.
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.
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.
20 #include "ao_product.h"
25 #define USB_DEBUG_DATA 0
28 #ifndef AO_PA11_PA12_RMP
29 #error "must define AO_PA11_PA12_RMP"
32 #ifndef AO_POWER_MANAGEMENT
33 #define AO_POWER_MANAGEMENT 0
37 #define USE_USB_STDIO 1
41 #define AO_USB_OUT_SLEEP_ADDR (&ao_stdin_ready)
43 #define AO_USB_OUT_SLEEP_ADDR (&ao_usb_out_avail)
47 #define debug(format, args...) printf(format, ## args);
49 #define debug(format, args...)
53 #define debug_data(format, args...) printf(format, ## args);
55 #define debug_data(format, args...)
59 uint8_t dir_type_recip;
66 static uint8_t ao_usb_ep0_state;
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 */
72 /* Temp buffer for smaller EP0 in data */
73 static uint8_t ao_usb_ep0_in_buf[2];
75 /* Pending EP0 OUT data */
76 static uint8_t *ao_usb_ep0_out_data;
77 static uint8_t ao_usb_ep0_out_len;
80 * Objects allocated in special USB memory
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;
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;
92 /* Pointer to interrupt buffer in USB memory */
93 static uint16_t ao_usb_int_tx_offset;
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;
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;
106 static uint8_t ao_usb_rx_buffer[AO_USB_OUT_SIZE];
107 static uint8_t ao_usb_rx_count, ao_usb_rx_pos;
110 * End point register indices
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
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
123 static uint8_t ao_usb_in_flushed;
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.
129 static uint8_t ao_usb_in_pending;
131 /* Marks when an OUT packet has been received by the hardware
132 * but not pulled to the shadow buffer.
134 static uint8_t ao_usb_out_avail;
135 uint8_t ao_usb_running;
136 static uint8_t ao_usb_configuration;
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
142 static uint8_t ao_usb_ep0_receive;
143 static uint8_t ao_usb_address;
144 static uint8_t ao_usb_address_pending;
146 static inline uint32_t set_toggle(uint32_t current_value,
148 uint32_t desired_value)
150 return (current_value ^ desired_value) & mask;
153 static inline uint16_t *ao_usb_packet_buffer_addr(uint16_t sram_addr)
155 return (uint16_t *) (stm_usb_sram + sram_addr);
158 static inline uint16_t ao_usb_packet_buffer_offset(uint16_t *addr)
160 return (uint16_t) ((uint8_t *) addr - stm_usb_sram);
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;
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;
171 static inline uint32_t ao_usb_epr_ctr_rx(uint32_t epr) {
172 return (epr >> STM_USB_EPR_CTR_RX) & 1;
175 static inline uint32_t ao_usb_epr_ctr_tx(uint32_t epr) {
176 return (epr >> STM_USB_EPR_CTR_TX) & 1;
179 static inline uint32_t ao_usb_epr_setup(uint32_t epr) {
180 return (epr >> STM_USB_EPR_SETUP) & 1;
183 static inline uint32_t ao_usb_epr_dtog_rx(uint32_t epr) {
184 return (epr >> STM_USB_EPR_DTOG_RX) & 1;
187 static inline uint32_t ao_usb_epr_dtog_tx(uint32_t epr) {
188 return (epr >> STM_USB_EPR_DTOG_TX) & 1;
192 * Set current device address and mark the
193 * interface as active
196 ao_usb_set_address(uint8_t address)
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;
204 * Write these values to preserve register contents under HW changes
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))
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))
222 * These bits are purely under sw control, so preserve them in the
223 * register by re-writing what was read
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))
233 #define _tx_dbg0(msg) _dbg(__LINE__,msg,0)
234 #define _tx_dbg1(msg,value) _dbg(__LINE__,msg,value)
236 #define _tx_dbg0(msg)
237 #define _tx_dbg1(msg,value)
241 #define _rx_dbg0(msg) _dbg(__LINE__,msg,0)
242 #define _rx_dbg1(msg,value) _dbg(__LINE__,msg,value)
244 #define _rx_dbg0(msg)
245 #define _rx_dbg1(msg,value)
249 static void _dbg(int line, char *msg, uint32_t value);
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...
259 _ao_usb_set_stat_tx(int ep, uint32_t stat_tx)
261 uint16_t epr_write, epr_old;
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);
275 ao_usb_set_stat_tx(int ep, uint32_t stat_tx)
277 ao_arch_block_interrupts();
278 _ao_usb_set_stat_tx(ep, stat_tx);
279 ao_arch_release_interrupts();
283 _ao_usb_set_stat_rx(int ep, uint32_t stat_rx) {
284 uint16_t epr_write, epr_old;
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;
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();
303 * Set just endpoint 0, for use during startup
307 ao_usb_init_ep(uint8_t ep, uint32_t addr, uint32_t type, uint32_t stat_rx, uint32_t stat_tx)
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)) |
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)) |
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);
333 ao_usb_alloc_buffers(void)
335 ao_usb_sram_addr = 0;
337 ao_usb_bdt = (void *) stm_usb_sram;
338 ao_usb_sram_addr += 8 * STM_USB_BDT_SIZE;
340 ao_usb_ep0_tx_buffer = ao_usb_packet_buffer_addr(ao_usb_sram_addr);
341 ao_usb_sram_addr += AO_USB_CONTROL_SIZE;
343 ao_usb_ep0_rx_buffer = ao_usb_packet_buffer_addr(ao_usb_sram_addr);
344 ao_usb_sram_addr += AO_USB_CONTROL_SIZE;
346 ao_usb_int_tx_offset = ao_usb_sram_addr;
347 ao_usb_sram_addr += AO_USB_INT_SIZE;
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;
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;
359 ao_usb_init_btable(void)
361 /* Set up EP 0 - a Control end point with 32 bytes of in and out buffers */
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;
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));
376 ao_usb_init_btable();
378 /* buffer table is at the start of USB memory */
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);
386 /* Clear all of the other endpoints */
387 for (e = 1; e < 8; e++) {
389 STM_USB_EPR_EP_TYPE_CONTROL,
390 STM_USB_EPR_STAT_RX_DISABLED,
391 STM_USB_EPR_STAT_TX_DISABLED);
394 ao_usb_set_address(0);
400 ao_usb_set_configuration(void)
402 debug ("ao_usb_set_configuration\n");
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;
409 ao_usb_init_ep(AO_USB_INT_EPR,
411 STM_USB_EPR_EP_TYPE_INTERRUPT,
412 STM_USB_EPR_STAT_RX_DISABLED,
413 STM_USB_EPR_STAT_TX_NAK);
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));
422 ao_usb_init_ep(AO_USB_OUT_EPR,
424 STM_USB_EPR_EP_TYPE_BULK,
425 STM_USB_EPR_STAT_RX_VALID,
426 STM_USB_EPR_STAT_TX_DISABLED);
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;
434 ao_usb_init_ep(AO_USB_IN_EPR,
436 STM_USB_EPR_EP_TYPE_BULK,
437 STM_USB_EPR_STAT_RX_DISABLED,
438 STM_USB_EPR_STAT_TX_NAK);
443 ao_wakeup(&ao_usb_running);
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;
453 /* The USB memory must be accessed in 16-bit units
457 ao_usb_copy_tx(const uint8_t *src, uint16_t *base, uint16_t bytes)
460 *base++ = src[0] | (src[1] << 8);
469 ao_usb_copy_rx(uint8_t *dst, uint16_t *base, uint16_t bytes)
472 uint16_t s = *base++;
482 /* Send an IN data packet */
484 ao_usb_ep0_flush(void)
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");
494 this_len = ao_usb_ep0_in_len;
495 if (this_len > AO_USB_CONTROL_SIZE)
496 this_len = AO_USB_CONTROL_SIZE;
498 if (this_len < AO_USB_CONTROL_SIZE)
499 ao_usb_ep0_state = AO_USB_EP0_IDLE;
501 ao_usb_ep0_in_len -= this_len;
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);
506 ao_usb_ep0_in_data += this_len;
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]);
514 /* Read data from the ep0 OUT fifo */
516 ao_usb_ep0_fill(void)
518 uint16_t len = ao_usb_bdt[0].single.count_rx & STM_USB_BDT_COUNT_RX_COUNT_RX_MASK;
520 if (len > ao_usb_ep0_out_len)
521 len = ao_usb_ep0_out_len;
522 ao_usb_ep0_out_len -= len;
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);
528 ao_usb_ep0_out_data += len;
531 ao_usb_set_stat_rx(0, STM_USB_EPR_STAT_RX_VALID);
535 ao_usb_ep0_in_reset(void)
537 ao_usb_ep0_in_data = ao_usb_ep0_in_buf;
538 ao_usb_ep0_in_len = 0;
542 ao_usb_ep0_in_queue_byte(uint8_t a)
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;
549 ao_usb_ep0_in_set(const uint8_t *data, uint8_t len)
551 ao_usb_ep0_in_data = data;
552 ao_usb_ep0_in_len = len;
556 ao_usb_ep0_out_set(uint8_t *data, uint8_t len)
558 ao_usb_ep0_out_data = data;
559 ao_usb_ep0_out_len = len;
563 ao_usb_ep0_in_start(uint16_t max)
565 /* Don't send more than asked for */
566 if (ao_usb_ep0_in_len > max)
567 ao_usb_ep0_in_len = max;
571 static struct ao_usb_line_coding ao_usb_line_coding = {115200, 0, 0, 8};
573 #if AO_USB_DEVICE_ID_SERIAL
574 static uint8_t ao_usb_serial[2 + 48];
576 /* Convert a 32-bit value to 8 hexidecimal UCS2 characters */
578 hex_to_ucs2(uint32_t in, uint8_t *out)
582 for (i = 28; i >= 0; i -= 4) {
583 uint8_t bits = (in >> i) & 0xf;
584 *out++ = (bits < 10) ? ('0' + bits) : ('a' + bits);
589 /* Encode the device ID (96 bits) in hexidecimal to use as a device
593 ao_usb_serial_init(void)
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);
603 /* Walk through the list of descriptors and find a match
606 ao_usb_get_descriptor(uint16_t value)
608 const uint8_t *descriptor;
609 uint8_t type = value >> 8;
610 uint8_t index = value;
612 descriptor = ao_usb_descriptors;
613 while (descriptor[0] != 0) {
614 if (descriptor[1] == type && index-- == 0) {
616 if (type == AO_USB_DESC_CONFIGURATION)
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);
627 ao_usb_ep0_in_set(descriptor, len);
630 descriptor += descriptor[0];
635 ao_usb_ep0_setup(void)
637 /* Pull the setup packet out of the fifo */
638 ao_usb_ep0_out_set((uint8_t *) &ao_usb_setup, 8);
640 if (ao_usb_ep0_out_len != 0) {
641 debug ("invalid setup packet length\n");
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;
648 ao_usb_ep0_state = AO_USB_EP0_DATA_OUT;
650 ao_usb_ep0_in_reset();
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);
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;
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);
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);
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();
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);
691 case AO_USB_REQ_GET_INTERFACE:
692 ao_usb_ep0_in_queue_byte(0);
694 case AO_USB_REQ_SET_INTERFACE:
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);
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);
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);
720 case AO_USB_SET_CONTROL_LINE_STATE:
726 /* If we're not waiting to receive data from the host,
727 * queue an IN response
729 if (ao_usb_ep0_state == AO_USB_EP0_DATA_IN)
730 ao_usb_ep0_in_start(ao_usb_setup.length);
734 ao_usb_ep0_handle(uint8_t receive)
736 ao_usb_ep0_receive = 0;
737 if (receive & AO_USB_EP0_GOT_SETUP) {
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) {
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);
751 if (receive & AO_USB_EP0_GOT_TX_ACK) {
752 debug ("\tgot tx ack\n");
754 #if HAS_FLIGHT && AO_USB_FORCE_IDLE
755 ao_flight_force_idle = 1;
757 /* Wait until the IN packet is received from addr 0
758 * before assigning our local address
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)
767 #if AO_POWER_MANAGEMENT
771 stm_usb.cntr |= (1 << STM_USB_CNTR_FSUSP);
773 stm_usb.cntr |= (1 << STM_USB_CNTR_LP_MODE);
781 stm_usb.cntr &= ~(1 << STM_USB_CNTR_FSUSP);
789 uint32_t istr = stm_usb.istr;
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;
796 /* Preserve the SW write bits, don't mess with most HW writable bits,
797 * clear the CTR_RX and CTR_TX bits
799 epr = stm_usb.epr[ep].r;
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;
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;
814 ao_usb_ep0_receive |= AO_USB_EP0_GOT_RX_DATA;
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);
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");
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);
840 if (ao_usb_epr_ctr_tx(epr))
841 _ao_usb_set_stat_tx(AO_USB_INT_EPR, STM_USB_EPR_STAT_TX_NAK);
847 if (istr & (1 << STM_USB_ISTR_RESET)) {
852 #if AO_POWER_MANAGEMENT
853 if (istr & (1 << STM_USB_ISTR_SUSP)) {
854 debug ("\tsuspend\n");
857 if (istr & (1 << STM_USB_ISTR_WKUP)) {
858 debug ("\twakeup\n");
864 /* Queue the current IN buffer for transmission */
866 _ao_usb_in_send(void)
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;
879 _ao_usb_set_stat_tx(AO_USB_IN_EPR, STM_USB_EPR_STAT_TX_VALID);
880 _tx_dbg0("in_send end");
883 /* Wait for a free IN buffer. Interrupts are blocked */
885 _ao_usb_in_wait(void)
888 /* Check if the current buffer is writable */
889 if (ao_usb_tx_count < AO_USB_IN_SIZE)
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");
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
913 ao_arch_block_interrupts();
914 while (!ao_usb_in_flushed) {
915 _tx_dbg0("flush top");
917 _tx_dbg0("flush end");
919 ao_arch_release_interrupts();
923 ao_usb_putchar(char c)
928 ao_arch_block_interrupts();
931 ao_usb_in_flushed = 0;
932 ao_usb_tx_buffer[ao_usb_tx_count++] = (uint8_t) c;
934 /* Send the packet when full */
935 if (ao_usb_tx_count == AO_USB_IN_SIZE) {
936 _tx_dbg0("putchar full");
938 _tx_dbg0("putchar flushed");
940 ao_arch_release_interrupts();
944 _ao_usb_out_recv(void)
946 _rx_dbg0("out_recv top");
947 ao_usb_out_avail = 0;
949 ao_usb_rx_count = ao_usb_bdt[AO_USB_OUT_EPR].single.count_rx & STM_USB_BDT_COUNT_RX_COUNT_RX_MASK;
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);
959 _ao_usb_set_stat_rx(AO_USB_OUT_EPR, STM_USB_EPR_STAT_RX_VALID);
963 _ao_usb_pollchar(void)
968 return AO_READ_AGAIN;
971 if (ao_usb_rx_pos != ao_usb_rx_count)
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;
983 /* Pull a character out of the fifo */
984 c = ao_usb_rx_buffer[ao_usb_rx_pos++];
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();
1006 buffer = ao_usb_packet_buffer_addr(ao_usb_sram_addr);
1007 ao_usb_sram_addr += AO_USB_IN_SIZE;
1012 ao_usb_free(uint16_t *addr)
1014 uint16_t offset = ao_usb_packet_buffer_offset(addr);
1015 if (offset < ao_usb_sram_addr)
1016 ao_usb_sram_addr = offset;
1020 ao_usb_write(uint16_t *buffer, uint16_t len)
1022 ao_arch_block_interrupts();
1024 /* Wait for everything to be ready at the same time */
1026 /* Make sure USB is connected */
1027 if (!ao_usb_running) {
1028 ao_sleep(&ao_usb_running);
1032 /* Flush any pending regular I/O */
1033 if (ao_usb_tx_count) {
1038 /* Wait for an idle IN buffer */
1039 if (ao_usb_in_pending) {
1040 ao_sleep(&ao_usb_in_pending);
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();
1056 ao_usb_disable(void)
1058 ao_arch_block_interrupts();
1059 stm_usb.cntr = (1 << STM_USB_CNTR_FRES);
1062 /* Disable USB pull-up */
1063 stm_usb.bcdr &= ~(1 << STM_USB_BCDR_DPPU);
1065 /* Switch off the device */
1066 stm_usb.cntr = (1 << STM_USB_CNTR_PDWN) | (1 << STM_USB_CNTR_FRES);
1068 /* Disable the interface */
1069 stm_rcc.apb1enr &= ~(1 << STM_RCC_APB1ENR_USBEN);
1070 ao_arch_release_interrupts();
1078 /* Select HSI48 as USB clock source */
1079 stm_rcc.cfgr3 &= ~(1 << STM_RCC_CFGR3_USBSW);
1081 /* Enable USB device */
1082 stm_rcc.apb1enr |= (1 << STM_RCC_APB1ENR_USBEN);
1084 /* Clear reset condition */
1085 stm_rcc.apb1rstr &= ~(1 << STM_RCC_APB1RSTR_USBRST);
1087 /* Disable USB pull-up */
1088 stm_usb.bcdr &= ~(1 << STM_USB_BCDR_DPPU);
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
1096 ao_arch_block_interrupts();
1098 /* Route interrupts */
1099 stm_nvic_set_enable(STM_ISR_USB_POS);
1100 stm_nvic_set_priority(STM_ISR_USB_POS, 3);
1102 ao_usb_configuration = 0;
1104 /* Set up buffer descriptors */
1105 ao_usb_init_btable();
1107 /* Reset the USB controller */
1108 stm_usb.cntr = (1 << STM_USB_CNTR_FRES);
1110 /* Clear the reset bit */
1113 /* Clear any spurious interrupts */
1118 debug ("ao_usb_enable\n");
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));
1135 ao_arch_release_interrupts();
1137 for (t = 0; t < 1000; t++)
1140 /* Enable USB pull-up */
1141 stm_usb.bcdr |= (1 << STM_USB_BCDR_DPPU);
1145 struct ao_task ao_usb_echo_task;
1153 c = ao_usb_getchar();
1164 printf ("control: %d out: %d in: %d int: %d reset: %d\n",
1165 control_count, out_count, in_count, int_count, reset_count);
1168 __code struct ao_cmds ao_usb_cmds[] = {
1169 { ao_usb_irq, "I\0Show USB interrupt counts" },
1177 /* Turn on syscfg */
1178 stm_rcc.apb2enr |= (1 << STM_RCC_APB2ENR_SYSCFGCOMPEN);
1180 /* Set PA11/PA12 remapping bit */
1181 stm_syscfg.cfgr1 |= (AO_PA11_PA12_RMP << STM_SYSCFG_CFGR1_PA11_PA12_RMP);
1185 #if AO_USB_DEVICE_ID_SERIAL
1186 ao_usb_serial_init();
1189 debug ("ao_usb_init\n");
1190 ao_usb_ep0_state = AO_USB_EP0_IDLE;
1192 ao_usb_alloc_buffers();
1195 ao_add_task(&ao_usb_echo_task, ao_usb_echo, "usb echo");
1198 ao_cmd_register(&ao_usb_cmds[0]);
1202 ao_add_stdio(_ao_usb_pollchar, ao_usb_putchar, ao_usb_flush);
1207 #if TX_DBG || RX_DBG
1217 uint32_t in_pending;
1219 uint32_t in_flushed;
1229 #define NUM_USB_DBG 128
1231 static struct ao_usb_dbg dbg[128];
1234 static void _dbg(int line, char *msg, uint32_t value)
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;
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;
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];
1255 if (++dbg_i == NUM_USB_DBG)