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