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 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 uint8_t ao_usb_running;
112 static uint8_t ao_usb_configuration;
114 #define AO_USB_EP0_GOT_RESET 1
115 #define AO_USB_EP0_GOT_SETUP 2
116 #define AO_USB_EP0_GOT_RX_DATA 4
117 #define AO_USB_EP0_GOT_TX_ACK 8
119 static uint8_t ao_usb_ep0_receive;
120 static uint8_t ao_usb_address;
121 static uint8_t ao_usb_address_pending;
123 static inline uint32_t set_toggle(uint32_t current_value,
125 uint32_t desired_value)
127 return (current_value ^ desired_value) & mask;
131 * Set current device address and mark the
132 * interface as active
135 ao_usb_set_address(uint8_t address)
137 debug("ao_usb_set_address %02x\n", address);
138 lpc_usb.devcmdstat = ((address << LPC_USB_DEVCMDSTAT_DEV_ADDR) |
139 (1 << LPC_USB_DEVCMDSTAT_DEV_EN) |
140 (0 << LPC_USB_DEVCMDSTAT_SETUP) |
141 (0 << LPC_USB_DEVCMDSTAT_PLL_ON) |
142 (0 << LPC_USB_DEVCMDSTAT_LPM_SUP) |
143 (0 << LPC_USB_DEVCMDSTAT_INTONNAK_AO) |
144 (0 << LPC_USB_DEVCMDSTAT_INTONNAK_AI) |
145 (0 << LPC_USB_DEVCMDSTAT_INTONNAK_CO) |
146 (0 << LPC_USB_DEVCMDSTAT_INTONNAK_CI) |
147 (1 << LPC_USB_DEVCMDSTAT_DCON) |
148 (0 << LPC_USB_DEVCMDSTAT_DSUS) |
149 (0 << LPC_USB_DEVCMDSTAT_DCON_C) |
150 (0 << LPC_USB_DEVCMDSTAT_DSUS_C) |
151 (0 << LPC_USB_DEVCMDSTAT_DRES_C) |
152 (0 << LPC_USB_DEVCMDSTAT_VBUSDEBOUNCED));
153 ao_usb_address_pending = 0;
160 #define _tx_dbg0(msg) _dbg(__LINE__,msg,0)
161 #define _tx_dbg1(msg,value) _dbg(__LINE__,msg,value)
163 #define _tx_dbg0(msg)
164 #define _tx_dbg1(msg,value)
168 #define _rx_dbg0(msg) _dbg(__LINE__,msg,0)
169 #define _rx_dbg1(msg,value) _dbg(__LINE__,msg,value)
171 #define _rx_dbg0(msg)
172 #define _rx_dbg1(msg,value)
176 static void _dbg(int line, char *msg, uint32_t value);
180 * Set just endpoint 0, for use during startup
184 ao_usb_alloc_sram(uint16_t size)
186 uint8_t *addr = ao_usb_sram;
188 ao_usb_sram += (size + 63) & ~63;
193 ao_usb_sram_offset(uint8_t *addr)
195 return (uint16_t) ((intptr_t) addr >> 6);
199 ao_usb_set_ep(vuint32_t *ep, uint8_t *addr, uint16_t nbytes)
201 *ep = ((ao_usb_sram_offset(addr) << LPC_USB_EP_OFFSET) |
202 (nbytes << LPC_USB_EP_NBYTES) |
203 (0 << LPC_USB_EP_ENDPOINT_ISO) |
204 (0 << LPC_USB_EP_RATE_FEEDBACK) |
205 (0 << LPC_USB_EP_TOGGLE_RESET) |
206 (0 << LPC_USB_EP_STALL) |
207 (0 << LPC_USB_EP_DISABLED) |
208 (1 << LPC_USB_EP_ACTIVE));
211 static inline uint16_t
212 ao_usb_ep_count(vuint32_t *ep)
214 return (*ep >> LPC_USB_EP_NBYTES) & LPC_USB_EP_NBYTES_MASK;
217 static inline uint8_t
218 ao_usb_ep_stall(vuint32_t *ep)
220 return (*ep >> LPC_USB_EP_STALL) & 1;
223 static inline vuint32_t *
226 return &lpc_usb_endpoint.ep0_out;
229 static inline vuint32_t *
232 return &lpc_usb_endpoint.ep0_in;
235 static inline vuint32_t *
236 ao_usb_epn_out(uint8_t n, uint8_t i)
238 return &lpc_usb_endpoint.epn[n-1].out[i];
241 static inline vuint32_t *
242 ao_usb_epn_in(uint8_t n, uint8_t i)
244 return &lpc_usb_endpoint.epn[n-1].in[i];
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);
256 ao_usb_set_epn_out(uint8_t n, uint8_t *addr, uint16_t nbytes)
258 ao_usb_set_ep(ao_usb_epn_out(n, 0), addr, nbytes);
261 static inline uint16_t
262 ao_usb_epn_out_count(uint8_t n)
264 return ao_usb_ep_count(ao_usb_epn_out(n, 0));
267 static inline uint16_t
268 ao_usb_epn_in_count(uint8_t n)
270 return ao_usb_ep_count(ao_usb_epn_in(n, 0));
274 ao_usb_enable_ep(vuint32_t *ep, uint8_t *addr, uint16_t set_nbytes)
276 ao_usb_set_ep(ep, addr, set_nbytes);
280 ao_usb_disable_ep(vuint32_t *ep)
282 *ep = ((0 << LPC_USB_EP_OFFSET) |
283 (0 << LPC_USB_EP_NBYTES) |
284 (0 << LPC_USB_EP_ENDPOINT_ISO) |
285 (0 << LPC_USB_EP_RATE_FEEDBACK) |
286 (0 << LPC_USB_EP_TOGGLE_RESET) |
287 (0 << LPC_USB_EP_STALL) |
288 (1 << LPC_USB_EP_DISABLED) |
289 (0 << LPC_USB_EP_ACTIVE));
293 ao_usb_enable_epn(uint8_t n,
294 uint16_t out_bytes, uint8_t *out_addr,
297 ao_usb_enable_ep(ao_usb_epn_out(n, 0), out_addr, out_bytes);
298 ao_usb_disable_ep(ao_usb_epn_out(n, 1));
300 ao_usb_enable_ep(ao_usb_epn_in(n, 0), in_addr, 0);
301 ao_usb_disable_ep(ao_usb_epn_in(n, 1));
305 ao_usb_disable_epn(uint8_t n)
307 ao_usb_disable_ep(ao_usb_epn_out(n, 0));
308 ao_usb_disable_ep(ao_usb_epn_out(n, 1));
309 ao_usb_disable_ep(ao_usb_epn_in(n, 0));
310 ao_usb_disable_ep(ao_usb_epn_in(n, 1));
316 ao_usb_set_address(0);
317 ao_usb_configuration = 0;
319 ao_usb_ep0_state = AO_USB_EP0_IDLE;
320 ao_usb_ep0_in_data = NULL;
321 ao_usb_ep0_in_len = 0;
322 ao_usb_ep0_in_max = 0;
324 ao_usb_ep0_out_data = NULL;
325 ao_usb_ep0_out_len = 0;
333 /* Everything is single buffered for now */
334 lpc_usb.epbufcfg = 0;
336 lpc_usb.epskip = 0xffffffff;
338 lpc_usb.intstat = 0xc00003ff;
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_enable_ep(ao_usb_ep0_out(), ao_usb_ep0_rx_buffer, AO_USB_CONTROL_SIZE);
346 lpc_usb_endpoint.setup = ao_usb_sram_offset(ao_usb_ep0_setup_buffer);
347 ao_usb_enable_ep(ao_usb_ep0_in(), ao_usb_ep0_tx_buffer, 0);
349 /* Clear all of the other endpoints */
350 for (e = 1; e <= 4; e++)
351 ao_usb_disable_epn(e);
356 ao_usb_set_configuration(void)
358 debug ("ao_usb_set_configuration\n");
360 /* Set up the INT end point */
361 ao_usb_enable_epn(AO_USB_INT_EP, 0, NULL, NULL);
363 /* Set up the OUT end point */
364 ao_usb_enable_epn(AO_USB_OUT_EP, AO_USB_OUT_SIZE, ao_usb_out_rx_buffer[0], NULL);
366 /* Set the current RX pointer to the *other* buffer so that when buffer 0 gets
367 * data, we'll switch to it and pull bytes from there
369 ao_usb_out_rx_cur = 1;
371 /* Set up the IN end point */
372 ao_usb_enable_epn(AO_USB_IN_EP, 0, NULL, ao_usb_in_tx_buffer[0]);
373 ao_usb_in_tx_cur = 0;
375 ao_usb_in_flushed = 0;
376 ao_usb_in_pending = 0;
377 ao_wakeup(&ao_usb_in_pending);
379 ao_usb_out_avail = 0;
380 ao_usb_configuration = 0;
383 ao_wakeup(&ao_usb_running);
386 /* Send an IN data packet */
388 ao_usb_ep0_flush(void)
392 this_len = ao_usb_ep0_in_len;
393 if (this_len > AO_USB_CONTROL_SIZE)
394 this_len = AO_USB_CONTROL_SIZE;
396 ao_usb_ep0_in_len -= this_len;
397 ao_usb_ep0_in_max -= this_len;
399 if (this_len < AO_USB_CONTROL_SIZE || ao_usb_ep0_in_max == 0)
400 ao_usb_ep0_state = AO_USB_EP0_IDLE;
402 debug_data ("Flush EP0 len %d:", this_len);
403 memcpy(ao_usb_ep0_tx_buffer, ao_usb_ep0_in_data, this_len);
405 ao_usb_ep0_in_data += this_len;
407 /* Mark the endpoint as TX valid to send the packet */
408 ao_usb_set_ep(ao_usb_ep0_in(), ao_usb_ep0_tx_buffer, this_len);
409 debug ("queue tx. 0 now %08x\n", *ao_usb_ep0_in());
412 /* Read data from the ep0 OUT fifo */
414 ao_usb_ep0_fill(void)
419 /* Pull all of the data out of the packet */
420 if (lpc_usb.devcmdstat & (1 << LPC_USB_DEVCMDSTAT_SETUP)) {
421 rx_buffer = ao_usb_ep0_setup_buffer;
424 rx_buffer = ao_usb_ep0_rx_buffer;
425 len = AO_USB_CONTROL_SIZE - ao_usb_ep_count(ao_usb_ep0_out());
428 if (len > ao_usb_ep0_out_len)
429 len = ao_usb_ep0_out_len;
430 ao_usb_ep0_out_len -= len;
432 debug_data ("Fill EP0 len %d:", len);
433 memcpy(ao_usb_ep0_out_data, rx_buffer, len);
435 ao_usb_ep0_out_data += len;
438 ao_usb_set_ep(ao_usb_ep0_out(), ao_usb_ep0_rx_buffer, AO_USB_CONTROL_SIZE);
439 lpc_usb.devcmdstat |= (1 << LPC_USB_DEVCMDSTAT_SETUP);
443 ao_usb_ep0_in_reset(void)
445 ao_usb_ep0_in_data = ao_usb_ep0_in_buf;
446 ao_usb_ep0_in_len = 0;
450 ao_usb_ep0_in_queue_byte(uint8_t a)
452 if (ao_usb_ep0_in_len < sizeof (ao_usb_ep0_in_buf))
453 ao_usb_ep0_in_buf[ao_usb_ep0_in_len++] = a;
457 ao_usb_ep0_in_set(const uint8_t *data, uint8_t len)
459 ao_usb_ep0_in_data = data;
460 ao_usb_ep0_in_len = len;
464 ao_usb_ep0_out_set(uint8_t *data, uint8_t len)
466 ao_usb_ep0_out_data = data;
467 ao_usb_ep0_out_len = len;
471 ao_usb_ep0_in_start(uint16_t max)
473 ao_usb_ep0_in_max = max;
474 /* Don't send more than asked for */
475 if (ao_usb_ep0_in_len > max)
476 ao_usb_ep0_in_len = max;
480 struct ao_usb_line_coding ao_usb_line_coding = {115200, 0, 0, 8};
482 /* Walk through the list of descriptors and find a match
485 ao_usb_get_descriptor(uint16_t value, uint16_t length)
487 const uint8_t *descriptor;
488 uint8_t type = value >> 8;
489 uint8_t index = value;
491 descriptor = ao_usb_descriptors;
492 while (descriptor[0] != 0) {
493 if (descriptor[1] == type && index-- == 0) {
495 if (type == AO_USB_DESC_CONFIGURATION)
501 ao_usb_ep0_in_set(descriptor, len);
504 descriptor += descriptor[0];
509 ao_usb_ep0_setup(void)
511 /* Pull the setup packet out of the fifo */
512 ao_usb_ep0_out_set((uint8_t *) &ao_usb_setup, 8);
514 if (ao_usb_ep0_out_len != 0) {
515 debug ("invalid setup packet length\n");
519 if ((ao_usb_setup.dir_type_recip & AO_USB_DIR_IN) || ao_usb_setup.length == 0)
520 ao_usb_ep0_state = AO_USB_EP0_DATA_IN;
522 ao_usb_ep0_state = AO_USB_EP0_DATA_OUT;
524 ao_usb_ep0_in_reset();
526 switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_TYPE_MASK) {
527 case AO_USB_TYPE_STANDARD:
528 debug ("Standard setup packet\n");
529 switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_RECIP_MASK) {
530 case AO_USB_RECIP_DEVICE:
531 debug ("Device setup packet\n");
532 switch(ao_usb_setup.request) {
533 case AO_USB_REQ_GET_STATUS:
534 debug ("get status\n");
535 ao_usb_ep0_in_queue_byte(0);
536 ao_usb_ep0_in_queue_byte(0);
538 case AO_USB_REQ_SET_ADDRESS:
539 debug ("set address %d\n", ao_usb_setup.value);
540 ao_usb_address = ao_usb_setup.value;
541 ao_usb_address_pending = 1;
543 case AO_USB_REQ_GET_DESCRIPTOR:
544 debug ("get descriptor %d\n", ao_usb_setup.value);
545 ao_usb_get_descriptor(ao_usb_setup.value, ao_usb_setup.length);
547 case AO_USB_REQ_GET_CONFIGURATION:
548 debug ("get configuration %d\n", ao_usb_configuration);
549 ao_usb_ep0_in_queue_byte(ao_usb_configuration);
551 case AO_USB_REQ_SET_CONFIGURATION:
552 ao_usb_configuration = ao_usb_setup.value;
553 debug ("set configuration %d\n", ao_usb_configuration);
554 ao_usb_set_configuration();
558 case AO_USB_RECIP_INTERFACE:
559 debug ("Interface setup packet\n");
560 switch(ao_usb_setup.request) {
561 case AO_USB_REQ_GET_STATUS:
562 ao_usb_ep0_in_queue_byte(0);
563 ao_usb_ep0_in_queue_byte(0);
565 case AO_USB_REQ_GET_INTERFACE:
566 ao_usb_ep0_in_queue_byte(0);
568 case AO_USB_REQ_SET_INTERFACE:
572 case AO_USB_RECIP_ENDPOINT:
573 debug ("Endpoint setup packet\n");
574 switch(ao_usb_setup.request) {
575 case AO_USB_REQ_GET_STATUS:
576 ao_usb_ep0_in_queue_byte(0);
577 ao_usb_ep0_in_queue_byte(0);
583 case AO_USB_TYPE_CLASS:
584 debug ("Class setup packet\n");
585 switch (ao_usb_setup.request) {
586 case AO_USB_SET_LINE_CODING:
587 debug ("set line coding\n");
588 ao_usb_ep0_out_set((uint8_t *) &ao_usb_line_coding, 7);
590 case AO_USB_GET_LINE_CODING:
591 debug ("get line coding\n");
592 ao_usb_ep0_in_set((const uint8_t *) &ao_usb_line_coding, 7);
594 case AO_USB_SET_CONTROL_LINE_STATE:
600 /* If we're not waiting to receive data from the host,
601 * queue an IN response
603 if (ao_usb_ep0_state == AO_USB_EP0_DATA_IN)
604 ao_usb_ep0_in_start(ao_usb_setup.length);
608 ao_usb_ep0_handle(uint8_t receive)
610 ao_usb_ep0_receive = 0;
612 if (receive & AO_USB_EP0_GOT_RESET) {
617 if (receive & AO_USB_EP0_GOT_SETUP) {
621 if (receive & AO_USB_EP0_GOT_RX_DATA) {
622 debug ("\tgot rx data\n");
623 if (ao_usb_ep0_state == AO_USB_EP0_DATA_OUT) {
625 if (ao_usb_ep0_out_len == 0) {
626 ao_usb_ep0_state = AO_USB_EP0_DATA_IN;
627 ao_usb_ep0_in_start(0);
631 if (receive & AO_USB_EP0_GOT_TX_ACK) {
632 debug ("\tgot tx ack\n");
634 /* Wait until the IN packet is received from addr 0
635 * before assigning our local address
637 if (ao_usb_address_pending) {
639 /* Go to idle mode if USB is connected
641 ao_flight_force_idle = 1;
643 ao_usb_set_address(ao_usb_address);
645 if (ao_usb_ep0_state == AO_USB_EP0_DATA_IN)
651 static uint16_t int_count;
652 static uint16_t in_count;
653 static uint16_t out_count;
654 static uint16_t reset_count;
658 lpc_usb_irq_isr(void)
660 uint32_t intstat = lpc_usb.intstat & lpc_usb.inten;
662 lpc_usb.intstat = intstat;
663 /* Handle EP0 OUT packets */
664 if (intstat & (1 << LPC_USB_INT_EPOUT(0))) {
665 if (lpc_usb.devcmdstat & (1 << LPC_USB_DEVCMDSTAT_SETUP))
666 ao_usb_ep0_receive |= AO_USB_EP0_GOT_SETUP;
668 ao_usb_ep0_receive |= AO_USB_EP0_GOT_RX_DATA;
670 ao_usb_ep0_handle(ao_usb_ep0_receive);
673 /* Handle EP0 IN packets */
674 if (intstat & (1 << LPC_USB_INT_EPIN(0))) {
675 ao_usb_ep0_receive |= AO_USB_EP0_GOT_TX_ACK;
677 ao_usb_ep0_handle(ao_usb_ep0_receive);
681 /* Handle OUT packets */
682 if (intstat & (1 << LPC_USB_INT_EPOUT(AO_USB_OUT_EP))) {
686 _rx_dbg1("RX ISR", *ao_usb_epn_out(AO_USB_OUT_EP));
687 ao_usb_out_avail = 1;
688 _rx_dbg0("out avail set");
689 ao_wakeup(AO_USB_OUT_SLEEP_ADDR)
690 _rx_dbg0("stdin awoken");
693 /* Handle IN packets */
694 if (intstat & (1 << LPC_USB_INT_EPIN(AO_USB_IN_EP))) {
698 _tx_dbg1("TX ISR", *ao_usb_epn_in(AO_USB_IN_EP));
699 ao_usb_in_pending = 0;
700 ao_wakeup(&ao_usb_in_pending);
703 /* NAK all INT EP IN packets */
704 if (intstat & (1 << LPC_USB_INT_EPIN(AO_USB_INT_EP))) {
708 /* Check for reset */
709 if (intstat & (1 << LPC_USB_INT_DEV)) {
710 if (lpc_usb.devcmdstat & (1 << LPC_USB_DEVCMDSTAT_DRES_C))
712 lpc_usb.devcmdstat |= (1 << LPC_USB_DEVCMDSTAT_DRES_C);
713 ao_usb_ep0_receive |= AO_USB_EP0_GOT_RESET;
714 ao_usb_ep0_handle(ao_usb_ep0_receive);
721 /* Queue the current IN buffer for transmission */
723 _ao_usb_in_send(void)
725 _tx_dbg0("in_send start");
726 debug ("send %d\n", ao_usb_tx_count);
727 while (ao_usb_in_pending)
728 ao_sleep(&ao_usb_in_pending);
729 ao_usb_in_pending = 1;
730 if (ao_usb_tx_count != AO_USB_IN_SIZE)
731 ao_usb_in_flushed = 1;
732 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);
733 ao_usb_in_tx_cur = 1 - ao_usb_in_tx_cur;
735 _tx_dbg0("in_send end");
738 /* Wait for a free IN buffer. Interrupts are blocked */
740 _ao_usb_in_wait(void)
743 /* Check if the current buffer is writable */
744 if (ao_usb_tx_count < AO_USB_IN_SIZE)
747 _tx_dbg0("in_wait top");
748 /* Wait for an IN buffer to be ready */
749 while (ao_usb_in_pending)
750 ao_sleep(&ao_usb_in_pending);
751 _tx_dbg0("in_wait bottom");
761 /* Anytime we've sent a character since
762 * the last time we flushed, we'll need
763 * to send a packet -- the only other time
764 * we would send a packet is when that
765 * packet was full, in which case we now
766 * want to send an empty packet
768 ao_arch_block_interrupts();
769 while (!ao_usb_in_flushed) {
770 _tx_dbg0("flush top");
772 _tx_dbg0("flush end");
774 ao_arch_release_interrupts();
778 ao_usb_putchar(char c)
783 ao_arch_block_interrupts();
786 ao_usb_in_flushed = 0;
787 ao_usb_in_tx_buffer[ao_usb_in_tx_cur][ao_usb_tx_count++] = (uint8_t) c;
789 /* Send the packet when full */
790 if (ao_usb_tx_count == AO_USB_IN_SIZE) {
791 _tx_dbg0("putchar full");
793 _tx_dbg0("putchar flushed");
795 ao_arch_release_interrupts();
799 _ao_usb_out_recv(void)
801 _rx_dbg0("out_recv top");
802 ao_usb_out_avail = 0;
804 ao_usb_rx_count = AO_USB_OUT_SIZE - ao_usb_epn_out_count(AO_USB_OUT_EP);
806 _rx_dbg1("out_recv count", ao_usb_rx_count);
807 debug ("recv %d\n", ao_usb_rx_count);
808 debug_data("Fill OUT len %d\n", ao_usb_rx_count);
810 ao_usb_out_rx_cur = 1 - ao_usb_out_rx_cur;
813 ao_usb_set_epn_out(AO_USB_OUT_EP, ao_usb_out_rx_buffer[1-ao_usb_out_rx_cur], AO_USB_OUT_SIZE);
817 _ao_usb_pollchar(void)
822 return AO_READ_AGAIN;
825 if (ao_usb_rx_pos != ao_usb_rx_count)
828 _rx_dbg0("poll check");
829 /* Check to see if a packet has arrived */
830 if (!ao_usb_out_avail) {
831 _rx_dbg0("poll none");
832 return AO_READ_AGAIN;
837 /* Pull a character out of the fifo */
838 c = ao_usb_out_rx_buffer[ao_usb_out_rx_cur][ao_usb_rx_pos++];
847 ao_arch_block_interrupts();
848 while ((c = _ao_usb_pollchar()) == AO_READ_AGAIN)
849 ao_sleep(AO_USB_OUT_SLEEP_ADDR);
850 ao_arch_release_interrupts();
857 ao_arch_block_interrupts();
860 ao_gpio_set(AO_USB_PULLUP_PORT, AO_USB_PULLUP_PIN, AO_USB_PULLUP, 0);
862 /* Disable interrupts */
865 lpc_nvic_clear_enable(LPC_ISR_USB_IRQ_POS);
867 /* Disable the device */
868 lpc_usb.devcmdstat = 0;
870 /* Turn off USB clock */
871 lpc_scb.usbclkdiv = 0;
873 /* Disable USB PHY and PLL */
874 lpc_scb.pdruncfg |= ((1 << LPC_SCB_PDRUNCFG_USBPAD_PD) |
875 (1 << LPC_SCB_PDRUNCFG_USBPLL_PD));
877 /* Disable USB registers and RAM */
878 lpc_scb.sysahbclkctrl &= ~((1 << LPC_SCB_SYSAHBCLKCTRL_USB) |
879 (1 << LPC_SCB_SYSAHBCLKCTRL_USBRAM));
881 ao_arch_release_interrupts();
889 /* Enable USB pins */
890 #if HAS_LPC_USB_CONNECT
891 lpc_ioconf.pio0_6 = ((LPC_IOCONF_FUNC_USB_CONNECT << LPC_IOCONF_FUNC) |
892 (LPC_IOCONF_MODE_INACTIVE << LPC_IOCONF_MODE) |
893 (0 << LPC_IOCONF_HYS) |
894 (0 << LPC_IOCONF_INV) |
895 (0 << LPC_IOCONF_OD) |
899 lpc_ioconf.pio0_3 = ((LPC_IOCONF_FUNC_USB_VBUS << LPC_IOCONF_FUNC) |
900 (LPC_IOCONF_MODE_INACTIVE << LPC_IOCONF_MODE) |
901 (0 << LPC_IOCONF_HYS) |
902 (0 << LPC_IOCONF_INV) |
903 (0 << LPC_IOCONF_OD) |
906 /* Enable USB registers and RAM */
907 lpc_scb.sysahbclkctrl |= ((1 << LPC_SCB_SYSAHBCLKCTRL_USB) |
908 (1 << LPC_SCB_SYSAHBCLKCTRL_USBRAM));
911 lpc_scb.pdruncfg &= ~(1 << LPC_SCB_PDRUNCFG_USBPAD_PD);
913 /* Turn on USB PLL */
914 lpc_scb.pdruncfg &= ~(1 << LPC_SCB_PDRUNCFG_USBPLL_PD);
916 lpc_scb.usbpllclksel = (LPC_SCB_SYSPLLCLKSEL_SEL_SYSOSC << LPC_SCB_SYSPLLCLKSEL_SEL);
917 lpc_scb.usbpllclkuen = (0 << LPC_SCB_USBPLLCLKUEN_ENA);
918 lpc_scb.usbpllclkuen = (1 << LPC_SCB_USBPLLCLKUEN_ENA);
919 while (!(lpc_scb.usbpllclkuen & (1 << LPC_SCB_USBPLLCLKUEN_ENA)))
921 lpc_scb.usbpllctrl = 0x23;
922 while (!(lpc_scb.usbpllstat & 1))
925 lpc_scb.usbclksel = 0;
926 lpc_scb.usbclkuen = (0 << LPC_SCB_USBCLKUEN_ENA);
927 lpc_scb.usbclkuen = (1 << LPC_SCB_USBCLKUEN_ENA);
928 while (!(lpc_scb.usbclkuen & (1 << LPC_SCB_USBCLKUEN_ENA)))
931 /* Turn on USB clock, use 48MHz clock unchanged */
932 lpc_scb.usbclkdiv = 1;
934 /* Configure interrupts */
935 ao_arch_block_interrupts();
937 /* Route all interrupts to the main isr */
938 lpc_usb.introuting = 0;
942 lpc_nvic_set_enable(LPC_ISR_USB_IRQ_POS);
943 lpc_nvic_set_priority(LPC_ISR_USB_IRQ_POS, 0);
945 /* Clear any spurious interrupts */
946 lpc_usb.intstat = 0xffffffff;
948 debug ("ao_usb_enable\n");
950 /* Enable interrupts */
951 lpc_usb.inten = ((1 << LPC_USB_INT_EPOUT(0)) |
952 (1 << LPC_USB_INT_EPIN(0)) |
953 (1 << LPC_USB_INT_EPIN(AO_USB_INT_EP)) |
954 (1 << LPC_USB_INT_EPOUT(AO_USB_OUT_EP)) |
955 (1 << LPC_USB_INT_EPIN(AO_USB_IN_EP)) |
956 (1 << LPC_USB_INT_DEV));
958 ao_arch_release_interrupts();
960 lpc_usb.devcmdstat = 0;
961 for (t = 0; t < 1000; t++)
964 ao_usb_sram = lpc_usb_sram;
966 ao_usb_ep0_rx_buffer = ao_usb_alloc_sram(AO_USB_CONTROL_SIZE);
967 ao_usb_ep0_tx_buffer = ao_usb_alloc_sram(AO_USB_CONTROL_SIZE);
968 ao_usb_ep0_setup_buffer = ao_usb_alloc_sram(AO_USB_CONTROL_SIZE);
970 ao_usb_out_rx_buffer[0] = ao_usb_alloc_sram(AO_USB_OUT_SIZE);
971 ao_usb_out_rx_buffer[1] = ao_usb_alloc_sram(AO_USB_OUT_SIZE);
972 ao_usb_in_tx_buffer[0] = ao_usb_alloc_sram(AO_USB_IN_SIZE);
973 ao_usb_in_tx_buffer[1] = ao_usb_alloc_sram(AO_USB_IN_SIZE);
978 ao_gpio_set(AO_USB_PULLUP_PORT, AO_USB_PULLUP_PIN, AO_USB_PULLUP, 1);
983 struct ao_task ao_usb_echo_task;
991 c = ao_usb_getchar();
1002 printf ("control: %d out: %d in: %d int: %d reset: %d\n",
1003 control_count, out_count, in_count, int_count, reset_count);
1006 __code struct ao_cmds ao_usb_cmds[] = {
1007 { ao_usb_irq, "I\0Show USB interrupt counts" },
1016 ao_enable_output(AO_USB_PULLUP_PORT, AO_USB_PULLUP_PIN, AO_USB_PULLUP, 0);
1020 debug ("ao_usb_init\n");
1022 ao_add_task(&ao_usb_echo_task, ao_usb_echo, "usb echo");
1025 ao_cmd_register(&ao_usb_cmds[0]);
1028 ao_add_stdio(_ao_usb_pollchar, ao_usb_putchar, ao_usb_flush);
1032 #if TX_DBG || RX_DBG
1042 uint32_t in_pending;
1044 uint32_t in_flushed;
1054 #define NUM_USB_DBG 8
1056 static struct ao_usb_dbg dbg[NUM_USB_DBG];
1059 static void _dbg(int line, char *msg, uint32_t value)
1062 dbg[dbg_i].line = line;
1063 dbg[dbg_i].msg = msg;
1064 dbg[dbg_i].value = value;
1065 asm("mrs %0,primask" : "=&r" (primask));
1066 dbg[dbg_i].primask = primask;
1068 dbg[dbg_i].in_count = in_count;
1069 dbg[dbg_i].in_ep = *ao_usb_epn_in(AO_USB_IN_EP, 0);
1070 dbg[dbg_i].in_pending = ao_usb_in_pending;
1071 dbg[dbg_i].tx_count = ao_usb_tx_count;
1072 dbg[dbg_i].in_flushed = ao_usb_in_flushed;
1075 dbg[dbg_i].rx_count = ao_usb_rx_count;
1076 dbg[dbg_i].rx_pos = ao_usb_rx_pos;
1077 dbg[dbg_i].out_avail = ao_usb_out_avail;
1078 dbg[dbg_i].out_ep = *ao_usb_epn_out(AO_USB_OUT_EP, 0);
1080 if (++dbg_i == NUM_USB_DBG)