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; 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.
24 #define debug(format, args...) printf(format, ## args)
26 #define debug(format, args...)
29 struct ao_task __xdata ao_usb_task;
32 uint8_t dir_type_recip;
37 } __xdata ao_usb_setup;
39 static __xdata uint8_t ao_usb_ep0_state;
40 static const uint8_t * __xdata ao_usb_ep0_in_data;
41 static __xdata uint8_t ao_usb_ep0_in_len;
42 static __xdata uint8_t ao_usb_ep0_in_pending;
43 static __xdata uint8_t ao_usb_addr_pending;
44 static __xdata uint8_t ao_usb_ep0_in_buf[2];
45 static __xdata uint8_t ao_usb_ep0_out_len;
46 static __xdata uint8_t *__xdata ao_usb_ep0_out_data;
48 static __xdata uint8_t ao_usb_in_flushed;
49 static __xdata uint8_t ao_usb_running;
50 static __xdata uint8_t ao_usb_configuration;
51 static __xdata uint8_t ueienx_0;
54 ao_usb_set_address(uint8_t address)
56 UDADDR = (0 << ADDEN) | address;
57 ao_usb_addr_pending = 1;
60 #define EP_SIZE(s) ((s) == 64 ? 0x30 : \
66 ao_usb_dump_ep(uint8_t ep)
69 debug ("EP %d: UECONX %02x UECFG0X %02x UECFG1X %02x UEIENX %02x UESTA0X %02x UESTA1X %02X\n",
70 ep, UECONX, UECFG0X, UECFG1X, UEIENX, UESTA0X, UESTA1X);
77 /* Set the CONTROL max packet size, single buffered */
79 UECONX = (1 << EPEN); /* Enable */
81 UECFG0X = ((0 << EPTYPE0) | /* Control */
82 (0 << EPDIR)); /* Out (ish) */
84 UECFG1X = (EP_SIZE(AO_USB_CONTROL_SIZE) | /* Size */
85 (0 << EPBK0) | /* Single bank */
88 ueienx_0 = ((1 << RXSTPE) | /* Enable SETUP interrupt */
89 (1 << RXOUTE)); /* Enable OUT interrupt */
92 ao_usb_addr_pending = 0;
96 ao_usb_set_configuration(void)
98 /* Set the IN max packet size, double buffered */
100 UECONX = (1 << EPEN); /* Enable */
102 UECFG0X = ((2 << EPTYPE0) | /* Bulk */
103 (1 << EPDIR)); /* In */
105 UECFG1X = (EP_SIZE(AO_USB_IN_SIZE) | /* Size */
106 (1 << EPBK0) | /* Double bank */
107 (1 << ALLOC)); /* Allocate */
110 UEIENX = ((1 << TXINE)); /* Enable IN complete interrupt */
113 ao_usb_dump_ep(AO_USB_IN_EP);
115 /* Set the OUT max packet size, double buffered */
116 UENUM = AO_USB_OUT_EP;
117 UECONX |= (1 << EPEN); /* Enable */
119 UECFG0X = ((2 << EPTYPE0) | /* Bulk */
120 (0 << EPDIR)); /* Out */
122 UECFG1X = (EP_SIZE(AO_USB_OUT_SIZE) | /* Size */
123 (1 << EPBK0) | /* Double bank */
124 (1 << ALLOC)); /* Allocate */
126 UEIENX = ((1 << RXOUTE)); /* Enable OUT complete interrupt */
128 ao_usb_dump_ep(AO_USB_OUT_EP);
134 ao_wakeup(&ao_usb_task);
138 __xdata static struct ao_usb_line_coding ao_usb_line_coding = {115200, 0, 0, 8};
140 /* Walk through the list of descriptors and find a match
143 ao_usb_get_descriptor(uint16_t value)
145 const uint8_t *__xdata descriptor;
146 __xdata uint8_t type = value >> 8;
147 __xdata uint8_t index = value;
149 descriptor = ao_usb_descriptors;
150 while (descriptor[0] != 0) {
151 if (descriptor[1] == type && index-- == 0) {
152 if (type == AO_USB_DESC_CONFIGURATION)
153 ao_usb_ep0_in_len = descriptor[2];
155 ao_usb_ep0_in_len = descriptor[0];
156 ao_usb_ep0_in_data = descriptor;
159 descriptor += descriptor[0];
164 ao_usb_ep0_set_in_pending(uint8_t in_pending)
166 ao_usb_ep0_in_pending = in_pending;
169 ueienx_0 = ((1 << RXSTPE) | (1 << RXOUTE) | (1 << TXINE)); /* Enable IN interrupt */
172 /* Send an IN data packet */
174 ao_usb_ep0_flush(void)
176 __xdata uint8_t this_len;
180 if (!(UEINTX & (1 << TXINI))) {
181 debug("EP0 not accepting IN data\n");
182 ao_usb_ep0_set_in_pending(1);
184 this_len = ao_usb_ep0_in_len;
185 if (this_len > AO_USB_CONTROL_SIZE)
186 this_len = AO_USB_CONTROL_SIZE;
188 ao_usb_ep0_in_len -= this_len;
190 /* Set IN interrupt enable */
191 if (ao_usb_ep0_in_len == 0 && this_len != AO_USB_CONTROL_SIZE)
192 ao_usb_ep0_set_in_pending(0);
194 ao_usb_ep0_set_in_pending(1);
196 debug ("Flush EP0 len %d:", this_len);
198 uint8_t c = *ao_usb_ep0_in_data++;
204 /* Clear the TXINI bit to send the packet */
205 UEINTX &= ~(1 << TXINI);
210 /* Read data from the ep0 OUT fifo */
212 ao_usb_ep0_fill(uint8_t len, uint8_t ack)
214 if (len > ao_usb_ep0_out_len)
215 len = ao_usb_ep0_out_len;
216 ao_usb_ep0_out_len -= len;
218 // debug ("EP0 UEINTX %02x UEBCLX %d UEBCHX %d\n",
219 // UEINTX, UEBCLX, UEBCHX);
220 /* Pull all of the data out of the packet */
221 debug ("Fill EP0 len %d:", len);
225 *ao_usb_ep0_out_data++ = c;
235 ao_usb_ep0_queue_byte(uint8_t a)
237 ao_usb_ep0_in_buf[ao_usb_ep0_in_len++] = a;
241 ao_usb_ep0_setup(void)
243 /* Pull the setup packet out of the fifo */
244 ao_usb_ep0_out_data = (__xdata uint8_t *) &ao_usb_setup;
245 ao_usb_ep0_out_len = 8;
246 ao_usb_ep0_fill(8, (1 << RXSTPI) | (1 << RXOUTI) | (1 << TXINI));
247 if (ao_usb_ep0_out_len != 0) {
248 debug ("invalid setup packet length\n");
252 /* Figure out how to ACK the setup packet */
253 if (ao_usb_setup.dir_type_recip & AO_USB_DIR_IN) {
254 if (ao_usb_setup.length)
255 ao_usb_ep0_state = AO_USB_EP0_DATA_IN;
257 ao_usb_ep0_state = AO_USB_EP0_IDLE;
259 if (ao_usb_setup.length)
260 ao_usb_ep0_state = AO_USB_EP0_DATA_OUT;
262 ao_usb_ep0_state = AO_USB_EP0_IDLE;
266 if (ao_usb_ep0_state == AO_USB_EP0_IDLE)
267 USBCS0 = USBCS0_CLR_OUTPKT_RDY | USBCS0_DATA_END;
269 USBCS0 = USBCS0_CLR_OUTPKT_RDY;
272 ao_usb_ep0_in_data = ao_usb_ep0_in_buf;
273 ao_usb_ep0_in_len = 0;
274 switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_TYPE_MASK) {
275 case AO_USB_TYPE_STANDARD:
276 debug ("Standard setup packet\n");
277 switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_RECIP_MASK) {
278 case AO_USB_RECIP_DEVICE:
279 debug ("Device setup packet\n");
280 switch(ao_usb_setup.request) {
281 case AO_USB_REQ_GET_STATUS:
282 debug ("get status\n");
283 ao_usb_ep0_queue_byte(0);
284 ao_usb_ep0_queue_byte(0);
286 case AO_USB_REQ_SET_ADDRESS:
287 debug ("set address %d\n", ao_usb_setup.value);
288 ao_usb_set_address(ao_usb_setup.value);
290 case AO_USB_REQ_GET_DESCRIPTOR:
291 debug ("get descriptor %d\n", ao_usb_setup.value);
292 ao_usb_get_descriptor(ao_usb_setup.value);
294 case AO_USB_REQ_GET_CONFIGURATION:
295 debug ("get configuration %d\n", ao_usb_configuration);
296 ao_usb_ep0_queue_byte(ao_usb_configuration);
298 case AO_USB_REQ_SET_CONFIGURATION:
299 ao_usb_configuration = ao_usb_setup.value;
300 debug ("set configuration %d\n", ao_usb_configuration);
301 ao_usb_set_configuration();
305 case AO_USB_RECIP_INTERFACE:
306 debug ("Interface setup packet\n");
307 switch(ao_usb_setup.request) {
308 case AO_USB_REQ_GET_STATUS:
309 ao_usb_ep0_queue_byte(0);
310 ao_usb_ep0_queue_byte(0);
312 case AO_USB_REQ_GET_INTERFACE:
313 ao_usb_ep0_queue_byte(0);
315 case AO_USB_REQ_SET_INTERFACE:
319 case AO_USB_RECIP_ENDPOINT:
320 debug ("Endpoint setup packet\n");
321 switch(ao_usb_setup.request) {
322 case AO_USB_REQ_GET_STATUS:
323 ao_usb_ep0_queue_byte(0);
324 ao_usb_ep0_queue_byte(0);
330 case AO_USB_TYPE_CLASS:
331 debug ("Class setup packet\n");
332 switch (ao_usb_setup.request) {
333 case AO_USB_SET_LINE_CODING:
334 debug ("set line coding\n");
335 ao_usb_ep0_out_len = 7;
336 ao_usb_ep0_out_data = (__xdata uint8_t *) &ao_usb_line_coding;
338 case AO_USB_GET_LINE_CODING:
339 debug ("get line coding\n");
340 ao_usb_ep0_in_len = 7;
341 ao_usb_ep0_in_data = (uint8_t *) &ao_usb_line_coding;
343 case AO_USB_SET_CONTROL_LINE_STATE:
348 if (ao_usb_ep0_state != AO_USB_EP0_DATA_OUT) {
349 if (ao_usb_setup.length < ao_usb_ep0_in_len)
350 ao_usb_ep0_in_len = ao_usb_setup.length;
351 debug ("Start ep0 in delivery %d\n", ao_usb_ep0_in_len);
352 ao_usb_ep0_set_in_pending(1);
356 /* End point 0 receives all of the control messages. */
362 debug ("usb task started\n");
363 ao_usb_ep0_state = AO_USB_EP0_IDLE;
369 // debug ("UDINT %02x\n", udint);
370 if (udint & (1 << EORSTI)) {
371 ao_usb_configuration = 0;
376 // debug ("UEINTX %02x\n", intx);
377 if (intx & ((1 << RXSTPI) | (1 << RXOUTI)))
379 if ((intx & (1 << TXINI))) {
380 if (ao_usb_ep0_in_pending)
384 if (ao_usb_addr_pending) {
385 UDADDR |= (1 << ADDEN);
386 ao_usb_addr_pending = 0;
388 ueienx_0 = ((1 << RXSTPE) | (1 << RXOUTE)); /* Disable IN interrupt */
391 // debug ("usb task sleeping...\n");
394 ao_sleep(&ao_usb_task);
397 // debug ("UEINTX for ep0 is %02x\n", intx);
398 if (intx & (1 << RXSTPI)) {
401 if (intx & (1 << RXOUTI)) {
402 ao_usb_ep0_fill(UEBCLX, (1 << RXOUTI));
403 ao_usb_ep0_set_in_pending(1);
405 if (intx & (1 << TXINI) && ao_usb_ep0_in_pending) {
406 debug ("continue sending ep0 IN data\n");
412 /* Wait for a free IN buffer */
414 _ao_usb_in_wait(void)
417 /* Check if the current buffer is writable */
418 UENUM = AO_USB_IN_EP;
419 if (UEINTX & (1 << RWAL))
422 /* Wait for an IN buffer to be ready */
424 UENUM = AO_USB_IN_EP;
425 if ((UEINTX & (1 << TXINI)))
427 UEIENX = (1 << TXINE);
428 ao_sleep(&ao_usb_in_flushed);
430 /* Ack the interrupt */
431 UEINTX &= ~(1 << TXINI);
435 /* Queue the current IN buffer for transmission */
437 _ao_usb_in_send(void)
439 UENUM = AO_USB_IN_EP;
440 UEINTX &= ~(1 << FIFOCON);
449 ao_arch_block_interrupts();
450 /* Anytime we've sent a character since
451 * the last time we flushed, we'll need
452 * to send a packet -- the only other time
453 * we would send a packet is when that
454 * packet was full, in which case we now
455 * want to send an empty packet
457 if (!ao_usb_in_flushed) {
458 ao_usb_in_flushed = 1;
462 ao_arch_release_interrupts();
466 ao_usb_putchar(char c)
471 ao_arch_block_interrupts();
475 UENUM = AO_USB_IN_EP;
478 /* Send the packet when full */
479 if ((UEINTX & (1 << RWAL)) == 0)
481 ao_usb_in_flushed = 0;
482 ao_arch_release_interrupts();
486 _ao_usb_pollchar(void)
492 return AO_READ_AGAIN;
495 UENUM = AO_USB_OUT_EP;
497 debug("usb_pollchar UEINTX %02d\n", intx);
498 if (intx & (1 << RWAL))
501 if (intx & (1 << FIFOCON)) {
502 /* Ack the last packet */
503 UEINTX = (uint8_t) ~(1 << FIFOCON);
506 /* Check to see if a packet has arrived */
507 if ((intx & (1 << RXOUTI)) == 0) {
508 UENUM = AO_USB_OUT_EP;
509 UEIENX = (1 << RXOUTE);
510 return AO_READ_AGAIN;
513 /* Ack the interrupt */
514 UEINTX = ~(1 << RXOUTI);
517 /* Pull a character out of the fifo */
527 ao_arch_block_interrupts();
528 while ((c = _ao_usb_pollchar()) == AO_READ_AGAIN)
529 ao_sleep(&ao_stdin_ready);
530 ao_arch_release_interrupts();
534 uint16_t control_count;
538 /* Endpoint interrupt */
541 uint8_t old_num = UENUM;
545 ao_led_toggle(AO_LED_RED);
551 ao_wakeup(&ao_usb_task);
554 if (i & (1 << AO_USB_IN_EP)) {
555 UENUM = AO_USB_IN_EP;
557 ao_wakeup(&ao_usb_in_flushed);
560 if (i & (1 << AO_USB_OUT_EP)) {
561 UENUM = AO_USB_OUT_EP;
563 ao_wakeup(&ao_stdin_ready);
570 #define AO_PAD_REGULATOR_INIT (1 << UVREGE) /* Turn on pad regulator */
573 /* TeleScience V0.1 has a hardware bug -- UVcc is hooked up, but UCap is not
574 * Make this work by running power through UVcc to the USB system
576 #define AO_PAD_REGULATOR_INIT (1 << UVREGE) /* Turn off pad regulator */
579 #if AVR_CLOCK == 16000000UL
580 #define AO_USB_PLL_INPUT_PRESCALER (1 << PINDIV) /* Divide 16MHz clock by 2 */
582 #if AVR_CLOCK == 8000000UL
583 #define AO_USB_PLL_INPUT_PRESCALER 0 /* Don't divide clock */
589 /* Unplug from the bus */
590 UDCON = (1 << DETACH);
592 /* Disable the interface */
595 /* Disable the PLL */
598 /* Turn off the pad regulator */
602 #define AO_USB_CON ((1 << USBE) | /* USB enable */ \
603 (0 << RSTCPU) | /* do not reset CPU */ \
604 (0 << LSM) | /* Full speed mode */ \
605 (0 << RMWKUP)) /* no remote wake-up */ \
610 /* Configure pad regulator */
611 UHWCON = AO_PAD_REGULATOR_INIT;
613 /* Enable USB device, but freeze the clocks until initialized */
614 USBCON = AO_USB_CON | (1 <<FRZCLK);
616 /* Enable PLL with appropriate divider */
617 PLLCSR = AO_USB_PLL_INPUT_PRESCALER | (1 << PLLE);
619 /* Wait for PLL to lock */
620 loop_until_bit_is_set(PLLCSR, (1 << PLOCK));
622 /* Enable USB, enable the VBUS pad */
623 USBCON = AO_USB_CON | (1 << OTGPADE);
625 /* Enable global interrupts */
626 UDIEN = (1 << EORSTE); /* End of reset interrupt */
628 ao_usb_configuration = 0;
630 debug ("ao_usb_enable\n");
632 debug ("UHWCON %02x USBCON %02x PLLCSR %02x UDIEN %02x\n",
633 UHWCON, USBCON, PLLCSR, UDIEN);
634 UDCON = (0 << DETACH); /* Clear the DETACH bit to plug into the bus */
638 struct ao_task __xdata ao_usb_echo_task;
646 c = ao_usb_getchar();
658 debug ("ao_usb_init\n");
659 ao_add_task(&ao_usb_task, ao_usb_ep0, "usb");
661 ao_add_task(&ao_usb_echo_task, ao_usb_echo, "usb echo");
663 ao_add_stdio(_ao_usb_pollchar, ao_usb_putchar, ao_usb_flush);