altos/telelco-v2.0: Fix button names and pin mappings. Add drag LED.
[fw/altos] / src / avr / ao_usb_avr.c
1 /*
2  * Copyright © 2011 Keith Packard <keithp@keithp.com>
3  *
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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include "ao.h"
20 #include "ao_usb.h"
21
22 #define USB_DEBUG 0
23
24 #if USB_DEBUG
25 #define debug(format, args...)  printf(format, ## args)
26 #else
27 #define debug(format, args...)
28 #endif
29
30 struct ao_task __xdata ao_usb_task;
31
32 struct ao_usb_setup {
33         uint8_t         dir_type_recip;
34         uint8_t         request;
35         uint16_t        value;
36         uint16_t        index;
37         uint16_t        length;
38 } __xdata ao_usb_setup;
39
40 static __xdata uint8_t  ao_usb_ep0_state;
41 static const uint8_t * __xdata ao_usb_ep0_in_data;
42 static __xdata uint8_t  ao_usb_ep0_in_len;
43 static __xdata uint8_t  ao_usb_ep0_in_pending;
44 static __xdata uint8_t  ao_usb_addr_pending;
45 static __xdata uint8_t  ao_usb_ep0_in_buf[2];
46 static __xdata uint8_t  ao_usb_ep0_out_len;
47 static __xdata uint8_t *__xdata ao_usb_ep0_out_data;
48
49 static __xdata uint8_t  ao_usb_in_flushed;
50 __xdata uint8_t         ao_usb_running;
51 static __xdata uint8_t  ao_usb_configuration;
52 static __xdata uint8_t  ueienx_0;
53
54 void
55 ao_usb_set_address(uint8_t address)
56 {
57         UDADDR = (0 << ADDEN) | address;
58         ao_usb_addr_pending = 1;
59 }
60
61 #define EP_SIZE(s)      ((s) == 64 ? 0x30 :     \
62                         ((s) == 32 ? 0x20 :     \
63                         ((s) == 16 ? 0x10 :     \
64                                      0x00)))
65
66 static void
67 ao_usb_dump_ep(uint8_t ep)
68 {
69         UENUM = ep;
70         debug ("EP %d: UECONX %02x UECFG0X %02x UECFG1X %02x UEIENX %02x UESTA0X %02x UESTA1X %02X\n",
71                 ep, UECONX, UECFG0X, UECFG1X, UEIENX, UESTA0X, UESTA1X);
72 }
73
74 static void
75 ao_usb_set_ep0(void)
76 {
77         debug ("set_ep0\n");
78         /* Set the CONTROL max packet size, single buffered */
79         UENUM = 0;
80         UECONX = (1 << EPEN);                                   /* Enable */
81
82         UECFG0X = ((0 << EPTYPE0) |                             /* Control */
83                    (0 << EPDIR));                               /* Out (ish) */
84
85         UECFG1X = (EP_SIZE(AO_USB_CONTROL_SIZE) |               /* Size */
86                    (0 << EPBK0) |                               /* Single bank */
87                    (1 << ALLOC));
88
89         ueienx_0 = ((1 << RXSTPE) |                             /* Enable SETUP interrupt */
90                     (1 << RXOUTE));                             /* Enable OUT interrupt */
91
92 //      ao_usb_dump_ep(0);
93         ao_usb_addr_pending = 0;
94 }
95
96 static void
97 ao_usb_set_configuration(void)
98 {
99         /* Set the IN max packet size, double buffered */
100         UENUM = AO_USB_IN_EP;
101         UECONX = (1 << EPEN);                                   /* Enable */
102
103         UECFG0X = ((2 << EPTYPE0) |                             /* Bulk */
104                    (1 << EPDIR));                               /* In */
105
106         UECFG1X = (EP_SIZE(AO_USB_IN_SIZE) |                    /* Size */
107                    (1 << EPBK0) |                               /* Double bank */
108                    (1 << ALLOC));                               /* Allocate */
109
110 #if 0
111         UEIENX = ((1 << TXINE));                                /* Enable IN complete interrupt */
112 #endif
113
114         ao_usb_dump_ep(AO_USB_IN_EP);
115
116         /* Set the OUT max packet size, double buffered */
117         UENUM = AO_USB_OUT_EP;
118         UECONX |= (1 << EPEN);                                  /* Enable */
119
120         UECFG0X = ((2 << EPTYPE0) |                             /* Bulk */
121                    (0 << EPDIR));                               /* Out */
122
123         UECFG1X = (EP_SIZE(AO_USB_OUT_SIZE) |                   /* Size */
124                    (1 << EPBK0) |                               /* Double bank */
125                    (1 << ALLOC));                               /* Allocate */
126
127         UEIENX = ((1 << RXOUTE));                               /* Enable OUT complete interrupt */
128
129         ao_usb_dump_ep(AO_USB_OUT_EP);
130         ao_usb_running = 1;
131 }
132
133 ISR(USB_GEN_vect)
134 {
135         ao_wakeup(&ao_usb_task);
136 }
137
138
139 struct ao_usb_line_coding ao_usb_line_coding = {115200, 0, 0, 8};
140
141 /* Walk through the list of descriptors and find a match
142  */
143 static void
144 ao_usb_get_descriptor(uint16_t value)
145 {
146         const uint8_t           *__xdata descriptor;
147         __xdata uint8_t         type = value >> 8;
148         __xdata uint8_t         index = value;
149
150         descriptor = ao_usb_descriptors;
151         while (descriptor[0] != 0) {
152                 if (descriptor[1] == type && index-- == 0) {
153                         if (type == AO_USB_DESC_CONFIGURATION)
154                                 ao_usb_ep0_in_len = descriptor[2];
155                         else
156                                 ao_usb_ep0_in_len = descriptor[0];
157                         ao_usb_ep0_in_data = descriptor;
158                         break;
159                 }
160                 descriptor += descriptor[0];
161         }
162 }
163
164 static void
165 ao_usb_ep0_set_in_pending(uint8_t in_pending)
166 {
167         ao_usb_ep0_in_pending = in_pending;
168
169         if (in_pending)
170                 ueienx_0 = ((1 << RXSTPE) | (1 << RXOUTE) | (1 << TXINE));      /* Enable IN interrupt */
171 }
172
173 /* Send an IN data packet */
174 static void
175 ao_usb_ep0_flush(void)
176 {
177         __xdata uint8_t this_len;
178
179         cli();
180         UENUM = 0;
181         if (!(UEINTX & (1 << TXINI))) {
182                 debug("EP0 not accepting IN data\n");
183                 ao_usb_ep0_set_in_pending(1);
184         } else {
185                 this_len = ao_usb_ep0_in_len;
186                 if (this_len > AO_USB_CONTROL_SIZE)
187                         this_len = AO_USB_CONTROL_SIZE;
188
189                 ao_usb_ep0_in_len -= this_len;
190
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);
194                 else
195                         ao_usb_ep0_set_in_pending(1);
196
197                 debug ("Flush EP0 len %d:", this_len);
198                 while (this_len--) {
199                         uint8_t c = *ao_usb_ep0_in_data++;
200                         debug(" %02x", c);
201                         UEDATX = c;
202                 }
203                 debug ("\n");
204
205                 /* Clear the TXINI bit to send the packet */
206                 UEINTX &= ~(1 << TXINI);
207         }
208         sei();
209 }
210
211 /* Read data from the ep0 OUT fifo */
212 static void
213 ao_usb_ep0_fill(uint8_t len, uint8_t ack)
214 {
215         if (len > ao_usb_ep0_out_len)
216                 len = ao_usb_ep0_out_len;
217         ao_usb_ep0_out_len -= len;
218
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);
223         UENUM = 0;
224         while (len--) {
225                 uint8_t c = UEDATX;
226                 *ao_usb_ep0_out_data++ = c;
227                 debug (" %02x", c);
228         }
229         debug ("\n");
230
231         /* ACK the packet */
232         UEINTX &= ~ack;
233 }
234
235 void
236 ao_usb_ep0_queue_byte(uint8_t a)
237 {
238         ao_usb_ep0_in_buf[ao_usb_ep0_in_len++] = a;
239 }
240
241 static void
242 ao_usb_ep0_setup(void)
243 {
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");
250                 return;
251         }
252
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;
257                 else
258                         ao_usb_ep0_state = AO_USB_EP0_IDLE;
259         } else {
260                 if (ao_usb_setup.length)
261                         ao_usb_ep0_state = AO_USB_EP0_DATA_OUT;
262                 else
263                         ao_usb_ep0_state = AO_USB_EP0_IDLE;
264         }
265 /*
266         UENUM = 0;
267         if (ao_usb_ep0_state == AO_USB_EP0_IDLE)
268                 USBCS0 = USBCS0_CLR_OUTPKT_RDY | USBCS0_DATA_END;
269         else
270                 USBCS0 = USBCS0_CLR_OUTPKT_RDY;
271 */
272
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);
286                                 break;
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);
290                                 break;
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);
294                                 break;
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);
298                                 break;
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();
303                                 break;
304                         }
305                         break;
306                 case AO_USB_RECIP_INTERFACE:
307                         debug ("Interface setup packet\n");
308                         switch(ao_usb_setup.request) {
309                         case AO_USB_REQ_GET_STATUS:
310                                 ao_usb_ep0_queue_byte(0);
311                                 ao_usb_ep0_queue_byte(0);
312                                 break;
313                         case AO_USB_REQ_GET_INTERFACE:
314                                 ao_usb_ep0_queue_byte(0);
315                                 break;
316                         case AO_USB_REQ_SET_INTERFACE:
317                                 break;
318                         }
319                         break;
320                 case AO_USB_RECIP_ENDPOINT:
321                         debug ("Endpoint setup packet\n");
322                         switch(ao_usb_setup.request) {
323                         case AO_USB_REQ_GET_STATUS:
324                                 ao_usb_ep0_queue_byte(0);
325                                 ao_usb_ep0_queue_byte(0);
326                                 break;
327                         }
328                         break;
329                 }
330                 break;
331         case AO_USB_TYPE_CLASS:
332                 debug ("Class setup packet\n");
333                 switch (ao_usb_setup.request) {
334                 case AO_USB_SET_LINE_CODING:
335                         debug ("set line coding\n");
336                         ao_usb_ep0_out_len = 7;
337                         ao_usb_ep0_out_data = (__xdata uint8_t *) &ao_usb_line_coding;
338                         break;
339                 case AO_USB_GET_LINE_CODING:
340                         debug ("get line coding\n");
341                         ao_usb_ep0_in_len = 7;
342                         ao_usb_ep0_in_data = (uint8_t *) &ao_usb_line_coding;
343                         break;
344                 case AO_USB_SET_CONTROL_LINE_STATE:
345                         break;
346                 }
347                 break;
348         }
349         if (ao_usb_ep0_state != AO_USB_EP0_DATA_OUT) {
350                 if (ao_usb_setup.length < ao_usb_ep0_in_len)
351                         ao_usb_ep0_in_len = ao_usb_setup.length;
352                 debug ("Start ep0 in delivery %d\n", ao_usb_ep0_in_len);
353                 ao_usb_ep0_set_in_pending(1);
354         }
355 }
356
357 /* End point 0 receives all of the control messages. */
358 static void
359 ao_usb_ep0(void)
360 {
361         uint8_t intx, udint;
362
363         debug ("usb task started\n");
364         ao_usb_ep0_state = AO_USB_EP0_IDLE;
365         for (;;) {
366                 cli();
367                 for (;;) {
368                         udint = UDINT;
369                         UDINT = 0;
370 //                      debug ("UDINT %02x\n", udint);
371                         if (udint & (1 << EORSTI)) {
372                                 ao_usb_configuration = 0;
373                                 ao_usb_set_ep0();
374                         }
375                         UENUM = 0;
376                         intx = UEINTX;
377 //                      debug ("UEINTX %02x\n", intx);
378                         if (intx & ((1 << RXSTPI) | (1 << RXOUTI)))
379                                 break;
380                         if ((intx & (1 << TXINI))) {
381                                 if (ao_usb_ep0_in_pending)
382                                         break;
383                                 else
384                                 {
385                                         if (ao_usb_addr_pending) {
386                                                 UDADDR |= (1 << ADDEN);
387                                                 ao_usb_addr_pending = 0;
388                                         }
389                                         ueienx_0 = ((1 << RXSTPE) | (1 << RXOUTE));     /* Disable IN interrupt */
390                                 }
391                         }
392 //                      debug ("usb task sleeping...\n");
393                         UENUM = 0;
394                         UEIENX = ueienx_0;
395                         ao_sleep(&ao_usb_task);
396                 }
397                 sei();
398 //              debug ("UEINTX for ep0 is %02x\n", intx);
399                 if (intx & (1 << RXSTPI)) {
400                         ao_usb_ep0_setup();
401                 }
402                 if (intx & (1 << RXOUTI)) {
403                         ao_usb_ep0_fill(UEBCLX, (1 << RXOUTI));
404                         ao_usb_ep0_set_in_pending(1);
405                 }
406                 if (intx & (1 << TXINI) && ao_usb_ep0_in_pending) {
407                         debug ("continue sending ep0 IN data\n");
408                         ao_usb_ep0_flush();
409                 }
410         }
411 }
412
413 /* Wait for a free IN buffer */
414 static void
415 _ao_usb_in_wait(void)
416 {
417         for (;;) {
418                 /* Check if the current buffer is writable */
419                 UENUM = AO_USB_IN_EP;
420                 if (UEINTX & (1 << RWAL))
421                         break;
422
423                 /* Wait for an IN buffer to be ready */
424                 for (;;) {
425                         UENUM = AO_USB_IN_EP;
426                         if ((UEINTX & (1 << TXINI)))
427                                 break;
428                         UEIENX = (1 << TXINE);
429                         ao_sleep(&ao_usb_in_flushed);
430                 }
431                 /* Ack the interrupt */
432                 UEINTX &= ~(1 << TXINI);
433         }
434 }
435
436 /* Queue the current IN buffer for transmission */
437 static void
438 _ao_usb_in_send(void)
439 {
440         UENUM = AO_USB_IN_EP;
441         UEINTX &= ~(1 << FIFOCON);
442 }
443
444 void
445 ao_usb_flush(void)
446 {
447         if (!ao_usb_running)
448                 return;
449
450         ao_arch_block_interrupts();
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
457          */
458         if (!ao_usb_in_flushed) {
459                 ao_usb_in_flushed = 1;
460                 _ao_usb_in_wait();
461                 _ao_usb_in_send();
462         }
463         ao_arch_release_interrupts();
464 }
465
466 void
467 ao_usb_putchar(char c)
468 {
469         if (!ao_usb_running)
470                 return;
471
472         ao_arch_block_interrupts();
473         _ao_usb_in_wait();
474
475         /* Queue a byte */
476         UENUM = AO_USB_IN_EP;
477         UEDATX = c;
478
479         /* Send the packet when full */
480         if ((UEINTX & (1 << RWAL)) == 0)
481                 _ao_usb_in_send();
482         ao_usb_in_flushed = 0;
483         ao_arch_release_interrupts();
484 }
485
486 int
487 _ao_usb_pollchar(void)
488 {
489         uint8_t c;
490         uint8_t intx;
491
492         if (!ao_usb_running)
493                 return AO_READ_AGAIN;
494
495         for (;;) {
496                 UENUM = AO_USB_OUT_EP;
497                 intx = UEINTX;
498                 debug("usb_pollchar UEINTX %02d\n", intx);
499                 if (intx & (1 << RWAL))
500                         break;
501
502                 if (intx & (1 << FIFOCON)) {
503                         /* Ack the last packet */
504                         UEINTX = (uint8_t) ~(1 << FIFOCON);
505                 }
506
507                 /* Check to see if a packet has arrived */
508                 if ((intx & (1 << RXOUTI)) == 0) {
509                         UENUM = AO_USB_OUT_EP;
510                         UEIENX = (1 << RXOUTE);
511                         return AO_READ_AGAIN;
512                 }
513
514                 /* Ack the interrupt */
515                 UEINTX = ~(1 << RXOUTI);
516         }
517
518         /* Pull a character out of the fifo */
519         c = UEDATX;
520         return c;
521 }
522
523 char
524 ao_usb_getchar(void)
525 {
526         int     c;
527
528         ao_arch_block_interrupts();
529         while ((c = _ao_usb_pollchar()) == AO_READ_AGAIN)
530                 ao_sleep(&ao_stdin_ready);
531         ao_arch_release_interrupts();
532         return c;
533 }
534
535 uint16_t        control_count;
536 uint16_t        in_count;
537 uint16_t        out_count;
538
539 /* Endpoint interrupt */
540 ISR(USB_COM_vect)
541 {
542         uint8_t old_num = UENUM;
543         uint8_t i = UEINT;
544
545 #ifdef AO_LED_RED
546         ao_led_toggle(AO_LED_RED);
547 #endif
548         UEINT = 0;
549         if (i & (1 << 0)) {
550                 UENUM = 0;
551                 UEIENX = 0;
552                 ao_wakeup(&ao_usb_task);
553                 ++control_count;
554         }
555         if (i & (1 << AO_USB_IN_EP)) {
556                 UENUM = AO_USB_IN_EP;
557                 UEIENX = 0;
558                 ao_wakeup(&ao_usb_in_flushed);
559                 in_count++;
560         }
561         if (i & (1 << AO_USB_OUT_EP)) {
562                 UENUM = AO_USB_OUT_EP;
563                 UEIENX = 0;
564                 ao_wakeup(&ao_stdin_ready);
565                 ++out_count;
566         }
567         UENUM = old_num;
568 }
569
570 #if AVR_VCC_5V
571 #define AO_PAD_REGULATOR_INIT   (1 << UVREGE)   /* Turn on pad regulator */
572 #endif
573 #if AVR_VCC_3V3
574 /* TeleScience V0.1 has a hardware bug -- UVcc is hooked up, but UCap is not
575  * Make this work by running power through UVcc to the USB system
576  */
577 #define AO_PAD_REGULATOR_INIT   (1 << UVREGE)   /* Turn off pad regulator */
578 #endif
579
580 #if AVR_CLOCK == 16000000UL
581 #define AO_USB_PLL_INPUT_PRESCALER      (1 << PINDIV)   /* Divide 16MHz clock by 2 */
582 #endif
583 #if AVR_CLOCK == 8000000UL
584 #define AO_USB_PLL_INPUT_PRESCALER      0               /* Don't divide clock */
585 #endif
586
587 void
588 ao_usb_disable(void)
589 {
590         /* Unplug from the bus */
591         UDCON = (1 << DETACH);
592
593         /* Disable the interface */
594         USBCON = 0;
595
596         /* Disable the PLL */
597         PLLCSR = 0;
598
599         /* Turn off the pad regulator */
600         UHWCON = 0;
601 }
602
603 #define AO_USB_CON ((1 << USBE) |       /* USB enable */ \
604                     (0 << RSTCPU) |     /* do not reset CPU */  \
605                     (0 << LSM) |        /* Full speed mode */   \
606                     (0 << RMWKUP))      /* no remote wake-up */ \
607
608 void
609 ao_usb_enable(void)
610 {
611         /* Configure pad regulator */
612         UHWCON = AO_PAD_REGULATOR_INIT;
613
614         /* Enable USB device, but freeze the clocks until initialized */
615         USBCON = AO_USB_CON | (1 <<FRZCLK);
616
617         /* Enable PLL with appropriate divider */
618         PLLCSR = AO_USB_PLL_INPUT_PRESCALER | (1 << PLLE);
619
620         /* Wait for PLL to lock */
621         loop_until_bit_is_set(PLLCSR, (1 << PLOCK));
622
623         /* Enable USB, enable the VBUS pad */
624         USBCON = AO_USB_CON | (1 << OTGPADE);
625
626         /* Enable global interrupts */
627         UDIEN = (1 << EORSTE);          /* End of reset interrupt */
628
629         ao_usb_configuration = 0;
630
631         debug ("ao_usb_enable\n");
632
633         debug ("UHWCON %02x USBCON %02x PLLCSR %02x UDIEN %02x\n",
634                UHWCON, USBCON, PLLCSR, UDIEN);
635         UDCON = (0 << DETACH);  /* Clear the DETACH bit to plug into the bus */
636 }
637
638 #if USB_DEBUG
639 struct ao_task __xdata ao_usb_echo_task;
640
641 static void
642 ao_usb_echo(void)
643 {
644         char    c;
645
646         for (;;) {
647                 c = ao_usb_getchar();
648                 ao_usb_putchar(c);
649                 ao_usb_flush();
650         }
651 }
652 #endif
653
654 void
655 ao_usb_init(void)
656 {
657         ao_usb_enable();
658
659         debug ("ao_usb_init\n");
660         ao_add_task(&ao_usb_task, ao_usb_ep0, "usb");
661 #if USB_DEBUG
662         ao_add_task(&ao_usb_echo_task, ao_usb_echo, "usb echo");
663 #endif
664         ao_add_stdio(_ao_usb_pollchar, ao_usb_putchar, ao_usb_flush);
665 }