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 */
108 UEIENX = ((1 << TXINE)); /* Enable IN complete interrupt */
110 ao_usb_dump_ep(AO_USB_IN_EP);
112 /* Set the OUT max packet size, double buffered */
113 UENUM = AO_USB_OUT_EP;
114 UECONX |= (1 << EPEN); /* Enable */
116 UECFG0X = ((2 << EPTYPE0) | /* Bulk */
117 (0 << EPDIR)); /* Out */
119 UECFG1X = (EP_SIZE(AO_USB_OUT_SIZE) | /* Size */
120 (1 << EPBK0) | /* Double bank */
121 (1 << ALLOC)); /* Allocate */
123 UEIENX = ((1 << RXOUTE)); /* Enable OUT complete interrupt */
125 ao_usb_dump_ep(AO_USB_OUT_EP);
131 ao_wakeup(&ao_usb_task);
135 __xdata static struct ao_usb_line_coding ao_usb_line_coding = {115200, 0, 0, 8};
137 /* Walk through the list of descriptors and find a match
140 ao_usb_get_descriptor(uint16_t value)
142 const uint8_t *__xdata descriptor;
143 __xdata uint8_t type = value >> 8;
144 __xdata uint8_t index = value;
146 descriptor = ao_usb_descriptors;
147 while (descriptor[0] != 0) {
148 if (descriptor[1] == type && index-- == 0) {
149 if (type == AO_USB_DESC_CONFIGURATION)
150 ao_usb_ep0_in_len = descriptor[2];
152 ao_usb_ep0_in_len = descriptor[0];
153 ao_usb_ep0_in_data = descriptor;
156 descriptor += descriptor[0];
161 ao_usb_ep0_set_in_pending(uint8_t in_pending)
163 ao_usb_ep0_in_pending = in_pending;
167 UEIENX = ((1 << RXSTPE) | (1 << RXOUTE) | (1 << TXINE)); /* Enable IN interrupt */
171 /* Send an IN data packet */
173 ao_usb_ep0_flush(void)
175 __xdata uint8_t this_len;
179 if (!(UEINTX & (1 << TXINI))) {
180 debug("EP0 not accepting IN data\n");
181 ao_usb_ep0_set_in_pending(1);
183 this_len = ao_usb_ep0_in_len;
184 if (this_len > AO_USB_CONTROL_SIZE)
185 this_len = AO_USB_CONTROL_SIZE;
187 ao_usb_ep0_in_len -= this_len;
189 /* Set IN interrupt enable */
190 if (ao_usb_ep0_in_len == 0 && this_len != AO_USB_CONTROL_SIZE)
191 ao_usb_ep0_set_in_pending(0);
193 ao_usb_ep0_set_in_pending(1);
195 debug ("Flush EP0 len %d:", this_len);
197 uint8_t c = *ao_usb_ep0_in_data++;
203 /* Clear the TXINI bit to send the packet */
204 UEINTX &= ~(1 << TXINI);
209 /* Read data from the ep0 OUT fifo */
211 ao_usb_ep0_fill(uint8_t len, uint8_t ack)
213 if (len > ao_usb_ep0_out_len)
214 len = ao_usb_ep0_out_len;
215 ao_usb_ep0_out_len -= len;
217 // debug ("EP0 UEINTX %02x UEBCLX %d UEBCHX %d\n",
218 // UEINTX, UEBCLX, UEBCHX);
219 /* Pull all of the data out of the packet */
220 debug ("Fill EP0 len %d:", len);
224 *ao_usb_ep0_out_data++ = c;
234 ao_usb_ep0_queue_byte(uint8_t a)
236 ao_usb_ep0_in_buf[ao_usb_ep0_in_len++] = a;
240 ao_usb_ep0_setup(void)
242 /* Pull the setup packet out of the fifo */
243 ao_usb_ep0_out_data = (__xdata uint8_t *) &ao_usb_setup;
244 ao_usb_ep0_out_len = 8;
245 ao_usb_ep0_fill(8, (1 << RXSTPI) | (1 << RXOUTI) | (1 << TXINI));
246 if (ao_usb_ep0_out_len != 0) {
247 debug ("invalid setup packet length\n");
251 /* Figure out how to ACK the setup packet */
252 if (ao_usb_setup.dir_type_recip & AO_USB_DIR_IN) {
253 if (ao_usb_setup.length)
254 ao_usb_ep0_state = AO_USB_EP0_DATA_IN;
256 ao_usb_ep0_state = AO_USB_EP0_IDLE;
258 if (ao_usb_setup.length)
259 ao_usb_ep0_state = AO_USB_EP0_DATA_OUT;
261 ao_usb_ep0_state = AO_USB_EP0_IDLE;
265 if (ao_usb_ep0_state == AO_USB_EP0_IDLE)
266 USBCS0 = USBCS0_CLR_OUTPKT_RDY | USBCS0_DATA_END;
268 USBCS0 = USBCS0_CLR_OUTPKT_RDY;
271 ao_usb_ep0_in_data = ao_usb_ep0_in_buf;
272 ao_usb_ep0_in_len = 0;
273 switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_TYPE_MASK) {
274 case AO_USB_TYPE_STANDARD:
275 debug ("Standard setup packet\n");
276 switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_RECIP_MASK) {
277 case AO_USB_RECIP_DEVICE:
278 debug ("Device setup packet\n");
279 switch(ao_usb_setup.request) {
280 case AO_USB_REQ_GET_STATUS:
281 debug ("get status\n");
282 ao_usb_ep0_queue_byte(0);
283 ao_usb_ep0_queue_byte(0);
285 case AO_USB_REQ_SET_ADDRESS:
286 debug ("set address %d\n", ao_usb_setup.value);
287 ao_usb_set_address(ao_usb_setup.value);
289 case AO_USB_REQ_GET_DESCRIPTOR:
290 debug ("get descriptor %d\n", ao_usb_setup.value);
291 ao_usb_get_descriptor(ao_usb_setup.value);
293 case AO_USB_REQ_GET_CONFIGURATION:
294 debug ("get configuration %d\n", ao_usb_configuration);
295 ao_usb_ep0_queue_byte(ao_usb_configuration);
297 case AO_USB_REQ_SET_CONFIGURATION:
298 ao_usb_configuration = ao_usb_setup.value;
299 debug ("set configuration %d\n", ao_usb_configuration);
300 ao_usb_set_configuration();
304 case AO_USB_RECIP_INTERFACE:
306 #pragma disable_warning 110
308 debug ("Interface setup packet\n");
309 switch(ao_usb_setup.request) {
310 case AO_USB_REQ_GET_STATUS:
311 ao_usb_ep0_queue_byte(0);
312 ao_usb_ep0_queue_byte(0);
314 case AO_USB_REQ_GET_INTERFACE:
315 ao_usb_ep0_queue_byte(0);
317 case AO_USB_REQ_SET_INTERFACE:
321 case AO_USB_RECIP_ENDPOINT:
322 debug ("Endpoint setup packet\n");
323 switch(ao_usb_setup.request) {
324 case AO_USB_REQ_GET_STATUS:
325 ao_usb_ep0_queue_byte(0);
326 ao_usb_ep0_queue_byte(0);
332 case AO_USB_TYPE_CLASS:
333 debug ("Class setup packet\n");
334 switch (ao_usb_setup.request) {
335 case SET_LINE_CODING:
336 debug ("set line coding\n");
337 ao_usb_ep0_out_len = 7;
338 ao_usb_ep0_out_data = (__xdata uint8_t *) &ao_usb_line_coding;
340 case GET_LINE_CODING:
341 debug ("get line coding\n");
342 ao_usb_ep0_in_len = 7;
343 ao_usb_ep0_in_data = (uint8_t *) &ao_usb_line_coding;
345 case SET_CONTROL_LINE_STATE:
350 if (ao_usb_ep0_state != AO_USB_EP0_DATA_OUT) {
351 if (ao_usb_setup.length < ao_usb_ep0_in_len)
352 ao_usb_ep0_in_len = ao_usb_setup.length;
353 debug ("Start ep0 in delivery %d\n", ao_usb_ep0_in_len);
354 ao_usb_ep0_set_in_pending(1);
358 /* End point 0 receives all of the control messages. */
364 debug ("usb task started\n");
365 ao_usb_ep0_state = AO_USB_EP0_IDLE;
371 // debug ("UDINT %02x\n", udint);
372 if (udint & (1 << EORSTI)) {
373 ao_usb_configuration = 0;
378 // debug ("UEINTX %02x\n", intx);
379 if (intx & ((1 << RXSTPI) | (1 << RXOUTI)))
381 if ((intx & (1 << TXINI))) {
382 if (ao_usb_ep0_in_pending)
386 if (ao_usb_addr_pending) {
387 UDADDR |= (1 << ADDEN);
388 ao_usb_addr_pending = 0;
390 UEIENX = ((1 << RXSTPE) | (1 << RXOUTE)); /* Disable IN interrupt */
393 // 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))
422 /* Wait for an IN buffer to be ready */
424 UENUM = AO_USB_IN_EP;
425 if ((UEINTX & (1 << TXINI)))
427 ao_sleep(&ao_usb_in_flushed);
429 /* Ack the interrupt */
430 UEINTX &= ~(1 << TXINI);
434 /* Queue the current IN buffer for transmission */
438 UENUM = AO_USB_IN_EP;
439 UEINTX &= ~(1 << FIFOCON);
443 ao_usb_flush(void) __critical
448 /* Anytime we've sent a character since
449 * the last time we flushed, we'll need
450 * to send a packet -- the only other time
451 * we would send a packet is when that
452 * packet was full, in which case we now
453 * want to send an empty packet
455 if (!ao_usb_in_flushed) {
456 ao_usb_in_flushed = 1;
463 ao_usb_putchar(char c) __critical __reentrant
471 UENUM = AO_USB_IN_EP;
474 /* Send the packet when full */
475 if ((UEINTX & (1 << RWAL)) == 0)
477 ao_usb_in_flushed = 0;
481 _ao_usb_pollchar(void)
487 return AO_READ_AGAIN;
490 UENUM = AO_USB_OUT_EP;
492 debug("usb_pollchar UEINTX %02d\n", intx);
493 if (intx & (1 << RWAL))
496 if (intx & (1 << FIFOCON)) {
497 /* Ack the last packet */
498 UEINTX &= ~(1 << FIFOCON);
501 /* Check to see if a packet has arrived */
502 if ((intx & (1 << RXOUTI)) == 0)
503 return AO_READ_AGAIN;
505 /* Ack the interrupt */
506 UEINTX &= ~(1 << RXOUTI);
509 /* Pull a character out of the fifo */
515 ao_usb_pollchar(void)
519 c = _ao_usb_pollchar();
525 ao_usb_getchar(void) __critical
530 while ((c = ao_usb_pollchar()) == AO_READ_AGAIN)
531 ao_sleep(&ao_stdin_ready);
536 /* Endpoint interrupt */
542 ao_led_toggle(AO_LED_RED);
546 ao_wakeup(&ao_usb_task);
547 if (i & (1 << AO_USB_IN_EP))
548 ao_wakeup(&ao_usb_in_flushed);
549 if (i & (1 << AO_USB_OUT_EP))
550 ao_wakeup(&ao_stdin_ready);
554 #define AO_PAD_REGULATOR_INIT (1 << UVREGE) /* Turn on pad regulator */
557 /* TeleScience V0.1 has a hardware bug -- UVcc is hooked up, but UCap is not
558 * Make this work by running power through UVcc to the USB system
560 #define AO_PAD_REGULATOR_INIT (1 << UVREGE) /* Turn off pad regulator */
563 #if AVR_CLOCK == 16000000UL
564 #define AO_USB_PLL_INPUT_PRESCALER (1 << PINDIV) /* Divide 16MHz clock by 2 */
566 #if AVR_CLOCK == 8000000UL
567 #define AO_USB_PLL_INPUT_PRESCALER 0 /* Don't divide clock */
573 /* Unplug from the bus */
574 UDCON = (1 << DETACH);
576 /* Disable the interface */
579 /* Disable the PLL */
582 /* Turn off the pad regulator */
586 #define AO_USB_CON ((1 << USBE) | /* USB enable */ \
587 (0 << RSTCPU) | /* do not reset CPU */ \
588 (0 << LSM) | /* Full speed mode */ \
589 (0 << RMWKUP)) /* no remote wake-up */ \
594 /* Configure pad regulator */
595 UHWCON = AO_PAD_REGULATOR_INIT;
597 /* Enable USB device, but freeze the clocks until initialized */
598 USBCON = AO_USB_CON | (1 <<FRZCLK);
600 /* Enable PLL with appropriate divider */
601 PLLCSR = AO_USB_PLL_INPUT_PRESCALER | (1 << PLLE);
603 /* Wait for PLL to lock */
604 loop_until_bit_is_set(PLLCSR, (1 << PLOCK));
606 /* Enable USB, enable the VBUS pad */
607 USBCON = AO_USB_CON | (1 << OTGPADE);
609 /* Enable global interrupts */
610 UDIEN = (1 << EORSTE); /* End of reset interrupt */
612 ao_usb_configuration = 0;
614 debug ("ao_usb_enable\n");
616 debug ("UHWCON %02x USBCON %02x PLLCSR %02x UDIEN %02x\n",
617 UHWCON, USBCON, PLLCSR, UDIEN);
618 UDCON = (0 << DETACH); /* Clear the DETACH bit to plug into the bus */
622 struct ao_task __xdata ao_usb_echo_task;
630 c = ao_usb_getchar();
642 debug ("ao_usb_init\n");
643 ao_add_task(&ao_usb_task, ao_usb_ep0, "usb");
644 // ao_add_task(&ao_usb_echo_task, ao_usb_echo, "usb echo");
645 ao_add_stdio(ao_usb_pollchar, ao_usb_putchar, ao_usb_flush);