2 * Copyright © 2009 Keith Packard <keithp@keithp.com>
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.
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.
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.
21 struct ao_task __xdata ao_usb_task;
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;
30 ao_usb_set_interrupts(void)
32 /* IN interrupts on the control an IN endpoints */
33 USBIIE = (1 << AO_USB_CONTROL_EP) | (1 << AO_USB_IN_EP);
35 /* OUT interrupts on the OUT endpoint */
36 USBOIE = (1 << AO_USB_OUT_EP);
38 /* Only care about reset */
39 USBCIE = USBCIE_RSTIE;
42 /* This interrupt is shared with port 2,
43 * so when we hook that up, fix this
46 ao_usb_isr(void) __interrupt 6
49 IRCON2 &= ~IRCON2_USBIF;
52 ao_wakeup(&ao_usb_task);
53 if (ao_usb_iif & (1 << AO_USB_IN_EP))
54 ao_wakeup(&ao_usb_in_bytes);
56 if (USBOIF & (1 << AO_USB_OUT_EP))
57 ao_wakeup(&ao_stdin_ready);
59 if (USBCIF & USBCIF_RSTIF)
60 ao_usb_set_interrupts();
69 uint8_t dir_type_recip;
74 } __xdata ao_usb_setup;
76 __xdata uint8_t ao_usb_ep0_state;
77 uint8_t * __xdata ao_usb_ep0_in_data;
78 __xdata uint8_t ao_usb_ep0_in_len;
79 __xdata uint8_t ao_usb_ep0_in_buf[2];
80 __xdata uint8_t ao_usb_ep0_out_len;
81 __xdata uint8_t *__xdata ao_usb_ep0_out_data;
82 __xdata uint8_t ao_usb_configuration;
84 /* Send an IN data packet */
86 ao_usb_ep0_flush(void)
88 __xdata uint8_t this_len;
91 /* If the IN packet hasn't been picked up, just return */
94 if (cs0 & USBCS0_INPKT_RDY)
97 this_len = ao_usb_ep0_in_len;
98 if (this_len > AO_USB_CONTROL_SIZE)
99 this_len = AO_USB_CONTROL_SIZE;
100 cs0 = USBCS0_INPKT_RDY;
101 if (this_len != AO_USB_CONTROL_SIZE) {
102 cs0 = USBCS0_INPKT_RDY | USBCS0_DATA_END;
103 ao_usb_ep0_state = AO_USB_EP0_IDLE;
105 ao_usb_ep0_in_len -= this_len;
107 USBFIFO[0] = *ao_usb_ep0_in_data++;
112 __xdata static struct ao_usb_line_coding ao_usb_line_coding = {115200, 0, 0, 8};
114 /* Walk through the list of descriptors and find a match
117 ao_usb_get_descriptor(uint16_t value)
119 const uint8_t *__xdata descriptor;
120 __xdata uint8_t type = value >> 8;
121 __xdata uint8_t index = value;
123 descriptor = ao_usb_descriptors;
124 while (descriptor[0] != 0) {
125 if (descriptor[1] == type && index-- == 0) {
126 if (type == AO_USB_DESC_CONFIGURATION)
127 ao_usb_ep0_in_len = descriptor[2];
129 ao_usb_ep0_in_len = descriptor[0];
130 ao_usb_ep0_in_data = descriptor;
133 descriptor += descriptor[0];
137 /* Read data from the ep0 OUT fifo
140 ao_usb_ep0_fill(void)
146 if (len > ao_usb_ep0_out_len)
147 len = ao_usb_ep0_out_len;
148 ao_usb_ep0_out_len -= len;
150 *ao_usb_ep0_out_data++ = USBFIFO[0];
154 ao_usb_ep0_queue_byte(uint8_t a)
156 ao_usb_ep0_in_buf[ao_usb_ep0_in_len++] = a;
160 ao_usb_set_address(uint8_t address)
163 USBADDR = address | 0x80;
164 while (USBADDR & 0x80)
169 ao_usb_set_configuration(void)
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;
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;
183 ao_usb_ep0_setup(void)
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;
189 if (ao_usb_ep0_out_len != 0)
192 /* Figure out how to ACK the setup packet */
193 if (ao_usb_setup.dir_type_recip & AO_USB_DIR_IN) {
194 if (ao_usb_setup.length)
195 ao_usb_ep0_state = AO_USB_EP0_DATA_IN;
197 ao_usb_ep0_state = AO_USB_EP0_IDLE;
199 if (ao_usb_setup.length)
200 ao_usb_ep0_state = AO_USB_EP0_DATA_OUT;
202 ao_usb_ep0_state = AO_USB_EP0_IDLE;
205 if (ao_usb_ep0_state == AO_USB_EP0_IDLE)
206 USBCS0 = USBCS0_CLR_OUTPKT_RDY | USBCS0_DATA_END;
208 USBCS0 = USBCS0_CLR_OUTPKT_RDY;
210 ao_usb_ep0_in_data = ao_usb_ep0_in_buf;
211 ao_usb_ep0_in_len = 0;
212 switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_TYPE_MASK) {
213 case AO_USB_TYPE_STANDARD:
214 switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_RECIP_MASK) {
215 case AO_USB_RECIP_DEVICE:
216 switch(ao_usb_setup.request) {
217 case AO_USB_REQ_GET_STATUS:
218 ao_usb_ep0_queue_byte(0);
219 ao_usb_ep0_queue_byte(0);
221 case AO_USB_REQ_SET_ADDRESS:
222 ao_usb_set_address(ao_usb_setup.value);
224 case AO_USB_REQ_GET_DESCRIPTOR:
225 ao_usb_get_descriptor(ao_usb_setup.value);
227 case AO_USB_REQ_GET_CONFIGURATION:
228 ao_usb_ep0_queue_byte(ao_usb_configuration);
230 case AO_USB_REQ_SET_CONFIGURATION:
231 ao_usb_configuration = ao_usb_setup.value;
232 ao_usb_set_configuration();
236 case AO_USB_RECIP_INTERFACE:
237 #pragma disable_warning 110
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);
243 case AO_USB_REQ_GET_INTERFACE:
244 ao_usb_ep0_queue_byte(0);
246 case AO_USB_REQ_SET_INTERFACE:
250 case AO_USB_RECIP_ENDPOINT:
251 switch(ao_usb_setup.request) {
252 case AO_USB_REQ_GET_STATUS:
253 ao_usb_ep0_queue_byte(0);
254 ao_usb_ep0_queue_byte(0);
260 case AO_USB_TYPE_CLASS:
261 switch (ao_usb_setup.request) {
262 case SET_LINE_CODING:
263 ao_usb_ep0_out_len = 7;
264 ao_usb_ep0_out_data = (__xdata uint8_t *) &ao_usb_line_coding;
266 case GET_LINE_CODING:
267 ao_usb_ep0_in_len = 7;
268 ao_usb_ep0_in_data = (uint8_t *) &ao_usb_line_coding;
270 case SET_CONTROL_LINE_STATE:
275 if (ao_usb_ep0_state != AO_USB_EP0_DATA_OUT) {
276 if (ao_usb_setup.length < ao_usb_ep0_in_len)
277 ao_usb_ep0_in_len = ao_usb_setup.length;
282 /* End point 0 receives all of the control messages. */
288 ao_usb_ep0_state = AO_USB_EP0_IDLE;
290 __critical for (;;) {
291 if (ao_usb_iif & 1) {
295 ao_sleep(&ao_usb_task);
299 if (cs0 & USBCS0_SETUP_END) {
300 ao_usb_ep0_state = AO_USB_EP0_IDLE;
301 USBCS0 = USBCS0_CLR_SETUP_END;
303 if (cs0 & USBCS0_SENT_STALL) {
304 ao_usb_ep0_state = AO_USB_EP0_IDLE;
305 USBCS0 &= ~USBCS0_SENT_STALL;
307 if (ao_usb_ep0_state == AO_USB_EP0_DATA_IN &&
308 (cs0 & USBCS0_INPKT_RDY) == 0)
312 if (cs0 & USBCS0_OUTPKT_RDY) {
313 switch (ao_usb_ep0_state) {
314 case AO_USB_EP0_IDLE:
317 case AO_USB_EP0_DATA_OUT:
319 if (ao_usb_ep0_out_len == 0)
320 ao_usb_ep0_state = AO_USB_EP0_IDLE;
322 if (ao_usb_ep0_state == AO_USB_EP0_IDLE)
323 USBCS0 = USBCS0_CLR_OUTPKT_RDY | USBCS0_DATA_END;
325 USBCS0 = USBCS0_CLR_OUTPKT_RDY;
332 /* Wait for a free IN buffer */
337 USBINDEX = AO_USB_IN_EP;
338 if ((USBCSIL & USBCSIL_INPKT_RDY) == 0)
340 ao_sleep(&ao_usb_in_bytes);
344 /* Send the current IN packet */
348 USBINDEX = AO_USB_IN_EP;
349 USBCSIL |= USBCSIL_INPKT_RDY;
350 ao_usb_in_bytes_last = ao_usb_in_bytes;
355 ao_usb_flush(void) __critical
360 /* If there are pending bytes, or if the last packet was full,
361 * send another IN packet
363 if (ao_usb_in_bytes || (ao_usb_in_bytes_last == AO_USB_IN_SIZE)) {
370 ao_usb_putchar(char c) __critical __reentrant
377 /* Queue a byte, sending the packet when full */
378 USBFIFO[AO_USB_IN_EP << 1] = c;
379 if (++ao_usb_in_bytes == AO_USB_IN_SIZE)
384 ao_usb_pollchar(void) __critical
387 if (ao_usb_out_bytes == 0) {
388 USBINDEX = AO_USB_OUT_EP;
389 if ((USBCSOL & USBCSOL_OUTPKT_RDY) == 0)
390 return AO_READ_AGAIN;
391 ao_usb_out_bytes = (USBCNTH << 8) | USBCNTL;
392 if (ao_usb_out_bytes == 0) {
393 USBINDEX = AO_USB_OUT_EP;
394 USBCSOL &= ~USBCSOL_OUTPKT_RDY;
395 return AO_READ_AGAIN;
399 c = USBFIFO[AO_USB_OUT_EP << 1];
400 if (ao_usb_out_bytes == 0) {
401 USBINDEX = AO_USB_OUT_EP;
402 USBCSOL &= ~USBCSOL_OUTPKT_RDY;
408 ao_usb_getchar(void) __critical
412 while ((c = ao_usb_pollchar()) == AO_READ_AGAIN)
413 ao_sleep(&ao_stdin_ready);
420 /* Turn on the USB controller */
421 SLEEP |= SLEEP_USB_EN;
423 ao_usb_set_configuration();
425 ao_usb_set_interrupts();
427 /* enable USB interrupts */
430 /* Clear any pending interrupts */
439 /* Disable USB interrupts */
445 /* Clear any pending interrupts */
450 /* Turn off the USB controller */
451 SLEEP &= ~SLEEP_USB_EN;
459 ao_add_task(&ao_usb_task, ao_usb_ep0, "usb");
460 ao_add_stdio(ao_usb_pollchar, ao_usb_putchar, ao_usb_flush);