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]);
322 ao_usb_set_address(0);
323 ao_usb_configuration = 0;
331 /* Everything is single buffered for now */
332 lpc_usb.epbufcfg = 0;
334 lpc_usb.epskip = 0xffffffff;
336 lpc_usb.intstat = 0xc00003ff;
338 ao_usb_sram = lpc_usb_sram;
340 lpc_usb.epliststart = (uint32_t) (intptr_t) &lpc_usb_endpoint;
341 lpc_usb.databufstart = ((uint32_t) (intptr_t) ao_usb_sram) & 0xffc00000;
343 /* Set up EP 0 - a Control end point with 32 bytes of in and out buffers */
345 ao_usb_ep0_rx_buffer = ao_usb_enable_ep(ao_usb_ep0_out(), AO_USB_CONTROL_SIZE, AO_USB_CONTROL_SIZE);
346 ao_usb_ep0_setup_buffer = ao_usb_alloc_sram(AO_USB_CONTROL_SIZE);
347 lpc_usb_endpoint.setup = ao_usb_sram_offset(ao_usb_ep0_setup_buffer);
348 ao_usb_ep0_tx_buffer = ao_usb_enable_ep(ao_usb_ep0_in(), AO_USB_CONTROL_SIZE, 0);
350 /* Clear all of the other endpoints */
351 for (e = 1; e <= 4; e++)
352 ao_usb_disable_epn(e);
357 ao_usb_set_configuration(void)
359 debug ("ao_usb_set_configuration\n");
361 /* Set up the INT end point */
362 ao_usb_enable_epn(AO_USB_INT_EP, 0, NULL, 0, NULL);
364 /* Set up the OUT end point */
365 ao_usb_enable_epn(AO_USB_OUT_EP, AO_USB_OUT_SIZE, &ao_usb_out_rx_buffer, 0, NULL);
367 /* Set up the IN end point */
368 ao_usb_enable_epn(AO_USB_IN_EP, 0, NULL, AO_USB_IN_SIZE, &ao_usb_in_tx_buffer);
373 /* Send an IN data packet */
375 ao_usb_ep0_flush(void)
379 this_len = ao_usb_ep0_in_len;
380 if (this_len > AO_USB_CONTROL_SIZE)
381 this_len = AO_USB_CONTROL_SIZE;
383 if (this_len < AO_USB_CONTROL_SIZE)
384 ao_usb_ep0_state = AO_USB_EP0_IDLE;
386 ao_usb_ep0_in_len -= this_len;
388 debug_data ("Flush EP0 len %d:", this_len);
389 memcpy(ao_usb_ep0_tx_buffer, ao_usb_ep0_in_data, this_len);
391 ao_usb_ep0_in_data += this_len;
393 /* Mark the endpoint as TX valid to send the packet */
394 ao_usb_set_ep(ao_usb_ep0_in(), ao_usb_ep0_tx_buffer, this_len);
395 debug ("queue tx. 0 now %08x\n", *ao_usb_ep0_in());
398 /* Read data from the ep0 OUT fifo */
400 ao_usb_ep0_fill(void)
405 /* Pull all of the data out of the packet */
406 if (lpc_usb.devcmdstat & (1 << LPC_USB_DEVCMDSTAT_SETUP)) {
407 rx_buffer = ao_usb_ep0_setup_buffer;
410 rx_buffer = ao_usb_ep0_rx_buffer;
411 len = AO_USB_CONTROL_SIZE - ao_usb_ep_count(ao_usb_ep0_out());
414 if (len > ao_usb_ep0_out_len)
415 len = ao_usb_ep0_out_len;
416 ao_usb_ep0_out_len -= len;
418 debug_data ("Fill EP0 len %d:", len);
419 memcpy(ao_usb_ep0_out_data, rx_buffer, len);
421 ao_usb_ep0_out_data += len;
424 ao_usb_set_ep(ao_usb_ep0_out(), ao_usb_ep0_rx_buffer, AO_USB_CONTROL_SIZE);
425 lpc_usb.devcmdstat |= (1 << LPC_USB_DEVCMDSTAT_SETUP);
429 ao_usb_ep0_in_reset(void)
431 ao_usb_ep0_in_data = ao_usb_ep0_in_buf;
432 ao_usb_ep0_in_len = 0;
436 ao_usb_ep0_in_queue_byte(uint8_t a)
438 if (ao_usb_ep0_in_len < sizeof (ao_usb_ep0_in_buf))
439 ao_usb_ep0_in_buf[ao_usb_ep0_in_len++] = a;
443 ao_usb_ep0_in_set(const uint8_t *data, uint8_t len)
445 ao_usb_ep0_in_data = data;
446 ao_usb_ep0_in_len = len;
450 ao_usb_ep0_out_set(uint8_t *data, uint8_t len)
452 ao_usb_ep0_out_data = data;
453 ao_usb_ep0_out_len = len;
457 ao_usb_ep0_in_start(uint16_t max)
459 /* Don't send more than asked for */
460 if (ao_usb_ep0_in_len > max)
461 ao_usb_ep0_in_len = max;
465 static struct ao_usb_line_coding ao_usb_line_coding = {115200, 0, 0, 8};
467 /* Walk through the list of descriptors and find a match
470 ao_usb_get_descriptor(uint16_t value)
472 const uint8_t *descriptor;
473 uint8_t type = value >> 8;
474 uint8_t index = value;
476 descriptor = ao_usb_descriptors;
477 while (descriptor[0] != 0) {
478 if (descriptor[1] == type && index-- == 0) {
480 if (type == AO_USB_DESC_CONFIGURATION)
484 ao_usb_ep0_in_set(descriptor, len);
487 descriptor += descriptor[0];
492 ao_usb_ep0_setup(void)
494 /* Pull the setup packet out of the fifo */
495 ao_usb_ep0_out_set((uint8_t *) &ao_usb_setup, 8);
497 if (ao_usb_ep0_out_len != 0) {
498 debug ("invalid setup packet length\n");
502 if ((ao_usb_setup.dir_type_recip & AO_USB_DIR_IN) || ao_usb_setup.length == 0)
503 ao_usb_ep0_state = AO_USB_EP0_DATA_IN;
505 ao_usb_ep0_state = AO_USB_EP0_DATA_OUT;
507 ao_usb_ep0_in_reset();
509 switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_TYPE_MASK) {
510 case AO_USB_TYPE_STANDARD:
511 debug ("Standard setup packet\n");
512 switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_RECIP_MASK) {
513 case AO_USB_RECIP_DEVICE:
514 debug ("Device setup packet\n");
515 switch(ao_usb_setup.request) {
516 case AO_USB_REQ_GET_STATUS:
517 debug ("get status\n");
518 ao_usb_ep0_in_queue_byte(0);
519 ao_usb_ep0_in_queue_byte(0);
521 case AO_USB_REQ_SET_ADDRESS:
522 debug ("set address %d\n", ao_usb_setup.value);
523 ao_usb_address = ao_usb_setup.value;
524 ao_usb_address_pending = 1;
526 case AO_USB_REQ_GET_DESCRIPTOR:
527 debug ("get descriptor %d\n", ao_usb_setup.value);
528 ao_usb_get_descriptor(ao_usb_setup.value);
530 case AO_USB_REQ_GET_CONFIGURATION:
531 debug ("get configuration %d\n", ao_usb_configuration);
532 ao_usb_ep0_in_queue_byte(ao_usb_configuration);
534 case AO_USB_REQ_SET_CONFIGURATION:
535 ao_usb_configuration = ao_usb_setup.value;
536 debug ("set configuration %d\n", ao_usb_configuration);
537 ao_usb_set_configuration();
541 case AO_USB_RECIP_INTERFACE:
542 debug ("Interface 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);
548 case AO_USB_REQ_GET_INTERFACE:
549 ao_usb_ep0_in_queue_byte(0);
551 case AO_USB_REQ_SET_INTERFACE:
555 case AO_USB_RECIP_ENDPOINT:
556 debug ("Endpoint setup packet\n");
557 switch(ao_usb_setup.request) {
558 case AO_USB_REQ_GET_STATUS:
559 ao_usb_ep0_in_queue_byte(0);
560 ao_usb_ep0_in_queue_byte(0);
566 case AO_USB_TYPE_CLASS:
567 debug ("Class setup packet\n");
568 switch (ao_usb_setup.request) {
569 case AO_USB_SET_LINE_CODING:
570 debug ("set line coding\n");
571 ao_usb_ep0_out_set((uint8_t *) &ao_usb_line_coding, 7);
573 case AO_USB_GET_LINE_CODING:
574 debug ("get line coding\n");
575 ao_usb_ep0_in_set((const uint8_t *) &ao_usb_line_coding, 7);
577 case AO_USB_SET_CONTROL_LINE_STATE:
583 /* If we're not waiting to receive data from the host,
584 * queue an IN response
586 if (ao_usb_ep0_state == AO_USB_EP0_DATA_IN)
587 ao_usb_ep0_in_start(ao_usb_setup.length);
591 ao_usb_ep0_handle(uint8_t receive)
593 ao_usb_ep0_receive = 0;
595 if (receive & AO_USB_EP0_GOT_RESET) {
600 if (receive & AO_USB_EP0_GOT_SETUP) {
604 if (receive & AO_USB_EP0_GOT_RX_DATA) {
605 debug ("\tgot rx data\n");
606 if (ao_usb_ep0_state == AO_USB_EP0_DATA_OUT) {
608 if (ao_usb_ep0_out_len == 0) {
609 ao_usb_ep0_state = AO_USB_EP0_DATA_IN;
610 ao_usb_ep0_in_start(0);
614 if (receive & AO_USB_EP0_GOT_TX_ACK) {
615 debug ("\tgot tx ack\n");
617 /* Wait until the IN packet is received from addr 0
618 * before assigning our local address
620 if (ao_usb_address_pending) {
622 /* Go to idle mode if USB is connected
624 ao_flight_force_idle = 1;
626 ao_usb_set_address(ao_usb_address);
628 if (ao_usb_ep0_state == AO_USB_EP0_DATA_IN)
633 static uint16_t control_count;
634 static uint16_t int_count;
635 static uint16_t in_count;
636 static uint16_t out_count;
637 static uint16_t reset_count;
640 lpc_usb_irq_isr(void)
642 uint32_t intstat = lpc_usb.intstat & lpc_usb.inten;
644 lpc_usb.intstat = intstat;
645 /* Handle EP0 OUT packets */
646 if (intstat & (1 << LPC_USB_INT_EPOUT(0))) {
647 if (lpc_usb.devcmdstat & (1 << LPC_USB_DEVCMDSTAT_SETUP))
648 ao_usb_ep0_receive |= AO_USB_EP0_GOT_SETUP;
650 ao_usb_ep0_receive |= AO_USB_EP0_GOT_RX_DATA;
652 ao_usb_ep0_handle(ao_usb_ep0_receive);
655 /* Handle EP0 IN packets */
656 if (intstat & (1 << LPC_USB_INT_EPIN(0))) {
657 ao_usb_ep0_receive |= AO_USB_EP0_GOT_TX_ACK;
659 ao_usb_ep0_handle(ao_usb_ep0_receive);
663 /* Handle OUT packets */
664 if (intstat & (1 << LPC_USB_INT_EPOUT(AO_USB_OUT_EP))) {
666 _rx_dbg1("RX ISR", *ao_usb_epn_out(AO_USB_OUT_EP));
667 ao_usb_out_avail = 1;
668 _rx_dbg0("out avail set");
669 ao_wakeup(AO_USB_OUT_SLEEP_ADDR)
670 _rx_dbg0("stdin awoken");
673 /* Handle IN packets */
674 if (intstat & (1 << LPC_USB_INT_EPIN(AO_USB_IN_EP))) {
676 _tx_dbg1("TX ISR", *ao_usb_epn_in(AO_USB_IN_EP));
677 ao_usb_in_pending = 0;
678 ao_wakeup(&ao_usb_in_pending);
681 /* NAK all INT EP IN packets */
682 if (intstat & (1 << LPC_USB_INT_EPIN(AO_USB_INT_EP))) {
686 /* Check for reset */
687 if (intstat & (1 << LPC_USB_INT_DEV)) {
688 if (lpc_usb.devcmdstat & (1 << LPC_USB_DEVCMDSTAT_DRES_C))
690 lpc_usb.devcmdstat |= (1 << LPC_USB_DEVCMDSTAT_DRES_C);
691 ao_usb_ep0_receive |= AO_USB_EP0_GOT_RESET;
692 ao_usb_ep0_handle(ao_usb_ep0_receive);
699 /* Queue the current IN buffer for transmission */
701 _ao_usb_in_send(void)
703 _tx_dbg0("in_send start");
704 debug ("send %d\n", ao_usb_tx_count);
705 while (ao_usb_in_pending)
706 ao_sleep(&ao_usb_in_pending);
707 ao_usb_in_pending = 1;
708 if (ao_usb_tx_count != AO_USB_IN_SIZE)
709 ao_usb_in_flushed = 1;
710 memcpy(ao_usb_in_tx_buffer, ao_usb_tx_buffer, ao_usb_tx_count);
711 ao_usb_set_ep(ao_usb_epn_in(AO_USB_IN_EP), ao_usb_in_tx_buffer, ao_usb_tx_count);
713 _tx_dbg0("in_send end");
716 /* Wait for a free IN buffer. Interrupts are blocked */
718 _ao_usb_in_wait(void)
721 /* Check if the current buffer is writable */
722 if (ao_usb_tx_count < AO_USB_IN_SIZE)
725 _tx_dbg0("in_wait top");
726 /* Wait for an IN buffer to be ready */
727 while (ao_usb_in_pending)
728 ao_sleep(&ao_usb_in_pending);
729 _tx_dbg0("in_wait bottom");
739 /* Anytime we've sent a character since
740 * the last time we flushed, we'll need
741 * to send a packet -- the only other time
742 * we would send a packet is when that
743 * packet was full, in which case we now
744 * want to send an empty packet
746 ao_arch_block_interrupts();
747 while (!ao_usb_in_flushed) {
748 _tx_dbg0("flush top");
750 _tx_dbg0("flush end");
752 ao_arch_release_interrupts();
756 ao_usb_putchar(char c)
761 ao_arch_block_interrupts();
764 ao_usb_in_flushed = 0;
765 ao_usb_tx_buffer[ao_usb_tx_count++] = (uint8_t) c;
767 /* Send the packet when full */
768 if (ao_usb_tx_count == AO_USB_IN_SIZE) {
769 _tx_dbg0("putchar full");
771 _tx_dbg0("putchar flushed");
773 ao_arch_release_interrupts();
777 _ao_usb_out_recv(void)
779 _rx_dbg0("out_recv top");
780 ao_usb_out_avail = 0;
782 ao_usb_rx_count = AO_USB_OUT_SIZE - ao_usb_epn_out_count(AO_USB_OUT_EP);
784 _rx_dbg1("out_recv count", ao_usb_rx_count);
785 debug ("recv %d\n", ao_usb_rx_count);
786 debug_data("Fill OUT len %d:", ao_usb_rx_count);
787 memcpy(ao_usb_rx_buffer, ao_usb_out_rx_buffer, ao_usb_rx_count);
792 ao_usb_set_epn_out(AO_USB_OUT_EP, ao_usb_out_rx_buffer, AO_USB_OUT_SIZE);
796 _ao_usb_pollchar(void)
801 return AO_READ_AGAIN;
804 if (ao_usb_rx_pos != ao_usb_rx_count)
807 _rx_dbg0("poll check");
808 /* Check to see if a packet has arrived */
809 if (!ao_usb_out_avail) {
810 _rx_dbg0("poll none");
811 return AO_READ_AGAIN;
816 /* Pull a character out of the fifo */
817 c = ao_usb_rx_buffer[ao_usb_rx_pos++];
826 ao_arch_block_interrupts();
827 while ((c = _ao_usb_pollchar()) == AO_READ_AGAIN)
828 ao_sleep(AO_USB_OUT_SLEEP_ADDR);
829 ao_arch_release_interrupts();
836 ao_arch_block_interrupts();
839 ao_gpio_set(AO_USB_PULLUP_PORT, AO_USB_PULLUP_PIN, AO_USB_PULLUP, 0);
841 /* Disable interrupts */
844 lpc_nvic_clear_enable(LPC_ISR_USB_IRQ_POS);
846 /* Disable the device */
847 lpc_usb.devcmdstat = 0;
849 /* Turn off USB clock */
850 lpc_scb.usbclkdiv = 0;
852 /* Disable USB PHY and PLL */
853 lpc_scb.pdruncfg |= ((1 << LPC_SCB_PDRUNCFG_USBPAD_PD) |
854 (1 << LPC_SCB_PDRUNCFG_USBPLL_PD));
856 /* Disable USB registers and RAM */
857 lpc_scb.sysahbclkctrl &= ~((1 << LPC_SCB_SYSAHBCLKCTRL_USB) |
858 (1 << LPC_SCB_SYSAHBCLKCTRL_USBRAM));
860 ao_arch_release_interrupts();
868 /* Enable USB pins */
870 lpc_ioconf.pio0_6 = ((LPC_IOCONF_FUNC_USB_CONNECT << LPC_IOCONF_FUNC) |
871 (LPC_IOCONF_MODE_INACTIVE << LPC_IOCONF_MODE) |
872 (0 << LPC_IOCONF_HYS) |
873 (0 << LPC_IOCONF_INV) |
874 (0 << LPC_IOCONF_OD) |
878 lpc_ioconf.pio0_3 = ((LPC_IOCONF_FUNC_USB_VBUS << LPC_IOCONF_FUNC) |
879 (LPC_IOCONF_MODE_INACTIVE << LPC_IOCONF_MODE) |
880 (0 << LPC_IOCONF_HYS) |
881 (0 << LPC_IOCONF_INV) |
882 (0 << LPC_IOCONF_OD) |
885 /* Enable USB registers and RAM */
886 lpc_scb.sysahbclkctrl |= ((1 << LPC_SCB_SYSAHBCLKCTRL_USB) |
887 (1 << LPC_SCB_SYSAHBCLKCTRL_USBRAM));
890 lpc_scb.pdruncfg &= ~(1 << LPC_SCB_PDRUNCFG_USBPAD_PD);
892 /* Turn on USB PLL */
893 lpc_scb.pdruncfg &= ~(1 << LPC_SCB_PDRUNCFG_USBPLL_PD);
895 lpc_scb.usbpllclksel = (LPC_SCB_SYSPLLCLKSEL_SEL_SYSOSC << LPC_SCB_SYSPLLCLKSEL_SEL);
896 lpc_scb.usbpllclkuen = (0 << LPC_SCB_USBPLLCLKUEN_ENA);
897 lpc_scb.usbpllclkuen = (1 << LPC_SCB_USBPLLCLKUEN_ENA);
898 while (!(lpc_scb.usbpllclkuen & (1 << LPC_SCB_USBPLLCLKUEN_ENA)))
900 lpc_scb.usbpllctrl = 0x23;
901 while (!(lpc_scb.usbpllstat & 1))
904 lpc_scb.usbclksel = 0;
905 lpc_scb.usbclkuen = (0 << LPC_SCB_USBCLKUEN_ENA);
906 lpc_scb.usbclkuen = (1 << LPC_SCB_USBCLKUEN_ENA);
907 while (!(lpc_scb.usbclkuen & (1 << LPC_SCB_USBCLKUEN_ENA)))
910 /* Turn on USB clock, use 48MHz clock unchanged */
911 lpc_scb.usbclkdiv = 1;
913 /* Configure interrupts */
914 ao_arch_block_interrupts();
916 /* Route all interrupts to the main isr */
917 lpc_usb.introuting = 0;
921 lpc_nvic_set_enable(LPC_ISR_USB_IRQ_POS);
922 lpc_nvic_set_priority(LPC_ISR_USB_IRQ_POS, 0);
924 /* Clear any spurious interrupts */
925 lpc_usb.intstat = 0xffffffff;
927 debug ("ao_usb_enable\n");
929 /* Enable interrupts */
930 lpc_usb.inten = ((1 << LPC_USB_INT_EPOUT(0)) |
931 (1 << LPC_USB_INT_EPIN(0)) |
932 (1 << LPC_USB_INT_EPIN(AO_USB_INT_EP)) |
933 (1 << LPC_USB_INT_EPOUT(AO_USB_OUT_EP)) |
934 (1 << LPC_USB_INT_EPIN(AO_USB_IN_EP)) |
935 (1 << LPC_USB_INT_DEV));
937 ao_arch_release_interrupts();
939 lpc_usb.devcmdstat = 0;
940 for (t = 0; t < 1000; t++)
946 ao_gpio_set(AO_USB_PULLUP_PORT, AO_USB_PULLUP_PIN, AO_USB_PULLUP, 1);
951 struct ao_task ao_usb_echo_task;
959 c = ao_usb_getchar();
970 printf ("control: %d out: %d in: %d int: %d reset: %d\n",
971 control_count, out_count, in_count, int_count, reset_count);
974 __code struct ao_cmds ao_usb_cmds[] = {
975 { ao_usb_irq, "I\0Show USB interrupt counts" },
984 ao_enable_output(AO_USB_PULLUP_PORT, AO_USB_PULLUP_PIN, AO_USB_PULLUP, 0);
989 debug ("ao_usb_init\n");
991 ao_add_task(&ao_usb_echo_task, ao_usb_echo, "usb echo");
994 ao_cmd_register(&ao_usb_cmds[0]);
997 ao_add_stdio(_ao_usb_pollchar, ao_usb_putchar, ao_usb_flush);
1001 #if TX_DBG || RX_DBG
1011 uint32_t in_pending;
1013 uint32_t in_flushed;
1023 #define NUM_USB_DBG 8
1025 static struct ao_usb_dbg dbg[NUM_USB_DBG];
1028 static void _dbg(int line, char *msg, uint32_t value)
1031 dbg[dbg_i].line = line;
1032 dbg[dbg_i].msg = msg;
1033 dbg[dbg_i].value = value;
1034 asm("mrs %0,primask" : "=&r" (primask));
1035 dbg[dbg_i].primask = primask;
1037 dbg[dbg_i].in_count = in_count;
1038 dbg[dbg_i].in_ep = *ao_usb_epn_in(AO_USB_IN_EP);
1039 dbg[dbg_i].in_pending = ao_usb_in_pending;
1040 dbg[dbg_i].tx_count = ao_usb_tx_count;
1041 dbg[dbg_i].in_flushed = ao_usb_in_flushed;
1044 dbg[dbg_i].rx_count = ao_usb_rx_count;
1045 dbg[dbg_i].rx_pos = ao_usb_rx_pos;
1046 dbg[dbg_i].out_avail = ao_usb_out_avail;
1047 dbg[dbg_i].out_ep = *ao_usb_epn_out(AO_USB_OUT_EP);
1049 if (++dbg_i == NUM_USB_DBG)