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 */
61 static uint16_t ao_usb_ep0_in_max; /* Requested amount from host */
63 /* Temp buffer for smaller EP0 in data */
64 static uint8_t ao_usb_ep0_in_buf[2];
66 /* Pending EP0 OUT data */
67 static uint8_t *ao_usb_ep0_out_data;
68 static uint8_t ao_usb_ep0_out_len;
71 * Objects allocated in special USB memory
74 /* USB address of end of allocated storage */
75 static uint8_t *ao_usb_sram;
77 /* Pointer to ep0 tx/rx buffers in USB memory */
78 static uint8_t *ao_usb_ep0_tx_buffer;
79 static uint8_t *ao_usb_ep0_setup_buffer;
80 static uint8_t *ao_usb_ep0_rx_buffer;
82 /* Pointer to bulk data tx/rx buffers in USB memory */
83 static uint8_t *ao_usb_in_tx_buffer;
84 static uint8_t *ao_usb_out_rx_buffer;
86 /* Our data buffers */
87 static uint8_t ao_usb_tx_buffer[AO_USB_IN_SIZE];
88 static uint8_t ao_usb_tx_count;
90 static uint8_t ao_usb_rx_buffer[AO_USB_OUT_SIZE];
91 static uint8_t ao_usb_rx_count, ao_usb_rx_pos;
93 extern struct lpc_usb_endpoint lpc_usb_endpoint;
95 /* Marks when we don't need to send an IN packet.
96 * This happens only when the last IN packet is not full,
97 * otherwise the host will expect to keep seeing packets.
98 * Send a zero-length packet as required
100 static uint8_t ao_usb_in_flushed;
102 /* Marks when we have delivered an IN packet to the hardware
103 * and it has not been received yet. ao_sleep on this address
104 * to wait for it to be delivered.
106 static uint8_t ao_usb_in_pending;
108 /* Marks when an OUT packet has been received by the hardware
109 * but not pulled to the shadow buffer.
111 static uint8_t ao_usb_out_avail;
112 static uint8_t ao_usb_running;
113 static uint8_t ao_usb_configuration;
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];
250 ao_usb_set_epn_in(uint8_t n, uint8_t *addr, uint16_t nbytes)
252 ao_usb_set_ep(ao_usb_epn_in(n), addr, nbytes);
257 ao_usb_set_epn_out(uint8_t n, uint8_t *addr, uint16_t nbytes)
259 ao_usb_set_ep(ao_usb_epn_out(n), addr, nbytes);
262 static inline uint16_t
263 ao_usb_epn_out_count(uint8_t n)
265 return ao_usb_ep_count(ao_usb_epn_out(n));
268 static inline uint16_t
269 ao_usb_epn_in_count(uint8_t n)
271 return ao_usb_ep_count(ao_usb_epn_in(n));
275 ao_usb_enable_ep(vuint32_t *ep, uint16_t nbytes, uint16_t set_nbytes)
277 uint8_t *addr = ao_usb_alloc_sram(nbytes);
279 ao_usb_set_ep(ep, addr, set_nbytes);
284 ao_usb_disable_ep(vuint32_t *ep)
286 *ep = ((0 << LPC_USB_EP_OFFSET) |
287 (0 << LPC_USB_EP_NBYTES) |
288 (0 << LPC_USB_EP_ENDPOINT_ISO) |
289 (0 << LPC_USB_EP_RATE_FEEDBACK) |
290 (0 << LPC_USB_EP_TOGGLE_RESET) |
291 (0 << LPC_USB_EP_STALL) |
292 (1 << LPC_USB_EP_DISABLED) |
293 (0 << LPC_USB_EP_ACTIVE));
297 ao_usb_enable_epn(uint8_t n, uint16_t out_bytes, uint8_t **out_addr, uint16_t in_bytes, uint8_t **in_addr)
301 addr = ao_usb_enable_ep(ao_usb_epn_out(n), out_bytes, out_bytes);
304 ao_usb_disable_ep(&lpc_usb_endpoint.epn[n-1].out[1]);
306 addr = ao_usb_enable_ep(ao_usb_epn_in(n), in_bytes, 0);
309 ao_usb_disable_ep(&lpc_usb_endpoint.epn[n-1].in[1]);
313 ao_usb_disable_epn(uint8_t n)
315 ao_usb_disable_ep(ao_usb_epn_out(n));
316 ao_usb_disable_ep(&lpc_usb_endpoint.epn[n-1].out[1]);
317 ao_usb_disable_ep(ao_usb_epn_in(n));
318 ao_usb_disable_ep(&lpc_usb_endpoint.epn[n-1].in[1]);
324 ao_usb_set_address(0);
325 ao_usb_configuration = 0;
333 /* Everything is single buffered for now */
334 lpc_usb.epbufcfg = 0;
336 lpc_usb.epskip = 0xffffffff;
338 lpc_usb.intstat = 0xc00003ff;
340 ao_usb_sram = lpc_usb_sram;
342 lpc_usb.epliststart = (uint32_t) (intptr_t) &lpc_usb_endpoint;
343 lpc_usb.databufstart = ((uint32_t) (intptr_t) ao_usb_sram) & 0xffc00000;
345 /* Set up EP 0 - a Control end point with 32 bytes of in and out buffers */
347 ao_usb_ep0_rx_buffer = ao_usb_enable_ep(ao_usb_ep0_out(), AO_USB_CONTROL_SIZE, AO_USB_CONTROL_SIZE);
348 ao_usb_ep0_setup_buffer = ao_usb_alloc_sram(AO_USB_CONTROL_SIZE);
349 lpc_usb_endpoint.setup = ao_usb_sram_offset(ao_usb_ep0_setup_buffer);
350 ao_usb_ep0_tx_buffer = ao_usb_enable_ep(ao_usb_ep0_in(), AO_USB_CONTROL_SIZE, 0);
352 /* Clear all of the other endpoints */
353 for (e = 1; e <= 4; e++)
354 ao_usb_disable_epn(e);
359 ao_usb_set_configuration(void)
361 debug ("ao_usb_set_configuration\n");
363 /* Set up the INT end point */
364 ao_usb_enable_epn(AO_USB_INT_EP, 0, NULL, 0, NULL);
366 /* Set up the OUT end point */
367 ao_usb_enable_epn(AO_USB_OUT_EP, AO_USB_OUT_SIZE, &ao_usb_out_rx_buffer, 0, NULL);
369 /* Set up the IN end point */
370 ao_usb_enable_epn(AO_USB_IN_EP, 0, NULL, AO_USB_IN_SIZE, &ao_usb_in_tx_buffer);
375 /* Send an IN data packet */
377 ao_usb_ep0_flush(void)
381 this_len = ao_usb_ep0_in_len;
382 if (this_len > AO_USB_CONTROL_SIZE)
383 this_len = AO_USB_CONTROL_SIZE;
385 ao_usb_ep0_in_len -= this_len;
386 ao_usb_ep0_in_max -= this_len;
388 if (this_len < AO_USB_CONTROL_SIZE || ao_usb_ep0_in_max == 0)
389 ao_usb_ep0_state = AO_USB_EP0_IDLE;
391 debug_data ("Flush EP0 len %d:", this_len);
392 memcpy(ao_usb_ep0_tx_buffer, ao_usb_ep0_in_data, this_len);
394 ao_usb_ep0_in_data += this_len;
396 /* Mark the endpoint as TX valid to send the packet */
397 ao_usb_set_ep(ao_usb_ep0_in(), ao_usb_ep0_tx_buffer, this_len);
398 debug ("queue tx. 0 now %08x\n", *ao_usb_ep0_in());
401 /* Read data from the ep0 OUT fifo */
403 ao_usb_ep0_fill(void)
408 /* Pull all of the data out of the packet */
409 if (lpc_usb.devcmdstat & (1 << LPC_USB_DEVCMDSTAT_SETUP)) {
410 rx_buffer = ao_usb_ep0_setup_buffer;
413 rx_buffer = ao_usb_ep0_rx_buffer;
414 len = AO_USB_CONTROL_SIZE - ao_usb_ep_count(ao_usb_ep0_out());
417 if (len > ao_usb_ep0_out_len)
418 len = ao_usb_ep0_out_len;
419 ao_usb_ep0_out_len -= len;
421 debug_data ("Fill EP0 len %d:", len);
422 memcpy(ao_usb_ep0_out_data, rx_buffer, len);
424 ao_usb_ep0_out_data += len;
427 ao_usb_set_ep(ao_usb_ep0_out(), ao_usb_ep0_rx_buffer, AO_USB_CONTROL_SIZE);
428 lpc_usb.devcmdstat |= (1 << LPC_USB_DEVCMDSTAT_SETUP);
432 ao_usb_ep0_in_reset(void)
434 ao_usb_ep0_in_data = ao_usb_ep0_in_buf;
435 ao_usb_ep0_in_len = 0;
439 ao_usb_ep0_in_queue_byte(uint8_t a)
441 if (ao_usb_ep0_in_len < sizeof (ao_usb_ep0_in_buf))
442 ao_usb_ep0_in_buf[ao_usb_ep0_in_len++] = a;
446 ao_usb_ep0_in_set(const uint8_t *data, uint8_t len)
448 ao_usb_ep0_in_data = data;
449 ao_usb_ep0_in_len = len;
453 ao_usb_ep0_out_set(uint8_t *data, uint8_t len)
455 ao_usb_ep0_out_data = data;
456 ao_usb_ep0_out_len = len;
460 ao_usb_ep0_in_start(uint16_t max)
462 ao_usb_ep0_in_max = max;
463 /* Don't send more than asked for */
464 if (ao_usb_ep0_in_len > max)
465 ao_usb_ep0_in_len = max;
469 static struct ao_usb_line_coding ao_usb_line_coding = {115200, 0, 0, 8};
471 /* Walk through the list of descriptors and find a match
474 ao_usb_get_descriptor(uint16_t value)
476 const uint8_t *descriptor;
477 uint8_t type = value >> 8;
478 uint8_t index = value;
480 descriptor = ao_usb_descriptors;
481 while (descriptor[0] != 0) {
482 if (descriptor[1] == type && index-- == 0) {
484 if (type == AO_USB_DESC_CONFIGURATION)
488 ao_usb_ep0_in_set(descriptor, len);
491 descriptor += descriptor[0];
496 ao_usb_ep0_setup(void)
498 /* Pull the setup packet out of the fifo */
499 ao_usb_ep0_out_set((uint8_t *) &ao_usb_setup, 8);
501 if (ao_usb_ep0_out_len != 0) {
502 debug ("invalid setup packet length\n");
506 if ((ao_usb_setup.dir_type_recip & AO_USB_DIR_IN) || ao_usb_setup.length == 0)
507 ao_usb_ep0_state = AO_USB_EP0_DATA_IN;
509 ao_usb_ep0_state = AO_USB_EP0_DATA_OUT;
511 ao_usb_ep0_in_reset();
513 switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_TYPE_MASK) {
514 case AO_USB_TYPE_STANDARD:
515 debug ("Standard setup packet\n");
516 switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_RECIP_MASK) {
517 case AO_USB_RECIP_DEVICE:
518 debug ("Device setup packet\n");
519 switch(ao_usb_setup.request) {
520 case AO_USB_REQ_GET_STATUS:
521 debug ("get status\n");
522 ao_usb_ep0_in_queue_byte(0);
523 ao_usb_ep0_in_queue_byte(0);
525 case AO_USB_REQ_SET_ADDRESS:
526 debug ("set address %d\n", ao_usb_setup.value);
527 ao_usb_address = ao_usb_setup.value;
528 ao_usb_address_pending = 1;
530 case AO_USB_REQ_GET_DESCRIPTOR:
531 debug ("get descriptor %d\n", ao_usb_setup.value);
532 ao_usb_get_descriptor(ao_usb_setup.value);
534 case AO_USB_REQ_GET_CONFIGURATION:
535 debug ("get configuration %d\n", ao_usb_configuration);
536 ao_usb_ep0_in_queue_byte(ao_usb_configuration);
538 case AO_USB_REQ_SET_CONFIGURATION:
539 ao_usb_configuration = ao_usb_setup.value;
540 debug ("set configuration %d\n", ao_usb_configuration);
541 ao_usb_set_configuration();
545 case AO_USB_RECIP_INTERFACE:
546 debug ("Interface setup packet\n");
547 switch(ao_usb_setup.request) {
548 case AO_USB_REQ_GET_STATUS:
549 ao_usb_ep0_in_queue_byte(0);
550 ao_usb_ep0_in_queue_byte(0);
552 case AO_USB_REQ_GET_INTERFACE:
553 ao_usb_ep0_in_queue_byte(0);
555 case AO_USB_REQ_SET_INTERFACE:
559 case AO_USB_RECIP_ENDPOINT:
560 debug ("Endpoint setup packet\n");
561 switch(ao_usb_setup.request) {
562 case AO_USB_REQ_GET_STATUS:
563 ao_usb_ep0_in_queue_byte(0);
564 ao_usb_ep0_in_queue_byte(0);
570 case AO_USB_TYPE_CLASS:
571 debug ("Class setup packet\n");
572 switch (ao_usb_setup.request) {
573 case AO_USB_SET_LINE_CODING:
574 debug ("set line coding\n");
575 ao_usb_ep0_out_set((uint8_t *) &ao_usb_line_coding, 7);
577 case AO_USB_GET_LINE_CODING:
578 debug ("get line coding\n");
579 ao_usb_ep0_in_set((const uint8_t *) &ao_usb_line_coding, 7);
581 case AO_USB_SET_CONTROL_LINE_STATE:
587 /* If we're not waiting to receive data from the host,
588 * queue an IN response
590 if (ao_usb_ep0_state == AO_USB_EP0_DATA_IN)
591 ao_usb_ep0_in_start(ao_usb_setup.length);
595 ao_usb_ep0_handle(uint8_t receive)
597 ao_usb_ep0_receive = 0;
599 if (receive & AO_USB_EP0_GOT_RESET) {
604 if (receive & AO_USB_EP0_GOT_SETUP) {
608 if (receive & AO_USB_EP0_GOT_RX_DATA) {
609 debug ("\tgot rx data\n");
610 if (ao_usb_ep0_state == AO_USB_EP0_DATA_OUT) {
612 if (ao_usb_ep0_out_len == 0) {
613 ao_usb_ep0_state = AO_USB_EP0_DATA_IN;
614 ao_usb_ep0_in_start(0);
618 if (receive & AO_USB_EP0_GOT_TX_ACK) {
619 debug ("\tgot tx ack\n");
621 /* Wait until the IN packet is received from addr 0
622 * before assigning our local address
624 if (ao_usb_address_pending) {
626 /* Go to idle mode if USB is connected
628 ao_flight_force_idle = 1;
630 ao_usb_set_address(ao_usb_address);
632 if (ao_usb_ep0_state == AO_USB_EP0_DATA_IN)
638 static uint16_t int_count;
639 static uint16_t in_count;
640 static uint16_t out_count;
641 static uint16_t reset_count;
645 lpc_usb_irq_isr(void)
647 uint32_t intstat = lpc_usb.intstat & lpc_usb.inten;
649 lpc_usb.intstat = intstat;
650 /* Handle EP0 OUT packets */
651 if (intstat & (1 << LPC_USB_INT_EPOUT(0))) {
652 if (lpc_usb.devcmdstat & (1 << LPC_USB_DEVCMDSTAT_SETUP))
653 ao_usb_ep0_receive |= AO_USB_EP0_GOT_SETUP;
655 ao_usb_ep0_receive |= AO_USB_EP0_GOT_RX_DATA;
657 ao_usb_ep0_handle(ao_usb_ep0_receive);
660 /* Handle EP0 IN packets */
661 if (intstat & (1 << LPC_USB_INT_EPIN(0))) {
662 ao_usb_ep0_receive |= AO_USB_EP0_GOT_TX_ACK;
664 ao_usb_ep0_handle(ao_usb_ep0_receive);
668 /* Handle OUT packets */
669 if (intstat & (1 << LPC_USB_INT_EPOUT(AO_USB_OUT_EP))) {
673 _rx_dbg1("RX ISR", *ao_usb_epn_out(AO_USB_OUT_EP));
674 ao_usb_out_avail = 1;
675 _rx_dbg0("out avail set");
676 ao_wakeup(AO_USB_OUT_SLEEP_ADDR)
677 _rx_dbg0("stdin awoken");
680 /* Handle IN packets */
681 if (intstat & (1 << LPC_USB_INT_EPIN(AO_USB_IN_EP))) {
685 _tx_dbg1("TX ISR", *ao_usb_epn_in(AO_USB_IN_EP));
686 ao_usb_in_pending = 0;
687 ao_wakeup(&ao_usb_in_pending);
690 /* NAK all INT EP IN packets */
691 if (intstat & (1 << LPC_USB_INT_EPIN(AO_USB_INT_EP))) {
695 /* Check for reset */
696 if (intstat & (1 << LPC_USB_INT_DEV)) {
697 if (lpc_usb.devcmdstat & (1 << LPC_USB_DEVCMDSTAT_DRES_C))
699 lpc_usb.devcmdstat |= (1 << LPC_USB_DEVCMDSTAT_DRES_C);
700 ao_usb_ep0_receive |= AO_USB_EP0_GOT_RESET;
701 ao_usb_ep0_handle(ao_usb_ep0_receive);
708 /* Queue the current IN buffer for transmission */
710 _ao_usb_in_send(void)
712 _tx_dbg0("in_send start");
713 debug ("send %d\n", ao_usb_tx_count);
714 while (ao_usb_in_pending)
715 ao_sleep(&ao_usb_in_pending);
716 ao_usb_in_pending = 1;
717 if (ao_usb_tx_count != AO_USB_IN_SIZE)
718 ao_usb_in_flushed = 1;
719 memcpy(ao_usb_in_tx_buffer, ao_usb_tx_buffer, ao_usb_tx_count);
720 ao_usb_set_ep(ao_usb_epn_in(AO_USB_IN_EP), ao_usb_in_tx_buffer, ao_usb_tx_count);
722 _tx_dbg0("in_send end");
725 /* Wait for a free IN buffer. Interrupts are blocked */
727 _ao_usb_in_wait(void)
730 /* Check if the current buffer is writable */
731 if (ao_usb_tx_count < AO_USB_IN_SIZE)
734 _tx_dbg0("in_wait top");
735 /* Wait for an IN buffer to be ready */
736 while (ao_usb_in_pending)
737 ao_sleep(&ao_usb_in_pending);
738 _tx_dbg0("in_wait bottom");
748 /* Anytime we've sent a character since
749 * the last time we flushed, we'll need
750 * to send a packet -- the only other time
751 * we would send a packet is when that
752 * packet was full, in which case we now
753 * want to send an empty packet
755 ao_arch_block_interrupts();
756 while (!ao_usb_in_flushed) {
757 _tx_dbg0("flush top");
759 _tx_dbg0("flush end");
761 ao_arch_release_interrupts();
765 ao_usb_putchar(char c)
770 ao_arch_block_interrupts();
773 ao_usb_in_flushed = 0;
774 ao_usb_tx_buffer[ao_usb_tx_count++] = (uint8_t) c;
776 /* Send the packet when full */
777 if (ao_usb_tx_count == AO_USB_IN_SIZE) {
778 _tx_dbg0("putchar full");
780 _tx_dbg0("putchar flushed");
782 ao_arch_release_interrupts();
786 _ao_usb_out_recv(void)
788 _rx_dbg0("out_recv top");
789 ao_usb_out_avail = 0;
791 ao_usb_rx_count = AO_USB_OUT_SIZE - ao_usb_epn_out_count(AO_USB_OUT_EP);
793 _rx_dbg1("out_recv count", ao_usb_rx_count);
794 debug ("recv %d\n", ao_usb_rx_count);
795 debug_data("Fill OUT len %d:", ao_usb_rx_count);
796 memcpy(ao_usb_rx_buffer, ao_usb_out_rx_buffer, ao_usb_rx_count);
801 ao_usb_set_epn_out(AO_USB_OUT_EP, ao_usb_out_rx_buffer, AO_USB_OUT_SIZE);
805 _ao_usb_pollchar(void)
810 return AO_READ_AGAIN;
813 if (ao_usb_rx_pos != ao_usb_rx_count)
816 _rx_dbg0("poll check");
817 /* Check to see if a packet has arrived */
818 if (!ao_usb_out_avail) {
819 _rx_dbg0("poll none");
820 return AO_READ_AGAIN;
825 /* Pull a character out of the fifo */
826 c = ao_usb_rx_buffer[ao_usb_rx_pos++];
835 ao_arch_block_interrupts();
836 while ((c = _ao_usb_pollchar()) == AO_READ_AGAIN)
837 ao_sleep(AO_USB_OUT_SLEEP_ADDR);
838 ao_arch_release_interrupts();
845 ao_arch_block_interrupts();
848 ao_gpio_set(AO_USB_PULLUP_PORT, AO_USB_PULLUP_PIN, AO_USB_PULLUP, 0);
850 /* Disable interrupts */
853 lpc_nvic_clear_enable(LPC_ISR_USB_IRQ_POS);
855 /* Disable the device */
856 lpc_usb.devcmdstat = 0;
858 /* Turn off USB clock */
859 lpc_scb.usbclkdiv = 0;
861 /* Disable USB PHY and PLL */
862 lpc_scb.pdruncfg |= ((1 << LPC_SCB_PDRUNCFG_USBPAD_PD) |
863 (1 << LPC_SCB_PDRUNCFG_USBPLL_PD));
865 /* Disable USB registers and RAM */
866 lpc_scb.sysahbclkctrl &= ~((1 << LPC_SCB_SYSAHBCLKCTRL_USB) |
867 (1 << LPC_SCB_SYSAHBCLKCTRL_USBRAM));
869 ao_arch_release_interrupts();
877 /* Enable USB pins */
879 lpc_ioconf.pio0_6 = ((LPC_IOCONF_FUNC_USB_CONNECT << LPC_IOCONF_FUNC) |
880 (LPC_IOCONF_MODE_INACTIVE << LPC_IOCONF_MODE) |
881 (0 << LPC_IOCONF_HYS) |
882 (0 << LPC_IOCONF_INV) |
883 (0 << LPC_IOCONF_OD) |
887 lpc_ioconf.pio0_3 = ((LPC_IOCONF_FUNC_USB_VBUS << LPC_IOCONF_FUNC) |
888 (LPC_IOCONF_MODE_INACTIVE << LPC_IOCONF_MODE) |
889 (0 << LPC_IOCONF_HYS) |
890 (0 << LPC_IOCONF_INV) |
891 (0 << LPC_IOCONF_OD) |
894 /* Enable USB registers and RAM */
895 lpc_scb.sysahbclkctrl |= ((1 << LPC_SCB_SYSAHBCLKCTRL_USB) |
896 (1 << LPC_SCB_SYSAHBCLKCTRL_USBRAM));
899 lpc_scb.pdruncfg &= ~(1 << LPC_SCB_PDRUNCFG_USBPAD_PD);
901 /* Turn on USB PLL */
902 lpc_scb.pdruncfg &= ~(1 << LPC_SCB_PDRUNCFG_USBPLL_PD);
904 lpc_scb.usbpllclksel = (LPC_SCB_SYSPLLCLKSEL_SEL_SYSOSC << LPC_SCB_SYSPLLCLKSEL_SEL);
905 lpc_scb.usbpllclkuen = (0 << LPC_SCB_USBPLLCLKUEN_ENA);
906 lpc_scb.usbpllclkuen = (1 << LPC_SCB_USBPLLCLKUEN_ENA);
907 while (!(lpc_scb.usbpllclkuen & (1 << LPC_SCB_USBPLLCLKUEN_ENA)))
909 lpc_scb.usbpllctrl = 0x23;
910 while (!(lpc_scb.usbpllstat & 1))
913 lpc_scb.usbclksel = 0;
914 lpc_scb.usbclkuen = (0 << LPC_SCB_USBCLKUEN_ENA);
915 lpc_scb.usbclkuen = (1 << LPC_SCB_USBCLKUEN_ENA);
916 while (!(lpc_scb.usbclkuen & (1 << LPC_SCB_USBCLKUEN_ENA)))
919 /* Turn on USB clock, use 48MHz clock unchanged */
920 lpc_scb.usbclkdiv = 1;
922 /* Configure interrupts */
923 ao_arch_block_interrupts();
925 /* Route all interrupts to the main isr */
926 lpc_usb.introuting = 0;
930 lpc_nvic_set_enable(LPC_ISR_USB_IRQ_POS);
931 lpc_nvic_set_priority(LPC_ISR_USB_IRQ_POS, 0);
933 /* Clear any spurious interrupts */
934 lpc_usb.intstat = 0xffffffff;
936 debug ("ao_usb_enable\n");
938 /* Enable interrupts */
939 lpc_usb.inten = ((1 << LPC_USB_INT_EPOUT(0)) |
940 (1 << LPC_USB_INT_EPIN(0)) |
941 (1 << LPC_USB_INT_EPIN(AO_USB_INT_EP)) |
942 (1 << LPC_USB_INT_EPOUT(AO_USB_OUT_EP)) |
943 (1 << LPC_USB_INT_EPIN(AO_USB_IN_EP)) |
944 (1 << LPC_USB_INT_DEV));
946 ao_arch_release_interrupts();
948 lpc_usb.devcmdstat = 0;
949 for (t = 0; t < 1000; t++)
955 ao_gpio_set(AO_USB_PULLUP_PORT, AO_USB_PULLUP_PIN, AO_USB_PULLUP, 1);
960 struct ao_task ao_usb_echo_task;
968 c = ao_usb_getchar();
979 printf ("control: %d out: %d in: %d int: %d reset: %d\n",
980 control_count, out_count, in_count, int_count, reset_count);
983 __code struct ao_cmds ao_usb_cmds[] = {
984 { ao_usb_irq, "I\0Show USB interrupt counts" },
993 ao_enable_output(AO_USB_PULLUP_PORT, AO_USB_PULLUP_PIN, AO_USB_PULLUP, 0);
998 debug ("ao_usb_init\n");
1000 ao_add_task(&ao_usb_echo_task, ao_usb_echo, "usb echo");
1003 ao_cmd_register(&ao_usb_cmds[0]);
1006 ao_add_stdio(_ao_usb_pollchar, ao_usb_putchar, ao_usb_flush);
1010 #if TX_DBG || RX_DBG
1020 uint32_t in_pending;
1022 uint32_t in_flushed;
1032 #define NUM_USB_DBG 8
1034 static struct ao_usb_dbg dbg[NUM_USB_DBG];
1037 static void _dbg(int line, char *msg, uint32_t value)
1040 dbg[dbg_i].line = line;
1041 dbg[dbg_i].msg = msg;
1042 dbg[dbg_i].value = value;
1043 asm("mrs %0,primask" : "=&r" (primask));
1044 dbg[dbg_i].primask = primask;
1046 dbg[dbg_i].in_count = in_count;
1047 dbg[dbg_i].in_ep = *ao_usb_epn_in(AO_USB_IN_EP);
1048 dbg[dbg_i].in_pending = ao_usb_in_pending;
1049 dbg[dbg_i].tx_count = ao_usb_tx_count;
1050 dbg[dbg_i].in_flushed = ao_usb_in_flushed;
1053 dbg[dbg_i].rx_count = ao_usb_rx_count;
1054 dbg[dbg_i].rx_pos = ao_usb_rx_pos;
1055 dbg[dbg_i].out_avail = ao_usb_out_avail;
1056 dbg[dbg_i].out_ep = *ao_usb_epn_out(AO_USB_OUT_EP);
1058 if (++dbg_i == NUM_USB_DBG)