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.
21 //#define printf(format, args...)
23 struct ao_task __xdata ao_usb_task;
26 uint8_t dir_type_recip;
31 } __xdata ao_usb_setup;
33 static __xdata uint8_t ao_usb_ep0_state;
34 static const uint8_t * __xdata ao_usb_ep0_in_data;
35 static __xdata uint8_t ao_usb_ep0_in_len;
36 static __xdata uint8_t ao_usb_ep0_in_pending;
37 static __xdata uint8_t ao_usb_addr_pending;
38 static __xdata uint8_t ao_usb_ep0_in_buf[2];
39 static __xdata uint8_t ao_usb_ep0_out_len;
40 static __xdata uint8_t *__xdata ao_usb_ep0_out_data;
42 static __xdata uint8_t ao_usb_in_flushed;
43 static __xdata uint8_t ao_usb_running;
44 static __xdata uint8_t ao_usb_configuration;
47 ao_usb_set_address(uint8_t address)
49 UDADDR = (0 << ADDEN) | address;
51 ao_usb_addr_pending = 1;
54 #define EP_SIZE(s) ((s) == 64 ? 0x30 : \
60 ao_usb_dump_ep(uint8_t ep)
63 printf ("EP %d: UECONX %02x UECFG0X %02x UECFG1X %02x UEIENX %02x UESTA0X %02x UESTA1X %02X\n",
64 ep, UECONX, UECFG0X, UECFG1X, UEIENX, UESTA0X, UESTA1X);
71 /* Set the CONTROL max packet size, single buffered */
73 UECONX = (1 << EPEN); /* Enable */
75 UECFG0X = ((0 << EPTYPE0) | /* Control */
76 (0 << EPDIR)); /* Out (ish) */
78 UECFG1X = (EP_SIZE(AO_USB_CONTROL_SIZE) | /* Size */
79 (0 << EPBK0) | /* Single bank */
82 UEIENX = ((1 << RXSTPE) | /* Enable SETUP interrupt */
83 (1 << RXOUTE)); /* Enable OUT interrupt */
86 ao_usb_addr_pending = 0;
90 ao_usb_set_configuration(void)
92 /* Set the IN max packet size, double buffered */
94 UECONX = (1 << EPEN); /* Enable */
96 UECFG0X = ((2 << EPTYPE0) | /* Bulk */
97 (1 << EPDIR)); /* In */
99 UECFG1X = (EP_SIZE(AO_USB_IN_SIZE) | /* Size */
100 (1 << EPBK0) | /* Double bank */
101 (1 << ALLOC)); /* Allocate */
103 UEIENX = ((1 << TXINE)); /* Enable IN complete interrupt */
105 ao_usb_dump_ep(AO_USB_IN_EP);
107 /* Set the OUT max packet size, double buffered */
108 UENUM = AO_USB_OUT_EP;
109 UECONX |= (1 << EPEN); /* Enable */
111 UECFG0X = ((2 << EPTYPE0) | /* Bulk */
112 (0 << EPDIR)); /* Out */
114 UECFG1X = (EP_SIZE(AO_USB_OUT_SIZE) | /* Size */
115 (1 << EPBK0) | /* Double bank */
116 (1 << ALLOC)); /* Allocate */
118 UEIENX = ((1 << RXOUTE)); /* Enable OUT complete interrupt */
120 ao_usb_dump_ep(AO_USB_OUT_EP);
125 ao_wakeup(&ao_usb_task);
129 __xdata static struct ao_usb_line_coding ao_usb_line_coding = {115200, 0, 0, 8};
131 /* Walk through the list of descriptors and find a match
134 ao_usb_get_descriptor(uint16_t value)
136 const uint8_t *__xdata descriptor;
137 __xdata uint8_t type = value >> 8;
138 __xdata uint8_t index = value;
140 descriptor = ao_usb_descriptors;
141 while (descriptor[0] != 0) {
142 if (descriptor[1] == type && index-- == 0) {
143 if (type == AO_USB_DESC_CONFIGURATION)
144 ao_usb_ep0_in_len = descriptor[2];
146 ao_usb_ep0_in_len = descriptor[0];
147 ao_usb_ep0_in_data = descriptor;
150 descriptor += descriptor[0];
155 ao_usb_ep0_set_in_pending(uint8_t in_pending)
157 ao_usb_ep0_in_pending = in_pending;
161 UEIENX = ((1 << RXSTPE) | (1 << RXOUTE) | (1 << TXINE)); /* Enable IN interrupt */
165 /* Send an IN data packet */
167 ao_usb_ep0_flush(void)
169 __xdata uint8_t this_len;
173 if (!(UEINTX & (1 << TXINI))) {
174 printf("EP0 not accepting IN data\n");
175 ao_usb_ep0_set_in_pending(1);
177 this_len = ao_usb_ep0_in_len;
178 if (this_len > AO_USB_CONTROL_SIZE)
179 this_len = AO_USB_CONTROL_SIZE;
181 ao_usb_ep0_in_len -= this_len;
183 /* Set IN interrupt enable */
184 if (ao_usb_ep0_in_len == 0 && this_len != AO_USB_CONTROL_SIZE)
185 ao_usb_ep0_set_in_pending(0);
187 ao_usb_ep0_set_in_pending(1);
189 printf ("Flush EP0 len %d:", this_len);
191 uint8_t c = *ao_usb_ep0_in_data++;
197 /* Clear the TXINI bit to send the packet */
198 UEINTX &= ~(1 << TXINI);
203 /* Read data from the ep0 OUT fifo */
205 ao_usb_ep0_fill(uint8_t len, uint8_t ack)
207 if (len > ao_usb_ep0_out_len)
208 len = ao_usb_ep0_out_len;
209 ao_usb_ep0_out_len -= len;
211 // printf ("EP0 UEINTX %02x UEBCLX %d UEBCHX %d\n",
212 // UEINTX, UEBCLX, UEBCHX);
213 /* Pull all of the data out of the packet */
214 printf ("Fill EP0 len %d:", len);
218 *ao_usb_ep0_out_data++ = c;
228 ao_usb_ep0_queue_byte(uint8_t a)
230 ao_usb_ep0_in_buf[ao_usb_ep0_in_len++] = a;
234 ao_usb_ep0_setup(void)
236 /* Pull the setup packet out of the fifo */
237 ao_usb_ep0_out_data = (__xdata uint8_t *) &ao_usb_setup;
238 ao_usb_ep0_out_len = 8;
239 ao_usb_ep0_fill(8, (1 << RXSTPI) | (1 << RXOUTI) | (1 << TXINI));
240 if (ao_usb_ep0_out_len != 0) {
241 printf ("invalid setup packet length\n");
245 /* Figure out how to ACK the setup packet */
246 if (ao_usb_setup.dir_type_recip & AO_USB_DIR_IN) {
247 if (ao_usb_setup.length)
248 ao_usb_ep0_state = AO_USB_EP0_DATA_IN;
250 ao_usb_ep0_state = AO_USB_EP0_IDLE;
252 if (ao_usb_setup.length)
253 ao_usb_ep0_state = AO_USB_EP0_DATA_OUT;
255 ao_usb_ep0_state = AO_USB_EP0_IDLE;
259 if (ao_usb_ep0_state == AO_USB_EP0_IDLE)
260 USBCS0 = USBCS0_CLR_OUTPKT_RDY | USBCS0_DATA_END;
262 USBCS0 = USBCS0_CLR_OUTPKT_RDY;
265 ao_usb_ep0_in_data = ao_usb_ep0_in_buf;
266 ao_usb_ep0_in_len = 0;
267 switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_TYPE_MASK) {
268 case AO_USB_TYPE_STANDARD:
269 printf ("Standard setup packet\n");
270 switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_RECIP_MASK) {
271 case AO_USB_RECIP_DEVICE:
272 printf ("Device setup packet\n");
273 switch(ao_usb_setup.request) {
274 case AO_USB_REQ_GET_STATUS:
275 printf ("get status\n");
276 ao_usb_ep0_queue_byte(0);
277 ao_usb_ep0_queue_byte(0);
279 case AO_USB_REQ_SET_ADDRESS:
280 printf ("set address %d\n", ao_usb_setup.value);
281 ao_usb_set_address(ao_usb_setup.value);
283 case AO_USB_REQ_GET_DESCRIPTOR:
284 printf ("get descriptor %d\n", ao_usb_setup.value);
285 ao_usb_get_descriptor(ao_usb_setup.value);
287 case AO_USB_REQ_GET_CONFIGURATION:
288 printf ("get configuration %d\n", ao_usb_configuration);
289 ao_usb_ep0_queue_byte(ao_usb_configuration);
291 case AO_USB_REQ_SET_CONFIGURATION:
292 ao_usb_configuration = ao_usb_setup.value;
293 printf ("set configuration %d\n", ao_usb_configuration);
294 ao_usb_set_configuration();
298 case AO_USB_RECIP_INTERFACE:
300 #pragma disable_warning 110
302 printf ("Interface setup packet\n");
303 switch(ao_usb_setup.request) {
304 case AO_USB_REQ_GET_STATUS:
305 ao_usb_ep0_queue_byte(0);
306 ao_usb_ep0_queue_byte(0);
308 case AO_USB_REQ_GET_INTERFACE:
309 ao_usb_ep0_queue_byte(0);
311 case AO_USB_REQ_SET_INTERFACE:
315 case AO_USB_RECIP_ENDPOINT:
316 printf ("Endpoint setup packet\n");
317 switch(ao_usb_setup.request) {
318 case AO_USB_REQ_GET_STATUS:
319 ao_usb_ep0_queue_byte(0);
320 ao_usb_ep0_queue_byte(0);
326 case AO_USB_TYPE_CLASS:
327 printf ("Class setup packet\n");
328 switch (ao_usb_setup.request) {
329 case SET_LINE_CODING:
330 printf ("set line coding\n");
331 ao_usb_ep0_out_len = 7;
332 ao_usb_ep0_out_data = (__xdata uint8_t *) &ao_usb_line_coding;
334 case GET_LINE_CODING:
335 printf ("get line coding\n");
336 ao_usb_ep0_in_len = 7;
337 ao_usb_ep0_in_data = (uint8_t *) &ao_usb_line_coding;
339 case SET_CONTROL_LINE_STATE:
344 if (ao_usb_ep0_state != AO_USB_EP0_DATA_OUT) {
345 if (ao_usb_setup.length < ao_usb_ep0_in_len)
346 ao_usb_ep0_in_len = ao_usb_setup.length;
347 printf ("Start ep0 in delivery %d\n", ao_usb_ep0_in_len);
348 ao_usb_ep0_set_in_pending(1);
352 /* End point 0 receives all of the control messages. */
358 printf ("usb task started\n");
359 ao_usb_ep0_state = AO_USB_EP0_IDLE;
365 // printf ("UDINT %02x\n", udint);
366 if (udint & (1 << EORSTI)) {
367 ao_usb_configuration = 0;
372 // printf ("UEINTX %02x\n", intx);
373 if (intx & ((1 << RXSTPI) | (1 << RXOUTI)))
375 if ((intx & (1 << TXINI))) {
376 if (ao_usb_ep0_in_pending)
380 if (ao_usb_addr_pending) {
381 UDADDR |= (1 << ADDEN);
382 ao_usb_addr_pending = 0;
384 UEIENX = ((1 << RXSTPE) | (1 << RXOUTE)); /* Disable IN interrupt */
387 // printf ("usb task sleeping...\n");
388 ao_sleep(&ao_usb_task);
391 // printf ("UEINTX for ep0 is %02x\n", intx);
392 if (intx & (1 << RXSTPI)) {
395 if (intx & (1 << RXOUTI)) {
396 ao_usb_ep0_fill(UEBCLX, (1 << RXOUTI));
397 ao_usb_ep0_set_in_pending(1);
399 if (intx & (1 << TXINI) && ao_usb_ep0_in_pending) {
400 printf ("continue sending ep0 IN data\n");
406 /* Wait for a free IN buffer */
411 /* Check if the current buffer is writable */
412 UENUM = AO_USB_IN_EP;
413 if (UEINTX & (1 << RWAL))
416 /* Wait for an IN buffer to be ready */
418 UENUM = AO_USB_IN_EP;
419 if ((UEINTX & (1 << TXINI)))
421 ao_sleep(&ao_usb_in_flushed);
423 /* Ack the interrupt */
424 UEINTX &= ~(1 << TXINI);
428 /* Queue the current IN buffer for transmission */
432 UENUM = AO_USB_IN_EP;
433 UEINTX &= ~(1 << FIFOCON);
437 ao_usb_flush(void) __critical
442 /* Anytime we've sent a character since
443 * the last time we flushed, we'll need
444 * to send a packet -- the only other time
445 * we would send a packet is when that
446 * packet was full, in which case we now
447 * want to send an empty packet
449 if (!ao_usb_in_flushed) {
450 ao_usb_in_flushed = 1;
457 ao_usb_putchar(char c) __critical __reentrant
462 ao_usb_in_flushed = 0;
467 UENUM = AO_USB_IN_EP;
470 /* Send the packet when full */
471 if ((UEINTX & (1 << RWAL)) == 0)
476 _ao_usb_pollchar(void)
482 UENUM = AO_USB_OUT_EP;
484 printf("usb_pollchar UEINTX %02d\n", intx);
485 if (intx & (1 << RWAL))
488 if (intx & (1 << FIFOCON)) {
489 /* Ack the last packet */
490 UEINTX &= ~(1 << FIFOCON);
493 /* Check to see if a packet has arrived */
494 if ((intx & (1 << RXOUTI)) == 0)
495 return AO_READ_AGAIN;
497 /* Ack the interrupt */
498 UEINTX &= ~(1 << RXOUTI);
501 /* Pull a character out of the fifo */
507 ao_usb_pollchar(void)
511 c = _ao_usb_pollchar();
517 ao_usb_getchar(void) __critical
522 while ((c = ao_usb_pollchar()) == AO_READ_AGAIN)
523 ao_sleep(&ao_stdin_ready);
528 /* Endpoint interrupt */
533 ao_led_toggle(AO_LED_RED);
536 ao_wakeup(&ao_usb_task);
537 if (i & (1 << AO_USB_IN_EP))
538 ao_wakeup(&ao_usb_in_flushed);
539 if (i & (1 << AO_USB_OUT_EP))
540 ao_wakeup(&ao_stdin_ready);
544 #define AO_PAD_REGULATOR_INIT (1 << UVREGE) /* Turn on pad regulator */
547 #define AO_PAD_REGULATOR_INIT 0 /* Turn off pad regulator */
550 #if AVR_CLOCK == 16000000UL
551 #define AO_USB_PLL_INPUT_PRESCALER (1 << PINDIV) /* Divide 16MHz clock by 2 */
553 #if AVR_CLOCK == 8000000UL
554 #define AO_USB_PLL_INPUT_PRESCALER 0 /* Don't divide clock */
560 /* Unplug from the bus */
561 UDCON = (1 << DETACH);
563 /* Disable the interface */
566 /* Disable the PLL */
569 /* Turn off the pad regulator */
573 #define AO_USB_CON ((1 << USBE) | /* USB enable */ \
574 (0 << RSTCPU) | /* do not reset CPU */ \
575 (0 << LSM) | /* Full speed mode */ \
576 (0 << RMWKUP)) /* no remote wake-up */ \
581 /* Configure pad regulator */
582 UHWCON = AO_PAD_REGULATOR_INIT;
584 /* Enable USB device, but freeze the clocks until initialized */
585 USBCON = AO_USB_CON | (1 <<FRZCLK);
587 /* Enable PLL with appropriate divider */
588 PLLCSR = AO_USB_PLL_INPUT_PRESCALER | (1 << PLLE);
590 /* Wait for PLL to lock */
591 loop_until_bit_is_set(PLLCSR, (1 << PLOCK));
593 /* Enable USB, enable the VBUS pad */
594 USBCON = AO_USB_CON | (1 << OTGPADE);
596 /* Enable global interrupts */
597 UDIEN = (1 << EORSTE); /* End of reset interrupt */
599 ao_usb_configuration = 0;
601 printf ("ao_usb_enable\n");
603 UDCON = (0 << DETACH); /* Clear the DETACH bit to plug into the bus */
606 struct ao_task __xdata ao_usb_echo_task;
614 c = ao_usb_getchar();
625 printf ("ao_usb_init\n");
626 ao_add_task(&ao_usb_task, ao_usb_ep0, "usb");
627 ao_add_task(&ao_usb_echo_task, ao_usb_echo, "usb echo");
628 // ao_add_stdio(ao_usb_pollchar, ao_usb_putchar, ao_usb_flush);