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...)
39 uint8_t dir_type_recip;
46 static uint8_t ao_usb_ep0_state;
48 /* Pending EP0 IN data */
49 static const uint8_t *ao_usb_ep0_in_data; /* Remaining data */
50 static uint8_t ao_usb_ep0_in_len; /* Remaining amount */
52 /* Temp buffer for smaller EP0 in data */
53 static uint8_t ao_usb_ep0_in_buf[2];
55 /* Pending EP0 OUT data */
56 static uint8_t *ao_usb_ep0_out_data;
57 static uint8_t ao_usb_ep0_out_len;
60 * Objects allocated in special USB memory
63 /* USB address of end of allocated storage */
64 static uint8_t *ao_usb_sram;
66 /* Pointer to ep0 tx/rx buffers in USB memory */
67 static uint8_t *ao_usb_ep0_tx_buffer;
68 static uint8_t *ao_usb_ep0_setup_buffer;
69 static uint8_t *ao_usb_ep0_rx_buffer;
71 /* Pointer to bulk data tx/rx buffers in USB memory */
72 static uint8_t *ao_usb_in_tx_buffer;
73 static uint8_t *ao_usb_out_rx_buffer;
75 /* Our data buffers */
76 static uint8_t ao_usb_tx_buffer[AO_USB_IN_SIZE];
77 static uint8_t ao_usb_tx_count;
79 static uint8_t ao_usb_rx_buffer[AO_USB_OUT_SIZE];
80 static uint8_t ao_usb_rx_count, ao_usb_rx_pos;
82 extern struct lpc_usb_endpoint lpc_usb_endpoint;
84 /* Marks when we don't need to send an IN packet.
85 * This happens only when the last IN packet is not full,
86 * otherwise the host will expect to keep seeing packets.
87 * Send a zero-length packet as required
89 static uint8_t ao_usb_in_flushed;
91 /* Marks when we have delivered an IN packet to the hardware
92 * and it has not been received yet. ao_sleep on this address
93 * to wait for it to be delivered.
95 static uint8_t ao_usb_in_pending;
97 /* Marks when an OUT packet has been received by the hardware
98 * but not pulled to the shadow buffer.
100 static uint8_t ao_usb_out_avail;
101 static uint8_t ao_usb_running;
102 static uint8_t ao_usb_configuration;
103 static uint8_t ueienx_0;
105 #define AO_USB_EP0_GOT_RESET 1
106 #define AO_USB_EP0_GOT_SETUP 2
107 #define AO_USB_EP0_GOT_RX_DATA 4
108 #define AO_USB_EP0_GOT_TX_ACK 8
110 static uint8_t ao_usb_ep0_receive;
111 static uint8_t ao_usb_address;
112 static uint8_t ao_usb_address_pending;
114 static inline uint32_t set_toggle(uint32_t current_value,
116 uint32_t desired_value)
118 return (current_value ^ desired_value) & mask;
122 * Set current device address and mark the
123 * interface as active
126 ao_usb_set_address(uint8_t address)
128 debug("ao_usb_set_address %02x\n", address);
129 lpc_usb.devcmdstat = ((address << LPC_USB_DEVCMDSTAT_DEV_ADDR) |
130 (1 << LPC_USB_DEVCMDSTAT_DEV_EN) |
131 (0 << LPC_USB_DEVCMDSTAT_SETUP) |
132 (0 << LPC_USB_DEVCMDSTAT_PLL_ON) |
133 (0 << LPC_USB_DEVCMDSTAT_LPM_SUP) |
134 (0 << LPC_USB_DEVCMDSTAT_INTONNAK_AO) |
135 (0 << LPC_USB_DEVCMDSTAT_INTONNAK_AI) |
136 (0 << LPC_USB_DEVCMDSTAT_INTONNAK_CO) |
137 (0 << LPC_USB_DEVCMDSTAT_INTONNAK_CI) |
138 (1 << LPC_USB_DEVCMDSTAT_DCON) |
139 (0 << LPC_USB_DEVCMDSTAT_DSUS) |
140 (0 << LPC_USB_DEVCMDSTAT_DCON_C) |
141 (0 << LPC_USB_DEVCMDSTAT_DSUS_C) |
142 (0 << LPC_USB_DEVCMDSTAT_DRES_C) |
143 (0 << LPC_USB_DEVCMDSTAT_VBUSDEBOUNCED));
144 ao_usb_address_pending = 0;
151 #define _tx_dbg0(msg) _dbg(__LINE__,msg,0)
152 #define _tx_dbg1(msg,value) _dbg(__LINE__,msg,value)
154 #define _tx_dbg0(msg)
155 #define _tx_dbg1(msg,value)
159 #define _rx_dbg0(msg) _dbg(__LINE__,msg,0)
160 #define _rx_dbg1(msg,value) _dbg(__LINE__,msg,value)
162 #define _rx_dbg0(msg)
163 #define _rx_dbg1(msg,value)
167 static void _dbg(int line, char *msg, uint32_t value);
171 * Set just endpoint 0, for use during startup
175 ao_usb_alloc_sram(uint16_t size)
177 uint8_t *addr = ao_usb_sram;
179 ao_usb_sram += (size + 63) & ~63;
184 ao_usb_sram_offset(uint8_t *addr)
186 return (uint16_t) ((intptr_t) addr >> 6);
190 ao_usb_set_ep(vuint32_t *ep, uint8_t *addr, uint16_t nbytes)
192 *ep = ((ao_usb_sram_offset(addr) << LPC_USB_EP_OFFSET) |
193 (nbytes << LPC_USB_EP_NBYTES) |
194 (0 << LPC_USB_EP_ENDPOINT_ISO) |
195 (0 << LPC_USB_EP_RATE_FEEDBACK) |
196 (0 << LPC_USB_EP_TOGGLE_RESET) |
197 (0 << LPC_USB_EP_STALL) |
198 (0 << LPC_USB_EP_DISABLED) |
199 (1 << LPC_USB_EP_ACTIVE));
202 static inline uint16_t
203 ao_usb_ep_count(vuint32_t *ep)
205 return (*ep >> LPC_USB_EP_NBYTES) & LPC_USB_EP_NBYTES_MASK;
208 static inline uint8_t
209 ao_usb_ep_stall(vuint32_t *ep)
211 return (*ep >> LPC_USB_EP_STALL) & 1;
214 static inline vuint32_t *
217 return &lpc_usb_endpoint.ep0_out;
220 static inline vuint32_t *
223 return &lpc_usb_endpoint.ep0_in;
226 static inline vuint32_t *
227 ao_usb_epn_out(uint8_t n)
229 return &lpc_usb_endpoint.epn[n-1].out[0];
232 static inline vuint32_t *
233 ao_usb_epn_in(uint8_t n)
235 return &lpc_usb_endpoint.epn[n-1].in[0];
239 ao_usb_set_epn_in(uint8_t n, uint8_t *addr, uint16_t nbytes)
241 ao_usb_set_ep(ao_usb_epn_in(n), addr, nbytes);
245 ao_usb_set_epn_out(uint8_t n, uint8_t *addr, uint16_t nbytes)
247 ao_usb_set_ep(ao_usb_epn_out(n), addr, nbytes);
250 static inline uint16_t
251 ao_usb_epn_out_count(uint8_t n)
253 return ao_usb_ep_count(ao_usb_epn_out(n));
256 static inline uint16_t
257 ao_usb_epn_in_count(uint8_t n)
259 return ao_usb_ep_count(ao_usb_epn_in(n));
263 ao_usb_enable_ep(vuint32_t *ep, uint16_t nbytes, uint16_t set_nbytes)
265 uint8_t *addr = ao_usb_alloc_sram(nbytes);
267 ao_usb_set_ep(ep, addr, set_nbytes);
272 ao_usb_disable_ep(vuint32_t *ep)
274 *ep = ((0 << LPC_USB_EP_OFFSET) |
275 (0 << LPC_USB_EP_NBYTES) |
276 (0 << LPC_USB_EP_ENDPOINT_ISO) |
277 (0 << LPC_USB_EP_RATE_FEEDBACK) |
278 (0 << LPC_USB_EP_TOGGLE_RESET) |
279 (0 << LPC_USB_EP_STALL) |
280 (1 << LPC_USB_EP_DISABLED) |
281 (0 << LPC_USB_EP_ACTIVE));
285 ao_usb_enable_epn(uint8_t n, uint16_t out_bytes, uint8_t **out_addr, uint16_t in_bytes, uint8_t **in_addr)
289 addr = ao_usb_enable_ep(ao_usb_epn_out(n), out_bytes, out_bytes);
292 ao_usb_disable_ep(&lpc_usb_endpoint.epn[n-1].out[1]);
294 addr = ao_usb_enable_ep(ao_usb_epn_in(n), in_bytes, 0);
297 ao_usb_disable_ep(&lpc_usb_endpoint.epn[n-1].in[1]);
301 ao_usb_disable_epn(uint8_t n)
303 ao_usb_disable_ep(ao_usb_epn_out(n));
304 ao_usb_disable_ep(&lpc_usb_endpoint.epn[n-1].out[1]);
305 ao_usb_disable_ep(ao_usb_epn_in(n));
306 ao_usb_disable_ep(&lpc_usb_endpoint.epn[n-1].in[1]);
314 /* Everything is single buffered for now */
315 lpc_usb.epbufcfg = 0;
317 lpc_usb.epskip = 0xffffffff;
319 ao_usb_set_address(0);
320 lpc_usb.intstat = 0xc00003ff;
322 ao_usb_configuration = 0;
324 ao_usb_sram = lpc_usb_sram;
326 lpc_usb.epliststart = (uint32_t) (intptr_t) &lpc_usb_endpoint;
327 lpc_usb.databufstart = ((uint32_t) (intptr_t) ao_usb_sram) & 0xffc00000;
329 /* Set up EP 0 - a Control end point with 32 bytes of in and out buffers */
331 ao_usb_ep0_rx_buffer = ao_usb_enable_ep(ao_usb_ep0_out(), AO_USB_CONTROL_SIZE, AO_USB_CONTROL_SIZE);
332 ao_usb_ep0_setup_buffer = ao_usb_alloc_sram(AO_USB_CONTROL_SIZE);
333 lpc_usb_endpoint.setup = ao_usb_sram_offset(ao_usb_ep0_setup_buffer);
334 ao_usb_ep0_tx_buffer = ao_usb_enable_ep(ao_usb_ep0_in(), AO_USB_CONTROL_SIZE, 0);
336 /* Clear all of the other endpoints */
337 for (e = 1; e <= 4; e++)
338 ao_usb_disable_epn(e);
343 ao_usb_set_configuration(void)
345 debug ("ao_usb_set_configuration\n");
347 /* Set up the INT end point */
348 ao_usb_enable_epn(AO_USB_INT_EP, 0, NULL, 0, NULL);
350 /* Set up the OUT end point */
351 ao_usb_enable_epn(AO_USB_OUT_EP, AO_USB_OUT_SIZE, &ao_usb_out_rx_buffer, 0, NULL);
353 /* Set up the IN end point */
354 ao_usb_enable_epn(AO_USB_IN_EP, 0, NULL, AO_USB_IN_SIZE, &ao_usb_in_tx_buffer);
359 /* Send an IN data packet */
361 ao_usb_ep0_flush(void)
365 this_len = ao_usb_ep0_in_len;
366 if (this_len > AO_USB_CONTROL_SIZE)
367 this_len = AO_USB_CONTROL_SIZE;
369 if (this_len < AO_USB_CONTROL_SIZE)
370 ao_usb_ep0_state = AO_USB_EP0_IDLE;
372 ao_usb_ep0_in_len -= this_len;
374 debug_data ("Flush EP0 len %d:", this_len);
375 memcpy(ao_usb_ep0_tx_buffer, ao_usb_ep0_in_data, this_len);
377 ao_usb_ep0_in_data += this_len;
379 /* Mark the endpoint as TX valid to send the packet */
380 ao_usb_set_ep(ao_usb_ep0_in(), ao_usb_ep0_tx_buffer, this_len);
381 debug ("queue tx. 0 now %08x\n", *ao_usb_ep0_in());
384 /* Read data from the ep0 OUT fifo */
386 ao_usb_ep0_fill(void)
391 /* Pull all of the data out of the packet */
392 if (lpc_usb.devcmdstat & (1 << LPC_USB_DEVCMDSTAT_SETUP)) {
393 rx_buffer = ao_usb_ep0_setup_buffer;
396 rx_buffer = ao_usb_ep0_rx_buffer;
397 len = AO_USB_CONTROL_SIZE - ao_usb_ep_count(ao_usb_ep0_out());
400 if (len > ao_usb_ep0_out_len)
401 len = ao_usb_ep0_out_len;
402 ao_usb_ep0_out_len -= len;
404 debug_data ("Fill EP0 len %d:", len);
405 memcpy(ao_usb_ep0_out_data, rx_buffer, len);
407 ao_usb_ep0_out_data += len;
410 ao_usb_set_ep(ao_usb_ep0_out(), ao_usb_ep0_rx_buffer, AO_USB_CONTROL_SIZE);
411 lpc_usb.devcmdstat |= (1 << LPC_USB_DEVCMDSTAT_SETUP);
415 ao_usb_ep0_in_reset(void)
417 ao_usb_ep0_in_data = ao_usb_ep0_in_buf;
418 ao_usb_ep0_in_len = 0;
422 ao_usb_ep0_in_queue_byte(uint8_t a)
424 if (ao_usb_ep0_in_len < sizeof (ao_usb_ep0_in_buf))
425 ao_usb_ep0_in_buf[ao_usb_ep0_in_len++] = a;
429 ao_usb_ep0_in_set(const uint8_t *data, uint8_t len)
431 ao_usb_ep0_in_data = data;
432 ao_usb_ep0_in_len = len;
436 ao_usb_ep0_out_set(uint8_t *data, uint8_t len)
438 ao_usb_ep0_out_data = data;
439 ao_usb_ep0_out_len = len;
443 ao_usb_ep0_in_start(uint8_t max)
445 /* Don't send more than asked for */
446 if (ao_usb_ep0_in_len > max)
447 ao_usb_ep0_in_len = max;
451 static struct ao_usb_line_coding ao_usb_line_coding = {115200, 0, 0, 8};
453 /* Walk through the list of descriptors and find a match
456 ao_usb_get_descriptor(uint16_t value)
458 const uint8_t *descriptor;
459 uint8_t type = value >> 8;
460 uint8_t index = value;
462 descriptor = ao_usb_descriptors;
463 while (descriptor[0] != 0) {
464 if (descriptor[1] == type && index-- == 0) {
466 if (type == AO_USB_DESC_CONFIGURATION)
470 ao_usb_ep0_in_set(descriptor, len);
473 descriptor += descriptor[0];
478 ao_usb_ep0_setup(void)
480 /* Pull the setup packet out of the fifo */
481 ao_usb_ep0_out_set((uint8_t *) &ao_usb_setup, 8);
483 if (ao_usb_ep0_out_len != 0) {
484 debug ("invalid setup packet length\n");
488 if ((ao_usb_setup.dir_type_recip & AO_USB_DIR_IN) || ao_usb_setup.length == 0)
489 ao_usb_ep0_state = AO_USB_EP0_DATA_IN;
491 ao_usb_ep0_state = AO_USB_EP0_DATA_OUT;
493 ao_usb_ep0_in_reset();
495 switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_TYPE_MASK) {
496 case AO_USB_TYPE_STANDARD:
497 debug ("Standard setup packet\n");
498 switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_RECIP_MASK) {
499 case AO_USB_RECIP_DEVICE:
500 debug ("Device setup packet\n");
501 switch(ao_usb_setup.request) {
502 case AO_USB_REQ_GET_STATUS:
503 debug ("get status\n");
504 ao_usb_ep0_in_queue_byte(0);
505 ao_usb_ep0_in_queue_byte(0);
507 case AO_USB_REQ_SET_ADDRESS:
508 debug ("set address %d\n", ao_usb_setup.value);
509 ao_usb_address = ao_usb_setup.value;
510 ao_usb_address_pending = 1;
512 case AO_USB_REQ_GET_DESCRIPTOR:
513 debug ("get descriptor %d\n", ao_usb_setup.value);
514 ao_usb_get_descriptor(ao_usb_setup.value);
516 case AO_USB_REQ_GET_CONFIGURATION:
517 debug ("get configuration %d\n", ao_usb_configuration);
518 ao_usb_ep0_in_queue_byte(ao_usb_configuration);
520 case AO_USB_REQ_SET_CONFIGURATION:
521 ao_usb_configuration = ao_usb_setup.value;
522 debug ("set configuration %d\n", ao_usb_configuration);
523 ao_usb_set_configuration();
527 case AO_USB_RECIP_INTERFACE:
528 debug ("Interface setup packet\n");
529 switch(ao_usb_setup.request) {
530 case AO_USB_REQ_GET_STATUS:
531 ao_usb_ep0_in_queue_byte(0);
532 ao_usb_ep0_in_queue_byte(0);
534 case AO_USB_REQ_GET_INTERFACE:
535 ao_usb_ep0_in_queue_byte(0);
537 case AO_USB_REQ_SET_INTERFACE:
541 case AO_USB_RECIP_ENDPOINT:
542 debug ("Endpoint setup packet\n");
543 switch(ao_usb_setup.request) {
544 case AO_USB_REQ_GET_STATUS:
545 ao_usb_ep0_in_queue_byte(0);
546 ao_usb_ep0_in_queue_byte(0);
552 case AO_USB_TYPE_CLASS:
553 debug ("Class setup packet\n");
554 switch (ao_usb_setup.request) {
555 case AO_USB_SET_LINE_CODING:
556 debug ("set line coding\n");
557 ao_usb_ep0_out_set((uint8_t *) &ao_usb_line_coding, 7);
559 case AO_USB_GET_LINE_CODING:
560 debug ("get line coding\n");
561 ao_usb_ep0_in_set((const uint8_t *) &ao_usb_line_coding, 7);
563 case AO_USB_SET_CONTROL_LINE_STATE:
569 /* If we're not waiting to receive data from the host,
570 * queue an IN response
572 if (ao_usb_ep0_state == AO_USB_EP0_DATA_IN)
573 ao_usb_ep0_in_start(ao_usb_setup.length);
577 ao_usb_ep0_handle(uint8_t receive)
579 ao_usb_ep0_receive = 0;
581 if (receive & AO_USB_EP0_GOT_RESET) {
586 if (receive & AO_USB_EP0_GOT_SETUP) {
590 if (receive & AO_USB_EP0_GOT_RX_DATA) {
591 debug ("\tgot rx data\n");
592 if (ao_usb_ep0_state == AO_USB_EP0_DATA_OUT) {
594 if (ao_usb_ep0_out_len == 0) {
595 ao_usb_ep0_state = AO_USB_EP0_DATA_IN;
596 ao_usb_ep0_in_start(0);
600 if (receive & AO_USB_EP0_GOT_TX_ACK) {
601 debug ("\tgot tx ack\n");
603 /* Wait until the IN packet is received from addr 0
604 * before assigning our local address
606 if (ao_usb_address_pending) {
608 /* Go to idle mode if USB is connected
610 ao_flight_force_idle = 1;
612 ao_usb_set_address(ao_usb_address);
614 if (ao_usb_ep0_state == AO_USB_EP0_DATA_IN)
619 static uint16_t control_count;
620 static uint16_t int_count;
621 static uint16_t in_count;
622 static uint16_t out_count;
623 static uint16_t reset_count;
626 lpc_usb_irq_isr(void)
628 uint32_t intstat = lpc_usb.intstat & lpc_usb.inten;
630 lpc_usb.intstat = intstat;
631 /* Handle EP0 OUT packets */
632 if (intstat & (1 << LPC_USB_INT_EPOUT(0))) {
633 if (lpc_usb.devcmdstat & (1 << LPC_USB_DEVCMDSTAT_SETUP))
634 ao_usb_ep0_receive |= AO_USB_EP0_GOT_SETUP;
636 ao_usb_ep0_receive |= AO_USB_EP0_GOT_RX_DATA;
638 ao_usb_ep0_handle(ao_usb_ep0_receive);
641 /* Handle EP0 IN packets */
642 if (intstat & (1 << LPC_USB_INT_EPIN(0))) {
643 ao_usb_ep0_receive |= AO_USB_EP0_GOT_TX_ACK;
645 ao_usb_ep0_handle(ao_usb_ep0_receive);
649 /* Handle OUT packets */
650 if (intstat & (1 << LPC_USB_INT_EPOUT(AO_USB_OUT_EP))) {
652 _rx_dbg1("RX ISR", *ao_usb_epn_out(AO_USB_OUT_EP));
653 ao_usb_out_avail = 1;
654 _rx_dbg0("out avail set");
655 ao_wakeup(&ao_stdin_ready);
656 _rx_dbg0("stdin awoken");
659 /* Handle IN packets */
660 if (intstat & (1 << LPC_USB_INT_EPIN(AO_USB_IN_EP))) {
662 _tx_dbg1("TX ISR", *ao_usb_epn_in(AO_USB_IN_EP));
663 ao_usb_in_pending = 0;
664 ao_wakeup(&ao_usb_in_pending);
667 /* NAK all INT EP IN packets */
668 if (intstat & (1 << LPC_USB_INT_EPIN(AO_USB_INT_EP))) {
672 /* Check for reset */
673 if (intstat & (1 << LPC_USB_INT_DEV)) {
674 if (lpc_usb.devcmdstat & (1 << LPC_USB_DEVCMDSTAT_DRES_C))
676 lpc_usb.devcmdstat |= (1 << LPC_USB_DEVCMDSTAT_DRES_C);
677 ao_usb_ep0_receive |= AO_USB_EP0_GOT_RESET;
678 ao_usb_ep0_handle(ao_usb_ep0_receive);
685 /* Queue the current IN buffer for transmission */
687 _ao_usb_in_send(void)
689 _tx_dbg0("in_send start");
690 debug ("send %d\n", ao_usb_tx_count);
691 while (ao_usb_in_pending)
692 ao_sleep(&ao_usb_in_pending);
693 ao_usb_in_pending = 1;
694 if (ao_usb_tx_count != AO_USB_IN_SIZE)
695 ao_usb_in_flushed = 1;
696 memcpy(ao_usb_in_tx_buffer, ao_usb_tx_buffer, ao_usb_tx_count);
697 ao_usb_set_ep(ao_usb_epn_in(AO_USB_IN_EP), ao_usb_in_tx_buffer, ao_usb_tx_count);
699 _tx_dbg0("in_send end");
702 /* Wait for a free IN buffer. Interrupts are blocked */
704 _ao_usb_in_wait(void)
707 /* Check if the current buffer is writable */
708 if (ao_usb_tx_count < AO_USB_IN_SIZE)
711 _tx_dbg0("in_wait top");
712 /* Wait for an IN buffer to be ready */
713 while (ao_usb_in_pending)
714 ao_sleep(&ao_usb_in_pending);
715 _tx_dbg0("in_wait bottom");
725 /* Anytime we've sent a character since
726 * the last time we flushed, we'll need
727 * to send a packet -- the only other time
728 * we would send a packet is when that
729 * packet was full, in which case we now
730 * want to send an empty packet
732 ao_arch_block_interrupts();
733 while (!ao_usb_in_flushed) {
734 _tx_dbg0("flush top");
736 _tx_dbg0("flush end");
738 ao_arch_release_interrupts();
742 ao_usb_putchar(char c)
747 ao_arch_block_interrupts();
750 ao_usb_in_flushed = 0;
751 ao_usb_tx_buffer[ao_usb_tx_count++] = (uint8_t) c;
753 /* Send the packet when full */
754 if (ao_usb_tx_count == AO_USB_IN_SIZE) {
755 _tx_dbg0("putchar full");
757 _tx_dbg0("putchar flushed");
759 ao_arch_release_interrupts();
763 _ao_usb_out_recv(void)
765 _rx_dbg0("out_recv top");
766 ao_usb_out_avail = 0;
768 ao_usb_rx_count = AO_USB_OUT_SIZE - ao_usb_epn_out_count(AO_USB_OUT_EP);
770 _rx_dbg1("out_recv count", ao_usb_rx_count);
771 debug ("recv %d\n", ao_usb_rx_count);
772 debug_data("Fill OUT len %d:", ao_usb_rx_count);
773 memcpy(ao_usb_rx_buffer, ao_usb_out_rx_buffer, ao_usb_rx_count);
778 ao_usb_set_epn_out(AO_USB_OUT_EP, ao_usb_out_rx_buffer, AO_USB_OUT_SIZE);
782 _ao_usb_pollchar(void)
787 return AO_READ_AGAIN;
790 if (ao_usb_rx_pos != ao_usb_rx_count)
793 _rx_dbg0("poll check");
794 /* Check to see if a packet has arrived */
795 if (!ao_usb_out_avail) {
796 _rx_dbg0("poll none");
797 return AO_READ_AGAIN;
802 /* Pull a character out of the fifo */
803 c = ao_usb_rx_buffer[ao_usb_rx_pos++];
812 ao_arch_block_interrupts();
813 while ((c = _ao_usb_pollchar()) == AO_READ_AGAIN)
814 ao_sleep(&ao_stdin_ready);
815 ao_arch_release_interrupts();
822 ao_arch_block_interrupts();
824 /* Disable interrupts */
827 lpc_nvic_clear_enable(LPC_ISR_USB_IRQ_POS);
829 /* Disable the device */
830 lpc_usb.devcmdstat = 0;
832 /* Turn off USB clock */
833 lpc_scb.usbclkdiv = 0;
835 /* Disable USB PHY and PLL */
836 lpc_scb.pdruncfg |= ((1 << LPC_SCB_PDRUNCFG_USBPAD_PD) |
837 (1 << LPC_SCB_PDRUNCFG_USBPLL_PD));
839 /* Disable USB registers and RAM */
840 lpc_scb.sysahbclkctrl &= ~((1 << LPC_SCB_SYSAHBCLKCTRL_USB) |
841 (1 << LPC_SCB_SYSAHBCLKCTRL_USBRAM));
843 ao_arch_release_interrupts();
851 /* Enable USB pins */
853 lpc_ioconf.pio0_6 = ((LPC_IOCONF_FUNC_USB_CONNECT << LPC_IOCONF_FUNC) |
854 (LPC_IOCONF_MODE_INACTIVE << LPC_IOCONF_MODE) |
855 (0 << LPC_IOCONF_HYS) |
856 (0 << LPC_IOCONF_INV) |
857 (0 << LPC_IOCONF_OD) |
861 lpc_ioconf.pio0_3 = ((LPC_IOCONF_FUNC_USB_VBUS << LPC_IOCONF_FUNC) |
862 (LPC_IOCONF_MODE_INACTIVE << LPC_IOCONF_MODE) |
863 (0 << LPC_IOCONF_HYS) |
864 (0 << LPC_IOCONF_INV) |
865 (0 << LPC_IOCONF_OD) |
868 /* Enable USB registers and RAM */
869 lpc_scb.sysahbclkctrl |= ((1 << LPC_SCB_SYSAHBCLKCTRL_USB) |
870 (1 << LPC_SCB_SYSAHBCLKCTRL_USBRAM));
873 lpc_scb.pdruncfg &= ~(1 << LPC_SCB_PDRUNCFG_USBPAD_PD);
875 /* Turn on USB PLL */
876 lpc_scb.pdruncfg &= ~(1 << LPC_SCB_PDRUNCFG_USBPLL_PD);
878 lpc_scb.usbpllclksel = (LPC_SCB_SYSPLLCLKSEL_SEL_SYSOSC << LPC_SCB_SYSPLLCLKSEL_SEL);
879 lpc_scb.usbpllclkuen = (0 << LPC_SCB_USBPLLCLKUEN_ENA);
880 lpc_scb.usbpllclkuen = (1 << LPC_SCB_USBPLLCLKUEN_ENA);
881 while (!(lpc_scb.usbpllclkuen & (1 << LPC_SCB_USBPLLCLKUEN_ENA)))
883 lpc_scb.usbpllctrl = 0x23;
884 while (!(lpc_scb.usbpllstat & 1))
887 lpc_scb.usbclksel = 0;
888 lpc_scb.usbclkuen = (0 << LPC_SCB_USBCLKUEN_ENA);
889 lpc_scb.usbclkuen = (1 << LPC_SCB_USBCLKUEN_ENA);
890 while (!(lpc_scb.usbclkuen & (1 << LPC_SCB_USBCLKUEN_ENA)))
893 /* Turn on USB clock, use 48MHz clock unchanged */
894 lpc_scb.usbclkdiv = 1;
896 /* Configure interrupts */
897 ao_arch_block_interrupts();
899 /* Route all interrupts to the main isr */
900 lpc_usb.introuting = 0;
904 lpc_nvic_set_enable(LPC_ISR_USB_IRQ_POS);
905 lpc_nvic_set_priority(LPC_ISR_USB_IRQ_POS, 0);
907 /* Clear any spurious interrupts */
908 lpc_usb.intstat = 0xffffffff;
910 debug ("ao_usb_enable\n");
912 /* Enable interrupts */
913 lpc_usb.inten = ((1 << LPC_USB_INT_EPOUT(0)) |
914 (1 << LPC_USB_INT_EPIN(0)) |
915 (1 << LPC_USB_INT_EPIN(AO_USB_INT_EP)) |
916 (1 << LPC_USB_INT_EPOUT(AO_USB_OUT_EP)) |
917 (1 << LPC_USB_INT_EPIN(AO_USB_IN_EP)) |
918 (1 << LPC_USB_INT_DEV));
920 ao_arch_release_interrupts();
922 lpc_usb.devcmdstat = 0;
923 for (t = 0; t < 1000; t++)
930 struct ao_task ao_usb_echo_task;
938 c = ao_usb_getchar();
949 printf ("control: %d out: %d in: %d int: %d reset: %d\n",
950 control_count, out_count, in_count, int_count, reset_count);
953 __code struct ao_cmds ao_usb_cmds[] = {
954 { ao_usb_irq, "I\0Show USB interrupt counts" },
964 debug ("ao_usb_init\n");
966 ao_add_task(&ao_usb_echo_task, ao_usb_echo, "usb echo");
969 ao_cmd_register(&ao_usb_cmds[0]);
972 ao_add_stdio(_ao_usb_pollchar, ao_usb_putchar, ao_usb_flush);
998 #define NUM_USB_DBG 8
1000 static struct ao_usb_dbg dbg[NUM_USB_DBG];
1003 static void _dbg(int line, char *msg, uint32_t value)
1006 dbg[dbg_i].line = line;
1007 dbg[dbg_i].msg = msg;
1008 dbg[dbg_i].value = value;
1009 asm("mrs %0,primask" : "=&r" (primask));
1010 dbg[dbg_i].primask = primask;
1012 dbg[dbg_i].in_count = in_count;
1013 dbg[dbg_i].in_ep = *ao_usb_epn_in(AO_USB_IN_EP);
1014 dbg[dbg_i].in_pending = ao_usb_in_pending;
1015 dbg[dbg_i].tx_count = ao_usb_tx_count;
1016 dbg[dbg_i].in_flushed = ao_usb_in_flushed;
1019 dbg[dbg_i].rx_count = ao_usb_rx_count;
1020 dbg[dbg_i].rx_pos = ao_usb_rx_pos;
1021 dbg[dbg_i].out_avail = ao_usb_out_avail;
1022 dbg[dbg_i].out_ep = *ao_usb_epn_out(AO_USB_OUT_EP);
1024 if (++dbg_i == NUM_USB_DBG)