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 */
417 /* Check if the current buffer is writable */
418 UENUM = AO_USB_IN_EP;
419 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);
437 /* Queue the current IN buffer for transmission */
441 UENUM = AO_USB_IN_EP;
442 UEINTX &= ~(1 << FIFOCON);
446 ao_usb_flush(void) __critical
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;
466 ao_usb_putchar(char c) __critical __reentrant
474 UENUM = AO_USB_IN_EP;
477 /* Send the packet when full */
478 if ((UEINTX & (1 << RWAL)) == 0)
480 ao_usb_in_flushed = 0;
484 _ao_usb_pollchar(void)
490 return AO_READ_AGAIN;
493 UENUM = AO_USB_OUT_EP;
495 debug("usb_pollchar UEINTX %02d\n", intx);
496 if (intx & (1 << RWAL))
499 if (intx & (1 << FIFOCON)) {
500 /* Ack the last packet */
501 UEINTX = (uint8_t) ~(1 << FIFOCON);
504 /* Check to see if a packet has arrived */
505 if ((intx & (1 << RXOUTI)) == 0) {
506 UENUM = AO_USB_OUT_EP;
507 UEIENX = (1 << RXOUTE);
508 return AO_READ_AGAIN;
511 /* Ack the interrupt */
512 UEINTX = ~(1 << RXOUTI);
515 /* Pull a character out of the fifo */
521 ao_usb_pollchar(void)
525 c = _ao_usb_pollchar();
531 ao_usb_getchar(void) __critical
536 while ((c = _ao_usb_pollchar()) == AO_READ_AGAIN)
537 ao_sleep(&ao_stdin_ready);
542 uint16_t control_count;
546 /* Endpoint interrupt */
549 uint8_t old_num = UENUM;
553 ao_led_toggle(AO_LED_RED);
559 ao_wakeup(&ao_usb_task);
562 if (i & (1 << AO_USB_IN_EP)) {
563 UENUM = AO_USB_IN_EP;
565 ao_wakeup(&ao_usb_in_flushed);
568 if (i & (1 << AO_USB_OUT_EP)) {
569 UENUM = AO_USB_OUT_EP;
571 ao_wakeup(&ao_stdin_ready);
578 #define AO_PAD_REGULATOR_INIT (1 << UVREGE) /* Turn on pad regulator */
581 /* TeleScience V0.1 has a hardware bug -- UVcc is hooked up, but UCap is not
582 * Make this work by running power through UVcc to the USB system
584 #define AO_PAD_REGULATOR_INIT (1 << UVREGE) /* Turn off pad regulator */
587 #if AVR_CLOCK == 16000000UL
588 #define AO_USB_PLL_INPUT_PRESCALER (1 << PINDIV) /* Divide 16MHz clock by 2 */
590 #if AVR_CLOCK == 8000000UL
591 #define AO_USB_PLL_INPUT_PRESCALER 0 /* Don't divide clock */
597 /* Unplug from the bus */
598 UDCON = (1 << DETACH);
600 /* Disable the interface */
603 /* Disable the PLL */
606 /* Turn off the pad regulator */
610 #define AO_USB_CON ((1 << USBE) | /* USB enable */ \
611 (0 << RSTCPU) | /* do not reset CPU */ \
612 (0 << LSM) | /* Full speed mode */ \
613 (0 << RMWKUP)) /* no remote wake-up */ \
618 /* Configure pad regulator */
619 UHWCON = AO_PAD_REGULATOR_INIT;
621 /* Enable USB device, but freeze the clocks until initialized */
622 USBCON = AO_USB_CON | (1 <<FRZCLK);
624 /* Enable PLL with appropriate divider */
625 PLLCSR = AO_USB_PLL_INPUT_PRESCALER | (1 << PLLE);
627 /* Wait for PLL to lock */
628 loop_until_bit_is_set(PLLCSR, (1 << PLOCK));
630 /* Enable USB, enable the VBUS pad */
631 USBCON = AO_USB_CON | (1 << OTGPADE);
633 /* Enable global interrupts */
634 UDIEN = (1 << EORSTE); /* End of reset interrupt */
636 ao_usb_configuration = 0;
638 debug ("ao_usb_enable\n");
640 debug ("UHWCON %02x USBCON %02x PLLCSR %02x UDIEN %02x\n",
641 UHWCON, USBCON, PLLCSR, UDIEN);
642 UDCON = (0 << DETACH); /* Clear the DETACH bit to plug into the bus */
646 struct ao_task __xdata ao_usb_echo_task;
654 c = ao_usb_getchar();
664 printf ("control: %d out: %d in: %d\n",
665 control_count, out_count, in_count);
668 __code struct ao_cmds ao_usb_cmds[] = {
669 { ao_usb_irq, "i\0Show USB interrupt counts" },
678 debug ("ao_usb_init\n");
679 ao_add_task(&ao_usb_task, ao_usb_ep0, "usb");
681 ao_add_task(&ao_usb_echo_task, ao_usb_echo, "usb echo");
683 ao_cmd_register(&ao_usb_cmds[0]);
684 ao_add_stdio(ao_usb_pollchar, ao_usb_putchar, ao_usb_flush);