2 * Copyright © 2011 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.
25 #define debug(format, args...) printf(format, ## args)
27 #define debug(format, args...)
30 struct ao_task ao_usb_task;
33 uint8_t dir_type_recip;
40 static uint8_t ao_usb_ep0_state;
41 static const uint8_t * ao_usb_ep0_in_data;
42 static uint8_t ao_usb_ep0_in_len;
43 static uint8_t ao_usb_ep0_in_pending;
44 static uint8_t ao_usb_addr_pending;
45 static uint8_t ao_usb_ep0_in_buf[2];
46 static uint8_t ao_usb_ep0_out_len;
47 static uint8_t *ao_usb_ep0_out_data;
49 static uint8_t ao_usb_in_flushed;
50 uint8_t ao_usb_running;
51 static uint8_t ao_usb_configuration;
52 static uint8_t ueienx_0;
55 ao_usb_set_address(uint8_t address)
57 UDADDR = (0 << ADDEN) | address;
58 ao_usb_addr_pending = 1;
61 #define EP_SIZE(s) ((s) == 64 ? 0x30 : \
67 ao_usb_dump_ep(uint8_t ep)
70 debug ("EP %d: UECONX %02x UECFG0X %02x UECFG1X %02x UEIENX %02x UESTA0X %02x UESTA1X %02X\n",
71 ep, UECONX, UECFG0X, UECFG1X, UEIENX, UESTA0X, UESTA1X);
78 /* Set the CONTROL max packet size, single buffered */
80 UECONX = (1 << EPEN); /* Enable */
82 UECFG0X = ((0 << EPTYPE0) | /* Control */
83 (0 << EPDIR)); /* Out (ish) */
85 UECFG1X = (EP_SIZE(AO_USB_CONTROL_SIZE) | /* Size */
86 (0 << EPBK0) | /* Single bank */
89 ueienx_0 = ((1 << RXSTPE) | /* Enable SETUP interrupt */
90 (1 << RXOUTE)); /* Enable OUT interrupt */
93 ao_usb_addr_pending = 0;
97 ao_usb_set_configuration(void)
99 /* Set the IN max packet size, double buffered */
100 UENUM = AO_USB_IN_EP;
101 UECONX = (1 << EPEN); /* Enable */
103 UECFG0X = ((2 << EPTYPE0) | /* Bulk */
104 (1 << EPDIR)); /* In */
106 UECFG1X = (EP_SIZE(AO_USB_IN_SIZE) | /* Size */
107 (1 << EPBK0) | /* Double bank */
108 (1 << ALLOC)); /* Allocate */
111 UEIENX = ((1 << TXINE)); /* Enable IN complete interrupt */
114 ao_usb_dump_ep(AO_USB_IN_EP);
116 /* Set the OUT max packet size, double buffered */
117 UENUM = AO_USB_OUT_EP;
118 UECONX |= (1 << EPEN); /* Enable */
120 UECFG0X = ((2 << EPTYPE0) | /* Bulk */
121 (0 << EPDIR)); /* Out */
123 UECFG1X = (EP_SIZE(AO_USB_OUT_SIZE) | /* Size */
124 (1 << EPBK0) | /* Double bank */
125 (1 << ALLOC)); /* Allocate */
127 UEIENX = ((1 << RXOUTE)); /* Enable OUT complete interrupt */
129 ao_usb_dump_ep(AO_USB_OUT_EP);
135 ao_wakeup(&ao_usb_task);
139 struct ao_usb_line_coding ao_usb_line_coding = {115200, 0, 0, 8};
141 /* Walk through the list of descriptors and find a match
144 ao_usb_get_descriptor(uint16_t value)
146 const uint8_t *descriptor;
147 uint8_t type = value >> 8;
148 uint8_t index = value;
150 descriptor = ao_usb_descriptors;
151 while (descriptor[0] != 0) {
152 if (descriptor[1] == type && index-- == 0) {
153 if (type == AO_USB_DESC_CONFIGURATION)
154 ao_usb_ep0_in_len = descriptor[2];
156 ao_usb_ep0_in_len = descriptor[0];
157 ao_usb_ep0_in_data = descriptor;
160 descriptor += descriptor[0];
165 ao_usb_ep0_set_in_pending(uint8_t in_pending)
167 ao_usb_ep0_in_pending = in_pending;
170 ueienx_0 = ((1 << RXSTPE) | (1 << RXOUTE) | (1 << TXINE)); /* Enable IN interrupt */
173 /* Send an IN data packet */
175 ao_usb_ep0_flush(void)
181 if (!(UEINTX & (1 << TXINI))) {
182 debug("EP0 not accepting IN data\n");
183 ao_usb_ep0_set_in_pending(1);
185 this_len = ao_usb_ep0_in_len;
186 if (this_len > AO_USB_CONTROL_SIZE)
187 this_len = AO_USB_CONTROL_SIZE;
189 ao_usb_ep0_in_len -= this_len;
191 /* Set IN interrupt enable */
192 if (ao_usb_ep0_in_len == 0 && this_len != AO_USB_CONTROL_SIZE)
193 ao_usb_ep0_set_in_pending(0);
195 ao_usb_ep0_set_in_pending(1);
197 debug ("Flush EP0 len %d:", this_len);
199 uint8_t c = *ao_usb_ep0_in_data++;
205 /* Clear the TXINI bit to send the packet */
206 UEINTX &= ~(1 << TXINI);
211 /* Read data from the ep0 OUT fifo */
213 ao_usb_ep0_fill(uint8_t len, uint8_t ack)
215 if (len > ao_usb_ep0_out_len)
216 len = ao_usb_ep0_out_len;
217 ao_usb_ep0_out_len -= len;
219 // debug ("EP0 UEINTX %02x UEBCLX %d UEBCHX %d\n",
220 // UEINTX, UEBCLX, UEBCHX);
221 /* Pull all of the data out of the packet */
222 debug ("Fill EP0 len %d:", len);
226 *ao_usb_ep0_out_data++ = c;
236 ao_usb_ep0_queue_byte(uint8_t a)
238 ao_usb_ep0_in_buf[ao_usb_ep0_in_len++] = a;
242 ao_usb_ep0_setup(void)
244 /* Pull the setup packet out of the fifo */
245 ao_usb_ep0_out_data = (uint8_t *) &ao_usb_setup;
246 ao_usb_ep0_out_len = 8;
247 ao_usb_ep0_fill(8, (1 << RXSTPI) | (1 << RXOUTI) | (1 << TXINI));
248 if (ao_usb_ep0_out_len != 0) {
249 debug ("invalid setup packet length\n");
253 /* Figure out how to ACK the setup packet */
254 if (ao_usb_setup.dir_type_recip & AO_USB_DIR_IN) {
255 if (ao_usb_setup.length)
256 ao_usb_ep0_state = AO_USB_EP0_DATA_IN;
258 ao_usb_ep0_state = AO_USB_EP0_IDLE;
260 if (ao_usb_setup.length)
261 ao_usb_ep0_state = AO_USB_EP0_DATA_OUT;
263 ao_usb_ep0_state = AO_USB_EP0_IDLE;
267 if (ao_usb_ep0_state == AO_USB_EP0_IDLE)
268 USBCS0 = USBCS0_CLR_OUTPKT_RDY | USBCS0_DATA_END;
270 USBCS0 = USBCS0_CLR_OUTPKT_RDY;
273 ao_usb_ep0_in_data = ao_usb_ep0_in_buf;
274 ao_usb_ep0_in_len = 0;
275 switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_TYPE_MASK) {
276 case AO_USB_TYPE_STANDARD:
277 debug ("Standard setup packet\n");
278 switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_RECIP_MASK) {
279 case AO_USB_RECIP_DEVICE:
280 debug ("Device setup packet\n");
281 switch(ao_usb_setup.request) {
282 case AO_USB_REQ_GET_STATUS:
283 debug ("get status\n");
284 ao_usb_ep0_queue_byte(0);
285 ao_usb_ep0_queue_byte(0);
287 case AO_USB_REQ_SET_ADDRESS:
288 debug ("set address %d\n", ao_usb_setup.value);
289 ao_usb_set_address(ao_usb_setup.value);
291 case AO_USB_REQ_GET_DESCRIPTOR:
292 debug ("get descriptor %d\n", ao_usb_setup.value);
293 ao_usb_get_descriptor(ao_usb_setup.value);
295 case AO_USB_REQ_GET_CONFIGURATION:
296 debug ("get configuration %d\n", ao_usb_configuration);
297 ao_usb_ep0_queue_byte(ao_usb_configuration);
299 case AO_USB_REQ_SET_CONFIGURATION:
300 ao_usb_configuration = ao_usb_setup.value;
301 debug ("set configuration %d\n", ao_usb_configuration);
302 ao_usb_set_configuration();
306 case AO_USB_RECIP_INTERFACE:
307 debug ("Interface setup packet\n");
308 switch(ao_usb_setup.request) {
309 case AO_USB_REQ_GET_STATUS:
310 ao_usb_ep0_queue_byte(0);
311 ao_usb_ep0_queue_byte(0);
313 case AO_USB_REQ_GET_INTERFACE:
314 ao_usb_ep0_queue_byte(0);
316 case AO_USB_REQ_SET_INTERFACE:
320 case AO_USB_RECIP_ENDPOINT:
321 debug ("Endpoint setup packet\n");
322 switch(ao_usb_setup.request) {
323 case AO_USB_REQ_GET_STATUS:
324 ao_usb_ep0_queue_byte(0);
325 ao_usb_ep0_queue_byte(0);
331 case AO_USB_TYPE_CLASS:
332 debug ("Class setup packet\n");
333 switch (ao_usb_setup.request) {
334 case AO_USB_SET_LINE_CODING:
335 debug ("set line coding\n");
336 ao_usb_ep0_out_len = 7;
337 ao_usb_ep0_out_data = (uint8_t *) &ao_usb_line_coding;
339 case AO_USB_GET_LINE_CODING:
340 debug ("get line coding\n");
341 ao_usb_ep0_in_len = 7;
342 ao_usb_ep0_in_data = (uint8_t *) &ao_usb_line_coding;
344 case AO_USB_SET_CONTROL_LINE_STATE:
349 if (ao_usb_ep0_state != AO_USB_EP0_DATA_OUT) {
350 if (ao_usb_setup.length < ao_usb_ep0_in_len)
351 ao_usb_ep0_in_len = ao_usb_setup.length;
352 debug ("Start ep0 in delivery %d\n", ao_usb_ep0_in_len);
353 ao_usb_ep0_set_in_pending(1);
357 /* End point 0 receives all of the control messages. */
363 debug ("usb task started\n");
364 ao_usb_ep0_state = AO_USB_EP0_IDLE;
370 // debug ("UDINT %02x\n", udint);
371 if (udint & (1 << EORSTI)) {
372 ao_usb_configuration = 0;
377 // debug ("UEINTX %02x\n", intx);
378 if (intx & ((1 << RXSTPI) | (1 << RXOUTI)))
380 if ((intx & (1 << TXINI))) {
381 if (ao_usb_ep0_in_pending)
385 if (ao_usb_addr_pending) {
386 UDADDR |= (1 << ADDEN);
387 ao_usb_addr_pending = 0;
389 ueienx_0 = ((1 << RXSTPE) | (1 << RXOUTE)); /* Disable IN interrupt */
392 // debug ("usb task sleeping...\n");
395 ao_sleep(&ao_usb_task);
398 // debug ("UEINTX for ep0 is %02x\n", intx);
399 if (intx & (1 << RXSTPI)) {
402 if (intx & (1 << RXOUTI)) {
403 ao_usb_ep0_fill(UEBCLX, (1 << RXOUTI));
404 ao_usb_ep0_set_in_pending(1);
406 if (intx & (1 << TXINI) && ao_usb_ep0_in_pending) {
407 debug ("continue sending ep0 IN data\n");
413 /* Wait for a free IN buffer */
415 _ao_usb_in_wait(void)
418 /* Check if the current buffer is writable */
419 UENUM = AO_USB_IN_EP;
420 if (UEINTX & (1 << RWAL))
423 /* Wait for an IN buffer to be ready */
425 UENUM = AO_USB_IN_EP;
426 if ((UEINTX & (1 << TXINI)))
428 UEIENX = (1 << TXINE);
429 ao_sleep(&ao_usb_in_flushed);
431 /* Ack the interrupt */
432 UEINTX &= ~(1 << TXINI);
436 /* Queue the current IN buffer for transmission */
438 _ao_usb_in_send(void)
440 UENUM = AO_USB_IN_EP;
441 UEINTX &= ~(1 << FIFOCON);
450 ao_arch_block_interrupts();
451 /* Anytime we've sent a character since
452 * the last time we flushed, we'll need
453 * to send a packet -- the only other time
454 * we would send a packet is when that
455 * packet was full, in which case we now
456 * want to send an empty packet
458 if (!ao_usb_in_flushed) {
459 ao_usb_in_flushed = 1;
463 ao_arch_release_interrupts();
467 ao_usb_putchar(char c)
472 ao_arch_block_interrupts();
476 UENUM = AO_USB_IN_EP;
479 /* Send the packet when full */
480 if ((UEINTX & (1 << RWAL)) == 0)
482 ao_usb_in_flushed = 0;
483 ao_arch_release_interrupts();
487 _ao_usb_pollchar(void)
493 return AO_READ_AGAIN;
496 UENUM = AO_USB_OUT_EP;
498 debug("usb_pollchar UEINTX %02d\n", intx);
499 if (intx & (1 << RWAL))
502 if (intx & (1 << FIFOCON)) {
503 /* Ack the last packet */
504 UEINTX = (uint8_t) ~(1 << FIFOCON);
507 /* Check to see if a packet has arrived */
508 if ((intx & (1 << RXOUTI)) == 0) {
509 UENUM = AO_USB_OUT_EP;
510 UEIENX = (1 << RXOUTE);
511 return AO_READ_AGAIN;
514 /* Ack the interrupt */
515 UEINTX = ~(1 << RXOUTI);
518 /* Pull a character out of the fifo */
528 ao_arch_block_interrupts();
529 while ((c = _ao_usb_pollchar()) == AO_READ_AGAIN)
530 ao_sleep(&ao_stdin_ready);
531 ao_arch_release_interrupts();
535 uint16_t control_count;
539 /* Endpoint interrupt */
542 uint8_t old_num = UENUM;
546 ao_led_toggle(AO_LED_RED);
552 ao_wakeup(&ao_usb_task);
555 if (i & (1 << AO_USB_IN_EP)) {
556 UENUM = AO_USB_IN_EP;
558 ao_wakeup(&ao_usb_in_flushed);
561 if (i & (1 << AO_USB_OUT_EP)) {
562 UENUM = AO_USB_OUT_EP;
564 ao_wakeup(&ao_stdin_ready);
571 #define AO_PAD_REGULATOR_INIT (1 << UVREGE) /* Turn on pad regulator */
574 /* TeleScience V0.1 has a hardware bug -- UVcc is hooked up, but UCap is not
575 * Make this work by running power through UVcc to the USB system
577 #define AO_PAD_REGULATOR_INIT (1 << UVREGE) /* Turn off pad regulator */
580 #if AVR_CLOCK == 16000000UL
581 #define AO_USB_PLL_INPUT_PRESCALER (1 << PINDIV) /* Divide 16MHz clock by 2 */
583 #if AVR_CLOCK == 8000000UL
584 #define AO_USB_PLL_INPUT_PRESCALER 0 /* Don't divide clock */
590 /* Unplug from the bus */
591 UDCON = (1 << DETACH);
593 /* Disable the interface */
596 /* Disable the PLL */
599 /* Turn off the pad regulator */
603 #define AO_USB_CON ((1 << USBE) | /* USB enable */ \
604 (0 << RSTCPU) | /* do not reset CPU */ \
605 (0 << LSM) | /* Full speed mode */ \
606 (0 << RMWKUP)) /* no remote wake-up */ \
611 /* Configure pad regulator */
612 UHWCON = AO_PAD_REGULATOR_INIT;
614 /* Enable USB device, but freeze the clocks until initialized */
615 USBCON = AO_USB_CON | (1 <<FRZCLK);
617 /* Enable PLL with appropriate divider */
618 PLLCSR = AO_USB_PLL_INPUT_PRESCALER | (1 << PLLE);
620 /* Wait for PLL to lock */
621 loop_until_bit_is_set(PLLCSR, (1 << PLOCK));
623 /* Enable USB, enable the VBUS pad */
624 USBCON = AO_USB_CON | (1 << OTGPADE);
626 /* Enable global interrupts */
627 UDIEN = (1 << EORSTE); /* End of reset interrupt */
629 ao_usb_configuration = 0;
631 debug ("ao_usb_enable\n");
633 debug ("UHWCON %02x USBCON %02x PLLCSR %02x UDIEN %02x\n",
634 UHWCON, USBCON, PLLCSR, UDIEN);
635 UDCON = (0 << DETACH); /* Clear the DETACH bit to plug into the bus */
639 struct ao_task ao_usb_echo_task;
647 c = ao_usb_getchar();
659 debug ("ao_usb_init\n");
660 ao_add_task(&ao_usb_task, ao_usb_ep0, "usb");
662 ao_add_task(&ao_usb_echo_task, ao_usb_echo, "usb echo");
664 ao_add_stdio(_ao_usb_pollchar, ao_usb_putchar, ao_usb_flush);