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 USE_USB_STDIO 1
27 #define AO_USB_OUT_SLEEP_ADDR (&ao_stdin_ready)
29 #define AO_USB_OUT_SLEEP_ADDR (&ao_usb_out_avail)
33 #define USB_DEBUG_DATA 0
37 #define debug(format, args...) printf(format, ## args);
39 #define debug(format, args...)
43 #define debug_data(format, args...) printf(format, ## args);
45 #define debug_data(format, args...)
49 uint8_t dir_type_recip;
56 static uint8_t ao_usb_ep0_state;
58 /* Pending EP0 IN data */
59 static const uint8_t *ao_usb_ep0_in_data; /* Remaining data */
60 static uint8_t ao_usb_ep0_in_len; /* Remaining amount */
62 /* Temp buffer for smaller EP0 in data */
63 static uint8_t ao_usb_ep0_in_buf[2];
65 /* Pending EP0 OUT data */
66 static uint8_t *ao_usb_ep0_out_data;
67 static uint8_t ao_usb_ep0_out_len;
70 * Objects allocated in special USB memory
73 /* USB address of end of allocated storage */
74 static uint8_t *ao_usb_sram;
76 /* Pointer to ep0 tx/rx buffers in USB memory */
77 static uint8_t *ao_usb_ep0_tx_buffer;
78 static uint8_t *ao_usb_ep0_setup_buffer;
79 static uint8_t *ao_usb_ep0_rx_buffer;
81 /* Pointer to bulk data tx/rx buffers in USB memory */
82 static uint8_t *ao_usb_in_tx_buffer;
83 static uint8_t *ao_usb_out_rx_buffer;
85 /* Our data buffers */
86 static uint8_t ao_usb_tx_buffer[AO_USB_IN_SIZE];
87 static uint8_t ao_usb_tx_count;
89 static uint8_t ao_usb_rx_buffer[AO_USB_OUT_SIZE];
90 static uint8_t ao_usb_rx_count, ao_usb_rx_pos;
92 extern struct lpc_usb_endpoint lpc_usb_endpoint;
94 /* Marks when we don't need to send an IN packet.
95 * This happens only when the last IN packet is not full,
96 * otherwise the host will expect to keep seeing packets.
97 * Send a zero-length packet as required
99 static uint8_t ao_usb_in_flushed;
101 /* Marks when we have delivered an IN packet to the hardware
102 * and it has not been received yet. ao_sleep on this address
103 * to wait for it to be delivered.
105 static uint8_t ao_usb_in_pending;
107 /* Marks when an OUT packet has been received by the hardware
108 * but not pulled to the shadow buffer.
110 static uint8_t ao_usb_out_avail;
111 static uint8_t ao_usb_running;
112 static uint8_t ao_usb_configuration;
113 static uint8_t ueienx_0;
115 #define AO_USB_EP0_GOT_RESET 1
116 #define AO_USB_EP0_GOT_SETUP 2
117 #define AO_USB_EP0_GOT_RX_DATA 4
118 #define AO_USB_EP0_GOT_TX_ACK 8
120 static uint8_t ao_usb_ep0_receive;
121 static uint8_t ao_usb_address;
122 static uint8_t ao_usb_address_pending;
124 static inline uint32_t set_toggle(uint32_t current_value,
126 uint32_t desired_value)
128 return (current_value ^ desired_value) & mask;
132 * Set current device address and mark the
133 * interface as active
136 ao_usb_set_address(uint8_t address)
138 debug("ao_usb_set_address %02x\n", address);
139 lpc_usb.devcmdstat = ((address << LPC_USB_DEVCMDSTAT_DEV_ADDR) |
140 (1 << LPC_USB_DEVCMDSTAT_DEV_EN) |
141 (0 << LPC_USB_DEVCMDSTAT_SETUP) |
142 (0 << LPC_USB_DEVCMDSTAT_PLL_ON) |
143 (0 << LPC_USB_DEVCMDSTAT_LPM_SUP) |
144 (0 << LPC_USB_DEVCMDSTAT_INTONNAK_AO) |
145 (0 << LPC_USB_DEVCMDSTAT_INTONNAK_AI) |
146 (0 << LPC_USB_DEVCMDSTAT_INTONNAK_CO) |
147 (0 << LPC_USB_DEVCMDSTAT_INTONNAK_CI) |
148 (1 << LPC_USB_DEVCMDSTAT_DCON) |
149 (0 << LPC_USB_DEVCMDSTAT_DSUS) |
150 (0 << LPC_USB_DEVCMDSTAT_DCON_C) |
151 (0 << LPC_USB_DEVCMDSTAT_DSUS_C) |
152 (0 << LPC_USB_DEVCMDSTAT_DRES_C) |
153 (0 << LPC_USB_DEVCMDSTAT_VBUSDEBOUNCED));
154 ao_usb_address_pending = 0;
161 #define _tx_dbg0(msg) _dbg(__LINE__,msg,0)
162 #define _tx_dbg1(msg,value) _dbg(__LINE__,msg,value)
164 #define _tx_dbg0(msg)
165 #define _tx_dbg1(msg,value)
169 #define _rx_dbg0(msg) _dbg(__LINE__,msg,0)
170 #define _rx_dbg1(msg,value) _dbg(__LINE__,msg,value)
172 #define _rx_dbg0(msg)
173 #define _rx_dbg1(msg,value)
177 static void _dbg(int line, char *msg, uint32_t value);
181 * Set just endpoint 0, for use during startup
185 ao_usb_alloc_sram(uint16_t size)
187 uint8_t *addr = ao_usb_sram;
189 ao_usb_sram += (size + 63) & ~63;
194 ao_usb_sram_offset(uint8_t *addr)
196 return (uint16_t) ((intptr_t) addr >> 6);
200 ao_usb_set_ep(vuint32_t *ep, uint8_t *addr, uint16_t nbytes)
202 *ep = ((ao_usb_sram_offset(addr) << LPC_USB_EP_OFFSET) |
203 (nbytes << LPC_USB_EP_NBYTES) |
204 (0 << LPC_USB_EP_ENDPOINT_ISO) |
205 (0 << LPC_USB_EP_RATE_FEEDBACK) |
206 (0 << LPC_USB_EP_TOGGLE_RESET) |
207 (0 << LPC_USB_EP_STALL) |
208 (0 << LPC_USB_EP_DISABLED) |
209 (1 << LPC_USB_EP_ACTIVE));
212 static inline uint16_t
213 ao_usb_ep_count(vuint32_t *ep)
215 return (*ep >> LPC_USB_EP_NBYTES) & LPC_USB_EP_NBYTES_MASK;
218 static inline uint8_t
219 ao_usb_ep_stall(vuint32_t *ep)
221 return (*ep >> LPC_USB_EP_STALL) & 1;
224 static inline vuint32_t *
227 return &lpc_usb_endpoint.ep0_out;
230 static inline vuint32_t *
233 return &lpc_usb_endpoint.ep0_in;
236 static inline vuint32_t *
237 ao_usb_epn_out(uint8_t n)
239 return &lpc_usb_endpoint.epn[n-1].out[0];
242 static inline vuint32_t *
243 ao_usb_epn_in(uint8_t n)
245 return &lpc_usb_endpoint.epn[n-1].in[0];
249 ao_usb_set_epn_in(uint8_t n, uint8_t *addr, uint16_t nbytes)
251 ao_usb_set_ep(ao_usb_epn_in(n), addr, nbytes);
255 ao_usb_set_epn_out(uint8_t n, uint8_t *addr, uint16_t nbytes)
257 ao_usb_set_ep(ao_usb_epn_out(n), addr, nbytes);
260 static inline uint16_t
261 ao_usb_epn_out_count(uint8_t n)
263 return ao_usb_ep_count(ao_usb_epn_out(n));
266 static inline uint16_t
267 ao_usb_epn_in_count(uint8_t n)
269 return ao_usb_ep_count(ao_usb_epn_in(n));
273 ao_usb_enable_ep(vuint32_t *ep, uint16_t nbytes, uint16_t set_nbytes)
275 uint8_t *addr = ao_usb_alloc_sram(nbytes);
277 ao_usb_set_ep(ep, addr, set_nbytes);
282 ao_usb_disable_ep(vuint32_t *ep)
284 *ep = ((0 << LPC_USB_EP_OFFSET) |
285 (0 << LPC_USB_EP_NBYTES) |
286 (0 << LPC_USB_EP_ENDPOINT_ISO) |
287 (0 << LPC_USB_EP_RATE_FEEDBACK) |
288 (0 << LPC_USB_EP_TOGGLE_RESET) |
289 (0 << LPC_USB_EP_STALL) |
290 (1 << LPC_USB_EP_DISABLED) |
291 (0 << LPC_USB_EP_ACTIVE));
295 ao_usb_enable_epn(uint8_t n, uint16_t out_bytes, uint8_t **out_addr, uint16_t in_bytes, uint8_t **in_addr)
299 addr = ao_usb_enable_ep(ao_usb_epn_out(n), out_bytes, out_bytes);
302 ao_usb_disable_ep(&lpc_usb_endpoint.epn[n-1].out[1]);
304 addr = ao_usb_enable_ep(ao_usb_epn_in(n), in_bytes, 0);
307 ao_usb_disable_ep(&lpc_usb_endpoint.epn[n-1].in[1]);
311 ao_usb_disable_epn(uint8_t n)
313 ao_usb_disable_ep(ao_usb_epn_out(n));
314 ao_usb_disable_ep(&lpc_usb_endpoint.epn[n-1].out[1]);
315 ao_usb_disable_ep(ao_usb_epn_in(n));
316 ao_usb_disable_ep(&lpc_usb_endpoint.epn[n-1].in[1]);
324 /* Everything is single buffered for now */
325 lpc_usb.epbufcfg = 0;
327 lpc_usb.epskip = 0xffffffff;
329 ao_usb_set_address(0);
330 lpc_usb.intstat = 0xc00003ff;
332 ao_usb_configuration = 0;
334 ao_usb_sram = lpc_usb_sram;
336 lpc_usb.epliststart = (uint32_t) (intptr_t) &lpc_usb_endpoint;
337 lpc_usb.databufstart = ((uint32_t) (intptr_t) ao_usb_sram) & 0xffc00000;
339 /* Set up EP 0 - a Control end point with 32 bytes of in and out buffers */
341 ao_usb_ep0_rx_buffer = ao_usb_enable_ep(ao_usb_ep0_out(), AO_USB_CONTROL_SIZE, AO_USB_CONTROL_SIZE);
342 ao_usb_ep0_setup_buffer = ao_usb_alloc_sram(AO_USB_CONTROL_SIZE);
343 lpc_usb_endpoint.setup = ao_usb_sram_offset(ao_usb_ep0_setup_buffer);
344 ao_usb_ep0_tx_buffer = ao_usb_enable_ep(ao_usb_ep0_in(), AO_USB_CONTROL_SIZE, 0);
346 /* Clear all of the other endpoints */
347 for (e = 1; e <= 4; e++)
348 ao_usb_disable_epn(e);
353 ao_usb_set_configuration(void)
355 debug ("ao_usb_set_configuration\n");
357 /* Set up the INT end point */
358 ao_usb_enable_epn(AO_USB_INT_EP, 0, NULL, 0, NULL);
360 /* Set up the OUT end point */
361 ao_usb_enable_epn(AO_USB_OUT_EP, AO_USB_OUT_SIZE, &ao_usb_out_rx_buffer, 0, NULL);
363 /* Set up the IN end point */
364 ao_usb_enable_epn(AO_USB_IN_EP, 0, NULL, AO_USB_IN_SIZE, &ao_usb_in_tx_buffer);
369 /* Send an IN data packet */
371 ao_usb_ep0_flush(void)
375 this_len = ao_usb_ep0_in_len;
376 if (this_len > AO_USB_CONTROL_SIZE)
377 this_len = AO_USB_CONTROL_SIZE;
379 if (this_len < AO_USB_CONTROL_SIZE)
380 ao_usb_ep0_state = AO_USB_EP0_IDLE;
382 ao_usb_ep0_in_len -= this_len;
384 debug_data ("Flush EP0 len %d:", this_len);
385 memcpy(ao_usb_ep0_tx_buffer, ao_usb_ep0_in_data, this_len);
387 ao_usb_ep0_in_data += this_len;
389 /* Mark the endpoint as TX valid to send the packet */
390 ao_usb_set_ep(ao_usb_ep0_in(), ao_usb_ep0_tx_buffer, this_len);
391 debug ("queue tx. 0 now %08x\n", *ao_usb_ep0_in());
394 /* Read data from the ep0 OUT fifo */
396 ao_usb_ep0_fill(void)
401 /* Pull all of the data out of the packet */
402 if (lpc_usb.devcmdstat & (1 << LPC_USB_DEVCMDSTAT_SETUP)) {
403 rx_buffer = ao_usb_ep0_setup_buffer;
406 rx_buffer = ao_usb_ep0_rx_buffer;
407 len = AO_USB_CONTROL_SIZE - ao_usb_ep_count(ao_usb_ep0_out());
410 if (len > ao_usb_ep0_out_len)
411 len = ao_usb_ep0_out_len;
412 ao_usb_ep0_out_len -= len;
414 debug_data ("Fill EP0 len %d:", len);
415 memcpy(ao_usb_ep0_out_data, rx_buffer, len);
417 ao_usb_ep0_out_data += len;
420 ao_usb_set_ep(ao_usb_ep0_out(), ao_usb_ep0_rx_buffer, AO_USB_CONTROL_SIZE);
421 lpc_usb.devcmdstat |= (1 << LPC_USB_DEVCMDSTAT_SETUP);
425 ao_usb_ep0_in_reset(void)
427 ao_usb_ep0_in_data = ao_usb_ep0_in_buf;
428 ao_usb_ep0_in_len = 0;
432 ao_usb_ep0_in_queue_byte(uint8_t a)
434 if (ao_usb_ep0_in_len < sizeof (ao_usb_ep0_in_buf))
435 ao_usb_ep0_in_buf[ao_usb_ep0_in_len++] = a;
439 ao_usb_ep0_in_set(const uint8_t *data, uint8_t len)
441 ao_usb_ep0_in_data = data;
442 ao_usb_ep0_in_len = len;
446 ao_usb_ep0_out_set(uint8_t *data, uint8_t len)
448 ao_usb_ep0_out_data = data;
449 ao_usb_ep0_out_len = len;
453 ao_usb_ep0_in_start(uint8_t max)
455 /* Don't send more than asked for */
456 if (ao_usb_ep0_in_len > max)
457 ao_usb_ep0_in_len = max;
461 static struct ao_usb_line_coding ao_usb_line_coding = {115200, 0, 0, 8};
463 /* Walk through the list of descriptors and find a match
466 ao_usb_get_descriptor(uint16_t value)
468 const uint8_t *descriptor;
469 uint8_t type = value >> 8;
470 uint8_t index = value;
472 descriptor = ao_usb_descriptors;
473 while (descriptor[0] != 0) {
474 if (descriptor[1] == type && index-- == 0) {
476 if (type == AO_USB_DESC_CONFIGURATION)
480 ao_usb_ep0_in_set(descriptor, len);
483 descriptor += descriptor[0];
488 ao_usb_ep0_setup(void)
490 /* Pull the setup packet out of the fifo */
491 ao_usb_ep0_out_set((uint8_t *) &ao_usb_setup, 8);
493 if (ao_usb_ep0_out_len != 0) {
494 debug ("invalid setup packet length\n");
498 if ((ao_usb_setup.dir_type_recip & AO_USB_DIR_IN) || ao_usb_setup.length == 0)
499 ao_usb_ep0_state = AO_USB_EP0_DATA_IN;
501 ao_usb_ep0_state = AO_USB_EP0_DATA_OUT;
503 ao_usb_ep0_in_reset();
505 switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_TYPE_MASK) {
506 case AO_USB_TYPE_STANDARD:
507 debug ("Standard setup packet\n");
508 switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_RECIP_MASK) {
509 case AO_USB_RECIP_DEVICE:
510 debug ("Device setup packet\n");
511 switch(ao_usb_setup.request) {
512 case AO_USB_REQ_GET_STATUS:
513 debug ("get status\n");
514 ao_usb_ep0_in_queue_byte(0);
515 ao_usb_ep0_in_queue_byte(0);
517 case AO_USB_REQ_SET_ADDRESS:
518 debug ("set address %d\n", ao_usb_setup.value);
519 ao_usb_address = ao_usb_setup.value;
520 ao_usb_address_pending = 1;
522 case AO_USB_REQ_GET_DESCRIPTOR:
523 debug ("get descriptor %d\n", ao_usb_setup.value);
524 ao_usb_get_descriptor(ao_usb_setup.value);
526 case AO_USB_REQ_GET_CONFIGURATION:
527 debug ("get configuration %d\n", ao_usb_configuration);
528 ao_usb_ep0_in_queue_byte(ao_usb_configuration);
530 case AO_USB_REQ_SET_CONFIGURATION:
531 ao_usb_configuration = ao_usb_setup.value;
532 debug ("set configuration %d\n", ao_usb_configuration);
533 ao_usb_set_configuration();
537 case AO_USB_RECIP_INTERFACE:
538 debug ("Interface setup packet\n");
539 switch(ao_usb_setup.request) {
540 case AO_USB_REQ_GET_STATUS:
541 ao_usb_ep0_in_queue_byte(0);
542 ao_usb_ep0_in_queue_byte(0);
544 case AO_USB_REQ_GET_INTERFACE:
545 ao_usb_ep0_in_queue_byte(0);
547 case AO_USB_REQ_SET_INTERFACE:
551 case AO_USB_RECIP_ENDPOINT:
552 debug ("Endpoint setup packet\n");
553 switch(ao_usb_setup.request) {
554 case AO_USB_REQ_GET_STATUS:
555 ao_usb_ep0_in_queue_byte(0);
556 ao_usb_ep0_in_queue_byte(0);
562 case AO_USB_TYPE_CLASS:
563 debug ("Class setup packet\n");
564 switch (ao_usb_setup.request) {
565 case AO_USB_SET_LINE_CODING:
566 debug ("set line coding\n");
567 ao_usb_ep0_out_set((uint8_t *) &ao_usb_line_coding, 7);
569 case AO_USB_GET_LINE_CODING:
570 debug ("get line coding\n");
571 ao_usb_ep0_in_set((const uint8_t *) &ao_usb_line_coding, 7);
573 case AO_USB_SET_CONTROL_LINE_STATE:
579 /* If we're not waiting to receive data from the host,
580 * queue an IN response
582 if (ao_usb_ep0_state == AO_USB_EP0_DATA_IN)
583 ao_usb_ep0_in_start(ao_usb_setup.length);
587 ao_usb_ep0_handle(uint8_t receive)
589 ao_usb_ep0_receive = 0;
591 if (receive & AO_USB_EP0_GOT_RESET) {
596 if (receive & AO_USB_EP0_GOT_SETUP) {
600 if (receive & AO_USB_EP0_GOT_RX_DATA) {
601 debug ("\tgot rx data\n");
602 if (ao_usb_ep0_state == AO_USB_EP0_DATA_OUT) {
604 if (ao_usb_ep0_out_len == 0) {
605 ao_usb_ep0_state = AO_USB_EP0_DATA_IN;
606 ao_usb_ep0_in_start(0);
610 if (receive & AO_USB_EP0_GOT_TX_ACK) {
611 debug ("\tgot tx ack\n");
613 /* Wait until the IN packet is received from addr 0
614 * before assigning our local address
616 if (ao_usb_address_pending) {
618 /* Go to idle mode if USB is connected
620 ao_flight_force_idle = 1;
622 ao_usb_set_address(ao_usb_address);
624 if (ao_usb_ep0_state == AO_USB_EP0_DATA_IN)
629 static uint16_t control_count;
630 static uint16_t int_count;
631 static uint16_t in_count;
632 static uint16_t out_count;
633 static uint16_t reset_count;
636 lpc_usb_irq_isr(void)
638 uint32_t intstat = lpc_usb.intstat & lpc_usb.inten;
640 lpc_usb.intstat = intstat;
641 /* Handle EP0 OUT packets */
642 if (intstat & (1 << LPC_USB_INT_EPOUT(0))) {
643 if (lpc_usb.devcmdstat & (1 << LPC_USB_DEVCMDSTAT_SETUP))
644 ao_usb_ep0_receive |= AO_USB_EP0_GOT_SETUP;
646 ao_usb_ep0_receive |= AO_USB_EP0_GOT_RX_DATA;
648 ao_usb_ep0_handle(ao_usb_ep0_receive);
651 /* Handle EP0 IN packets */
652 if (intstat & (1 << LPC_USB_INT_EPIN(0))) {
653 ao_usb_ep0_receive |= AO_USB_EP0_GOT_TX_ACK;
655 ao_usb_ep0_handle(ao_usb_ep0_receive);
659 /* Handle OUT packets */
660 if (intstat & (1 << LPC_USB_INT_EPOUT(AO_USB_OUT_EP))) {
662 _rx_dbg1("RX ISR", *ao_usb_epn_out(AO_USB_OUT_EP));
663 ao_usb_out_avail = 1;
664 _rx_dbg0("out avail set");
665 ao_wakeup(AO_USB_OUT_SLEEP_ADDR)
666 _rx_dbg0("stdin awoken");
669 /* Handle IN packets */
670 if (intstat & (1 << LPC_USB_INT_EPIN(AO_USB_IN_EP))) {
672 _tx_dbg1("TX ISR", *ao_usb_epn_in(AO_USB_IN_EP));
673 ao_usb_in_pending = 0;
674 ao_wakeup(&ao_usb_in_pending);
677 /* NAK all INT EP IN packets */
678 if (intstat & (1 << LPC_USB_INT_EPIN(AO_USB_INT_EP))) {
682 /* Check for reset */
683 if (intstat & (1 << LPC_USB_INT_DEV)) {
684 if (lpc_usb.devcmdstat & (1 << LPC_USB_DEVCMDSTAT_DRES_C))
686 lpc_usb.devcmdstat |= (1 << LPC_USB_DEVCMDSTAT_DRES_C);
687 ao_usb_ep0_receive |= AO_USB_EP0_GOT_RESET;
688 ao_usb_ep0_handle(ao_usb_ep0_receive);
695 /* Queue the current IN buffer for transmission */
697 _ao_usb_in_send(void)
699 _tx_dbg0("in_send start");
700 debug ("send %d\n", ao_usb_tx_count);
701 while (ao_usb_in_pending)
702 ao_sleep(&ao_usb_in_pending);
703 ao_usb_in_pending = 1;
704 if (ao_usb_tx_count != AO_USB_IN_SIZE)
705 ao_usb_in_flushed = 1;
706 memcpy(ao_usb_in_tx_buffer, ao_usb_tx_buffer, ao_usb_tx_count);
707 ao_usb_set_ep(ao_usb_epn_in(AO_USB_IN_EP), ao_usb_in_tx_buffer, ao_usb_tx_count);
709 _tx_dbg0("in_send end");
712 /* Wait for a free IN buffer. Interrupts are blocked */
714 _ao_usb_in_wait(void)
717 /* Check if the current buffer is writable */
718 if (ao_usb_tx_count < AO_USB_IN_SIZE)
721 _tx_dbg0("in_wait top");
722 /* Wait for an IN buffer to be ready */
723 while (ao_usb_in_pending)
724 ao_sleep(&ao_usb_in_pending);
725 _tx_dbg0("in_wait bottom");
735 /* Anytime we've sent a character since
736 * the last time we flushed, we'll need
737 * to send a packet -- the only other time
738 * we would send a packet is when that
739 * packet was full, in which case we now
740 * want to send an empty packet
742 ao_arch_block_interrupts();
743 while (!ao_usb_in_flushed) {
744 _tx_dbg0("flush top");
746 _tx_dbg0("flush end");
748 ao_arch_release_interrupts();
752 ao_usb_putchar(char c)
757 ao_arch_block_interrupts();
760 ao_usb_in_flushed = 0;
761 ao_usb_tx_buffer[ao_usb_tx_count++] = (uint8_t) c;
763 /* Send the packet when full */
764 if (ao_usb_tx_count == AO_USB_IN_SIZE) {
765 _tx_dbg0("putchar full");
767 _tx_dbg0("putchar flushed");
769 ao_arch_release_interrupts();
773 _ao_usb_out_recv(void)
775 _rx_dbg0("out_recv top");
776 ao_usb_out_avail = 0;
778 ao_usb_rx_count = AO_USB_OUT_SIZE - ao_usb_epn_out_count(AO_USB_OUT_EP);
780 _rx_dbg1("out_recv count", ao_usb_rx_count);
781 debug ("recv %d\n", ao_usb_rx_count);
782 debug_data("Fill OUT len %d:", ao_usb_rx_count);
783 memcpy(ao_usb_rx_buffer, ao_usb_out_rx_buffer, ao_usb_rx_count);
788 ao_usb_set_epn_out(AO_USB_OUT_EP, ao_usb_out_rx_buffer, AO_USB_OUT_SIZE);
792 _ao_usb_pollchar(void)
797 return AO_READ_AGAIN;
800 if (ao_usb_rx_pos != ao_usb_rx_count)
803 _rx_dbg0("poll check");
804 /* Check to see if a packet has arrived */
805 if (!ao_usb_out_avail) {
806 _rx_dbg0("poll none");
807 return AO_READ_AGAIN;
812 /* Pull a character out of the fifo */
813 c = ao_usb_rx_buffer[ao_usb_rx_pos++];
822 ao_arch_block_interrupts();
823 while ((c = _ao_usb_pollchar()) == AO_READ_AGAIN)
824 ao_sleep(AO_USB_OUT_SLEEP_ADDR);
825 ao_arch_release_interrupts();
832 ao_arch_block_interrupts();
835 ao_gpio_set(AO_USB_PULLUP_PORT, AO_USB_PULLUP_PIN, AO_USB_PULLUP, 0);
837 /* Disable interrupts */
840 lpc_nvic_clear_enable(LPC_ISR_USB_IRQ_POS);
842 /* Disable the device */
843 lpc_usb.devcmdstat = 0;
845 /* Turn off USB clock */
846 lpc_scb.usbclkdiv = 0;
848 /* Disable USB PHY and PLL */
849 lpc_scb.pdruncfg |= ((1 << LPC_SCB_PDRUNCFG_USBPAD_PD) |
850 (1 << LPC_SCB_PDRUNCFG_USBPLL_PD));
852 /* Disable USB registers and RAM */
853 lpc_scb.sysahbclkctrl &= ~((1 << LPC_SCB_SYSAHBCLKCTRL_USB) |
854 (1 << LPC_SCB_SYSAHBCLKCTRL_USBRAM));
856 ao_arch_release_interrupts();
864 /* Enable USB pins */
866 lpc_ioconf.pio0_6 = ((LPC_IOCONF_FUNC_USB_CONNECT << LPC_IOCONF_FUNC) |
867 (LPC_IOCONF_MODE_INACTIVE << LPC_IOCONF_MODE) |
868 (0 << LPC_IOCONF_HYS) |
869 (0 << LPC_IOCONF_INV) |
870 (0 << LPC_IOCONF_OD) |
874 lpc_ioconf.pio0_3 = ((LPC_IOCONF_FUNC_USB_VBUS << LPC_IOCONF_FUNC) |
875 (LPC_IOCONF_MODE_INACTIVE << LPC_IOCONF_MODE) |
876 (0 << LPC_IOCONF_HYS) |
877 (0 << LPC_IOCONF_INV) |
878 (0 << LPC_IOCONF_OD) |
881 /* Enable USB registers and RAM */
882 lpc_scb.sysahbclkctrl |= ((1 << LPC_SCB_SYSAHBCLKCTRL_USB) |
883 (1 << LPC_SCB_SYSAHBCLKCTRL_USBRAM));
886 lpc_scb.pdruncfg &= ~(1 << LPC_SCB_PDRUNCFG_USBPAD_PD);
888 /* Turn on USB PLL */
889 lpc_scb.pdruncfg &= ~(1 << LPC_SCB_PDRUNCFG_USBPLL_PD);
891 lpc_scb.usbpllclksel = (LPC_SCB_SYSPLLCLKSEL_SEL_SYSOSC << LPC_SCB_SYSPLLCLKSEL_SEL);
892 lpc_scb.usbpllclkuen = (0 << LPC_SCB_USBPLLCLKUEN_ENA);
893 lpc_scb.usbpllclkuen = (1 << LPC_SCB_USBPLLCLKUEN_ENA);
894 while (!(lpc_scb.usbpllclkuen & (1 << LPC_SCB_USBPLLCLKUEN_ENA)))
896 lpc_scb.usbpllctrl = 0x23;
897 while (!(lpc_scb.usbpllstat & 1))
900 lpc_scb.usbclksel = 0;
901 lpc_scb.usbclkuen = (0 << LPC_SCB_USBCLKUEN_ENA);
902 lpc_scb.usbclkuen = (1 << LPC_SCB_USBCLKUEN_ENA);
903 while (!(lpc_scb.usbclkuen & (1 << LPC_SCB_USBCLKUEN_ENA)))
906 /* Turn on USB clock, use 48MHz clock unchanged */
907 lpc_scb.usbclkdiv = 1;
909 /* Configure interrupts */
910 ao_arch_block_interrupts();
912 /* Route all interrupts to the main isr */
913 lpc_usb.introuting = 0;
917 lpc_nvic_set_enable(LPC_ISR_USB_IRQ_POS);
918 lpc_nvic_set_priority(LPC_ISR_USB_IRQ_POS, 0);
920 /* Clear any spurious interrupts */
921 lpc_usb.intstat = 0xffffffff;
923 debug ("ao_usb_enable\n");
925 /* Enable interrupts */
926 lpc_usb.inten = ((1 << LPC_USB_INT_EPOUT(0)) |
927 (1 << LPC_USB_INT_EPIN(0)) |
928 (1 << LPC_USB_INT_EPIN(AO_USB_INT_EP)) |
929 (1 << LPC_USB_INT_EPOUT(AO_USB_OUT_EP)) |
930 (1 << LPC_USB_INT_EPIN(AO_USB_IN_EP)) |
931 (1 << LPC_USB_INT_DEV));
933 ao_arch_release_interrupts();
935 lpc_usb.devcmdstat = 0;
936 for (t = 0; t < 1000; t++)
940 ao_gpio_set(AO_USB_PULLUP_PORT, AO_USB_PULLUP_PIN, AO_USB_PULLUP, 1);
947 struct ao_task ao_usb_echo_task;
955 c = ao_usb_getchar();
966 printf ("control: %d out: %d in: %d int: %d reset: %d\n",
967 control_count, out_count, in_count, int_count, reset_count);
970 __code struct ao_cmds ao_usb_cmds[] = {
971 { ao_usb_irq, "I\0Show USB interrupt counts" },
980 ao_enable_output(AO_USB_PULLUP_PORT, AO_USB_PULLUP_PIN, AO_USB_PULLUP, 0);
985 debug ("ao_usb_init\n");
987 ao_add_task(&ao_usb_echo_task, ao_usb_echo, "usb echo");
990 ao_cmd_register(&ao_usb_cmds[0]);
993 ao_add_stdio(_ao_usb_pollchar, ao_usb_putchar, ao_usb_flush);
1007 uint32_t in_pending;
1009 uint32_t in_flushed;
1019 #define NUM_USB_DBG 8
1021 static struct ao_usb_dbg dbg[NUM_USB_DBG];
1024 static void _dbg(int line, char *msg, uint32_t value)
1027 dbg[dbg_i].line = line;
1028 dbg[dbg_i].msg = msg;
1029 dbg[dbg_i].value = value;
1030 asm("mrs %0,primask" : "=&r" (primask));
1031 dbg[dbg_i].primask = primask;
1033 dbg[dbg_i].in_count = in_count;
1034 dbg[dbg_i].in_ep = *ao_usb_epn_in(AO_USB_IN_EP);
1035 dbg[dbg_i].in_pending = ao_usb_in_pending;
1036 dbg[dbg_i].tx_count = ao_usb_tx_count;
1037 dbg[dbg_i].in_flushed = ao_usb_in_flushed;
1040 dbg[dbg_i].rx_count = ao_usb_rx_count;
1041 dbg[dbg_i].rx_pos = ao_usb_rx_pos;
1042 dbg[dbg_i].out_avail = ao_usb_out_avail;
1043 dbg[dbg_i].out_ep = *ao_usb_epn_out(AO_USB_OUT_EP);
1045 if (++dbg_i == NUM_USB_DBG)