altosui: Prevent voice altitude data from queueing up
[fw/altos] / src / ao_usb.c
1 /*
2  * Copyright © 2009 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 #include "ao_usb.h"
20
21 struct ao_task __xdata ao_usb_task;
22
23 static __xdata uint16_t ao_usb_in_bytes;
24 static __xdata uint16_t ao_usb_in_bytes_last;
25 static __xdata uint16_t ao_usb_out_bytes;
26 static __xdata uint8_t  ao_usb_iif;
27 static __xdata uint8_t  ao_usb_running;
28
29 static void
30 ao_usb_set_interrupts(void)
31 {
32         /* IN interrupts on the control an IN endpoints */
33         USBIIE = (1 << AO_USB_CONTROL_EP) | (1 << AO_USB_IN_EP);
34
35         /* OUT interrupts on the OUT endpoint */
36         USBOIE = (1 << AO_USB_OUT_EP);
37
38         /* Only care about reset */
39         USBCIE = USBCIE_RSTIE;
40 }
41
42 /* This interrupt is shared with port 2,
43  * so when we hook that up, fix this
44  */
45 void
46 ao_usb_isr(void) __interrupt 6
47 {
48         USBIF = 0;
49         ao_usb_iif |= USBIIF;
50         if (ao_usb_iif & 1)
51                 ao_wakeup(&ao_usb_task);
52         if (ao_usb_iif & (1 << AO_USB_IN_EP))
53                 ao_wakeup(&ao_usb_in_bytes);
54
55         if (USBOIF & (1 << AO_USB_OUT_EP))
56                 ao_wakeup(&ao_stdin_ready);
57
58         if (USBCIF & USBCIF_RSTIF)
59                 ao_usb_set_interrupts();
60 }
61
62 struct ao_usb_setup {
63         uint8_t         dir_type_recip;
64         uint8_t         request;
65         uint16_t        value;
66         uint16_t        index;
67         uint16_t        length;
68 } __xdata ao_usb_setup;
69
70 __xdata uint8_t ao_usb_ep0_state;
71 uint8_t * __xdata ao_usb_ep0_in_data;
72 __xdata uint8_t ao_usb_ep0_in_len;
73 __xdata uint8_t ao_usb_ep0_in_buf[2];
74 __xdata uint8_t ao_usb_ep0_out_len;
75 __xdata uint8_t *__xdata ao_usb_ep0_out_data;
76 __xdata uint8_t ao_usb_configuration;
77
78 /* Send an IN data packet */
79 static void
80 ao_usb_ep0_flush(void)
81 {
82         __xdata uint8_t this_len;
83         __xdata uint8_t cs0;
84
85         /* If the IN packet hasn't been picked up, just return */
86         USBINDEX = 0;
87         cs0 = USBCS0;
88         if (cs0 & USBCS0_INPKT_RDY)
89                 return;
90
91         this_len = ao_usb_ep0_in_len;
92         if (this_len > AO_USB_CONTROL_SIZE)
93                 this_len = AO_USB_CONTROL_SIZE;
94         cs0 = USBCS0_INPKT_RDY;
95         if (this_len != AO_USB_CONTROL_SIZE) {
96                 cs0 = USBCS0_INPKT_RDY | USBCS0_DATA_END;
97                 ao_usb_ep0_state = AO_USB_EP0_IDLE;
98         }
99         ao_usb_ep0_in_len -= this_len;
100         while (this_len--)
101                 USBFIFO[0] = *ao_usb_ep0_in_data++;
102         USBINDEX = 0;
103         USBCS0 = cs0;
104 }
105
106 __xdata static struct ao_usb_line_coding ao_usb_line_coding = {115200, 0, 0, 8};
107
108 /* Walk through the list of descriptors and find a match
109  */
110 static void
111 ao_usb_get_descriptor(uint16_t value)
112 {
113         const uint8_t           *__xdata descriptor;
114         __xdata uint8_t         type = value >> 8;
115         __xdata uint8_t         index = value;
116
117         descriptor = ao_usb_descriptors;
118         while (descriptor[0] != 0) {
119                 if (descriptor[1] == type && index-- == 0) {
120                         if (type == AO_USB_DESC_CONFIGURATION)
121                                 ao_usb_ep0_in_len = descriptor[2];
122                         else
123                                 ao_usb_ep0_in_len = descriptor[0];
124                         ao_usb_ep0_in_data = descriptor;
125                         break;
126                 }
127                 descriptor += descriptor[0];
128         }
129 }
130
131 /* Read data from the ep0 OUT fifo
132  */
133 static void
134 ao_usb_ep0_fill(void)
135 {
136         __xdata uint8_t len;
137
138         USBINDEX = 0;
139         len = USBCNT0;
140         if (len > ao_usb_ep0_out_len)
141                 len = ao_usb_ep0_out_len;
142         ao_usb_ep0_out_len -= len;
143         while (len--)
144                 *ao_usb_ep0_out_data++ = USBFIFO[0];
145 }
146
147 void
148 ao_usb_ep0_queue_byte(uint8_t a)
149 {
150         ao_usb_ep0_in_buf[ao_usb_ep0_in_len++] = a;
151 }
152
153 void
154 ao_usb_set_address(uint8_t address)
155 {
156         ao_usb_running = 1;
157         USBADDR = address | 0x80;
158         while (USBADDR & 0x80)
159                 ;
160 }
161
162 static void
163 ao_usb_set_configuration(void)
164 {
165         /* Set the IN max packet size, double buffered */
166         USBINDEX = AO_USB_IN_EP;
167         USBMAXI = AO_USB_IN_SIZE >> 3;
168         USBCSIH |= USBCSIH_IN_DBL_BUF;
169
170         /* Set the OUT max packet size, double buffered */
171         USBINDEX = AO_USB_OUT_EP;
172         USBMAXO = AO_USB_OUT_SIZE >> 3;
173         USBCSOH = USBCSOH_OUT_DBL_BUF;
174 }
175
176 static void
177 ao_usb_ep0_setup(void)
178 {
179         /* Pull the setup packet out of the fifo */
180         ao_usb_ep0_out_data = (__xdata uint8_t *) &ao_usb_setup;
181         ao_usb_ep0_out_len = 8;
182         ao_usb_ep0_fill();
183         if (ao_usb_ep0_out_len != 0)
184                 return;
185
186         /* Figure out how to ACK the setup packet */
187         if (ao_usb_setup.dir_type_recip & AO_USB_DIR_IN) {
188                 if (ao_usb_setup.length)
189                         ao_usb_ep0_state = AO_USB_EP0_DATA_IN;
190                 else
191                         ao_usb_ep0_state = AO_USB_EP0_IDLE;
192         } else {
193                 if (ao_usb_setup.length)
194                         ao_usb_ep0_state = AO_USB_EP0_DATA_OUT;
195                 else
196                         ao_usb_ep0_state = AO_USB_EP0_IDLE;
197         }
198         USBINDEX = 0;
199         if (ao_usb_ep0_state == AO_USB_EP0_IDLE)
200                 USBCS0 = USBCS0_CLR_OUTPKT_RDY | USBCS0_DATA_END;
201         else
202                 USBCS0 = USBCS0_CLR_OUTPKT_RDY;
203
204         ao_usb_ep0_in_data = ao_usb_ep0_in_buf;
205         ao_usb_ep0_in_len = 0;
206         switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_TYPE_MASK) {
207         case AO_USB_TYPE_STANDARD:
208                 switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_RECIP_MASK) {
209                 case AO_USB_RECIP_DEVICE:
210                         switch(ao_usb_setup.request) {
211                         case AO_USB_REQ_GET_STATUS:
212                                 ao_usb_ep0_queue_byte(0);
213                                 ao_usb_ep0_queue_byte(0);
214                                 break;
215                         case AO_USB_REQ_SET_ADDRESS:
216                                 ao_usb_set_address(ao_usb_setup.value);
217                                 break;
218                         case AO_USB_REQ_GET_DESCRIPTOR:
219                                 ao_usb_get_descriptor(ao_usb_setup.value);
220                                 break;
221                         case AO_USB_REQ_GET_CONFIGURATION:
222                                 ao_usb_ep0_queue_byte(ao_usb_configuration);
223                                 break;
224                         case AO_USB_REQ_SET_CONFIGURATION:
225                                 ao_usb_configuration = ao_usb_setup.value;
226                                 ao_usb_set_configuration();
227                                 break;
228                         }
229                         break;
230                 case AO_USB_RECIP_INTERFACE:
231                         #pragma disable_warning 110
232                         switch(ao_usb_setup.request) {
233                         case AO_USB_REQ_GET_STATUS:
234                                 ao_usb_ep0_queue_byte(0);
235                                 ao_usb_ep0_queue_byte(0);
236                                 break;
237                         case AO_USB_REQ_GET_INTERFACE:
238                                 ao_usb_ep0_queue_byte(0);
239                                 break;
240                         case AO_USB_REQ_SET_INTERFACE:
241                                 break;
242                         }
243                         break;
244                 case AO_USB_RECIP_ENDPOINT:
245                         switch(ao_usb_setup.request) {
246                         case AO_USB_REQ_GET_STATUS:
247                                 ao_usb_ep0_queue_byte(0);
248                                 ao_usb_ep0_queue_byte(0);
249                                 break;
250                         }
251                         break;
252                 }
253                 break;
254         case AO_USB_TYPE_CLASS:
255                 switch (ao_usb_setup.request) {
256                 case SET_LINE_CODING:
257                         ao_usb_ep0_out_len = 7;
258                         ao_usb_ep0_out_data = (__xdata uint8_t *) &ao_usb_line_coding;
259                         break;
260                 case GET_LINE_CODING:
261                         ao_usb_ep0_in_len = 7;
262                         ao_usb_ep0_in_data = (uint8_t *) &ao_usb_line_coding;
263                         break;
264                 case SET_CONTROL_LINE_STATE:
265                         break;
266                 }
267                 break;
268         }
269         if (ao_usb_ep0_state != AO_USB_EP0_DATA_OUT) {
270                 if (ao_usb_setup.length < ao_usb_ep0_in_len)
271                         ao_usb_ep0_in_len = ao_usb_setup.length;
272                 ao_usb_ep0_flush();
273         }
274 }
275
276 /* End point 0 receives all of the control messages. */
277 static void
278 ao_usb_ep0(void)
279 {
280         __xdata uint8_t cs0;
281
282         ao_usb_ep0_state = AO_USB_EP0_IDLE;
283         for (;;) {
284                 __critical for (;;) {
285                         if (ao_usb_iif & 1) {
286                                 ao_usb_iif &= ~1;
287                                 break;
288                         }
289                         ao_sleep(&ao_usb_task);
290                 }
291                 USBINDEX = 0;
292                 cs0 = USBCS0;
293                 if (cs0 & USBCS0_SETUP_END) {
294                         ao_usb_ep0_state = AO_USB_EP0_IDLE;
295                         USBCS0 = USBCS0_CLR_SETUP_END;
296                 }
297                 if (cs0 & USBCS0_SENT_STALL) {
298                         ao_usb_ep0_state = AO_USB_EP0_IDLE;
299                         USBCS0 &= ~USBCS0_SENT_STALL;
300                 }
301                 if (ao_usb_ep0_state == AO_USB_EP0_DATA_IN &&
302                     (cs0 & USBCS0_INPKT_RDY) == 0)
303                 {
304                         ao_usb_ep0_flush();
305                 }
306                 if (cs0 & USBCS0_OUTPKT_RDY) {
307                         switch (ao_usb_ep0_state) {
308                         case AO_USB_EP0_IDLE:
309                                 ao_usb_ep0_setup();
310                                 break;
311                         case AO_USB_EP0_DATA_OUT:
312                                 ao_usb_ep0_fill();
313                                 if (ao_usb_ep0_out_len == 0)
314                                         ao_usb_ep0_state = AO_USB_EP0_IDLE;
315                                 USBINDEX = 0;
316                                 if (ao_usb_ep0_state == AO_USB_EP0_IDLE)
317                                         USBCS0 = USBCS0_CLR_OUTPKT_RDY | USBCS0_DATA_END;
318                                 else
319                                         USBCS0 = USBCS0_CLR_OUTPKT_RDY;
320                                 break;
321                         }
322                 }
323         }
324 }
325
326 /* Wait for a free IN buffer */
327 static void
328 ao_usb_in_wait(void)
329 {
330         for (;;) {
331                 USBINDEX = AO_USB_IN_EP;
332                 if ((USBCSIL & USBCSIL_INPKT_RDY) == 0)
333                         break;
334                 ao_sleep(&ao_usb_in_bytes);
335         }
336 }
337
338 /* Send the current IN packet */
339 static void
340 ao_usb_in_send(void)
341 {
342         USBINDEX = AO_USB_IN_EP;
343         USBCSIL |= USBCSIL_INPKT_RDY;
344         ao_usb_in_bytes_last = ao_usb_in_bytes;
345         ao_usb_in_bytes = 0;
346 }
347
348 void
349 ao_usb_flush(void) __critical
350 {
351         if (!ao_usb_running)
352                 return;
353
354         /* If there are pending bytes, or if the last packet was full,
355          * send another IN packet
356          */
357         if (ao_usb_in_bytes || (ao_usb_in_bytes_last == AO_USB_IN_SIZE)) {
358                 ao_usb_in_wait();
359                 ao_usb_in_send();
360         }
361 }
362
363 void
364 ao_usb_putchar(char c) __critical __reentrant
365 {
366         if (!ao_usb_running)
367                 return;
368
369         ao_usb_in_wait();
370
371         /* Queue a byte, sending the packet when full */
372         USBFIFO[AO_USB_IN_EP << 1] = c;
373         if (++ao_usb_in_bytes == AO_USB_IN_SIZE)
374                 ao_usb_in_send();
375 }
376
377 char
378 ao_usb_pollchar(void) __critical
379 {
380         char c;
381         if (ao_usb_out_bytes == 0) {
382                 USBINDEX = AO_USB_OUT_EP;
383                 if ((USBCSOL & USBCSOL_OUTPKT_RDY) == 0)
384                         return AO_READ_AGAIN;
385                 ao_usb_out_bytes = (USBCNTH << 8) | USBCNTL;
386                 if (ao_usb_out_bytes == 0) {
387                         USBINDEX = AO_USB_OUT_EP;
388                         USBCSOL &= ~USBCSOL_OUTPKT_RDY;
389                         return AO_READ_AGAIN;
390                 }
391         }
392         --ao_usb_out_bytes;
393         c = USBFIFO[AO_USB_OUT_EP << 1];
394         if (ao_usb_out_bytes == 0) {
395                 USBINDEX = AO_USB_OUT_EP;
396                 USBCSOL &= ~USBCSOL_OUTPKT_RDY;
397         }
398         return c;
399 }
400
401 char
402 ao_usb_getchar(void) __critical
403 {
404         char    c;
405
406         while ((c = ao_usb_pollchar()) == AO_READ_AGAIN)
407                 ao_sleep(&ao_stdin_ready);
408         return c;
409 }
410
411 void
412 ao_usb_enable(void)
413 {
414         /* Turn on the USB controller */
415         SLEEP |= SLEEP_USB_EN;
416
417         ao_usb_set_configuration();
418
419         ao_usb_set_interrupts();
420
421         /* enable USB interrupts */
422         IEN2 |= IEN2_USBIE;
423
424         /* Clear any pending interrupts */
425         USBCIF = 0;
426         USBOIF = 0;
427         USBIIF = 0;
428 }
429
430 void
431 ao_usb_disable(void)
432 {
433         /* Disable USB interrupts */
434         USBIIE = 0;
435         USBOIE = 0;
436         USBCIE = 0;
437         IEN2 &= ~IEN2_USBIE;
438
439         /* Clear any pending interrupts */
440         USBCIF = 0;
441         USBOIF = 0;
442         USBIIF = 0;
443
444         /* Turn off the USB controller */
445         SLEEP &= ~SLEEP_USB_EN;
446 }
447
448 void
449 ao_usb_init(void)
450 {
451         ao_usb_enable();
452
453         ao_add_task(&ao_usb_task, ao_usb_ep0, "usb");
454         ao_add_stdio(ao_usb_pollchar, ao_usb_putchar, ao_usb_flush);
455 }