altos-avr: Start writing USB driver.
[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; version 2 of the License.
7  *
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.
12  *
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.
16  */
17
18 #include "ao.h"
19
20 struct ao_task __xdata ao_usb_task;
21
22 static __xdata uint16_t ao_usb_in_bytes;
23 static __xdata uint16_t ao_usb_in_bytes_last;
24 static __xdata uint16_t ao_usb_out_bytes;
25 static __xdata uint8_t  ao_usb_iif;
26 static __xdata uint8_t  ao_usb_running;
27
28 static void
29 ao_usb_set_interrupts(void)
30 {
31         /* IN interrupts on the control an IN endpoints */
32         USBIIE = (1 << AO_USB_CONTROL_EP) | (1 << AO_USB_IN_EP);
33
34         /* OUT interrupts on the OUT endpoint */
35         USBOIE = (1 << AO_USB_OUT_EP);
36
37         /* Only care about reset */
38         USBCIE = USBCIE_RSTIE;
39 }
40
41 void
42 ao_usb_set_address(uint8_t address)
43 {
44         UDADD = (0 << ADDEN) | address;
45         ao_usb_running = 1;
46 }
47
48 #define EP_SIZE(s)      ((s) == 64 ? 0x30 :     \
49                         ((s) == 32 ? 0x20 :     \
50                         ((s) == 16 ? 0x10 :     \
51                                      0x00)))
52
53 static void
54 ao_usb_set_ep0(void)
55 {
56         /* Set the CONTROL max packet size, single buffered */
57         UENUM = 0;
58         UECONX = (1 << EPEN);                                   /* Enable */
59
60         UECFG0X = ((0 << EPTYPE0) |                             /* Control */
61                    (0 << EPDIR));                               /* Out (ish) */
62
63         UECFG1X = ((EP_SIZE(AO_USB_CONTROL_SIZE) << EPSIZE0) |  /* Size */
64                    (0 << EPBK));                                /* Single bank */
65
66         UEIENX = ((1 << RXSTPE) |       /* Enable SETUP interrupt */
67                   (1 << RXOUTE) |       /* Enable OUT interrupt */
68                   (1 << TXINE));        /* Enable IN complete interrupt */
69 }
70
71 static void
72 ao_usb_set_configuration(void)
73 {
74         /* Set the IN max packet size, double buffered */
75         UENUM = AO_USB_IN_EP;
76         UECONX = (1 << EPEN);                                   /* Enable */
77
78         UECFG0X = ((2 << EPTYPE0) |                             /* Bulk */
79                    (1 << EPDIR));                               /* In */
80
81         UECFG1X = ((EP_SIZE(AO_USB_IN_SIZE) << EPSIZE0) |       /* Size */
82                    (1 << EPBK) |                                /* Double bank */
83                    (1 << ALLOC));                               /* Allocate */
84
85         /* Set the OUT max packet size, double buffered */
86         UENUM = AO_USB_OUT_EP;
87         UECONX |= (1 << EPEN);                                  /* Enable */
88
89         UECFG0X = ((2 << EPTYPE0) |                             /* Bulk */
90                    (0 << EPDIR));                               /* Out */
91
92         UECFG1X = ((EP_SIZE(AO_USB_OUT_SIZE) << EPSIZE0) |      /* Size */
93                    (1 << EPBK) |                                /* Double bank */
94                    (1 << ALLOC));                               /* Allocate */
95 }
96
97 ISR(USB_GEN_vect)
98 {
99         uint8_t intbits;
100
101         intbits = UDINT;
102         UDINT = 0;
103         if (intbits & (1<<EORSTI)) {
104                 ao_usb_set_ep0();
105                 usb_configuration = 0;
106         }
107         if (intbits & (1<<SOFI)) {
108                 if (usb_configuration) {
109                         t = transmit_flush_timer;
110                         if (t) {
111                                 transmit_flush_timer = --t;
112                                 if (!t) {
113                                         UENUM = CDC_TX_ENDPOINT;
114                                         UEINTX = 0x3A;
115                                 }
116                         }
117                 }
118         }
119 }
120
121 struct ao_usb_setup {
122         uint8_t         dir_type_recip;
123         uint8_t         request;
124         uint16_t        value;
125         uint16_t        index;
126         uint16_t        length;
127 } __xdata ao_usb_setup;
128
129 __xdata uint8_t ao_usb_ep0_state;
130 uint8_t * __xdata ao_usb_ep0_in_data;
131 __xdata uint8_t ao_usb_ep0_in_len;
132 __xdata uint8_t ao_usb_ep0_in_buf[2];
133 __xdata uint8_t ao_usb_ep0_out_len;
134 __xdata uint8_t *__xdata ao_usb_ep0_out_data;
135 __xdata uint8_t ao_usb_configuration;
136
137 /* Send an IN data packet */
138 static void
139 ao_usb_ep0_flush(void)
140 {
141         __xdata uint8_t this_len;
142         __xdata uint8_t cs0;
143
144         /* If the IN packet hasn't been picked up, just return */
145         UENUM = 0;
146         if (!(UEINTX & (1 << TXINI)))
147                 return;
148
149         this_len = ao_usb_ep0_in_len;
150         if (this_len > AO_USB_CONTROL_SIZE)
151                 this_len = AO_USB_CONTROL_SIZE;
152
153         ao_usb_ep0_in_len -= this_len;
154         while (this_len--)
155                 UEDATX = *ao_usb_ep0_in_data++;
156
157         /* Clear the TXINI bit to send the packet */
158         UEINTX &= ~(1 << TXINI);
159 }
160
161 __xdata static struct ao_usb_line_coding ao_usb_line_coding = {115200, 0, 0, 8};
162
163 /* Walk through the list of descriptors and find a match
164  */
165 static void
166 ao_usb_get_descriptor(uint16_t value)
167 {
168         const uint8_t           *__xdata descriptor;
169         __xdata uint8_t         type = value >> 8;
170         __xdata uint8_t         index = value;
171
172         descriptor = ao_usb_descriptors;
173         while (descriptor[0] != 0) {
174                 if (descriptor[1] == type && index-- == 0) {
175                         if (type == AO_USB_DESC_CONFIGURATION)
176                                 ao_usb_ep0_in_len = descriptor[2];
177                         else
178                                 ao_usb_ep0_in_len = descriptor[0];
179                         ao_usb_ep0_in_data = descriptor;
180                         break;
181                 }
182                 descriptor += descriptor[0];
183         }
184 }
185
186 /* Read data from the ep0 OUT fifo
187  */
188 static void
189 ao_usb_ep0_fill(void)
190 {
191         __xdata uint8_t len;
192
193         UENUM = 0;
194         len = UEBCLX;   /* read length */
195         if (len > ao_usb_ep0_out_len)
196                 len = ao_usb_ep0_out_len;
197         ao_usb_ep0_out_len -= len;
198
199         /* Pull all of the data out of the packet */
200         while (len--)
201                 *ao_usb_ep0_out_data++ = UEDATX;
202 }
203
204 void
205 ao_usb_ep0_queue_byte(uint8_t a)
206 {
207         ao_usb_ep0_in_buf[ao_usb_ep0_in_len++] = a;
208 }
209
210 static void
211 ao_usb_ep0_setup(void)
212 {
213         /* Pull the setup packet out of the fifo */
214         ao_usb_ep0_out_data = (__xdata uint8_t *) &ao_usb_setup;
215         ao_usb_ep0_out_len = 8;
216         ao_usb_ep0_fill();
217         UENUM = 0;
218         UEINTX &= ~(1 << RXSTPI);
219         if (ao_usb_ep0_out_len != 0)
220                 return;
221
222         /* Figure out how to ACK the setup packet */
223         if (ao_usb_setup.dir_type_recip & AO_USB_DIR_IN) {
224                 if (ao_usb_setup.length)
225                         ao_usb_ep0_state = AO_USB_EP0_DATA_IN;
226                 else
227                         ao_usb_ep0_state = AO_USB_EP0_IDLE;
228         } else {
229                 if (ao_usb_setup.length)
230                         ao_usb_ep0_state = AO_USB_EP0_DATA_OUT;
231                 else
232                         ao_usb_ep0_state = AO_USB_EP0_IDLE;
233         }
234         UENUM = 0;
235 /*
236         if (ao_usb_ep0_state == AO_USB_EP0_IDLE)
237                 USBCS0 = USBCS0_CLR_OUTPKT_RDY | USBCS0_DATA_END;
238         else
239                 USBCS0 = USBCS0_CLR_OUTPKT_RDY;
240 */
241
242         ao_usb_ep0_in_data = ao_usb_ep0_in_buf;
243         ao_usb_ep0_in_len = 0;
244         switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_TYPE_MASK) {
245         case AO_USB_TYPE_STANDARD:
246                 switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_RECIP_MASK) {
247                 case AO_USB_RECIP_DEVICE:
248                         switch(ao_usb_setup.request) {
249                         case AO_USB_REQ_GET_STATUS:
250                                 ao_usb_ep0_queue_byte(0);
251                                 ao_usb_ep0_queue_byte(0);
252                                 break;
253                         case AO_USB_REQ_SET_ADDRESS:
254                                 ao_usb_set_address(ao_usb_setup.value);
255                                 break;
256                         case AO_USB_REQ_GET_DESCRIPTOR:
257                                 ao_usb_get_descriptor(ao_usb_setup.value);
258                                 break;
259                         case AO_USB_REQ_GET_CONFIGURATION:
260                                 ao_usb_ep0_queue_byte(ao_usb_configuration);
261                                 break;
262                         case AO_USB_REQ_SET_CONFIGURATION:
263                                 ao_usb_configuration = ao_usb_setup.value;
264                                 ao_usb_set_configuration();
265                                 break;
266                         }
267                         break;
268                 case AO_USB_RECIP_INTERFACE:
269                         #pragma disable_warning 110
270                         switch(ao_usb_setup.request) {
271                         case AO_USB_REQ_GET_STATUS:
272                                 ao_usb_ep0_queue_byte(0);
273                                 ao_usb_ep0_queue_byte(0);
274                                 break;
275                         case AO_USB_REQ_GET_INTERFACE:
276                                 ao_usb_ep0_queue_byte(0);
277                                 break;
278                         case AO_USB_REQ_SET_INTERFACE:
279                                 break;
280                         }
281                         break;
282                 case AO_USB_RECIP_ENDPOINT:
283                         switch(ao_usb_setup.request) {
284                         case AO_USB_REQ_GET_STATUS:
285                                 ao_usb_ep0_queue_byte(0);
286                                 ao_usb_ep0_queue_byte(0);
287                                 break;
288                         }
289                         break;
290                 }
291                 break;
292         case AO_USB_TYPE_CLASS:
293                 switch (ao_usb_setup.request) {
294                 case SET_LINE_CODING:
295                         ao_usb_ep0_out_len = 7;
296                         ao_usb_ep0_out_data = (__xdata uint8_t *) &ao_usb_line_coding;
297                         break;
298                 case GET_LINE_CODING:
299                         ao_usb_ep0_in_len = 7;
300                         ao_usb_ep0_in_data = (uint8_t *) &ao_usb_line_coding;
301                         break;
302                 case SET_CONTROL_LINE_STATE:
303                         break;
304                 }
305                 break;
306         }
307         if (ao_usb_ep0_state != AO_USB_EP0_DATA_OUT) {
308                 if (ao_usb_setup.length < ao_usb_ep0_in_len)
309                         ao_usb_ep0_in_len = ao_usb_setup.length;
310                 ao_usb_ep0_flush();
311         }
312 }
313
314 /* End point 0 receives all of the control messages. */
315 static void
316 ao_usb_ep0(void)
317 {
318         __xdata uint8_t intx;
319
320         ao_usb_ep0_state = AO_USB_EP0_IDLE;
321         for (;;) {
322                 cli();
323                 for (;;) {
324                         UENUM = 0;
325                         intx = UEINTX;
326                         if (intx & ((1 << RXOUTI) | (1 <<RXSTPI)))
327                             break;
328                         ao_sleep(&ao_usb_task);
329                 }
330                 sei();
331                 if (intx & (1 << RXSTPI)) {
332                         ao_usb_ep0_setup();
333                         UENUM = 0;
334                         UEINTX &= ~(1 << RXSTPI);
335                 }
336                 if (intx & (1 << RXOUTI)) {
337                         ao_usb_ep0_fill();
338                         UENUM = 0;
339                         UEINTX &= ~(1 << RXOUTI);
340                 }
341         }
342 }
343
344 /* Wait for a free IN buffer */
345 static void
346 ao_usb_in_wait(void)
347 {
348         for (;;) {
349                 UENUM = AO_USB_IN_EP;
350                 if ((UEINTX & (1 << RWAL)))
351                         break;
352                 ao_sleep(&ao_usb_in_bytes);
353         }
354 }
355
356 void
357 ao_usb_flush(void) __critical
358 {
359         if (!ao_usb_running)
360                 return;
361
362         /* If there are pending bytes, or if the last packet was full,
363          * send another IN packet
364          */
365         if (ao_usb_in_bytes || (ao_usb_in_bytes_last == AO_USB_IN_SIZE)) {
366                 ao_usb_in_wait();
367                 ao_usb_in_send();
368         }
369 }
370
371 void
372 ao_usb_putchar(char c) __critical __reentrant
373 {
374         if (!ao_usb_running)
375                 return;
376
377         ao_usb_in_wait();
378
379         /* Queue a byte, sending the packet when full */
380         UENUM = AO_USB_IN_EP;
381         UEDATX = c;
382         if (++ao_usb_in_bytes == AO_USB_IN_SIZE)
383                 ao_usb_in_send();
384 }
385
386 char
387 ao_usb_pollchar(void) __critical
388 {
389         char c;
390         if (ao_usb_out_bytes == 0) {
391                 /* Check to see if a packet has arrived */
392                 UENUM = AO_USB_OUT_EP;
393                 if ((UEINTX & (1 << RXOUTI)) == 0)
394                         return AO_READ_AGAIN;
395                 ao_usb_out_bytes = (UEBCHX << 8) | UEBCLX;
396                 if (ao_usb_out_bytes == 0) {
397                         UEINTX &= ~(1 << RXOUTI);
398                         return AO_READ_AGAIN;
399                 }
400         }
401         --ao_usb_out_bytes;
402         UENUM = AO_USB_OUT_EP;
403         c = UEDATX;
404         if (ao_usb_out_bytes == 0) {
405                 UENUM = AO_USB_OUT_EP;
406                 UEINTX &= ~(1 << RXOUTI);
407         }
408         return c;
409 }
410
411 char
412 ao_usb_getchar(void) __critical
413 {
414         char    c;
415
416         while ((c = ao_usb_pollchar()) == AO_READ_AGAIN)
417                 ao_sleep(&ao_stdin_ready);
418         return c;
419 }
420
421 // Misc functions to wait for ready and send/receive packets
422 static inline void usb_wait_in_ready(void)
423 {
424         while (!(UEINTX & (1<<TXINI))) ;
425 }
426 static inline void usb_send_in(void)
427 {
428         UEINTX = ~(1<<TXINI);
429 }
430 static inline void usb_wait_receive_out(void)
431 {
432         while (!(UEINTX & (1<<RXOUTI))) ;
433 }
434 static inline void usb_ack_out(void)
435 {
436         UEINTX = ~(1<<RXOUTI);
437 }
438
439 /* Endpoint 0 interrupt */
440
441 ISR(USB_COM_vect)
442 {
443         ao_wakeup(&ao_usb_task);
444 }
445
446 #if AVR_VCC_5V
447 #define AO_PAD_REGULATOR_INIT   (1 << UVREGE)   /* Turn on pad regulator */
448 #endif
449 #if AVR_VCC_3V3
450 #define AO_PAD_REGULATOR_INIT   0               /* Turn off pad regulator */
451 #endif
452
453 #if AVR_CLOCK == 16000000UL
454 #define AO_USB_PLL_INPUT_PRESCALER      (1 << PINDIV)   /* Divide 16MHz clock by 2 */
455 #endif
456 #if AVR_CLOCK == 8000000UL
457 #define AO_USB_PLL_INPUT_PRESCALER      0               /* Don't divide clock */
458 #endif
459
460 void
461 ao_usb_disable(void)
462 {
463         /* Unplug from the bus */
464         UDCON = (1 << DETACH);
465         ao_usb_detatch_interface();
466
467         /* Disable the interface */
468         USBCON = 0;
469
470         /* Disable the PLL */
471         PLLCSR = 0;
472
473         /* Turn off the pad regulator */
474         UHWCON = 0;
475 }
476
477 #define AO_USB_CON ((1 << USBE) |       /* USB enable */ \
478                     (0 << RSTCPU) |     /* do not reset CPU */  \
479                     (0 << LSM) |        /* Full speed mode */   \
480                     (0 << RMWKUP))      /* no remote wake-up */ \
481
482 void
483 ao_usb_enable(void)
484 {
485         /* Configure pad regulator */
486         UHWCON = AO_PAD_REGULATOR_INIT;
487
488         /* Enable USB device, but freeze the clocks until initialized */
489         USBCON = AO_USB_CON | (1 <<FRZCLK);
490
491         /* Enable PLL with appropriate divider */
492         PLLCSR = AO_USB_PLL_INPUT_PRESCALER | (1 << PLLE);
493
494         /* Wait for PLL to lock */
495         loop_until_bit_is_set(PLLCSR, (1 << PLOCK));
496
497         /* Enable USB, enable the VBUS pad */
498         USBCON = AO_USB_CON | (1 << OTGPADE);
499
500         UDCON = (0 << DETACH);  /* Clear the DETACH bit to plug into the bus */
501
502         usb_configuration = 0;
503 }
504
505 void
506 ao_usb_init(void)
507 {
508         ao_usb_enable();
509
510         ao_add_task(&ao_usb_task, ao_usb_ep0, "usb");
511         ao_add_stdio(ao_usb_pollchar, ao_usb_putchar, ao_usb_flush);
512 }