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;
53 ao_usb_set_address(uint8_t address)
55 UDADDR = (0 << ADDEN) | address;
56 ao_usb_addr_pending = 1;
59 #define EP_SIZE(s) ((s) == 64 ? 0x30 : \
65 ao_usb_dump_ep(uint8_t ep)
68 debug ("EP %d: UECONX %02x UECFG0X %02x UECFG1X %02x UEIENX %02x UESTA0X %02x UESTA1X %02X\n",
69 ep, UECONX, UECFG0X, UECFG1X, UEIENX, UESTA0X, UESTA1X);
76 /* Set the CONTROL max packet size, single buffered */
78 UECONX = (1 << EPEN); /* Enable */
80 UECFG0X = ((0 << EPTYPE0) | /* Control */
81 (0 << EPDIR)); /* Out (ish) */
83 UECFG1X = (EP_SIZE(AO_USB_CONTROL_SIZE) | /* Size */
84 (0 << EPBK0) | /* Single bank */
87 UEIENX = ((1 << RXSTPE) | /* Enable SETUP interrupt */
88 (1 << RXOUTE)); /* Enable OUT interrupt */
91 ao_usb_addr_pending = 0;
95 ao_usb_set_configuration(void)
97 /* Set the IN max packet size, double buffered */
99 UECONX = (1 << EPEN); /* Enable */
101 UECFG0X = ((2 << EPTYPE0) | /* Bulk */
102 (1 << EPDIR)); /* In */
104 UECFG1X = (EP_SIZE(AO_USB_IN_SIZE) | /* Size */
105 (1 << EPBK0) | /* Double bank */
106 (1 << ALLOC)); /* Allocate */
109 UEIENX = ((1 << TXINE)); /* Enable IN complete interrupt */
112 ao_usb_dump_ep(AO_USB_IN_EP);
114 /* Set the OUT max packet size, double buffered */
115 UENUM = AO_USB_OUT_EP;
116 UECONX |= (1 << EPEN); /* Enable */
118 UECFG0X = ((2 << EPTYPE0) | /* Bulk */
119 (0 << EPDIR)); /* Out */
121 UECFG1X = (EP_SIZE(AO_USB_OUT_SIZE) | /* Size */
122 (1 << EPBK0) | /* Double bank */
123 (1 << ALLOC)); /* Allocate */
125 UEIENX = ((1 << RXOUTE)); /* Enable OUT complete interrupt */
127 ao_usb_dump_ep(AO_USB_OUT_EP);
133 ao_wakeup(&ao_usb_task);
137 __xdata static struct ao_usb_line_coding ao_usb_line_coding = {115200, 0, 0, 8};
139 /* Walk through the list of descriptors and find a match
142 ao_usb_get_descriptor(uint16_t value)
144 const uint8_t *__xdata descriptor;
145 __xdata uint8_t type = value >> 8;
146 __xdata uint8_t index = value;
148 descriptor = ao_usb_descriptors;
149 while (descriptor[0] != 0) {
150 if (descriptor[1] == type && index-- == 0) {
151 if (type == AO_USB_DESC_CONFIGURATION)
152 ao_usb_ep0_in_len = descriptor[2];
154 ao_usb_ep0_in_len = descriptor[0];
155 ao_usb_ep0_in_data = descriptor;
158 descriptor += descriptor[0];
163 ao_usb_ep0_set_in_pending(uint8_t in_pending)
165 ao_usb_ep0_in_pending = in_pending;
169 UEIENX = ((1 << RXSTPE) | (1 << RXOUTE) | (1 << TXINE)); /* Enable IN interrupt */
173 /* Send an IN data packet */
175 ao_usb_ep0_flush(void)
177 __xdata uint8_t this_len;
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 = (__xdata 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:
308 #pragma disable_warning 110
310 debug ("Interface setup packet\n");
311 switch(ao_usb_setup.request) {
312 case AO_USB_REQ_GET_STATUS:
313 ao_usb_ep0_queue_byte(0);
314 ao_usb_ep0_queue_byte(0);
316 case AO_USB_REQ_GET_INTERFACE:
317 ao_usb_ep0_queue_byte(0);
319 case AO_USB_REQ_SET_INTERFACE:
323 case AO_USB_RECIP_ENDPOINT:
324 debug ("Endpoint setup packet\n");
325 switch(ao_usb_setup.request) {
326 case AO_USB_REQ_GET_STATUS:
327 ao_usb_ep0_queue_byte(0);
328 ao_usb_ep0_queue_byte(0);
334 case AO_USB_TYPE_CLASS:
335 debug ("Class setup packet\n");
336 switch (ao_usb_setup.request) {
337 case SET_LINE_CODING:
338 debug ("set line coding\n");
339 ao_usb_ep0_out_len = 7;
340 ao_usb_ep0_out_data = (__xdata uint8_t *) &ao_usb_line_coding;
342 case GET_LINE_CODING:
343 debug ("get line coding\n");
344 ao_usb_ep0_in_len = 7;
345 ao_usb_ep0_in_data = (uint8_t *) &ao_usb_line_coding;
347 case SET_CONTROL_LINE_STATE:
352 if (ao_usb_ep0_state != AO_USB_EP0_DATA_OUT) {
353 if (ao_usb_setup.length < ao_usb_ep0_in_len)
354 ao_usb_ep0_in_len = ao_usb_setup.length;
355 debug ("Start ep0 in delivery %d\n", ao_usb_ep0_in_len);
356 ao_usb_ep0_set_in_pending(1);
360 /* End point 0 receives all of the control messages. */
366 debug ("usb task started\n");
367 ao_usb_ep0_state = AO_USB_EP0_IDLE;
373 // debug ("UDINT %02x\n", udint);
374 if (udint & (1 << EORSTI)) {
375 ao_usb_configuration = 0;
380 // debug ("UEINTX %02x\n", intx);
381 if (intx & ((1 << RXSTPI) | (1 << RXOUTI)))
383 if ((intx & (1 << TXINI))) {
384 if (ao_usb_ep0_in_pending)
388 if (ao_usb_addr_pending) {
389 UDADDR |= (1 << ADDEN);
390 ao_usb_addr_pending = 0;
392 UEIENX = ((1 << RXSTPE) | (1 << RXOUTE)); /* Disable IN interrupt */
395 // debug ("usb task sleeping...\n");
396 ao_sleep(&ao_usb_task);
399 // debug ("UEINTX for ep0 is %02x\n", intx);
400 if (intx & (1 << RXSTPI)) {
403 if (intx & (1 << RXOUTI)) {
404 ao_usb_ep0_fill(UEBCLX, (1 << RXOUTI));
405 ao_usb_ep0_set_in_pending(1);
407 if (intx & (1 << TXINI) && ao_usb_ep0_in_pending) {
408 debug ("continue sending ep0 IN data\n");
414 /* Wait for a free IN buffer */
419 /* Check if the current buffer is writable */
420 UENUM = AO_USB_IN_EP;
421 if (UEINTX & (1 << RWAL))
425 /* Wait for an IN buffer to be ready */
427 UENUM = AO_USB_IN_EP;
428 if ((UEINTX & (1 << TXINI)))
430 UEIENX = (1 << TXINE);
431 ao_sleep(&ao_usb_in_flushed);
433 /* Ack the interrupt */
434 UEINTX &= ~(1 << TXINI);
439 /* Queue the current IN buffer for transmission */
443 UENUM = AO_USB_IN_EP;
444 UEINTX &= ~(1 << FIFOCON);
448 ao_usb_flush(void) __critical
453 /* Anytime we've sent a character since
454 * the last time we flushed, we'll need
455 * to send a packet -- the only other time
456 * we would send a packet is when that
457 * packet was full, in which case we now
458 * want to send an empty packet
460 if (!ao_usb_in_flushed) {
461 ao_usb_in_flushed = 1;
468 ao_usb_putchar(char c) __critical __reentrant
476 UENUM = AO_USB_IN_EP;
479 /* Send the packet when full */
480 if ((UEINTX & (1 << RWAL)) == 0)
482 ao_usb_in_flushed = 0;
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 &= ~(1 << FIFOCON);
506 /* Check to see if a packet has arrived */
507 if ((intx & (1 << RXOUTI)) == 0)
508 return AO_READ_AGAIN;
510 /* Ack the interrupt */
511 UEINTX &= ~(1 << RXOUTI);
514 /* Pull a character out of the fifo */
520 ao_usb_pollchar(void)
524 c = _ao_usb_pollchar();
530 ao_usb_getchar(void) __critical
535 while ((c = ao_usb_pollchar()) == AO_READ_AGAIN)
536 ao_sleep(&ao_stdin_ready);
541 uint16_t control_count;
545 /* Endpoint interrupt */
548 uint8_t old_num = UENUM;
552 ao_led_toggle(AO_LED_RED);
556 ao_wakeup(&ao_usb_task);
559 if (i & (1 << AO_USB_IN_EP)) {
560 UENUM = AO_USB_IN_EP;
562 ao_wakeup(&ao_usb_in_flushed);
565 if (i & (1 << AO_USB_OUT_EP)) {
566 ao_wakeup(&ao_stdin_ready);
573 #define AO_PAD_REGULATOR_INIT (1 << UVREGE) /* Turn on pad regulator */
576 /* TeleScience V0.1 has a hardware bug -- UVcc is hooked up, but UCap is not
577 * Make this work by running power through UVcc to the USB system
579 #define AO_PAD_REGULATOR_INIT (1 << UVREGE) /* Turn off pad regulator */
582 #if AVR_CLOCK == 16000000UL
583 #define AO_USB_PLL_INPUT_PRESCALER (1 << PINDIV) /* Divide 16MHz clock by 2 */
585 #if AVR_CLOCK == 8000000UL
586 #define AO_USB_PLL_INPUT_PRESCALER 0 /* Don't divide clock */
592 /* Unplug from the bus */
593 UDCON = (1 << DETACH);
595 /* Disable the interface */
598 /* Disable the PLL */
601 /* Turn off the pad regulator */
605 #define AO_USB_CON ((1 << USBE) | /* USB enable */ \
606 (0 << RSTCPU) | /* do not reset CPU */ \
607 (0 << LSM) | /* Full speed mode */ \
608 (0 << RMWKUP)) /* no remote wake-up */ \
613 /* Configure pad regulator */
614 UHWCON = AO_PAD_REGULATOR_INIT;
616 /* Enable USB device, but freeze the clocks until initialized */
617 USBCON = AO_USB_CON | (1 <<FRZCLK);
619 /* Enable PLL with appropriate divider */
620 PLLCSR = AO_USB_PLL_INPUT_PRESCALER | (1 << PLLE);
622 /* Wait for PLL to lock */
623 loop_until_bit_is_set(PLLCSR, (1 << PLOCK));
625 /* Enable USB, enable the VBUS pad */
626 USBCON = AO_USB_CON | (1 << OTGPADE);
628 /* Enable global interrupts */
629 UDIEN = (1 << EORSTE); /* End of reset interrupt */
631 ao_usb_configuration = 0;
633 debug ("ao_usb_enable\n");
635 debug ("UHWCON %02x USBCON %02x PLLCSR %02x UDIEN %02x\n",
636 UHWCON, USBCON, PLLCSR, UDIEN);
637 UDCON = (0 << DETACH); /* Clear the DETACH bit to plug into the bus */
641 struct ao_task __xdata ao_usb_echo_task;
649 c = ao_usb_getchar();
659 printf ("control: %d out: %d in: %d\n",
660 control_count, out_count, in_count);
663 __code struct ao_cmds ao_usb_cmds[] = {
664 { ao_usb_irq, "i\0Show USB interrupt counts" },
673 debug ("ao_usb_init\n");
674 ao_add_task(&ao_usb_task, ao_usb_ep0, "usb");
675 // ao_add_task(&ao_usb_echo_task, ao_usb_echo, "usb echo");
676 ao_cmd_register(&ao_usb_cmds[0]);
677 ao_add_stdio(ao_usb_pollchar, ao_usb_putchar, ao_usb_flush);