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"
23 #define USB_DEBUG_DATA 0
27 #define debug(format, args...) printf(format, ## args);
29 #define debug(format, args...)
33 #define debug_data(format, args...) printf(format, ## args);
35 #define debug_data(format, args...)
38 struct ao_task ao_usb_task;
41 uint8_t dir_type_recip;
48 static uint8_t ao_usb_ep0_state;
50 /* Pending EP0 IN data */
51 static const uint8_t *ao_usb_ep0_in_data; /* Remaining data */
52 static uint8_t ao_usb_ep0_in_len; /* Remaining amount */
54 /* Temp buffer for smaller EP0 in data */
55 static uint8_t ao_usb_ep0_in_buf[2];
57 /* Pending EP0 OUT data */
58 static uint8_t *ao_usb_ep0_out_data;
59 static uint8_t ao_usb_ep0_out_len;
62 * Objects allocated in special USB memory
65 /* USB address of end of allocated storage */
66 static uint8_t *ao_usb_sram;
68 /* Pointer to ep0 tx/rx buffers in USB memory */
69 static uint8_t *ao_usb_ep0_tx_buffer;
70 static uint8_t *ao_usb_ep0_setup_buffer;
71 static uint8_t *ao_usb_ep0_rx_buffer;
73 /* Pointer to bulk data tx/rx buffers in USB memory */
74 static uint8_t *ao_usb_in_tx_buffer;
75 static uint8_t *ao_usb_out_rx_buffer;
77 /* Our data buffers */
78 static uint8_t ao_usb_tx_buffer[AO_USB_IN_SIZE];
79 static uint8_t ao_usb_tx_count;
81 static uint8_t ao_usb_rx_buffer[AO_USB_OUT_SIZE];
82 static uint8_t ao_usb_rx_count, ao_usb_rx_pos;
84 static struct lpc_usb_endpoint lpc_usb_endpoint __attribute((aligned(256)));
86 /* Marks when we don't need to send an IN packet.
87 * This happens only when the last IN packet is not full,
88 * otherwise the host will expect to keep seeing packets.
89 * Send a zero-length packet as required
91 static uint8_t ao_usb_in_flushed;
93 /* Marks when we have delivered an IN packet to the hardware
94 * and it has not been received yet. ao_sleep on this address
95 * to wait for it to be delivered.
97 static uint8_t ao_usb_in_pending;
99 /* Marks when an OUT packet has been received by the hardware
100 * but not pulled to the shadow buffer.
102 static uint8_t ao_usb_out_avail;
103 static uint8_t ao_usb_running;
104 static uint8_t ao_usb_configuration;
105 static uint8_t ueienx_0;
107 #define AO_USB_EP0_GOT_RESET 1
108 #define AO_USB_EP0_GOT_SETUP 2
109 #define AO_USB_EP0_GOT_RX_DATA 4
110 #define AO_USB_EP0_GOT_TX_ACK 8
112 static uint8_t ao_usb_ep0_receive;
113 static uint8_t ao_usb_address;
114 static uint8_t ao_usb_address_pending;
116 static inline uint32_t set_toggle(uint32_t current_value,
118 uint32_t desired_value)
120 return (current_value ^ desired_value) & mask;
124 * Set current device address and mark the
125 * interface as active
128 ao_usb_set_address(uint8_t address)
130 debug("ao_usb_set_address %02x\n", address);
131 lpc_usb.devcmdstat = ((address << LPC_USB_DEVCMDSTAT_DEV_ADDR) |
132 (1 << LPC_USB_DEVCMDSTAT_DEV_EN) |
133 (0 << LPC_USB_DEVCMDSTAT_SETUP) |
134 (0 << LPC_USB_DEVCMDSTAT_PLL_ON) |
135 (0 << LPC_USB_DEVCMDSTAT_LPM_SUP) |
136 (0 << LPC_USB_DEVCMDSTAT_INTONNAK_AO) |
137 (0 << LPC_USB_DEVCMDSTAT_INTONNAK_AI) |
138 (0 << LPC_USB_DEVCMDSTAT_INTONNAK_CO) |
139 (0 << LPC_USB_DEVCMDSTAT_INTONNAK_CI) |
140 (1 << LPC_USB_DEVCMDSTAT_DCON) |
141 (0 << LPC_USB_DEVCMDSTAT_DSUS) |
142 (0 << LPC_USB_DEVCMDSTAT_DCON_C) |
143 (0 << LPC_USB_DEVCMDSTAT_DSUS_C) |
144 (0 << LPC_USB_DEVCMDSTAT_DRES_C) |
145 (0 << LPC_USB_DEVCMDSTAT_VBUSDEBOUNCED));
146 ao_usb_address_pending = 0;
153 #define _tx_dbg0(msg) _dbg(__LINE__,msg,0)
154 #define _tx_dbg1(msg,value) _dbg(__LINE__,msg,value)
156 #define _tx_dbg0(msg)
157 #define _tx_dbg1(msg,value)
161 #define _rx_dbg0(msg) _dbg(__LINE__,msg,0)
162 #define _rx_dbg1(msg,value) _dbg(__LINE__,msg,value)
164 #define _rx_dbg0(msg)
165 #define _rx_dbg1(msg,value)
169 static void _dbg(int line, char *msg, uint32_t value);
173 * Set just endpoint 0, for use during startup
177 ao_usb_alloc_sram(uint16_t size)
179 uint8_t *addr = ao_usb_sram;
181 ao_usb_sram += (size + 63) & ~63;
186 ao_usb_sram_offset(uint8_t *addr)
188 return (uint16_t) ((intptr_t) addr >> 6);
192 ao_usb_set_ep(vuint32_t *ep, uint8_t *addr, uint16_t nbytes)
194 *ep = ((ao_usb_sram_offset(addr) << LPC_USB_EP_OFFSET) |
195 (nbytes << LPC_USB_EP_NBYTES) |
196 (0 << LPC_USB_EP_ENDPOINT_ISO) |
197 (0 << LPC_USB_EP_RATE_FEEDBACK) |
198 (0 << LPC_USB_EP_TOGGLE_RESET) |
199 (0 << LPC_USB_EP_STALL) |
200 (0 << LPC_USB_EP_DISABLED) |
201 (1 << LPC_USB_EP_ACTIVE));
204 static inline uint16_t
205 ao_usb_ep_count(vuint32_t *ep)
207 return (*ep >> LPC_USB_EP_NBYTES) & LPC_USB_EP_NBYTES_MASK;
210 static inline uint8_t
211 ao_usb_ep_stall(vuint32_t *ep)
213 return (*ep >> LPC_USB_EP_STALL) & 1;
216 static inline vuint32_t *
219 return &lpc_usb_endpoint.ep0_out;
222 static inline vuint32_t *
225 return &lpc_usb_endpoint.ep0_in;
228 static inline vuint32_t *
229 ao_usb_epn_out(uint8_t n)
231 return &lpc_usb_endpoint.epn[n-1].out[0];
234 static inline vuint32_t *
235 ao_usb_epn_in(uint8_t n)
237 return &lpc_usb_endpoint.epn[n-1].in[0];
241 ao_usb_set_epn_in(uint8_t n, uint8_t *addr, uint16_t nbytes)
243 ao_usb_set_ep(ao_usb_epn_in(n), addr, nbytes);
247 ao_usb_set_epn_out(uint8_t n, uint8_t *addr, uint16_t nbytes)
249 ao_usb_set_ep(ao_usb_epn_out(n), addr, nbytes);
252 static inline uint16_t
253 ao_usb_epn_out_count(uint8_t n)
255 return ao_usb_ep_count(ao_usb_epn_out(n));
258 static inline uint16_t
259 ao_usb_epn_in_count(uint8_t n)
261 return ao_usb_ep_count(ao_usb_epn_in(n));
265 ao_usb_enable_ep(vuint32_t *ep, uint16_t nbytes, uint16_t set_nbytes)
267 uint8_t *addr = ao_usb_alloc_sram(nbytes);
269 ao_usb_set_ep(ep, addr, set_nbytes);
274 ao_usb_disable_ep(vuint32_t *ep)
276 *ep = ((0 << LPC_USB_EP_OFFSET) |
277 (0 << LPC_USB_EP_NBYTES) |
278 (0 << LPC_USB_EP_ENDPOINT_ISO) |
279 (0 << LPC_USB_EP_RATE_FEEDBACK) |
280 (0 << LPC_USB_EP_TOGGLE_RESET) |
281 (0 << LPC_USB_EP_STALL) |
282 (1 << LPC_USB_EP_DISABLED) |
283 (0 << LPC_USB_EP_ACTIVE));
287 ao_usb_enable_epn(uint8_t n, uint16_t out_bytes, uint8_t **out_addr, uint16_t in_bytes, uint8_t **in_addr)
291 addr = ao_usb_enable_ep(ao_usb_epn_out(n), out_bytes, out_bytes);
294 ao_usb_disable_ep(&lpc_usb_endpoint.epn[n-1].out[1]);
296 addr = ao_usb_enable_ep(ao_usb_epn_in(n), in_bytes, 0);
299 ao_usb_disable_ep(&lpc_usb_endpoint.epn[n-1].in[1]);
303 ao_usb_disable_epn(uint8_t n)
305 ao_usb_disable_ep(ao_usb_epn_out(n));
306 ao_usb_disable_ep(&lpc_usb_endpoint.epn[n-1].out[1]);
307 ao_usb_disable_ep(ao_usb_epn_in(n));
308 ao_usb_disable_ep(&lpc_usb_endpoint.epn[n-1].in[1]);
316 /* Everything is single buffered for now */
317 lpc_usb.epbufcfg = 0;
319 lpc_usb.epskip = 0xffffffff;
321 ao_usb_set_address(0);
322 lpc_usb.intstat = 0xc00003ff;
324 ao_usb_configuration = 0;
326 ao_usb_sram = lpc_usb_sram;
328 lpc_usb.epliststart = (uint32_t) (intptr_t) &lpc_usb_endpoint;
329 lpc_usb.databufstart = ((uint32_t) (intptr_t) ao_usb_sram) & 0xffc00000;
331 /* Set up EP 0 - a Control end point with 32 bytes of in and out buffers */
333 ao_usb_ep0_rx_buffer = ao_usb_enable_ep(ao_usb_ep0_out(), AO_USB_CONTROL_SIZE, AO_USB_CONTROL_SIZE);
334 ao_usb_ep0_setup_buffer = ao_usb_alloc_sram(AO_USB_CONTROL_SIZE);
335 lpc_usb_endpoint.setup = ao_usb_sram_offset(ao_usb_ep0_setup_buffer);
336 ao_usb_ep0_tx_buffer = ao_usb_enable_ep(ao_usb_ep0_in(), AO_USB_CONTROL_SIZE, 0);
338 /* Clear all of the other endpoints */
339 for (e = 1; e <= 4; e++)
340 ao_usb_disable_epn(e);
345 ao_usb_set_configuration(void)
347 debug ("ao_usb_set_configuration\n");
349 /* Set up the INT end point */
350 ao_usb_enable_epn(AO_USB_INT_EP, 0, NULL, 0, NULL);
352 /* Set up the OUT end point */
353 ao_usb_enable_epn(AO_USB_OUT_EP, AO_USB_OUT_SIZE, &ao_usb_out_rx_buffer, 0, NULL);
355 /* Set up the IN end point */
356 ao_usb_enable_epn(AO_USB_IN_EP, 0, NULL, AO_USB_IN_SIZE, &ao_usb_in_tx_buffer);
361 /* Send an IN data packet */
363 ao_usb_ep0_flush(void)
367 this_len = ao_usb_ep0_in_len;
368 if (this_len > AO_USB_CONTROL_SIZE)
369 this_len = AO_USB_CONTROL_SIZE;
371 if (this_len < AO_USB_CONTROL_SIZE)
372 ao_usb_ep0_state = AO_USB_EP0_IDLE;
374 ao_usb_ep0_in_len -= this_len;
376 debug_data ("Flush EP0 len %d:", this_len);
377 memcpy(ao_usb_ep0_tx_buffer, ao_usb_ep0_in_data, this_len);
379 ao_usb_ep0_in_data += this_len;
381 /* Mark the endpoint as TX valid to send the packet */
382 ao_usb_set_ep(ao_usb_ep0_in(), ao_usb_ep0_tx_buffer, this_len);
383 debug ("queue tx. 0 now %08x\n", *ao_usb_ep0_in());
386 /* Read data from the ep0 OUT fifo */
388 ao_usb_ep0_fill(void)
393 /* Pull all of the data out of the packet */
394 if (lpc_usb.devcmdstat & (1 << LPC_USB_DEVCMDSTAT_SETUP)) {
395 rx_buffer = ao_usb_ep0_setup_buffer;
398 rx_buffer = ao_usb_ep0_rx_buffer;
399 len = AO_USB_CONTROL_SIZE - ao_usb_ep_count(ao_usb_ep0_out());
402 if (len > ao_usb_ep0_out_len)
403 len = ao_usb_ep0_out_len;
404 ao_usb_ep0_out_len -= len;
406 debug_data ("Fill EP0 len %d:", len);
407 memcpy(ao_usb_ep0_out_data, rx_buffer, len);
409 ao_usb_ep0_out_data += len;
412 ao_usb_set_ep(ao_usb_ep0_out(), ao_usb_ep0_rx_buffer, AO_USB_CONTROL_SIZE);
413 lpc_usb.devcmdstat |= (1 << LPC_USB_DEVCMDSTAT_SETUP);
417 ao_usb_ep0_in_reset(void)
419 ao_usb_ep0_in_data = ao_usb_ep0_in_buf;
420 ao_usb_ep0_in_len = 0;
424 ao_usb_ep0_in_queue_byte(uint8_t a)
426 if (ao_usb_ep0_in_len < sizeof (ao_usb_ep0_in_buf))
427 ao_usb_ep0_in_buf[ao_usb_ep0_in_len++] = a;
431 ao_usb_ep0_in_set(const uint8_t *data, uint8_t len)
433 ao_usb_ep0_in_data = data;
434 ao_usb_ep0_in_len = len;
438 ao_usb_ep0_out_set(uint8_t *data, uint8_t len)
440 ao_usb_ep0_out_data = data;
441 ao_usb_ep0_out_len = len;
445 ao_usb_ep0_in_start(uint8_t max)
447 /* Don't send more than asked for */
448 if (ao_usb_ep0_in_len > max)
449 ao_usb_ep0_in_len = max;
453 static struct ao_usb_line_coding ao_usb_line_coding = {115200, 0, 0, 8};
455 /* Walk through the list of descriptors and find a match
458 ao_usb_get_descriptor(uint16_t value)
460 const uint8_t *descriptor;
461 uint8_t type = value >> 8;
462 uint8_t index = value;
464 descriptor = ao_usb_descriptors;
465 while (descriptor[0] != 0) {
466 if (descriptor[1] == type && index-- == 0) {
468 if (type == AO_USB_DESC_CONFIGURATION)
472 ao_usb_ep0_in_set(descriptor, len);
475 descriptor += descriptor[0];
480 ao_usb_ep0_setup(void)
482 /* Pull the setup packet out of the fifo */
483 ao_usb_ep0_out_set((uint8_t *) &ao_usb_setup, 8);
485 if (ao_usb_ep0_out_len != 0) {
486 debug ("invalid setup packet length\n");
490 if ((ao_usb_setup.dir_type_recip & AO_USB_DIR_IN) || ao_usb_setup.length == 0)
491 ao_usb_ep0_state = AO_USB_EP0_DATA_IN;
493 ao_usb_ep0_state = AO_USB_EP0_DATA_OUT;
495 ao_usb_ep0_in_reset();
497 switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_TYPE_MASK) {
498 case AO_USB_TYPE_STANDARD:
499 debug ("Standard setup packet\n");
500 switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_RECIP_MASK) {
501 case AO_USB_RECIP_DEVICE:
502 debug ("Device setup packet\n");
503 switch(ao_usb_setup.request) {
504 case AO_USB_REQ_GET_STATUS:
505 debug ("get status\n");
506 ao_usb_ep0_in_queue_byte(0);
507 ao_usb_ep0_in_queue_byte(0);
509 case AO_USB_REQ_SET_ADDRESS:
510 debug ("set address %d\n", ao_usb_setup.value);
511 ao_usb_address = ao_usb_setup.value;
512 ao_usb_address_pending = 1;
514 case AO_USB_REQ_GET_DESCRIPTOR:
515 debug ("get descriptor %d\n", ao_usb_setup.value);
516 ao_usb_get_descriptor(ao_usb_setup.value);
518 case AO_USB_REQ_GET_CONFIGURATION:
519 debug ("get configuration %d\n", ao_usb_configuration);
520 ao_usb_ep0_in_queue_byte(ao_usb_configuration);
522 case AO_USB_REQ_SET_CONFIGURATION:
523 ao_usb_configuration = ao_usb_setup.value;
524 debug ("set configuration %d\n", ao_usb_configuration);
525 ao_usb_set_configuration();
529 case AO_USB_RECIP_INTERFACE:
530 debug ("Interface setup packet\n");
531 switch(ao_usb_setup.request) {
532 case AO_USB_REQ_GET_STATUS:
533 ao_usb_ep0_in_queue_byte(0);
534 ao_usb_ep0_in_queue_byte(0);
536 case AO_USB_REQ_GET_INTERFACE:
537 ao_usb_ep0_in_queue_byte(0);
539 case AO_USB_REQ_SET_INTERFACE:
543 case AO_USB_RECIP_ENDPOINT:
544 debug ("Endpoint setup packet\n");
545 switch(ao_usb_setup.request) {
546 case AO_USB_REQ_GET_STATUS:
547 ao_usb_ep0_in_queue_byte(0);
548 ao_usb_ep0_in_queue_byte(0);
554 case AO_USB_TYPE_CLASS:
555 debug ("Class setup packet\n");
556 switch (ao_usb_setup.request) {
557 case AO_USB_SET_LINE_CODING:
558 debug ("set line coding\n");
559 ao_usb_ep0_out_set((uint8_t *) &ao_usb_line_coding, 7);
561 case AO_USB_GET_LINE_CODING:
562 debug ("get line coding\n");
563 ao_usb_ep0_in_set((const uint8_t *) &ao_usb_line_coding, 7);
565 case AO_USB_SET_CONTROL_LINE_STATE:
571 /* If we're not waiting to receive data from the host,
572 * queue an IN response
574 if (ao_usb_ep0_state == AO_USB_EP0_DATA_IN)
575 ao_usb_ep0_in_start(ao_usb_setup.length);
579 ao_usb_ep0_handle(uint8_t receive)
581 ao_usb_ep0_receive = 0;
583 if (receive & AO_USB_EP0_GOT_RESET) {
588 if (receive & AO_USB_EP0_GOT_SETUP) {
592 if (receive & AO_USB_EP0_GOT_RX_DATA) {
593 debug ("\tgot rx data\n");
594 if (ao_usb_ep0_state == AO_USB_EP0_DATA_OUT) {
596 if (ao_usb_ep0_out_len == 0) {
597 ao_usb_ep0_state = AO_USB_EP0_DATA_IN;
598 ao_usb_ep0_in_start(0);
602 if (receive & AO_USB_EP0_GOT_TX_ACK) {
603 debug ("\tgot tx ack\n");
605 /* Wait until the IN packet is received from addr 0
606 * before assigning our local address
608 if (ao_usb_address_pending)
609 ao_usb_set_address(ao_usb_address);
610 if (ao_usb_ep0_state == AO_USB_EP0_DATA_IN)
615 static uint16_t control_count;
616 static uint16_t int_count;
617 static uint16_t in_count;
618 static uint16_t out_count;
619 static uint16_t reset_count;
622 lpc_usb_irq_isr(void)
624 uint32_t intstat = lpc_usb.intstat & lpc_usb.inten;
626 lpc_usb.intstat = intstat;
627 /* Handle EP0 OUT packets */
628 if (intstat & (1 << LPC_USB_INT_EPOUT(0))) {
629 if (lpc_usb.devcmdstat & (1 << LPC_USB_DEVCMDSTAT_SETUP))
630 ao_usb_ep0_receive |= AO_USB_EP0_GOT_SETUP;
632 ao_usb_ep0_receive |= AO_USB_EP0_GOT_RX_DATA;
634 ao_usb_ep0_handle(ao_usb_ep0_receive);
637 /* Handle EP0 IN packets */
638 if (intstat & (1 << LPC_USB_INT_EPIN(0))) {
639 ao_usb_ep0_receive |= AO_USB_EP0_GOT_TX_ACK;
641 ao_usb_ep0_handle(ao_usb_ep0_receive);
645 /* Handle OUT packets */
646 if (intstat & (1 << LPC_USB_INT_EPOUT(AO_USB_OUT_EP))) {
648 _rx_dbg1("RX ISR", *ao_usb_epn_out(AO_USB_OUT_EP));
649 ao_usb_out_avail = 1;
650 _rx_dbg0("out avail set");
651 ao_wakeup(&ao_stdin_ready);
652 _rx_dbg0("stdin awoken");
655 /* Handle IN packets */
656 if (intstat & (1 << LPC_USB_INT_EPIN(AO_USB_IN_EP))) {
658 _tx_dbg1("TX ISR", *ao_usb_epn_in(AO_USB_IN_EP));
659 ao_usb_in_pending = 0;
660 ao_wakeup(&ao_usb_in_pending);
663 /* NAK all INT EP IN packets */
664 if (intstat & (1 << LPC_USB_INT_EPIN(AO_USB_INT_EP))) {
668 /* Check for reset */
669 if (intstat & (1 << LPC_USB_INT_DEV)) {
670 if (lpc_usb.devcmdstat & (1 << LPC_USB_DEVCMDSTAT_DRES_C))
672 lpc_usb.devcmdstat |= (1 << LPC_USB_DEVCMDSTAT_DRES_C);
673 ao_usb_ep0_receive |= AO_USB_EP0_GOT_RESET;
674 ao_usb_ep0_handle(ao_usb_ep0_receive);
681 /* Queue the current IN buffer for transmission */
683 _ao_usb_in_send(void)
685 _tx_dbg0("in_send start");
686 debug ("send %d\n", ao_usb_tx_count);
687 while (ao_usb_in_pending)
688 ao_sleep(&ao_usb_in_pending);
689 ao_usb_in_pending = 1;
690 if (ao_usb_tx_count != AO_USB_IN_SIZE)
691 ao_usb_in_flushed = 1;
692 memcpy(ao_usb_in_tx_buffer, ao_usb_tx_buffer, ao_usb_tx_count);
693 ao_usb_set_ep(ao_usb_epn_in(AO_USB_IN_EP), ao_usb_in_tx_buffer, ao_usb_tx_count);
695 _tx_dbg0("in_send end");
698 /* Wait for a free IN buffer. Interrupts are blocked */
700 _ao_usb_in_wait(void)
703 /* Check if the current buffer is writable */
704 if (ao_usb_tx_count < AO_USB_IN_SIZE)
707 _tx_dbg0("in_wait top");
708 /* Wait for an IN buffer to be ready */
709 while (ao_usb_in_pending)
710 ao_sleep(&ao_usb_in_pending);
711 _tx_dbg0("in_wait bottom");
721 /* Anytime we've sent a character since
722 * the last time we flushed, we'll need
723 * to send a packet -- the only other time
724 * we would send a packet is when that
725 * packet was full, in which case we now
726 * want to send an empty packet
728 ao_arch_block_interrupts();
729 while (!ao_usb_in_flushed) {
730 _tx_dbg0("flush top");
732 _tx_dbg0("flush end");
734 ao_arch_release_interrupts();
738 ao_usb_putchar(char c)
743 ao_arch_block_interrupts();
746 ao_usb_in_flushed = 0;
747 ao_usb_tx_buffer[ao_usb_tx_count++] = (uint8_t) c;
749 /* Send the packet when full */
750 if (ao_usb_tx_count == AO_USB_IN_SIZE) {
751 _tx_dbg0("putchar full");
753 _tx_dbg0("putchar flushed");
755 ao_arch_release_interrupts();
759 _ao_usb_out_recv(void)
761 _rx_dbg0("out_recv top");
762 ao_usb_out_avail = 0;
764 ao_usb_rx_count = AO_USB_OUT_SIZE - ao_usb_epn_out_count(AO_USB_OUT_EP);
766 _rx_dbg1("out_recv count", ao_usb_rx_count);
767 debug ("recv %d\n", ao_usb_rx_count);
768 debug_data("Fill OUT len %d:", ao_usb_rx_count);
769 memcpy(ao_usb_rx_buffer, ao_usb_out_rx_buffer, ao_usb_rx_count);
774 ao_usb_set_epn_out(AO_USB_OUT_EP, ao_usb_out_rx_buffer, AO_USB_OUT_SIZE);
778 _ao_usb_pollchar(void)
783 return AO_READ_AGAIN;
786 if (ao_usb_rx_pos != ao_usb_rx_count)
789 _rx_dbg0("poll check");
790 /* Check to see if a packet has arrived */
791 if (!ao_usb_out_avail) {
792 _rx_dbg0("poll none");
793 return AO_READ_AGAIN;
798 /* Pull a character out of the fifo */
799 c = ao_usb_rx_buffer[ao_usb_rx_pos++];
808 ao_arch_block_interrupts();
809 while ((c = _ao_usb_pollchar()) == AO_READ_AGAIN)
810 ao_sleep(&ao_stdin_ready);
811 ao_arch_release_interrupts();
818 ao_arch_block_interrupts();
820 /* Disable interrupts */
823 lpc_nvic_clear_enable(LPC_ISR_USB_IRQ_POS);
825 /* Disable the device */
826 lpc_usb.devcmdstat = 0;
828 /* Turn off USB clock */
829 lpc_scb.usbclkdiv = 0;
831 /* Disable USB PHY */
832 lpc_scb.pdruncfg |= (1 << LPC_SCB_PDRUNCFG_USBPAD_PD);
834 /* Disable USB registers and RAM */
835 lpc_scb.sysahbclkctrl &= ~((1 << LPC_SCB_SYSAHBCLKCTRL_USB) |
836 (1 << LPC_SCB_SYSAHBCLKCTRL_USBRAM));
838 ao_arch_release_interrupts();
846 /* Enable USB pins */
848 lpc_ioconf.pio0_6 = ((LPC_IOCONF_FUNC_USB_CONNECT << LPC_IOCONF_FUNC) |
849 (LPC_IOCONF_MODE_INACTIVE << LPC_IOCONF_MODE) |
850 (0 << LPC_IOCONF_HYS) |
851 (0 << LPC_IOCONF_INV) |
852 (0 << LPC_IOCONF_OD) |
856 lpc_ioconf.pio0_3 = ((LPC_IOCONF_FUNC_USB_VBUS << LPC_IOCONF_FUNC) |
857 (LPC_IOCONF_MODE_INACTIVE << LPC_IOCONF_MODE) |
858 (0 << LPC_IOCONF_HYS) |
859 (0 << LPC_IOCONF_INV) |
860 (0 << LPC_IOCONF_OD) |
863 /* Enable USB registers and RAM */
864 lpc_scb.sysahbclkctrl |= ((1 << LPC_SCB_SYSAHBCLKCTRL_USB) |
865 (1 << LPC_SCB_SYSAHBCLKCTRL_USBRAM));
868 lpc_scb.pdruncfg &= ~(1 << LPC_SCB_PDRUNCFG_USBPAD_PD);
870 /* Turn on USB PLL */
871 lpc_scb.pdruncfg &= ~(1 << LPC_SCB_PDRUNCFG_USBPLL_PD);
873 lpc_scb.usbpllclksel = (LPC_SCB_SYSPLLCLKSEL_SEL_SYSOSC << LPC_SCB_SYSPLLCLKSEL_SEL);
874 lpc_scb.usbpllclkuen = (1 << LPC_SCB_USBPLLCLKUEN_ENA);
875 lpc_scb.usbpllclkuen = (0 << LPC_SCB_USBPLLCLKUEN_ENA);
876 lpc_scb.usbpllclkuen = (1 << LPC_SCB_USBPLLCLKUEN_ENA);
877 while (!(lpc_scb.usbpllclkuen & (1 << LPC_SCB_USBPLLCLKUEN_ENA)))
879 lpc_scb.usbpllctrl = 0x23;
880 while (!(lpc_scb.usbpllstat & 1))
883 lpc_scb.usbclksel = 0;
885 /* Turn on USB clock, use 48MHz clock unchanged */
886 lpc_scb.usbclkdiv = 1;
888 /* Configure interrupts */
889 ao_arch_block_interrupts();
891 /* Route all interrupts to the main isr */
892 lpc_usb.introuting = 0;
896 lpc_nvic_set_enable(LPC_ISR_USB_IRQ_POS);
897 lpc_nvic_set_priority(LPC_ISR_USB_IRQ_POS, 0);
899 /* Clear any spurious interrupts */
900 lpc_usb.intstat = 0xffffffff;
902 debug ("ao_usb_enable\n");
904 /* Enable interrupts */
905 lpc_usb.inten = ((1 << LPC_USB_INT_EPOUT(0)) |
906 (1 << LPC_USB_INT_EPIN(0)) |
907 (1 << LPC_USB_INT_EPIN(AO_USB_INT_EP)) |
908 (1 << LPC_USB_INT_EPOUT(AO_USB_OUT_EP)) |
909 (1 << LPC_USB_INT_EPIN(AO_USB_IN_EP)) |
910 (1 << LPC_USB_INT_DEV));
912 ao_arch_release_interrupts();
914 lpc_usb.devcmdstat = 0;
915 for (t = 0; t < 1000; t++)
922 struct ao_task ao_usb_echo_task;
930 c = ao_usb_getchar();
941 printf ("control: %d out: %d in: %d int: %d reset: %d\n",
942 control_count, out_count, in_count, int_count, reset_count);
945 __code struct ao_cmds ao_usb_cmds[] = {
946 { ao_usb_irq, "I\0Show USB interrupt counts" },
956 debug ("ao_usb_init\n");
958 ao_add_task(&ao_usb_echo_task, ao_usb_echo, "usb echo");
961 ao_cmd_register(&ao_usb_cmds[0]);
964 ao_add_stdio(_ao_usb_pollchar, ao_usb_putchar, ao_usb_flush);
990 #define NUM_USB_DBG 8
992 static struct ao_usb_dbg dbg[NUM_USB_DBG];
995 static void _dbg(int line, char *msg, uint32_t value)
998 dbg[dbg_i].line = line;
999 dbg[dbg_i].msg = msg;
1000 dbg[dbg_i].value = value;
1001 asm("mrs %0,primask" : "=&r" (primask));
1002 dbg[dbg_i].primask = primask;
1004 dbg[dbg_i].in_count = in_count;
1005 dbg[dbg_i].in_ep = *ao_usb_epn_in(AO_USB_IN_EP);
1006 dbg[dbg_i].in_pending = ao_usb_in_pending;
1007 dbg[dbg_i].tx_count = ao_usb_tx_count;
1008 dbg[dbg_i].in_flushed = ao_usb_in_flushed;
1011 dbg[dbg_i].rx_count = ao_usb_rx_count;
1012 dbg[dbg_i].rx_pos = ao_usb_rx_pos;
1013 dbg[dbg_i].out_avail = ao_usb_out_avail;
1014 dbg[dbg_i].out_ep = *ao_usb_epn_out(AO_USB_OUT_EP);
1016 if (++dbg_i == NUM_USB_DBG)