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 __attribute__((section(".usb")))
85 static struct lpc_usb_endpoint lpc_usb_endpoint __attribute((aligned(256)));
87 /* Marks when we don't need to send an IN packet.
88 * This happens only when the last IN packet is not full,
89 * otherwise the host will expect to keep seeing packets.
90 * Send a zero-length packet as required
92 static uint8_t ao_usb_in_flushed;
94 /* Marks when we have delivered an IN packet to the hardware
95 * and it has not been received yet. ao_sleep on this address
96 * to wait for it to be delivered.
98 static uint8_t ao_usb_in_pending;
100 /* Marks when an OUT packet has been received by the hardware
101 * but not pulled to the shadow buffer.
103 static uint8_t ao_usb_out_avail;
104 static uint8_t ao_usb_running;
105 static uint8_t ao_usb_configuration;
106 static uint8_t ueienx_0;
108 #define AO_USB_EP0_GOT_RESET 1
109 #define AO_USB_EP0_GOT_SETUP 2
110 #define AO_USB_EP0_GOT_RX_DATA 4
111 #define AO_USB_EP0_GOT_TX_ACK 8
113 static uint8_t ao_usb_ep0_receive;
114 static uint8_t ao_usb_address;
115 static uint8_t ao_usb_address_pending;
117 static inline uint32_t set_toggle(uint32_t current_value,
119 uint32_t desired_value)
121 return (current_value ^ desired_value) & mask;
125 * Set current device address and mark the
126 * interface as active
129 ao_usb_set_address(uint8_t address)
131 debug("ao_usb_set_address %02x\n", address);
132 lpc_usb.devcmdstat = ((address << LPC_USB_DEVCMDSTAT_DEV_ADDR) |
133 (1 << LPC_USB_DEVCMDSTAT_DEV_EN) |
134 (0 << LPC_USB_DEVCMDSTAT_SETUP) |
135 (0 << LPC_USB_DEVCMDSTAT_PLL_ON) |
136 (0 << LPC_USB_DEVCMDSTAT_LPM_SUP) |
137 (0 << LPC_USB_DEVCMDSTAT_INTONNAK_AO) |
138 (0 << LPC_USB_DEVCMDSTAT_INTONNAK_AI) |
139 (0 << LPC_USB_DEVCMDSTAT_INTONNAK_CO) |
140 (0 << LPC_USB_DEVCMDSTAT_INTONNAK_CI) |
141 (1 << LPC_USB_DEVCMDSTAT_DCON) |
142 (0 << LPC_USB_DEVCMDSTAT_DSUS) |
143 (0 << LPC_USB_DEVCMDSTAT_DCON_C) |
144 (0 << LPC_USB_DEVCMDSTAT_DSUS_C) |
145 (0 << LPC_USB_DEVCMDSTAT_DRES_C) |
146 (0 << LPC_USB_DEVCMDSTAT_VBUSDEBOUNCED));
147 ao_usb_address_pending = 0;
154 #define _tx_dbg0(msg) _dbg(__LINE__,msg,0)
155 #define _tx_dbg1(msg,value) _dbg(__LINE__,msg,value)
157 #define _tx_dbg0(msg)
158 #define _tx_dbg1(msg,value)
162 #define _rx_dbg0(msg) _dbg(__LINE__,msg,0)
163 #define _rx_dbg1(msg,value) _dbg(__LINE__,msg,value)
165 #define _rx_dbg0(msg)
166 #define _rx_dbg1(msg,value)
170 static void _dbg(int line, char *msg, uint32_t value);
174 * Set just endpoint 0, for use during startup
178 ao_usb_alloc_sram(uint16_t size)
180 uint8_t *addr = ao_usb_sram;
182 ao_usb_sram += (size + 63) & ~63;
187 ao_usb_sram_offset(uint8_t *addr)
189 return (uint16_t) ((intptr_t) addr >> 6);
193 ao_usb_set_ep(vuint32_t *ep, uint8_t *addr, uint16_t nbytes)
195 *ep = ((ao_usb_sram_offset(addr) << LPC_USB_EP_OFFSET) |
196 (nbytes << LPC_USB_EP_NBYTES) |
197 (0 << LPC_USB_EP_ENDPOINT_ISO) |
198 (0 << LPC_USB_EP_RATE_FEEDBACK) |
199 (0 << LPC_USB_EP_TOGGLE_RESET) |
200 (0 << LPC_USB_EP_STALL) |
201 (0 << LPC_USB_EP_DISABLED) |
202 (1 << LPC_USB_EP_ACTIVE));
205 static inline uint16_t
206 ao_usb_ep_count(vuint32_t *ep)
208 return (*ep >> LPC_USB_EP_NBYTES) & LPC_USB_EP_NBYTES_MASK;
211 static inline uint8_t
212 ao_usb_ep_stall(vuint32_t *ep)
214 return (*ep >> LPC_USB_EP_STALL) & 1;
217 static inline vuint32_t *
220 return &lpc_usb_endpoint.ep0_out;
223 static inline vuint32_t *
226 return &lpc_usb_endpoint.ep0_in;
229 static inline vuint32_t *
230 ao_usb_epn_out(uint8_t n)
232 return &lpc_usb_endpoint.epn[n-1].out[0];
235 static inline vuint32_t *
236 ao_usb_epn_in(uint8_t n)
238 return &lpc_usb_endpoint.epn[n-1].in[0];
242 ao_usb_set_epn_in(uint8_t n, uint8_t *addr, uint16_t nbytes)
244 ao_usb_set_ep(ao_usb_epn_in(n), addr, nbytes);
248 ao_usb_set_epn_out(uint8_t n, uint8_t *addr, uint16_t nbytes)
250 ao_usb_set_ep(ao_usb_epn_out(n), addr, nbytes);
253 static inline uint16_t
254 ao_usb_epn_out_count(uint8_t n)
256 return ao_usb_ep_count(ao_usb_epn_out(n));
259 static inline uint16_t
260 ao_usb_epn_in_count(uint8_t n)
262 return ao_usb_ep_count(ao_usb_epn_in(n));
266 ao_usb_enable_ep(vuint32_t *ep, uint16_t nbytes, uint16_t set_nbytes)
268 uint8_t *addr = ao_usb_alloc_sram(nbytes);
270 ao_usb_set_ep(ep, addr, set_nbytes);
275 ao_usb_disable_ep(vuint32_t *ep)
277 *ep = ((0 << LPC_USB_EP_OFFSET) |
278 (0 << LPC_USB_EP_NBYTES) |
279 (0 << LPC_USB_EP_ENDPOINT_ISO) |
280 (0 << LPC_USB_EP_RATE_FEEDBACK) |
281 (0 << LPC_USB_EP_TOGGLE_RESET) |
282 (0 << LPC_USB_EP_STALL) |
283 (1 << LPC_USB_EP_DISABLED) |
284 (0 << LPC_USB_EP_ACTIVE));
288 ao_usb_enable_epn(uint8_t n, uint16_t out_bytes, uint8_t **out_addr, uint16_t in_bytes, uint8_t **in_addr)
292 addr = ao_usb_enable_ep(ao_usb_epn_out(n), out_bytes, out_bytes);
295 ao_usb_disable_ep(&lpc_usb_endpoint.epn[n-1].out[1]);
297 addr = ao_usb_enable_ep(ao_usb_epn_in(n), in_bytes, 0);
300 ao_usb_disable_ep(&lpc_usb_endpoint.epn[n-1].in[1]);
304 ao_usb_disable_epn(uint8_t n)
306 ao_usb_disable_ep(ao_usb_epn_out(n));
307 ao_usb_disable_ep(&lpc_usb_endpoint.epn[n-1].out[1]);
308 ao_usb_disable_ep(ao_usb_epn_in(n));
309 ao_usb_disable_ep(&lpc_usb_endpoint.epn[n-1].in[1]);
317 /* Everything is single buffered for now */
318 lpc_usb.epbufcfg = 0;
320 lpc_usb.epskip = 0xffffffff;
322 ao_usb_set_address(0);
323 lpc_usb.intstat = 0xc00003ff;
325 ao_usb_configuration = 0;
327 ao_usb_sram = lpc_usb_sram;
329 lpc_usb.epliststart = (uint32_t) (intptr_t) &lpc_usb_endpoint;
330 lpc_usb.databufstart = ((uint32_t) (intptr_t) ao_usb_sram) & 0xffc00000;
332 /* Set up EP 0 - a Control end point with 32 bytes of in and out buffers */
334 ao_usb_ep0_rx_buffer = ao_usb_enable_ep(ao_usb_ep0_out(), AO_USB_CONTROL_SIZE, AO_USB_CONTROL_SIZE);
335 ao_usb_ep0_setup_buffer = ao_usb_alloc_sram(AO_USB_CONTROL_SIZE);
336 lpc_usb_endpoint.setup = ao_usb_sram_offset(ao_usb_ep0_setup_buffer);
337 ao_usb_ep0_tx_buffer = ao_usb_enable_ep(ao_usb_ep0_in(), AO_USB_CONTROL_SIZE, 0);
339 /* Clear all of the other endpoints */
340 for (e = 1; e <= 4; e++)
341 ao_usb_disable_epn(e);
346 ao_usb_set_configuration(void)
348 debug ("ao_usb_set_configuration\n");
350 /* Set up the INT end point */
351 ao_usb_enable_epn(AO_USB_INT_EP, 0, NULL, 0, NULL);
353 /* Set up the OUT end point */
354 ao_usb_enable_epn(AO_USB_OUT_EP, AO_USB_OUT_SIZE, &ao_usb_out_rx_buffer, 0, NULL);
356 /* Set up the IN end point */
357 ao_usb_enable_epn(AO_USB_IN_EP, 0, NULL, AO_USB_IN_SIZE, &ao_usb_in_tx_buffer);
362 /* Send an IN data packet */
364 ao_usb_ep0_flush(void)
368 this_len = ao_usb_ep0_in_len;
369 if (this_len > AO_USB_CONTROL_SIZE)
370 this_len = AO_USB_CONTROL_SIZE;
372 if (this_len < AO_USB_CONTROL_SIZE)
373 ao_usb_ep0_state = AO_USB_EP0_IDLE;
375 ao_usb_ep0_in_len -= this_len;
377 debug_data ("Flush EP0 len %d:", this_len);
378 memcpy(ao_usb_ep0_tx_buffer, ao_usb_ep0_in_data, this_len);
380 ao_usb_ep0_in_data += this_len;
382 /* Mark the endpoint as TX valid to send the packet */
383 ao_usb_set_ep(ao_usb_ep0_in(), ao_usb_ep0_tx_buffer, this_len);
384 debug ("queue tx. 0 now %08x\n", *ao_usb_ep0_in());
387 /* Read data from the ep0 OUT fifo */
389 ao_usb_ep0_fill(void)
394 /* Pull all of the data out of the packet */
395 if (lpc_usb.devcmdstat & (1 << LPC_USB_DEVCMDSTAT_SETUP)) {
396 rx_buffer = ao_usb_ep0_setup_buffer;
399 rx_buffer = ao_usb_ep0_rx_buffer;
400 len = AO_USB_CONTROL_SIZE - ao_usb_ep_count(ao_usb_ep0_out());
403 if (len > ao_usb_ep0_out_len)
404 len = ao_usb_ep0_out_len;
405 ao_usb_ep0_out_len -= len;
407 debug_data ("Fill EP0 len %d:", len);
408 memcpy(ao_usb_ep0_out_data, rx_buffer, len);
410 ao_usb_ep0_out_data += len;
413 ao_usb_set_ep(ao_usb_ep0_out(), ao_usb_ep0_rx_buffer, AO_USB_CONTROL_SIZE);
414 lpc_usb.devcmdstat |= (1 << LPC_USB_DEVCMDSTAT_SETUP);
418 ao_usb_ep0_in_reset(void)
420 ao_usb_ep0_in_data = ao_usb_ep0_in_buf;
421 ao_usb_ep0_in_len = 0;
425 ao_usb_ep0_in_queue_byte(uint8_t a)
427 if (ao_usb_ep0_in_len < sizeof (ao_usb_ep0_in_buf))
428 ao_usb_ep0_in_buf[ao_usb_ep0_in_len++] = a;
432 ao_usb_ep0_in_set(const uint8_t *data, uint8_t len)
434 ao_usb_ep0_in_data = data;
435 ao_usb_ep0_in_len = len;
439 ao_usb_ep0_out_set(uint8_t *data, uint8_t len)
441 ao_usb_ep0_out_data = data;
442 ao_usb_ep0_out_len = len;
446 ao_usb_ep0_in_start(uint8_t max)
448 /* Don't send more than asked for */
449 if (ao_usb_ep0_in_len > max)
450 ao_usb_ep0_in_len = max;
454 static struct ao_usb_line_coding ao_usb_line_coding = {115200, 0, 0, 8};
456 /* Walk through the list of descriptors and find a match
459 ao_usb_get_descriptor(uint16_t value)
461 const uint8_t *descriptor;
462 uint8_t type = value >> 8;
463 uint8_t index = value;
465 descriptor = ao_usb_descriptors;
466 while (descriptor[0] != 0) {
467 if (descriptor[1] == type && index-- == 0) {
469 if (type == AO_USB_DESC_CONFIGURATION)
473 ao_usb_ep0_in_set(descriptor, len);
476 descriptor += descriptor[0];
481 ao_usb_ep0_setup(void)
483 /* Pull the setup packet out of the fifo */
484 ao_usb_ep0_out_set((uint8_t *) &ao_usb_setup, 8);
486 if (ao_usb_ep0_out_len != 0) {
487 debug ("invalid setup packet length\n");
491 if ((ao_usb_setup.dir_type_recip & AO_USB_DIR_IN) || ao_usb_setup.length == 0)
492 ao_usb_ep0_state = AO_USB_EP0_DATA_IN;
494 ao_usb_ep0_state = AO_USB_EP0_DATA_OUT;
496 ao_usb_ep0_in_reset();
498 switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_TYPE_MASK) {
499 case AO_USB_TYPE_STANDARD:
500 debug ("Standard setup packet\n");
501 switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_RECIP_MASK) {
502 case AO_USB_RECIP_DEVICE:
503 debug ("Device setup packet\n");
504 switch(ao_usb_setup.request) {
505 case AO_USB_REQ_GET_STATUS:
506 debug ("get status\n");
507 ao_usb_ep0_in_queue_byte(0);
508 ao_usb_ep0_in_queue_byte(0);
510 case AO_USB_REQ_SET_ADDRESS:
511 debug ("set address %d\n", ao_usb_setup.value);
512 ao_usb_address = ao_usb_setup.value;
513 ao_usb_address_pending = 1;
515 case AO_USB_REQ_GET_DESCRIPTOR:
516 debug ("get descriptor %d\n", ao_usb_setup.value);
517 ao_usb_get_descriptor(ao_usb_setup.value);
519 case AO_USB_REQ_GET_CONFIGURATION:
520 debug ("get configuration %d\n", ao_usb_configuration);
521 ao_usb_ep0_in_queue_byte(ao_usb_configuration);
523 case AO_USB_REQ_SET_CONFIGURATION:
524 ao_usb_configuration = ao_usb_setup.value;
525 debug ("set configuration %d\n", ao_usb_configuration);
526 ao_usb_set_configuration();
530 case AO_USB_RECIP_INTERFACE:
531 debug ("Interface setup packet\n");
532 switch(ao_usb_setup.request) {
533 case AO_USB_REQ_GET_STATUS:
534 ao_usb_ep0_in_queue_byte(0);
535 ao_usb_ep0_in_queue_byte(0);
537 case AO_USB_REQ_GET_INTERFACE:
538 ao_usb_ep0_in_queue_byte(0);
540 case AO_USB_REQ_SET_INTERFACE:
544 case AO_USB_RECIP_ENDPOINT:
545 debug ("Endpoint setup packet\n");
546 switch(ao_usb_setup.request) {
547 case AO_USB_REQ_GET_STATUS:
548 ao_usb_ep0_in_queue_byte(0);
549 ao_usb_ep0_in_queue_byte(0);
555 case AO_USB_TYPE_CLASS:
556 debug ("Class setup packet\n");
557 switch (ao_usb_setup.request) {
558 case AO_USB_SET_LINE_CODING:
559 debug ("set line coding\n");
560 ao_usb_ep0_out_set((uint8_t *) &ao_usb_line_coding, 7);
562 case AO_USB_GET_LINE_CODING:
563 debug ("get line coding\n");
564 ao_usb_ep0_in_set((const uint8_t *) &ao_usb_line_coding, 7);
566 case AO_USB_SET_CONTROL_LINE_STATE:
572 /* If we're not waiting to receive data from the host,
573 * queue an IN response
575 if (ao_usb_ep0_state == AO_USB_EP0_DATA_IN)
576 ao_usb_ep0_in_start(ao_usb_setup.length);
580 ao_usb_ep0_handle(uint8_t receive)
582 ao_usb_ep0_receive = 0;
584 if (receive & AO_USB_EP0_GOT_RESET) {
589 if (receive & AO_USB_EP0_GOT_SETUP) {
593 if (receive & AO_USB_EP0_GOT_RX_DATA) {
594 debug ("\tgot rx data\n");
595 if (ao_usb_ep0_state == AO_USB_EP0_DATA_OUT) {
597 if (ao_usb_ep0_out_len == 0) {
598 ao_usb_ep0_state = AO_USB_EP0_DATA_IN;
599 ao_usb_ep0_in_start(0);
603 if (receive & AO_USB_EP0_GOT_TX_ACK) {
604 debug ("\tgot tx ack\n");
606 /* Wait until the IN packet is received from addr 0
607 * before assigning our local address
609 if (ao_usb_address_pending)
610 ao_usb_set_address(ao_usb_address);
611 if (ao_usb_ep0_state == AO_USB_EP0_DATA_IN)
616 static uint16_t control_count;
617 static uint16_t int_count;
618 static uint16_t in_count;
619 static uint16_t out_count;
620 static uint16_t reset_count;
623 lpc_usb_irq_isr(void)
625 uint32_t intstat = lpc_usb.intstat & lpc_usb.inten;
627 lpc_usb.intstat = intstat;
628 /* Handle EP0 OUT packets */
629 if (intstat & (1 << LPC_USB_INT_EPOUT(0))) {
630 if (lpc_usb.devcmdstat & (1 << LPC_USB_DEVCMDSTAT_SETUP))
631 ao_usb_ep0_receive |= AO_USB_EP0_GOT_SETUP;
633 ao_usb_ep0_receive |= AO_USB_EP0_GOT_RX_DATA;
635 ao_usb_ep0_handle(ao_usb_ep0_receive);
638 /* Handle EP0 IN packets */
639 if (intstat & (1 << LPC_USB_INT_EPIN(0))) {
640 ao_usb_ep0_receive |= AO_USB_EP0_GOT_TX_ACK;
642 ao_usb_ep0_handle(ao_usb_ep0_receive);
646 /* Handle OUT packets */
647 if (intstat & (1 << LPC_USB_INT_EPOUT(AO_USB_OUT_EP))) {
649 _rx_dbg1("RX ISR", *ao_usb_epn_out(AO_USB_OUT_EP));
650 ao_usb_out_avail = 1;
651 _rx_dbg0("out avail set");
652 ao_wakeup(&ao_stdin_ready);
653 _rx_dbg0("stdin awoken");
656 /* Handle IN packets */
657 if (intstat & (1 << LPC_USB_INT_EPIN(AO_USB_IN_EP))) {
659 _tx_dbg1("TX ISR", *ao_usb_epn_in(AO_USB_IN_EP));
660 ao_usb_in_pending = 0;
661 ao_wakeup(&ao_usb_in_pending);
664 /* NAK all INT EP IN packets */
665 if (intstat & (1 << LPC_USB_INT_EPIN(AO_USB_INT_EP))) {
669 /* Check for reset */
670 if (intstat & (1 << LPC_USB_INT_DEV)) {
671 if (lpc_usb.devcmdstat & (1 << LPC_USB_DEVCMDSTAT_DRES_C))
673 lpc_usb.devcmdstat |= (1 << LPC_USB_DEVCMDSTAT_DRES_C);
674 ao_usb_ep0_receive |= AO_USB_EP0_GOT_RESET;
675 ao_usb_ep0_handle(ao_usb_ep0_receive);
682 /* Queue the current IN buffer for transmission */
684 _ao_usb_in_send(void)
686 _tx_dbg0("in_send start");
687 debug ("send %d\n", ao_usb_tx_count);
688 while (ao_usb_in_pending)
689 ao_sleep(&ao_usb_in_pending);
690 ao_usb_in_pending = 1;
691 if (ao_usb_tx_count != AO_USB_IN_SIZE)
692 ao_usb_in_flushed = 1;
693 memcpy(ao_usb_in_tx_buffer, ao_usb_tx_buffer, ao_usb_tx_count);
694 ao_usb_set_ep(ao_usb_epn_in(AO_USB_IN_EP), ao_usb_in_tx_buffer, ao_usb_tx_count);
696 _tx_dbg0("in_send end");
699 /* Wait for a free IN buffer. Interrupts are blocked */
701 _ao_usb_in_wait(void)
704 /* Check if the current buffer is writable */
705 if (ao_usb_tx_count < AO_USB_IN_SIZE)
708 _tx_dbg0("in_wait top");
709 /* Wait for an IN buffer to be ready */
710 while (ao_usb_in_pending)
711 ao_sleep(&ao_usb_in_pending);
712 _tx_dbg0("in_wait bottom");
722 /* Anytime we've sent a character since
723 * the last time we flushed, we'll need
724 * to send a packet -- the only other time
725 * we would send a packet is when that
726 * packet was full, in which case we now
727 * want to send an empty packet
729 ao_arch_block_interrupts();
730 while (!ao_usb_in_flushed) {
731 _tx_dbg0("flush top");
733 _tx_dbg0("flush end");
735 ao_arch_release_interrupts();
739 ao_usb_putchar(char c)
744 ao_arch_block_interrupts();
747 ao_usb_in_flushed = 0;
748 ao_usb_tx_buffer[ao_usb_tx_count++] = (uint8_t) c;
750 /* Send the packet when full */
751 if (ao_usb_tx_count == AO_USB_IN_SIZE) {
752 _tx_dbg0("putchar full");
754 _tx_dbg0("putchar flushed");
756 ao_arch_release_interrupts();
760 _ao_usb_out_recv(void)
762 _rx_dbg0("out_recv top");
763 ao_usb_out_avail = 0;
765 ao_usb_rx_count = AO_USB_OUT_SIZE - ao_usb_epn_out_count(AO_USB_OUT_EP);
767 _rx_dbg1("out_recv count", ao_usb_rx_count);
768 debug ("recv %d\n", ao_usb_rx_count);
769 debug_data("Fill OUT len %d:", ao_usb_rx_count);
770 memcpy(ao_usb_rx_buffer, ao_usb_out_rx_buffer, ao_usb_rx_count);
775 ao_usb_set_epn_out(AO_USB_OUT_EP, ao_usb_out_rx_buffer, AO_USB_OUT_SIZE);
779 _ao_usb_pollchar(void)
784 return AO_READ_AGAIN;
787 if (ao_usb_rx_pos != ao_usb_rx_count)
790 _rx_dbg0("poll check");
791 /* Check to see if a packet has arrived */
792 if (!ao_usb_out_avail) {
793 _rx_dbg0("poll none");
794 return AO_READ_AGAIN;
799 /* Pull a character out of the fifo */
800 c = ao_usb_rx_buffer[ao_usb_rx_pos++];
809 ao_arch_block_interrupts();
810 while ((c = _ao_usb_pollchar()) == AO_READ_AGAIN)
811 ao_sleep(&ao_stdin_ready);
812 ao_arch_release_interrupts();
819 ao_arch_block_interrupts();
821 /* Disable interrupts */
824 lpc_nvic_clear_enable(LPC_ISR_USB_IRQ_POS);
826 /* Disable the device */
827 lpc_usb.devcmdstat = 0;
829 /* Turn off USB clock */
830 lpc_scb.usbclkdiv = 0;
832 /* Disable USB PHY */
833 lpc_scb.pdruncfg |= (1 << LPC_SCB_PDRUNCFG_USBPAD_PD);
835 /* Disable USB registers and RAM */
836 lpc_scb.sysahbclkctrl &= ~((1 << LPC_SCB_SYSAHBCLKCTRL_USB) |
837 (1 << LPC_SCB_SYSAHBCLKCTRL_USBRAM));
839 ao_arch_release_interrupts();
847 /* Enable USB pins */
849 lpc_ioconf.pio0_6 = ((LPC_IOCONF_FUNC_USB_CONNECT << LPC_IOCONF_FUNC) |
850 (LPC_IOCONF_MODE_INACTIVE << LPC_IOCONF_MODE) |
851 (0 << LPC_IOCONF_HYS) |
852 (0 << LPC_IOCONF_INV) |
853 (0 << LPC_IOCONF_OD) |
857 lpc_ioconf.pio0_3 = ((LPC_IOCONF_FUNC_USB_VBUS << LPC_IOCONF_FUNC) |
858 (LPC_IOCONF_MODE_INACTIVE << LPC_IOCONF_MODE) |
859 (0 << LPC_IOCONF_HYS) |
860 (0 << LPC_IOCONF_INV) |
861 (0 << LPC_IOCONF_OD) |
864 /* Enable USB registers and RAM */
865 lpc_scb.sysahbclkctrl |= ((1 << LPC_SCB_SYSAHBCLKCTRL_USB) |
866 (1 << LPC_SCB_SYSAHBCLKCTRL_USBRAM));
869 lpc_scb.pdruncfg &= ~(1 << LPC_SCB_PDRUNCFG_USBPAD_PD);
871 /* Turn on USB PLL */
872 lpc_scb.pdruncfg &= ~(1 << LPC_SCB_PDRUNCFG_USBPLL_PD);
874 lpc_scb.usbpllclksel = (LPC_SCB_SYSPLLCLKSEL_SEL_SYSOSC << LPC_SCB_SYSPLLCLKSEL_SEL);
875 lpc_scb.usbpllclkuen = (1 << LPC_SCB_USBPLLCLKUEN_ENA);
876 lpc_scb.usbpllclkuen = (0 << LPC_SCB_USBPLLCLKUEN_ENA);
877 lpc_scb.usbpllclkuen = (1 << LPC_SCB_USBPLLCLKUEN_ENA);
878 while (!(lpc_scb.usbpllclkuen & (1 << LPC_SCB_USBPLLCLKUEN_ENA)))
880 lpc_scb.usbpllctrl = 0x23;
881 while (!(lpc_scb.usbpllstat & 1))
884 lpc_scb.usbclksel = 0;
886 /* Turn on USB clock, use 48MHz clock unchanged */
887 lpc_scb.usbclkdiv = 1;
889 /* Configure interrupts */
890 ao_arch_block_interrupts();
892 /* Route all interrupts to the main isr */
893 lpc_usb.introuting = 0;
897 lpc_nvic_set_enable(LPC_ISR_USB_IRQ_POS);
898 lpc_nvic_set_priority(LPC_ISR_USB_IRQ_POS, 0);
900 /* Clear any spurious interrupts */
901 lpc_usb.intstat = 0xffffffff;
903 debug ("ao_usb_enable\n");
905 /* Enable interrupts */
906 lpc_usb.inten = ((1 << LPC_USB_INT_EPOUT(0)) |
907 (1 << LPC_USB_INT_EPIN(0)) |
908 (1 << LPC_USB_INT_EPIN(AO_USB_INT_EP)) |
909 (1 << LPC_USB_INT_EPOUT(AO_USB_OUT_EP)) |
910 (1 << LPC_USB_INT_EPIN(AO_USB_IN_EP)) |
911 (1 << LPC_USB_INT_DEV));
913 ao_arch_release_interrupts();
915 lpc_usb.devcmdstat = 0;
916 for (t = 0; t < 1000; t++)
923 struct ao_task ao_usb_echo_task;
931 c = ao_usb_getchar();
942 printf ("control: %d out: %d in: %d int: %d reset: %d\n",
943 control_count, out_count, in_count, int_count, reset_count);
946 __code struct ao_cmds ao_usb_cmds[] = {
947 { ao_usb_irq, "I\0Show USB interrupt counts" },
957 debug ("ao_usb_init\n");
959 ao_add_task(&ao_usb_echo_task, ao_usb_echo, "usb echo");
962 ao_cmd_register(&ao_usb_cmds[0]);
965 ao_add_stdio(_ao_usb_pollchar, ao_usb_putchar, ao_usb_flush);
991 #define NUM_USB_DBG 8
993 static struct ao_usb_dbg dbg[NUM_USB_DBG];
996 static void _dbg(int line, char *msg, uint32_t value)
999 dbg[dbg_i].line = line;
1000 dbg[dbg_i].msg = msg;
1001 dbg[dbg_i].value = value;
1002 asm("mrs %0,primask" : "=&r" (primask));
1003 dbg[dbg_i].primask = primask;
1005 dbg[dbg_i].in_count = in_count;
1006 dbg[dbg_i].in_ep = *ao_usb_epn_in(AO_USB_IN_EP);
1007 dbg[dbg_i].in_pending = ao_usb_in_pending;
1008 dbg[dbg_i].tx_count = ao_usb_tx_count;
1009 dbg[dbg_i].in_flushed = ao_usb_in_flushed;
1012 dbg[dbg_i].rx_count = ao_usb_rx_count;
1013 dbg[dbg_i].rx_pos = ao_usb_rx_pos;
1014 dbg[dbg_i].out_avail = ao_usb_out_avail;
1015 dbg[dbg_i].out_ep = *ao_usb_epn_out(AO_USB_OUT_EP);
1017 if (++dbg_i == NUM_USB_DBG)