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[2];
84 static uint8_t ao_usb_in_tx_cur;
85 static uint8_t ao_usb_tx_count;
87 static uint8_t *ao_usb_out_rx_buffer[2];
88 static uint8_t ao_usb_out_rx_cur;
89 static uint8_t ao_usb_rx_count, ao_usb_rx_pos;
91 extern struct lpc_usb_endpoint lpc_usb_endpoint;
93 /* Marks when we don't need to send an IN packet.
94 * This happens only when the last IN packet is not full,
95 * otherwise the host will expect to keep seeing packets.
96 * Send a zero-length packet as required
98 static uint8_t ao_usb_in_flushed;
100 /* Marks when we have delivered an IN packet to the hardware
101 * and it has not been received yet. ao_sleep on this address
102 * to wait for it to be delivered.
104 static uint8_t ao_usb_in_pending;
106 /* Marks when an OUT packet has been received by the hardware
107 * but not pulled to the shadow buffer.
109 static uint8_t ao_usb_out_avail;
110 uint8_t ao_usb_running;
111 static uint8_t ao_usb_configuration;
113 #define AO_USB_EP0_GOT_RESET 1
114 #define AO_USB_EP0_GOT_SETUP 2
115 #define AO_USB_EP0_GOT_RX_DATA 4
116 #define AO_USB_EP0_GOT_TX_ACK 8
118 static uint8_t ao_usb_ep0_receive;
119 static uint8_t ao_usb_address;
120 static uint8_t ao_usb_address_pending;
122 static inline uint32_t set_toggle(uint32_t current_value,
124 uint32_t desired_value)
126 return (current_value ^ desired_value) & mask;
130 * Set current device address and mark the
131 * interface as active
134 ao_usb_set_address(uint8_t address)
136 debug("ao_usb_set_address %02x\n", address);
137 lpc_usb.devcmdstat = ((address << LPC_USB_DEVCMDSTAT_DEV_ADDR) |
138 (1 << LPC_USB_DEVCMDSTAT_DEV_EN) |
139 (0 << LPC_USB_DEVCMDSTAT_SETUP) |
140 (0 << LPC_USB_DEVCMDSTAT_PLL_ON) |
141 (0 << LPC_USB_DEVCMDSTAT_LPM_SUP) |
142 (0 << LPC_USB_DEVCMDSTAT_INTONNAK_AO) |
143 (0 << LPC_USB_DEVCMDSTAT_INTONNAK_AI) |
144 (0 << LPC_USB_DEVCMDSTAT_INTONNAK_CO) |
145 (0 << LPC_USB_DEVCMDSTAT_INTONNAK_CI) |
146 (1 << LPC_USB_DEVCMDSTAT_DCON) |
147 (0 << LPC_USB_DEVCMDSTAT_DSUS) |
148 (0 << LPC_USB_DEVCMDSTAT_DCON_C) |
149 (0 << LPC_USB_DEVCMDSTAT_DSUS_C) |
150 (0 << LPC_USB_DEVCMDSTAT_DRES_C) |
151 (0 << LPC_USB_DEVCMDSTAT_VBUSDEBOUNCED));
152 ao_usb_address_pending = 0;
159 #define _tx_dbg0(msg) _dbg(__LINE__,msg,0)
160 #define _tx_dbg1(msg,value) _dbg(__LINE__,msg,value)
162 #define _tx_dbg0(msg)
163 #define _tx_dbg1(msg,value)
167 #define _rx_dbg0(msg) _dbg(__LINE__,msg,0)
168 #define _rx_dbg1(msg,value) _dbg(__LINE__,msg,value)
170 #define _rx_dbg0(msg)
171 #define _rx_dbg1(msg,value)
175 static void _dbg(int line, char *msg, uint32_t value);
179 * Set just endpoint 0, for use during startup
183 ao_usb_alloc_sram(uint16_t size)
185 uint8_t *addr = ao_usb_sram;
187 ao_usb_sram += (size + 63) & ~63;
192 ao_usb_sram_offset(uint8_t *addr)
194 return (uint16_t) ((intptr_t) addr >> 6);
198 ao_usb_set_ep(vuint32_t *ep, uint8_t *addr, uint16_t nbytes)
200 *ep = ((ao_usb_sram_offset(addr) << LPC_USB_EP_OFFSET) |
201 (nbytes << LPC_USB_EP_NBYTES) |
202 (0 << LPC_USB_EP_ENDPOINT_ISO) |
203 (0 << LPC_USB_EP_RATE_FEEDBACK) |
204 (0 << LPC_USB_EP_TOGGLE_RESET) |
205 (0 << LPC_USB_EP_STALL) |
206 (0 << LPC_USB_EP_DISABLED) |
207 (1 << LPC_USB_EP_ACTIVE));
210 static inline uint16_t
211 ao_usb_ep_count(vuint32_t *ep)
213 return (*ep >> LPC_USB_EP_NBYTES) & LPC_USB_EP_NBYTES_MASK;
216 static inline uint8_t
217 ao_usb_ep_stall(vuint32_t *ep)
219 return (*ep >> LPC_USB_EP_STALL) & 1;
222 static inline vuint32_t *
225 return &lpc_usb_endpoint.ep0_out;
228 static inline vuint32_t *
231 return &lpc_usb_endpoint.ep0_in;
234 static inline vuint32_t *
235 ao_usb_epn_out(uint8_t n, uint8_t i)
237 return &lpc_usb_endpoint.epn[n-1].out[i];
240 static inline vuint32_t *
241 ao_usb_epn_in(uint8_t n, uint8_t i)
243 return &lpc_usb_endpoint.epn[n-1].in[i];
248 ao_usb_set_epn_in(uint8_t n, uint8_t *addr, uint16_t nbytes)
250 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, 0), 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, 0));
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, 0));
273 ao_usb_enable_ep(vuint32_t *ep, uint8_t *addr, uint16_t set_nbytes)
275 ao_usb_set_ep(ep, addr, set_nbytes);
279 ao_usb_disable_ep(vuint32_t *ep)
281 *ep = ((0 << LPC_USB_EP_OFFSET) |
282 (0 << LPC_USB_EP_NBYTES) |
283 (0 << LPC_USB_EP_ENDPOINT_ISO) |
284 (0 << LPC_USB_EP_RATE_FEEDBACK) |
285 (0 << LPC_USB_EP_TOGGLE_RESET) |
286 (0 << LPC_USB_EP_STALL) |
287 (1 << LPC_USB_EP_DISABLED) |
288 (0 << LPC_USB_EP_ACTIVE));
292 ao_usb_enable_epn(uint8_t n,
293 uint16_t out_bytes, uint8_t *out_addr,
296 ao_usb_enable_ep(ao_usb_epn_out(n, 0), out_addr, out_bytes);
297 ao_usb_disable_ep(ao_usb_epn_out(n, 1));
299 ao_usb_enable_ep(ao_usb_epn_in(n, 0), in_addr, 0);
300 ao_usb_disable_ep(ao_usb_epn_in(n, 1));
304 ao_usb_disable_epn(uint8_t n)
306 ao_usb_disable_ep(ao_usb_epn_out(n, 0));
307 ao_usb_disable_ep(ao_usb_epn_out(n, 1));
308 ao_usb_disable_ep(ao_usb_epn_in(n, 0));
309 ao_usb_disable_ep(ao_usb_epn_in(n, 1));
315 ao_usb_set_address(0);
316 ao_usb_configuration = 0;
318 ao_usb_ep0_state = AO_USB_EP0_IDLE;
319 ao_usb_ep0_in_data = NULL;
320 ao_usb_ep0_in_len = 0;
321 ao_usb_ep0_in_max = 0;
323 ao_usb_ep0_out_data = NULL;
324 ao_usb_ep0_out_len = 0;
332 /* Everything is single buffered for now */
333 lpc_usb.epbufcfg = 0;
335 lpc_usb.epskip = 0xffffffff;
337 lpc_usb.intstat = 0xc00003ff;
339 lpc_usb.epliststart = (uint32_t) (intptr_t) &lpc_usb_endpoint;
340 lpc_usb.databufstart = ((uint32_t) (intptr_t) ao_usb_sram) & 0xffc00000;
342 /* Set up EP 0 - a Control end point with 32 bytes of in and out buffers */
344 ao_usb_enable_ep(ao_usb_ep0_out(), ao_usb_ep0_rx_buffer, AO_USB_CONTROL_SIZE);
345 lpc_usb_endpoint.setup = ao_usb_sram_offset(ao_usb_ep0_setup_buffer);
346 ao_usb_enable_ep(ao_usb_ep0_in(), ao_usb_ep0_tx_buffer, 0);
348 /* Clear all of the other endpoints */
349 for (e = 1; e <= 4; e++)
350 ao_usb_disable_epn(e);
355 ao_usb_set_configuration(void)
357 debug ("ao_usb_set_configuration\n");
359 /* Set up the INT end point */
360 ao_usb_enable_epn(AO_USB_INT_EP, 0, NULL, NULL);
362 /* Set up the OUT end point */
363 ao_usb_enable_epn(AO_USB_OUT_EP, AO_USB_OUT_SIZE, ao_usb_out_rx_buffer[0], NULL);
365 /* Set the current RX pointer to the *other* buffer so that when buffer 0 gets
366 * data, we'll switch to it and pull bytes from there
368 ao_usb_out_rx_cur = 1;
370 /* Set up the IN end point */
371 ao_usb_enable_epn(AO_USB_IN_EP, 0, NULL, ao_usb_in_tx_buffer[0]);
372 ao_usb_in_tx_cur = 0;
374 ao_usb_in_flushed = 0;
375 ao_usb_in_pending = 0;
376 ao_wakeup(&ao_usb_in_pending);
378 ao_usb_out_avail = 0;
379 ao_usb_configuration = 0;
382 ao_wakeup(&ao_usb_running);
385 /* Send an IN data packet */
387 ao_usb_ep0_flush(void)
391 this_len = ao_usb_ep0_in_len;
392 if (this_len > AO_USB_CONTROL_SIZE)
393 this_len = AO_USB_CONTROL_SIZE;
395 ao_usb_ep0_in_len -= this_len;
396 ao_usb_ep0_in_max -= this_len;
398 if (this_len < AO_USB_CONTROL_SIZE || ao_usb_ep0_in_max == 0)
399 ao_usb_ep0_state = AO_USB_EP0_IDLE;
401 debug_data ("Flush EP0 len %d:", this_len);
402 memcpy(ao_usb_ep0_tx_buffer, ao_usb_ep0_in_data, this_len);
404 ao_usb_ep0_in_data += this_len;
406 /* Mark the endpoint as TX valid to send the packet */
407 ao_usb_set_ep(ao_usb_ep0_in(), ao_usb_ep0_tx_buffer, this_len);
408 debug ("queue tx. 0 now %08x\n", *ao_usb_ep0_in());
411 /* Read data from the ep0 OUT fifo */
413 ao_usb_ep0_fill(void)
418 /* Pull all of the data out of the packet */
419 if (lpc_usb.devcmdstat & (1 << LPC_USB_DEVCMDSTAT_SETUP)) {
420 rx_buffer = ao_usb_ep0_setup_buffer;
423 rx_buffer = ao_usb_ep0_rx_buffer;
424 len = AO_USB_CONTROL_SIZE - ao_usb_ep_count(ao_usb_ep0_out());
427 if (len > ao_usb_ep0_out_len)
428 len = ao_usb_ep0_out_len;
429 ao_usb_ep0_out_len -= len;
431 debug_data ("Fill EP0 len %d:", len);
432 memcpy(ao_usb_ep0_out_data, rx_buffer, len);
434 ao_usb_ep0_out_data += len;
437 ao_usb_set_ep(ao_usb_ep0_out(), ao_usb_ep0_rx_buffer, AO_USB_CONTROL_SIZE);
438 lpc_usb.devcmdstat |= (1 << LPC_USB_DEVCMDSTAT_SETUP);
442 ao_usb_ep0_in_reset(void)
444 ao_usb_ep0_in_data = ao_usb_ep0_in_buf;
445 ao_usb_ep0_in_len = 0;
449 ao_usb_ep0_in_queue_byte(uint8_t a)
451 if (ao_usb_ep0_in_len < sizeof (ao_usb_ep0_in_buf))
452 ao_usb_ep0_in_buf[ao_usb_ep0_in_len++] = a;
456 ao_usb_ep0_in_set(const uint8_t *data, uint8_t len)
458 ao_usb_ep0_in_data = data;
459 ao_usb_ep0_in_len = len;
463 ao_usb_ep0_out_set(uint8_t *data, uint8_t len)
465 ao_usb_ep0_out_data = data;
466 ao_usb_ep0_out_len = len;
470 ao_usb_ep0_in_start(uint16_t max)
472 ao_usb_ep0_in_max = max;
473 /* Don't send more than asked for */
474 if (ao_usb_ep0_in_len > max)
475 ao_usb_ep0_in_len = max;
479 static struct ao_usb_line_coding ao_usb_line_coding = {115200, 0, 0, 8};
481 /* Walk through the list of descriptors and find a match
484 ao_usb_get_descriptor(uint16_t value, uint16_t length)
486 const uint8_t *descriptor;
487 uint8_t type = value >> 8;
488 uint8_t index = value;
490 descriptor = ao_usb_descriptors;
491 while (descriptor[0] != 0) {
492 if (descriptor[1] == type && index-- == 0) {
494 if (type == AO_USB_DESC_CONFIGURATION)
500 ao_usb_ep0_in_set(descriptor, len);
503 descriptor += descriptor[0];
508 ao_usb_ep0_setup(void)
510 /* Pull the setup packet out of the fifo */
511 ao_usb_ep0_out_set((uint8_t *) &ao_usb_setup, 8);
513 if (ao_usb_ep0_out_len != 0) {
514 debug ("invalid setup packet length\n");
518 if ((ao_usb_setup.dir_type_recip & AO_USB_DIR_IN) || ao_usb_setup.length == 0)
519 ao_usb_ep0_state = AO_USB_EP0_DATA_IN;
521 ao_usb_ep0_state = AO_USB_EP0_DATA_OUT;
523 ao_usb_ep0_in_reset();
525 switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_TYPE_MASK) {
526 case AO_USB_TYPE_STANDARD:
527 debug ("Standard setup packet\n");
528 switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_RECIP_MASK) {
529 case AO_USB_RECIP_DEVICE:
530 debug ("Device setup packet\n");
531 switch(ao_usb_setup.request) {
532 case AO_USB_REQ_GET_STATUS:
533 debug ("get status\n");
534 ao_usb_ep0_in_queue_byte(0);
535 ao_usb_ep0_in_queue_byte(0);
537 case AO_USB_REQ_SET_ADDRESS:
538 debug ("set address %d\n", ao_usb_setup.value);
539 ao_usb_address = ao_usb_setup.value;
540 ao_usb_address_pending = 1;
542 case AO_USB_REQ_GET_DESCRIPTOR:
543 debug ("get descriptor %d\n", ao_usb_setup.value);
544 ao_usb_get_descriptor(ao_usb_setup.value, ao_usb_setup.length);
546 case AO_USB_REQ_GET_CONFIGURATION:
547 debug ("get configuration %d\n", ao_usb_configuration);
548 ao_usb_ep0_in_queue_byte(ao_usb_configuration);
550 case AO_USB_REQ_SET_CONFIGURATION:
551 ao_usb_configuration = ao_usb_setup.value;
552 debug ("set configuration %d\n", ao_usb_configuration);
553 ao_usb_set_configuration();
557 case AO_USB_RECIP_INTERFACE:
558 debug ("Interface setup packet\n");
559 switch(ao_usb_setup.request) {
560 case AO_USB_REQ_GET_STATUS:
561 ao_usb_ep0_in_queue_byte(0);
562 ao_usb_ep0_in_queue_byte(0);
564 case AO_USB_REQ_GET_INTERFACE:
565 ao_usb_ep0_in_queue_byte(0);
567 case AO_USB_REQ_SET_INTERFACE:
571 case AO_USB_RECIP_ENDPOINT:
572 debug ("Endpoint setup packet\n");
573 switch(ao_usb_setup.request) {
574 case AO_USB_REQ_GET_STATUS:
575 ao_usb_ep0_in_queue_byte(0);
576 ao_usb_ep0_in_queue_byte(0);
582 case AO_USB_TYPE_CLASS:
583 debug ("Class setup packet\n");
584 switch (ao_usb_setup.request) {
585 case AO_USB_SET_LINE_CODING:
586 debug ("set line coding\n");
587 ao_usb_ep0_out_set((uint8_t *) &ao_usb_line_coding, 7);
589 case AO_USB_GET_LINE_CODING:
590 debug ("get line coding\n");
591 ao_usb_ep0_in_set((const uint8_t *) &ao_usb_line_coding, 7);
593 case AO_USB_SET_CONTROL_LINE_STATE:
599 /* If we're not waiting to receive data from the host,
600 * queue an IN response
602 if (ao_usb_ep0_state == AO_USB_EP0_DATA_IN)
603 ao_usb_ep0_in_start(ao_usb_setup.length);
607 ao_usb_ep0_handle(uint8_t receive)
609 ao_usb_ep0_receive = 0;
611 if (receive & AO_USB_EP0_GOT_RESET) {
616 if (receive & AO_USB_EP0_GOT_SETUP) {
620 if (receive & AO_USB_EP0_GOT_RX_DATA) {
621 debug ("\tgot rx data\n");
622 if (ao_usb_ep0_state == AO_USB_EP0_DATA_OUT) {
624 if (ao_usb_ep0_out_len == 0) {
625 ao_usb_ep0_state = AO_USB_EP0_DATA_IN;
626 ao_usb_ep0_in_start(0);
630 if (receive & AO_USB_EP0_GOT_TX_ACK) {
631 debug ("\tgot tx ack\n");
633 /* Wait until the IN packet is received from addr 0
634 * before assigning our local address
636 if (ao_usb_address_pending) {
638 /* Go to idle mode if USB is connected
640 ao_flight_force_idle = 1;
642 ao_usb_set_address(ao_usb_address);
644 if (ao_usb_ep0_state == AO_USB_EP0_DATA_IN)
650 static uint16_t int_count;
651 static uint16_t in_count;
652 static uint16_t out_count;
653 static uint16_t reset_count;
657 lpc_usb_irq_isr(void)
659 uint32_t intstat = lpc_usb.intstat & lpc_usb.inten;
661 lpc_usb.intstat = intstat;
662 /* Handle EP0 OUT packets */
663 if (intstat & (1 << LPC_USB_INT_EPOUT(0))) {
664 if (lpc_usb.devcmdstat & (1 << LPC_USB_DEVCMDSTAT_SETUP))
665 ao_usb_ep0_receive |= AO_USB_EP0_GOT_SETUP;
667 ao_usb_ep0_receive |= AO_USB_EP0_GOT_RX_DATA;
669 ao_usb_ep0_handle(ao_usb_ep0_receive);
672 /* Handle EP0 IN packets */
673 if (intstat & (1 << LPC_USB_INT_EPIN(0))) {
674 ao_usb_ep0_receive |= AO_USB_EP0_GOT_TX_ACK;
676 ao_usb_ep0_handle(ao_usb_ep0_receive);
680 /* Handle OUT packets */
681 if (intstat & (1 << LPC_USB_INT_EPOUT(AO_USB_OUT_EP))) {
685 _rx_dbg1("RX ISR", *ao_usb_epn_out(AO_USB_OUT_EP));
686 ao_usb_out_avail = 1;
687 _rx_dbg0("out avail set");
688 ao_wakeup(AO_USB_OUT_SLEEP_ADDR)
689 _rx_dbg0("stdin awoken");
692 /* Handle IN packets */
693 if (intstat & (1 << LPC_USB_INT_EPIN(AO_USB_IN_EP))) {
697 _tx_dbg1("TX ISR", *ao_usb_epn_in(AO_USB_IN_EP));
698 ao_usb_in_pending = 0;
699 ao_wakeup(&ao_usb_in_pending);
702 /* NAK all INT EP IN packets */
703 if (intstat & (1 << LPC_USB_INT_EPIN(AO_USB_INT_EP))) {
707 /* Check for reset */
708 if (intstat & (1 << LPC_USB_INT_DEV)) {
709 if (lpc_usb.devcmdstat & (1 << LPC_USB_DEVCMDSTAT_DRES_C))
711 lpc_usb.devcmdstat |= (1 << LPC_USB_DEVCMDSTAT_DRES_C);
712 ao_usb_ep0_receive |= AO_USB_EP0_GOT_RESET;
713 ao_usb_ep0_handle(ao_usb_ep0_receive);
720 /* Queue the current IN buffer for transmission */
722 _ao_usb_in_send(void)
724 _tx_dbg0("in_send start");
725 debug ("send %d\n", ao_usb_tx_count);
726 while (ao_usb_in_pending)
727 ao_sleep(&ao_usb_in_pending);
728 ao_usb_in_pending = 1;
729 if (ao_usb_tx_count != AO_USB_IN_SIZE)
730 ao_usb_in_flushed = 1;
731 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);
732 ao_usb_in_tx_cur = 1 - ao_usb_in_tx_cur;
734 _tx_dbg0("in_send end");
737 /* Wait for a free IN buffer. Interrupts are blocked */
739 _ao_usb_in_wait(void)
742 /* Check if the current buffer is writable */
743 if (ao_usb_tx_count < AO_USB_IN_SIZE)
746 _tx_dbg0("in_wait top");
747 /* Wait for an IN buffer to be ready */
748 while (ao_usb_in_pending)
749 ao_sleep(&ao_usb_in_pending);
750 _tx_dbg0("in_wait bottom");
760 /* Anytime we've sent a character since
761 * the last time we flushed, we'll need
762 * to send a packet -- the only other time
763 * we would send a packet is when that
764 * packet was full, in which case we now
765 * want to send an empty packet
767 ao_arch_block_interrupts();
768 while (!ao_usb_in_flushed) {
769 _tx_dbg0("flush top");
771 _tx_dbg0("flush end");
773 ao_arch_release_interrupts();
777 ao_usb_putchar(char c)
782 ao_arch_block_interrupts();
785 ao_usb_in_flushed = 0;
786 ao_usb_in_tx_buffer[ao_usb_in_tx_cur][ao_usb_tx_count++] = (uint8_t) c;
788 /* Send the packet when full */
789 if (ao_usb_tx_count == AO_USB_IN_SIZE) {
790 _tx_dbg0("putchar full");
792 _tx_dbg0("putchar flushed");
794 ao_arch_release_interrupts();
798 _ao_usb_out_recv(void)
800 _rx_dbg0("out_recv top");
801 ao_usb_out_avail = 0;
803 ao_usb_rx_count = AO_USB_OUT_SIZE - ao_usb_epn_out_count(AO_USB_OUT_EP);
805 _rx_dbg1("out_recv count", ao_usb_rx_count);
806 debug ("recv %d\n", ao_usb_rx_count);
807 debug_data("Fill OUT len %d\n", ao_usb_rx_count);
809 ao_usb_out_rx_cur = 1 - ao_usb_out_rx_cur;
812 ao_usb_set_epn_out(AO_USB_OUT_EP, ao_usb_out_rx_buffer[1-ao_usb_out_rx_cur], AO_USB_OUT_SIZE);
816 _ao_usb_pollchar(void)
821 return AO_READ_AGAIN;
824 if (ao_usb_rx_pos != ao_usb_rx_count)
827 _rx_dbg0("poll check");
828 /* Check to see if a packet has arrived */
829 if (!ao_usb_out_avail) {
830 _rx_dbg0("poll none");
831 return AO_READ_AGAIN;
836 /* Pull a character out of the fifo */
837 c = ao_usb_out_rx_buffer[ao_usb_out_rx_cur][ao_usb_rx_pos++];
846 ao_arch_block_interrupts();
847 while ((c = _ao_usb_pollchar()) == AO_READ_AGAIN)
848 ao_sleep(AO_USB_OUT_SLEEP_ADDR);
849 ao_arch_release_interrupts();
856 ao_arch_block_interrupts();
859 ao_gpio_set(AO_USB_PULLUP_PORT, AO_USB_PULLUP_PIN, AO_USB_PULLUP, 0);
861 /* Disable interrupts */
864 lpc_nvic_clear_enable(LPC_ISR_USB_IRQ_POS);
866 /* Disable the device */
867 lpc_usb.devcmdstat = 0;
869 /* Turn off USB clock */
870 lpc_scb.usbclkdiv = 0;
872 /* Disable USB PHY and PLL */
873 lpc_scb.pdruncfg |= ((1 << LPC_SCB_PDRUNCFG_USBPAD_PD) |
874 (1 << LPC_SCB_PDRUNCFG_USBPLL_PD));
876 /* Disable USB registers and RAM */
877 lpc_scb.sysahbclkctrl &= ~((1 << LPC_SCB_SYSAHBCLKCTRL_USB) |
878 (1 << LPC_SCB_SYSAHBCLKCTRL_USBRAM));
880 ao_arch_release_interrupts();
888 /* Enable USB pins */
889 #if HAS_LPC_USB_CONNECT
890 lpc_ioconf.pio0_6 = ((LPC_IOCONF_FUNC_USB_CONNECT << LPC_IOCONF_FUNC) |
891 (LPC_IOCONF_MODE_INACTIVE << LPC_IOCONF_MODE) |
892 (0 << LPC_IOCONF_HYS) |
893 (0 << LPC_IOCONF_INV) |
894 (0 << LPC_IOCONF_OD) |
898 lpc_ioconf.pio0_3 = ((LPC_IOCONF_FUNC_USB_VBUS << LPC_IOCONF_FUNC) |
899 (LPC_IOCONF_MODE_INACTIVE << LPC_IOCONF_MODE) |
900 (0 << LPC_IOCONF_HYS) |
901 (0 << LPC_IOCONF_INV) |
902 (0 << LPC_IOCONF_OD) |
905 /* Enable USB registers and RAM */
906 lpc_scb.sysahbclkctrl |= ((1 << LPC_SCB_SYSAHBCLKCTRL_USB) |
907 (1 << LPC_SCB_SYSAHBCLKCTRL_USBRAM));
910 lpc_scb.pdruncfg &= ~(1 << LPC_SCB_PDRUNCFG_USBPAD_PD);
912 /* Turn on USB PLL */
913 lpc_scb.pdruncfg &= ~(1 << LPC_SCB_PDRUNCFG_USBPLL_PD);
915 lpc_scb.usbpllclksel = (LPC_SCB_SYSPLLCLKSEL_SEL_SYSOSC << LPC_SCB_SYSPLLCLKSEL_SEL);
916 lpc_scb.usbpllclkuen = (0 << LPC_SCB_USBPLLCLKUEN_ENA);
917 lpc_scb.usbpllclkuen = (1 << LPC_SCB_USBPLLCLKUEN_ENA);
918 while (!(lpc_scb.usbpllclkuen & (1 << LPC_SCB_USBPLLCLKUEN_ENA)))
920 lpc_scb.usbpllctrl = 0x23;
921 while (!(lpc_scb.usbpllstat & 1))
924 lpc_scb.usbclksel = 0;
925 lpc_scb.usbclkuen = (0 << LPC_SCB_USBCLKUEN_ENA);
926 lpc_scb.usbclkuen = (1 << LPC_SCB_USBCLKUEN_ENA);
927 while (!(lpc_scb.usbclkuen & (1 << LPC_SCB_USBCLKUEN_ENA)))
930 /* Turn on USB clock, use 48MHz clock unchanged */
931 lpc_scb.usbclkdiv = 1;
933 /* Configure interrupts */
934 ao_arch_block_interrupts();
936 /* Route all interrupts to the main isr */
937 lpc_usb.introuting = 0;
941 lpc_nvic_set_enable(LPC_ISR_USB_IRQ_POS);
942 lpc_nvic_set_priority(LPC_ISR_USB_IRQ_POS, 0);
944 /* Clear any spurious interrupts */
945 lpc_usb.intstat = 0xffffffff;
947 debug ("ao_usb_enable\n");
949 /* Enable interrupts */
950 lpc_usb.inten = ((1 << LPC_USB_INT_EPOUT(0)) |
951 (1 << LPC_USB_INT_EPIN(0)) |
952 (1 << LPC_USB_INT_EPIN(AO_USB_INT_EP)) |
953 (1 << LPC_USB_INT_EPOUT(AO_USB_OUT_EP)) |
954 (1 << LPC_USB_INT_EPIN(AO_USB_IN_EP)) |
955 (1 << LPC_USB_INT_DEV));
957 ao_arch_release_interrupts();
959 lpc_usb.devcmdstat = 0;
960 for (t = 0; t < 1000; t++)
963 ao_usb_sram = lpc_usb_sram;
965 ao_usb_ep0_rx_buffer = ao_usb_alloc_sram(AO_USB_CONTROL_SIZE);
966 ao_usb_ep0_tx_buffer = ao_usb_alloc_sram(AO_USB_CONTROL_SIZE);
967 ao_usb_ep0_setup_buffer = ao_usb_alloc_sram(AO_USB_CONTROL_SIZE);
969 ao_usb_out_rx_buffer[0] = ao_usb_alloc_sram(AO_USB_OUT_SIZE);
970 ao_usb_out_rx_buffer[1] = ao_usb_alloc_sram(AO_USB_OUT_SIZE);
971 ao_usb_in_tx_buffer[0] = ao_usb_alloc_sram(AO_USB_IN_SIZE);
972 ao_usb_in_tx_buffer[1] = ao_usb_alloc_sram(AO_USB_IN_SIZE);
977 ao_gpio_set(AO_USB_PULLUP_PORT, AO_USB_PULLUP_PIN, AO_USB_PULLUP, 1);
982 struct ao_task ao_usb_echo_task;
990 c = ao_usb_getchar();
1001 printf ("control: %d out: %d in: %d int: %d reset: %d\n",
1002 control_count, out_count, in_count, int_count, reset_count);
1005 __code struct ao_cmds ao_usb_cmds[] = {
1006 { ao_usb_irq, "I\0Show USB interrupt counts" },
1015 ao_enable_output(AO_USB_PULLUP_PORT, AO_USB_PULLUP_PIN, AO_USB_PULLUP, 0);
1019 debug ("ao_usb_init\n");
1021 ao_add_task(&ao_usb_echo_task, ao_usb_echo, "usb echo");
1024 ao_cmd_register(&ao_usb_cmds[0]);
1027 ao_add_stdio(_ao_usb_pollchar, ao_usb_putchar, ao_usb_flush);
1031 #if TX_DBG || RX_DBG
1041 uint32_t in_pending;
1043 uint32_t in_flushed;
1053 #define NUM_USB_DBG 8
1055 static struct ao_usb_dbg dbg[NUM_USB_DBG];
1058 static void _dbg(int line, char *msg, uint32_t value)
1061 dbg[dbg_i].line = line;
1062 dbg[dbg_i].msg = msg;
1063 dbg[dbg_i].value = value;
1064 asm("mrs %0,primask" : "=&r" (primask));
1065 dbg[dbg_i].primask = primask;
1067 dbg[dbg_i].in_count = in_count;
1068 dbg[dbg_i].in_ep = *ao_usb_epn_in(AO_USB_IN_EP, 0);
1069 dbg[dbg_i].in_pending = ao_usb_in_pending;
1070 dbg[dbg_i].tx_count = ao_usb_tx_count;
1071 dbg[dbg_i].in_flushed = ao_usb_in_flushed;
1074 dbg[dbg_i].rx_count = ao_usb_rx_count;
1075 dbg[dbg_i].rx_pos = ao_usb_rx_pos;
1076 dbg[dbg_i].out_avail = ao_usb_out_avail;
1077 dbg[dbg_i].out_ep = *ao_usb_epn_out(AO_USB_OUT_EP, 0);
1079 if (++dbg_i == NUM_USB_DBG)