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