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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 #include "ao_product.h"
24 #define USE_USB_STDIO 1
28 #define AO_USB_OUT_SLEEP_ADDR (&ao_stdin_ready)
30 #define AO_USB_OUT_SLEEP_ADDR (&ao_usb_out_avail)
34 #define USB_DEBUG_DATA 0
38 #define debug(format, args...) printf(format, ## args);
40 #define debug(format, args...)
44 #define debug_data(format, args...) printf(format, ## args);
46 #define debug_data(format, args...)
50 uint8_t dir_type_recip;
57 static uint8_t ao_usb_ep0_state;
59 /* Pending EP0 IN data */
60 static const uint8_t *ao_usb_ep0_in_data; /* Remaining data */
61 static uint8_t ao_usb_ep0_in_len; /* Remaining amount */
62 static uint16_t ao_usb_ep0_in_max; /* Requested amount from host */
64 /* Temp buffer for smaller EP0 in data */
65 static uint8_t ao_usb_ep0_in_buf[2];
67 /* Pending EP0 OUT data */
68 static uint8_t *ao_usb_ep0_out_data;
69 static uint8_t ao_usb_ep0_out_len;
72 * Objects allocated in special USB memory
75 /* USB address of end of allocated storage */
76 static uint8_t *ao_usb_sram;
78 /* Pointer to ep0 tx/rx buffers in USB memory */
79 static uint8_t *ao_usb_ep0_tx_buffer;
80 static uint8_t *ao_usb_ep0_setup_buffer;
81 static uint8_t *ao_usb_ep0_rx_buffer;
83 /* Pointer to bulk data tx/rx buffers in USB memory */
84 static uint8_t *ao_usb_in_tx_buffer[2];
85 static uint8_t ao_usb_in_tx_cur;
86 static uint8_t ao_usb_tx_count;
88 static uint8_t *ao_usb_out_rx_buffer[2];
89 static uint8_t ao_usb_out_rx_cur;
90 static uint8_t ao_usb_rx_count, ao_usb_rx_pos;
92 /* Marks when we don't need to send an IN packet.
93 * This happens only when the last IN packet is not full,
94 * otherwise the host will expect to keep seeing packets.
95 * Send a zero-length packet as required
97 static uint8_t ao_usb_in_flushed;
99 /* Marks when we have delivered an IN packet to the hardware
100 * and it has not been received yet. ao_sleep on this address
101 * to wait for it to be delivered.
103 static uint8_t ao_usb_in_pending;
105 /* Marks when an OUT packet has been received by the hardware
106 * but not pulled to the shadow buffer.
108 static uint8_t ao_usb_out_avail;
109 uint8_t ao_usb_running;
110 static uint8_t ao_usb_configuration;
112 #define AO_USB_EP0_GOT_RESET 1
113 #define AO_USB_EP0_GOT_SETUP 2
114 #define AO_USB_EP0_GOT_RX_DATA 4
115 #define AO_USB_EP0_GOT_TX_ACK 8
117 static uint8_t ao_usb_ep0_receive;
118 static uint8_t ao_usb_address;
119 static uint8_t ao_usb_address_pending;
121 static inline uint32_t set_toggle(uint32_t current_value,
123 uint32_t desired_value)
125 return (current_value ^ desired_value) & mask;
129 * Set current device address and mark the
130 * interface as active
133 ao_usb_set_address(uint8_t address)
135 debug("ao_usb_set_address %02x\n", address);
136 lpc_usb.devcmdstat = ((address << LPC_USB_DEVCMDSTAT_DEV_ADDR) |
137 (1 << LPC_USB_DEVCMDSTAT_DEV_EN) |
138 (0 << LPC_USB_DEVCMDSTAT_SETUP) |
139 (0 << LPC_USB_DEVCMDSTAT_PLL_ON) |
140 (0 << LPC_USB_DEVCMDSTAT_LPM_SUP) |
141 (0 << LPC_USB_DEVCMDSTAT_INTONNAK_AO) |
142 (0 << LPC_USB_DEVCMDSTAT_INTONNAK_AI) |
143 (0 << LPC_USB_DEVCMDSTAT_INTONNAK_CO) |
144 (0 << LPC_USB_DEVCMDSTAT_INTONNAK_CI) |
145 (1 << LPC_USB_DEVCMDSTAT_DCON) |
146 (0 << LPC_USB_DEVCMDSTAT_DSUS) |
147 (0 << LPC_USB_DEVCMDSTAT_DCON_C) |
148 (0 << LPC_USB_DEVCMDSTAT_DSUS_C) |
149 (0 << LPC_USB_DEVCMDSTAT_DRES_C) |
150 (0 << LPC_USB_DEVCMDSTAT_VBUSDEBOUNCED));
151 ao_usb_address_pending = 0;
158 #define _tx_dbg0(msg) _dbg(__LINE__,msg,0)
159 #define _tx_dbg1(msg,value) _dbg(__LINE__,msg,value)
161 #define _tx_dbg0(msg)
162 #define _tx_dbg1(msg,value)
166 #define _rx_dbg0(msg) _dbg(__LINE__,msg,0)
167 #define _rx_dbg1(msg,value) _dbg(__LINE__,msg,value)
169 #define _rx_dbg0(msg)
170 #define _rx_dbg1(msg,value)
174 static void _dbg(int line, char *msg, uint32_t value);
178 * Set just endpoint 0, for use during startup
182 ao_usb_alloc_sram(uint16_t size)
184 uint8_t *addr = ao_usb_sram;
186 ao_usb_sram += (size + 63) & ~63;
191 ao_usb_sram_offset(uint8_t *addr)
193 return (uint16_t) ((intptr_t) addr >> 6);
197 ao_usb_set_ep(vuint32_t *ep, uint8_t *addr, uint16_t nbytes)
199 *ep = ((ao_usb_sram_offset(addr) << LPC_USB_EP_OFFSET) |
200 (nbytes << LPC_USB_EP_NBYTES) |
201 (0 << LPC_USB_EP_ENDPOINT_ISO) |
202 (0 << LPC_USB_EP_RATE_FEEDBACK) |
203 (0 << LPC_USB_EP_TOGGLE_RESET) |
204 (0 << LPC_USB_EP_STALL) |
205 (0 << LPC_USB_EP_DISABLED) |
206 (1 << LPC_USB_EP_ACTIVE));
209 static inline uint16_t
210 ao_usb_ep_count(vuint32_t *ep)
212 return (*ep >> LPC_USB_EP_NBYTES) & LPC_USB_EP_NBYTES_MASK;
215 static inline uint8_t
216 ao_usb_ep_stall(vuint32_t *ep)
218 return (*ep >> LPC_USB_EP_STALL) & 1;
221 static inline vuint32_t *
224 return &lpc_usb_endpoint.ep0_out;
227 static inline vuint32_t *
230 return &lpc_usb_endpoint.ep0_in;
233 static inline vuint32_t *
234 ao_usb_epn_out(uint8_t n, uint8_t i)
236 return &lpc_usb_endpoint.epn[n-1].out[i];
239 static inline vuint32_t *
240 ao_usb_epn_in(uint8_t n, uint8_t i)
242 return &lpc_usb_endpoint.epn[n-1].in[i];
247 ao_usb_set_epn_in(uint8_t n, uint8_t *addr, uint16_t nbytes)
249 ao_usb_set_ep(ao_usb_epn_in(n), addr, nbytes);
254 ao_usb_set_epn_out(uint8_t n, uint8_t *addr, uint16_t nbytes)
256 ao_usb_set_ep(ao_usb_epn_out(n, 0), addr, nbytes);
259 static inline uint16_t
260 ao_usb_epn_out_count(uint8_t n)
262 return ao_usb_ep_count(ao_usb_epn_out(n, 0));
265 static inline uint16_t
266 ao_usb_epn_in_count(uint8_t n)
268 return ao_usb_ep_count(ao_usb_epn_in(n, 0));
272 ao_usb_enable_ep(vuint32_t *ep, uint8_t *addr, uint16_t set_nbytes)
274 ao_usb_set_ep(ep, addr, set_nbytes);
278 ao_usb_disable_ep(vuint32_t *ep)
280 *ep = ((0 << LPC_USB_EP_OFFSET) |
281 (0 << LPC_USB_EP_NBYTES) |
282 (0 << LPC_USB_EP_ENDPOINT_ISO) |
283 (0 << LPC_USB_EP_RATE_FEEDBACK) |
284 (0 << LPC_USB_EP_TOGGLE_RESET) |
285 (0 << LPC_USB_EP_STALL) |
286 (1 << LPC_USB_EP_DISABLED) |
287 (0 << LPC_USB_EP_ACTIVE));
291 ao_usb_enable_epn(uint8_t n,
292 uint16_t out_bytes, uint8_t *out_addr,
295 ao_usb_enable_ep(ao_usb_epn_out(n, 0), out_addr, out_bytes);
296 ao_usb_disable_ep(ao_usb_epn_out(n, 1));
298 ao_usb_enable_ep(ao_usb_epn_in(n, 0), in_addr, 0);
299 ao_usb_disable_ep(ao_usb_epn_in(n, 1));
303 ao_usb_disable_epn(uint8_t n)
305 ao_usb_disable_ep(ao_usb_epn_out(n, 0));
306 ao_usb_disable_ep(ao_usb_epn_out(n, 1));
307 ao_usb_disable_ep(ao_usb_epn_in(n, 0));
308 ao_usb_disable_ep(ao_usb_epn_in(n, 1));
314 ao_usb_set_address(0);
315 ao_usb_configuration = 0;
317 ao_usb_ep0_state = AO_USB_EP0_IDLE;
318 ao_usb_ep0_in_data = NULL;
319 ao_usb_ep0_in_len = 0;
320 ao_usb_ep0_in_max = 0;
322 ao_usb_ep0_out_data = NULL;
323 ao_usb_ep0_out_len = 0;
331 /* Everything is single buffered for now */
332 lpc_usb.epbufcfg = 0;
334 lpc_usb.epskip = 0xffffffff;
336 lpc_usb.intstat = 0xc00003ff;
338 lpc_usb.epliststart = (uint32_t) (intptr_t) &lpc_usb_endpoint;
339 lpc_usb.databufstart = ((uint32_t) (intptr_t) ao_usb_sram) & 0xffc00000;
341 /* Set up EP 0 - a Control end point with 32 bytes of in and out buffers */
343 ao_usb_enable_ep(ao_usb_ep0_out(), ao_usb_ep0_rx_buffer, AO_USB_CONTROL_SIZE);
344 lpc_usb_endpoint.setup = ao_usb_sram_offset(ao_usb_ep0_setup_buffer);
345 ao_usb_enable_ep(ao_usb_ep0_in(), ao_usb_ep0_tx_buffer, 0);
347 /* Clear all of the other endpoints */
348 for (e = 1; e <= 4; e++)
349 ao_usb_disable_epn(e);
354 ao_usb_set_configuration(void)
356 debug ("ao_usb_set_configuration\n");
358 /* Set up the INT end point */
359 ao_usb_enable_epn(AO_USB_INT_EP, 0, NULL, NULL);
361 /* Set up the OUT end point */
362 ao_usb_enable_epn(AO_USB_OUT_EP, AO_USB_OUT_SIZE, ao_usb_out_rx_buffer[0], NULL);
364 /* Set the current RX pointer to the *other* buffer so that when buffer 0 gets
365 * data, we'll switch to it and pull bytes from there
367 ao_usb_out_rx_cur = 1;
369 /* Set up the IN end point */
370 ao_usb_enable_epn(AO_USB_IN_EP, 0, NULL, ao_usb_in_tx_buffer[0]);
371 ao_usb_in_tx_cur = 0;
373 ao_usb_in_flushed = 0;
374 ao_usb_in_pending = 0;
375 ao_wakeup(&ao_usb_in_pending);
377 ao_usb_out_avail = 0;
378 ao_usb_configuration = 0;
381 ao_wakeup(&ao_usb_running);
384 /* Send an IN data packet */
386 ao_usb_ep0_flush(void)
390 this_len = ao_usb_ep0_in_len;
391 if (this_len > AO_USB_CONTROL_SIZE)
392 this_len = AO_USB_CONTROL_SIZE;
394 ao_usb_ep0_in_len -= this_len;
395 ao_usb_ep0_in_max -= this_len;
397 if (this_len < AO_USB_CONTROL_SIZE || ao_usb_ep0_in_max == 0)
398 ao_usb_ep0_state = AO_USB_EP0_IDLE;
400 debug_data ("Flush EP0 len %d:", this_len);
401 memcpy(ao_usb_ep0_tx_buffer, ao_usb_ep0_in_data, this_len);
403 ao_usb_ep0_in_data += this_len;
405 /* Mark the endpoint as TX valid to send the packet */
406 ao_usb_set_ep(ao_usb_ep0_in(), ao_usb_ep0_tx_buffer, this_len);
407 debug ("queue tx. 0 now %08x\n", *ao_usb_ep0_in());
410 /* Read data from the ep0 OUT fifo */
412 ao_usb_ep0_fill(void)
417 /* Pull all of the data out of the packet */
418 if (lpc_usb.devcmdstat & (1 << LPC_USB_DEVCMDSTAT_SETUP)) {
419 rx_buffer = ao_usb_ep0_setup_buffer;
422 rx_buffer = ao_usb_ep0_rx_buffer;
423 len = AO_USB_CONTROL_SIZE - ao_usb_ep_count(ao_usb_ep0_out());
426 if (len > ao_usb_ep0_out_len)
427 len = ao_usb_ep0_out_len;
428 ao_usb_ep0_out_len -= len;
430 debug_data ("Fill EP0 len %d:", len);
431 memcpy(ao_usb_ep0_out_data, rx_buffer, len);
433 ao_usb_ep0_out_data += len;
436 ao_usb_set_ep(ao_usb_ep0_out(), ao_usb_ep0_rx_buffer, AO_USB_CONTROL_SIZE);
437 lpc_usb.devcmdstat |= (1 << LPC_USB_DEVCMDSTAT_SETUP);
441 ao_usb_ep0_in_reset(void)
443 ao_usb_ep0_in_data = ao_usb_ep0_in_buf;
444 ao_usb_ep0_in_len = 0;
448 ao_usb_ep0_in_queue_byte(uint8_t a)
450 if (ao_usb_ep0_in_len < sizeof (ao_usb_ep0_in_buf))
451 ao_usb_ep0_in_buf[ao_usb_ep0_in_len++] = a;
455 ao_usb_ep0_in_set(const uint8_t *data, uint8_t len)
457 ao_usb_ep0_in_data = data;
458 ao_usb_ep0_in_len = len;
462 ao_usb_ep0_out_set(uint8_t *data, uint8_t len)
464 ao_usb_ep0_out_data = data;
465 ao_usb_ep0_out_len = len;
469 ao_usb_ep0_in_start(uint16_t max)
471 ao_usb_ep0_in_max = max;
472 /* Don't send more than asked for */
473 if (ao_usb_ep0_in_len > max)
474 ao_usb_ep0_in_len = max;
478 struct ao_usb_line_coding ao_usb_line_coding = {115200, 0, 0, 8};
480 /* Walk through the list of descriptors and find a match
483 ao_usb_get_descriptor(uint16_t value, uint16_t length)
485 const uint8_t *descriptor;
486 uint8_t type = value >> 8;
487 uint8_t index = value;
489 descriptor = ao_usb_descriptors;
490 while (descriptor[0] != 0) {
491 if (descriptor[1] == type && index-- == 0) {
493 if (type == AO_USB_DESC_CONFIGURATION)
499 ao_usb_ep0_in_set(descriptor, len);
502 descriptor += descriptor[0];
507 ao_usb_ep0_setup(void)
509 /* Pull the setup packet out of the fifo */
510 ao_usb_ep0_out_set((uint8_t *) &ao_usb_setup, 8);
512 if (ao_usb_ep0_out_len != 0) {
513 debug ("invalid setup packet length\n");
517 if ((ao_usb_setup.dir_type_recip & AO_USB_DIR_IN) || ao_usb_setup.length == 0)
518 ao_usb_ep0_state = AO_USB_EP0_DATA_IN;
520 ao_usb_ep0_state = AO_USB_EP0_DATA_OUT;
522 ao_usb_ep0_in_reset();
524 switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_TYPE_MASK) {
525 case AO_USB_TYPE_STANDARD:
526 debug ("Standard setup packet\n");
527 switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_RECIP_MASK) {
528 case AO_USB_RECIP_DEVICE:
529 debug ("Device setup packet\n");
530 switch(ao_usb_setup.request) {
531 case AO_USB_REQ_GET_STATUS:
532 debug ("get status\n");
533 ao_usb_ep0_in_queue_byte(0);
534 ao_usb_ep0_in_queue_byte(0);
536 case AO_USB_REQ_SET_ADDRESS:
537 debug ("set address %d\n", ao_usb_setup.value);
538 ao_usb_address = ao_usb_setup.value;
539 ao_usb_address_pending = 1;
541 case AO_USB_REQ_GET_DESCRIPTOR:
542 debug ("get descriptor %d\n", ao_usb_setup.value);
543 ao_usb_get_descriptor(ao_usb_setup.value, ao_usb_setup.length);
545 case AO_USB_REQ_GET_CONFIGURATION:
546 debug ("get configuration %d\n", ao_usb_configuration);
547 ao_usb_ep0_in_queue_byte(ao_usb_configuration);
549 case AO_USB_REQ_SET_CONFIGURATION:
550 ao_usb_configuration = ao_usb_setup.value;
551 debug ("set configuration %d\n", ao_usb_configuration);
552 ao_usb_set_configuration();
556 case AO_USB_RECIP_INTERFACE:
557 debug ("Interface setup packet\n");
558 switch(ao_usb_setup.request) {
559 case AO_USB_REQ_GET_STATUS:
560 ao_usb_ep0_in_queue_byte(0);
561 ao_usb_ep0_in_queue_byte(0);
563 case AO_USB_REQ_GET_INTERFACE:
564 ao_usb_ep0_in_queue_byte(0);
566 case AO_USB_REQ_SET_INTERFACE:
570 case AO_USB_RECIP_ENDPOINT:
571 debug ("Endpoint setup packet\n");
572 switch(ao_usb_setup.request) {
573 case AO_USB_REQ_GET_STATUS:
574 ao_usb_ep0_in_queue_byte(0);
575 ao_usb_ep0_in_queue_byte(0);
581 case AO_USB_TYPE_CLASS:
582 debug ("Class setup packet\n");
583 switch (ao_usb_setup.request) {
584 case AO_USB_SET_LINE_CODING:
585 debug ("set line coding\n");
586 ao_usb_ep0_out_set((uint8_t *) &ao_usb_line_coding, 7);
588 case AO_USB_GET_LINE_CODING:
589 debug ("get line coding\n");
590 ao_usb_ep0_in_set((const uint8_t *) &ao_usb_line_coding, 7);
592 case AO_USB_SET_CONTROL_LINE_STATE:
598 /* If we're not waiting to receive data from the host,
599 * queue an IN response
601 if (ao_usb_ep0_state == AO_USB_EP0_DATA_IN)
602 ao_usb_ep0_in_start(ao_usb_setup.length);
606 ao_usb_ep0_handle(uint8_t receive)
608 ao_usb_ep0_receive = 0;
610 if (receive & AO_USB_EP0_GOT_RESET) {
615 if (receive & AO_USB_EP0_GOT_SETUP) {
619 if (receive & AO_USB_EP0_GOT_RX_DATA) {
620 debug ("\tgot rx data\n");
621 if (ao_usb_ep0_state == AO_USB_EP0_DATA_OUT) {
623 if (ao_usb_ep0_out_len == 0) {
624 ao_usb_ep0_state = AO_USB_EP0_DATA_IN;
625 ao_usb_ep0_in_start(0);
629 if (receive & AO_USB_EP0_GOT_TX_ACK) {
630 debug ("\tgot tx ack\n");
632 /* Wait until the IN packet is received from addr 0
633 * before assigning our local address
635 if (ao_usb_address_pending) {
637 /* Go to idle mode if USB is connected
639 ao_flight_force_idle = 1;
641 ao_usb_set_address(ao_usb_address);
643 if (ao_usb_ep0_state == AO_USB_EP0_DATA_IN)
649 static uint16_t int_count;
650 static uint16_t in_count;
651 static uint16_t out_count;
652 static uint16_t reset_count;
656 lpc_usb_irq_isr(void)
658 uint32_t intstat = lpc_usb.intstat & lpc_usb.inten;
660 lpc_usb.intstat = intstat;
661 /* Handle EP0 OUT packets */
662 if (intstat & (1 << LPC_USB_INT_EPOUT(0))) {
663 if (lpc_usb.devcmdstat & (1 << LPC_USB_DEVCMDSTAT_SETUP))
664 ao_usb_ep0_receive |= AO_USB_EP0_GOT_SETUP;
666 ao_usb_ep0_receive |= AO_USB_EP0_GOT_RX_DATA;
668 ao_usb_ep0_handle(ao_usb_ep0_receive);
671 /* Handle EP0 IN packets */
672 if (intstat & (1 << LPC_USB_INT_EPIN(0))) {
673 ao_usb_ep0_receive |= AO_USB_EP0_GOT_TX_ACK;
675 ao_usb_ep0_handle(ao_usb_ep0_receive);
679 /* Handle OUT packets */
680 if (intstat & (1 << LPC_USB_INT_EPOUT(AO_USB_OUT_EP))) {
684 _rx_dbg1("RX ISR", *ao_usb_epn_out(AO_USB_OUT_EP));
685 ao_usb_out_avail = 1;
686 _rx_dbg0("out avail set");
687 ao_wakeup(AO_USB_OUT_SLEEP_ADDR)
688 _rx_dbg0("stdin awoken");
691 /* Handle IN packets */
692 if (intstat & (1 << LPC_USB_INT_EPIN(AO_USB_IN_EP))) {
696 _tx_dbg1("TX ISR", *ao_usb_epn_in(AO_USB_IN_EP));
697 ao_usb_in_pending = 0;
698 ao_wakeup(&ao_usb_in_pending);
701 /* NAK all INT EP IN packets */
702 if (intstat & (1 << LPC_USB_INT_EPIN(AO_USB_INT_EP))) {
706 /* Check for reset */
707 if (intstat & (1 << LPC_USB_INT_DEV)) {
708 if (lpc_usb.devcmdstat & (1 << LPC_USB_DEVCMDSTAT_DRES_C))
710 lpc_usb.devcmdstat |= (1 << LPC_USB_DEVCMDSTAT_DRES_C);
711 ao_usb_ep0_receive |= AO_USB_EP0_GOT_RESET;
712 ao_usb_ep0_handle(ao_usb_ep0_receive);
719 /* Queue the current IN buffer for transmission */
721 _ao_usb_in_send(void)
723 _tx_dbg0("in_send start");
724 debug ("send %d\n", ao_usb_tx_count);
725 while (ao_usb_in_pending)
726 ao_sleep(&ao_usb_in_pending);
727 ao_usb_in_pending = 1;
728 if (ao_usb_tx_count != AO_USB_IN_SIZE)
729 ao_usb_in_flushed = 1;
730 ao_usb_set_ep(ao_usb_epn_in(AO_USB_IN_EP, 0), ao_usb_in_tx_buffer[ao_usb_in_tx_cur], ao_usb_tx_count);
731 ao_usb_in_tx_cur = 1 - ao_usb_in_tx_cur;
733 _tx_dbg0("in_send end");
736 /* Wait for a free IN buffer. Interrupts are blocked */
738 _ao_usb_in_wait(void)
741 /* Check if the current buffer is writable */
742 if (ao_usb_tx_count < AO_USB_IN_SIZE)
745 _tx_dbg0("in_wait top");
746 /* Wait for an IN buffer to be ready */
747 while (ao_usb_in_pending)
748 ao_sleep(&ao_usb_in_pending);
749 _tx_dbg0("in_wait bottom");
759 /* Anytime we've sent a character since
760 * the last time we flushed, we'll need
761 * to send a packet -- the only other time
762 * we would send a packet is when that
763 * packet was full, in which case we now
764 * want to send an empty packet
766 ao_arch_block_interrupts();
767 while (!ao_usb_in_flushed) {
768 _tx_dbg0("flush top");
770 _tx_dbg0("flush end");
772 ao_arch_release_interrupts();
776 ao_usb_putchar(char c)
781 ao_arch_block_interrupts();
784 ao_usb_in_flushed = 0;
785 ao_usb_in_tx_buffer[ao_usb_in_tx_cur][ao_usb_tx_count++] = (uint8_t) c;
787 /* Send the packet when full */
788 if (ao_usb_tx_count == AO_USB_IN_SIZE) {
789 _tx_dbg0("putchar full");
791 _tx_dbg0("putchar flushed");
793 ao_arch_release_interrupts();
797 _ao_usb_out_recv(void)
799 _rx_dbg0("out_recv top");
800 ao_usb_out_avail = 0;
802 ao_usb_rx_count = AO_USB_OUT_SIZE - ao_usb_epn_out_count(AO_USB_OUT_EP);
804 _rx_dbg1("out_recv count", ao_usb_rx_count);
805 debug ("recv %d\n", ao_usb_rx_count);
806 debug_data("Fill OUT len %d\n", ao_usb_rx_count);
808 ao_usb_out_rx_cur = 1 - ao_usb_out_rx_cur;
811 ao_usb_set_epn_out(AO_USB_OUT_EP, ao_usb_out_rx_buffer[1-ao_usb_out_rx_cur], AO_USB_OUT_SIZE);
815 _ao_usb_pollchar(void)
820 return AO_READ_AGAIN;
823 if (ao_usb_rx_pos != ao_usb_rx_count)
826 _rx_dbg0("poll check");
827 /* Check to see if a packet has arrived */
828 if (!ao_usb_out_avail) {
829 _rx_dbg0("poll none");
830 return AO_READ_AGAIN;
835 /* Pull a character out of the fifo */
836 c = ao_usb_out_rx_buffer[ao_usb_out_rx_cur][ao_usb_rx_pos++];
845 ao_arch_block_interrupts();
846 while ((c = _ao_usb_pollchar()) == AO_READ_AGAIN)
847 ao_sleep(AO_USB_OUT_SLEEP_ADDR);
848 ao_arch_release_interrupts();
855 ao_arch_block_interrupts();
858 ao_gpio_set(AO_USB_PULLUP_PORT, AO_USB_PULLUP_PIN, 0);
860 /* Disable interrupts */
863 lpc_nvic_clear_enable(LPC_ISR_USB_IRQ_POS);
865 /* Disable the device */
866 lpc_usb.devcmdstat = 0;
868 /* Turn off USB clock */
869 lpc_scb.usbclkdiv = 0;
871 /* Disable USB PHY and PLL */
872 lpc_scb.pdruncfg |= ((1 << LPC_SCB_PDRUNCFG_USBPAD_PD) |
873 (1 << LPC_SCB_PDRUNCFG_USBPLL_PD));
875 /* Disable USB registers and RAM */
876 lpc_scb.sysahbclkctrl &= ~((1 << LPC_SCB_SYSAHBCLKCTRL_USB) |
877 (1 << LPC_SCB_SYSAHBCLKCTRL_USBRAM));
879 ao_arch_release_interrupts();
887 /* Enable USB pins */
888 #if HAS_LPC_USB_CONNECT
889 lpc_ioconf.pio0_6 = ((LPC_IOCONF_FUNC_USB_CONNECT << LPC_IOCONF_FUNC) |
890 (LPC_IOCONF_MODE_INACTIVE << LPC_IOCONF_MODE) |
891 (0 << LPC_IOCONF_HYS) |
892 (0 << LPC_IOCONF_INV) |
893 (0 << LPC_IOCONF_OD) |
897 lpc_ioconf.pio0_3 = ((LPC_IOCONF_FUNC_USB_VBUS << LPC_IOCONF_FUNC) |
898 (LPC_IOCONF_MODE_INACTIVE << LPC_IOCONF_MODE) |
899 (0 << LPC_IOCONF_HYS) |
900 (0 << LPC_IOCONF_INV) |
901 (0 << LPC_IOCONF_OD) |
904 /* Enable USB registers and RAM */
905 lpc_scb.sysahbclkctrl |= ((1 << LPC_SCB_SYSAHBCLKCTRL_USB) |
906 (1 << LPC_SCB_SYSAHBCLKCTRL_USBRAM));
909 lpc_scb.pdruncfg &= ~(1 << LPC_SCB_PDRUNCFG_USBPAD_PD);
911 /* Turn on USB PLL */
912 lpc_scb.pdruncfg &= ~(1 << LPC_SCB_PDRUNCFG_USBPLL_PD);
914 lpc_scb.usbpllclksel = (LPC_SCB_SYSPLLCLKSEL_SEL_SYSOSC << LPC_SCB_SYSPLLCLKSEL_SEL);
915 lpc_scb.usbpllclkuen = (0 << LPC_SCB_USBPLLCLKUEN_ENA);
916 lpc_scb.usbpllclkuen = (1 << LPC_SCB_USBPLLCLKUEN_ENA);
917 while (!(lpc_scb.usbpllclkuen & (1 << LPC_SCB_USBPLLCLKUEN_ENA)))
919 lpc_scb.usbpllctrl = 0x23;
920 while (!(lpc_scb.usbpllstat & 1))
923 lpc_scb.usbclksel = 0;
924 lpc_scb.usbclkuen = (0 << LPC_SCB_USBCLKUEN_ENA);
925 lpc_scb.usbclkuen = (1 << LPC_SCB_USBCLKUEN_ENA);
926 while (!(lpc_scb.usbclkuen & (1 << LPC_SCB_USBCLKUEN_ENA)))
929 /* Turn on USB clock, use 48MHz clock unchanged */
930 lpc_scb.usbclkdiv = 1;
932 /* Configure interrupts */
933 ao_arch_block_interrupts();
935 /* Route all interrupts to the main isr */
936 lpc_usb.introuting = 0;
940 lpc_nvic_set_enable(LPC_ISR_USB_IRQ_POS);
941 lpc_nvic_set_priority(LPC_ISR_USB_IRQ_POS, 0);
943 /* Clear any spurious interrupts */
944 lpc_usb.intstat = 0xffffffff;
946 debug ("ao_usb_enable\n");
948 /* Enable interrupts */
949 lpc_usb.inten = ((1 << LPC_USB_INT_EPOUT(0)) |
950 (1 << LPC_USB_INT_EPIN(0)) |
951 (1 << LPC_USB_INT_EPIN(AO_USB_INT_EP)) |
952 (1 << LPC_USB_INT_EPOUT(AO_USB_OUT_EP)) |
953 (1 << LPC_USB_INT_EPIN(AO_USB_IN_EP)) |
954 (1 << LPC_USB_INT_DEV));
956 ao_arch_release_interrupts();
958 lpc_usb.devcmdstat = 0;
959 for (t = 0; t < 1000; t++)
962 ao_usb_sram = lpc_usb_sram;
964 ao_usb_ep0_rx_buffer = ao_usb_alloc_sram(AO_USB_CONTROL_SIZE);
965 ao_usb_ep0_tx_buffer = ao_usb_alloc_sram(AO_USB_CONTROL_SIZE);
966 ao_usb_ep0_setup_buffer = ao_usb_alloc_sram(AO_USB_CONTROL_SIZE);
968 ao_usb_out_rx_buffer[0] = ao_usb_alloc_sram(AO_USB_OUT_SIZE);
969 ao_usb_out_rx_buffer[1] = ao_usb_alloc_sram(AO_USB_OUT_SIZE);
970 ao_usb_in_tx_buffer[0] = ao_usb_alloc_sram(AO_USB_IN_SIZE);
971 ao_usb_in_tx_buffer[1] = ao_usb_alloc_sram(AO_USB_IN_SIZE);
976 ao_gpio_set(AO_USB_PULLUP_PORT, AO_USB_PULLUP_PIN, 1);
981 struct ao_task ao_usb_echo_task;
989 c = ao_usb_getchar();
1000 printf ("control: %d out: %d in: %d int: %d reset: %d\n",
1001 control_count, out_count, in_count, int_count, reset_count);
1004 const struct ao_cmds ao_usb_cmds[] = {
1005 { ao_usb_irq, "I\0Show USB interrupt counts" },
1015 ao_enable_output(AO_USB_PULLUP_PORT, AO_USB_PULLUP_PIN, 0);
1017 for (i = 0; i < 40000; i++)
1022 debug ("ao_usb_init\n");
1024 ao_add_task(&ao_usb_echo_task, ao_usb_echo, "usb echo");
1027 ao_cmd_register(&ao_usb_cmds[0]);
1030 ao_add_stdio(_ao_usb_pollchar, ao_usb_putchar, ao_usb_flush);
1034 #if TX_DBG || RX_DBG
1044 uint32_t in_pending;
1046 uint32_t in_flushed;
1056 #define NUM_USB_DBG 8
1058 static struct ao_usb_dbg dbg[NUM_USB_DBG];
1061 static void _dbg(int line, char *msg, uint32_t value)
1064 dbg[dbg_i].line = line;
1065 dbg[dbg_i].msg = msg;
1066 dbg[dbg_i].value = value;
1067 asm("mrs %0,primask" : "=&r" (primask));
1068 dbg[dbg_i].primask = primask;
1070 dbg[dbg_i].in_count = in_count;
1071 dbg[dbg_i].in_ep = *ao_usb_epn_in(AO_USB_IN_EP, 0);
1072 dbg[dbg_i].in_pending = ao_usb_in_pending;
1073 dbg[dbg_i].tx_count = ao_usb_tx_count;
1074 dbg[dbg_i].in_flushed = ao_usb_in_flushed;
1077 dbg[dbg_i].rx_count = ao_usb_rx_count;
1078 dbg[dbg_i].rx_pos = ao_usb_rx_pos;
1079 dbg[dbg_i].out_avail = ao_usb_out_avail;
1080 dbg[dbg_i].out_ep = *ao_usb_epn_out(AO_USB_OUT_EP, 0);
1082 if (++dbg_i == NUM_USB_DBG)