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
51 ao_wakeup(&ao_usb_task);
52 if (ao_usb_iif & (1 << AO_USB_IN_EP))
53 ao_wakeup(&ao_usb_in_bytes);
55 if (USBOIF & (1 << AO_USB_OUT_EP))
56 ao_wakeup(&ao_stdin_ready);
58 if (USBCIF & USBCIF_RSTIF)
59 ao_usb_set_interrupts();
68 uint8_t dir_type_recip;
73 } __xdata ao_usb_setup;
75 __xdata uint8_t ao_usb_ep0_state;
76 uint8_t * __xdata ao_usb_ep0_in_data;
77 __xdata uint8_t ao_usb_ep0_in_len;
78 __xdata uint8_t ao_usb_ep0_in_buf[2];
79 __xdata uint8_t ao_usb_ep0_out_len;
80 __xdata uint8_t *__xdata ao_usb_ep0_out_data;
81 __xdata uint8_t ao_usb_configuration;
83 /* Send an IN data packet */
85 ao_usb_ep0_flush(void)
87 __xdata uint8_t this_len;
90 /* If the IN packet hasn't been picked up, just return */
93 if (cs0 & USBCS0_INPKT_RDY)
96 this_len = ao_usb_ep0_in_len;
97 if (this_len > AO_USB_CONTROL_SIZE)
98 this_len = AO_USB_CONTROL_SIZE;
99 cs0 = USBCS0_INPKT_RDY;
100 if (this_len != AO_USB_CONTROL_SIZE) {
101 cs0 = USBCS0_INPKT_RDY | USBCS0_DATA_END;
102 ao_usb_ep0_state = AO_USB_EP0_IDLE;
104 ao_usb_ep0_in_len -= this_len;
106 USBFIFO[0] = *ao_usb_ep0_in_data++;
111 __xdata static struct ao_usb_line_coding ao_usb_line_coding = {115200, 0, 0, 8};
113 /* Walk through the list of descriptors and find a match
116 ao_usb_get_descriptor(uint16_t value)
118 const uint8_t *__xdata descriptor;
119 __xdata uint8_t type = value >> 8;
120 __xdata uint8_t index = value;
122 descriptor = ao_usb_descriptors;
123 while (descriptor[0] != 0) {
124 if (descriptor[1] == type && index-- == 0) {
125 if (type == AO_USB_DESC_CONFIGURATION)
126 ao_usb_ep0_in_len = descriptor[2];
128 ao_usb_ep0_in_len = descriptor[0];
129 ao_usb_ep0_in_data = descriptor;
132 descriptor += descriptor[0];
136 /* Read data from the ep0 OUT fifo
139 ao_usb_ep0_fill(void)
145 if (len > ao_usb_ep0_out_len)
146 len = ao_usb_ep0_out_len;
147 ao_usb_ep0_out_len -= len;
149 *ao_usb_ep0_out_data++ = USBFIFO[0];
153 ao_usb_ep0_queue_byte(uint8_t a)
155 ao_usb_ep0_in_buf[ao_usb_ep0_in_len++] = a;
159 ao_usb_set_address(uint8_t address)
162 USBADDR = address | 0x80;
163 while (USBADDR & 0x80)
168 ao_usb_set_configuration(void)
170 /* Set the IN max packet size, double buffered */
171 USBINDEX = AO_USB_IN_EP;
172 USBMAXI = AO_USB_IN_SIZE >> 3;
173 USBCSIH |= USBCSIH_IN_DBL_BUF;
175 /* Set the OUT max packet size, double buffered */
176 USBINDEX = AO_USB_OUT_EP;
177 USBMAXO = AO_USB_OUT_SIZE >> 3;
178 USBCSOH = USBCSOH_OUT_DBL_BUF;
182 ao_usb_ep0_setup(void)
184 /* Pull the setup packet out of the fifo */
185 ao_usb_ep0_out_data = (__xdata uint8_t *) &ao_usb_setup;
186 ao_usb_ep0_out_len = 8;
188 if (ao_usb_ep0_out_len != 0)
191 /* Figure out how to ACK the setup packet */
192 if (ao_usb_setup.dir_type_recip & AO_USB_DIR_IN) {
193 if (ao_usb_setup.length)
194 ao_usb_ep0_state = AO_USB_EP0_DATA_IN;
196 ao_usb_ep0_state = AO_USB_EP0_IDLE;
198 if (ao_usb_setup.length)
199 ao_usb_ep0_state = AO_USB_EP0_DATA_OUT;
201 ao_usb_ep0_state = AO_USB_EP0_IDLE;
204 if (ao_usb_ep0_state == AO_USB_EP0_IDLE)
205 USBCS0 = USBCS0_CLR_OUTPKT_RDY | USBCS0_DATA_END;
207 USBCS0 = USBCS0_CLR_OUTPKT_RDY;
209 ao_usb_ep0_in_data = ao_usb_ep0_in_buf;
210 ao_usb_ep0_in_len = 0;
211 switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_TYPE_MASK) {
212 case AO_USB_TYPE_STANDARD:
213 switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_RECIP_MASK) {
214 case AO_USB_RECIP_DEVICE:
215 switch(ao_usb_setup.request) {
216 case AO_USB_REQ_GET_STATUS:
217 ao_usb_ep0_queue_byte(0);
218 ao_usb_ep0_queue_byte(0);
220 case AO_USB_REQ_SET_ADDRESS:
221 ao_usb_set_address(ao_usb_setup.value);
223 case AO_USB_REQ_GET_DESCRIPTOR:
224 ao_usb_get_descriptor(ao_usb_setup.value);
226 case AO_USB_REQ_GET_CONFIGURATION:
227 ao_usb_ep0_queue_byte(ao_usb_configuration);
229 case AO_USB_REQ_SET_CONFIGURATION:
230 ao_usb_configuration = ao_usb_setup.value;
231 ao_usb_set_configuration();
235 case AO_USB_RECIP_INTERFACE:
236 #pragma disable_warning 110
237 switch(ao_usb_setup.request) {
238 case AO_USB_REQ_GET_STATUS:
239 ao_usb_ep0_queue_byte(0);
240 ao_usb_ep0_queue_byte(0);
242 case AO_USB_REQ_GET_INTERFACE:
243 ao_usb_ep0_queue_byte(0);
245 case AO_USB_REQ_SET_INTERFACE:
249 case AO_USB_RECIP_ENDPOINT:
250 switch(ao_usb_setup.request) {
251 case AO_USB_REQ_GET_STATUS:
252 ao_usb_ep0_queue_byte(0);
253 ao_usb_ep0_queue_byte(0);
259 case AO_USB_TYPE_CLASS:
260 switch (ao_usb_setup.request) {
261 case SET_LINE_CODING:
262 ao_usb_ep0_out_len = 7;
263 ao_usb_ep0_out_data = (__xdata uint8_t *) &ao_usb_line_coding;
265 case GET_LINE_CODING:
266 ao_usb_ep0_in_len = 7;
267 ao_usb_ep0_in_data = (uint8_t *) &ao_usb_line_coding;
269 case SET_CONTROL_LINE_STATE:
274 if (ao_usb_ep0_state != AO_USB_EP0_DATA_OUT) {
275 if (ao_usb_setup.length < ao_usb_ep0_in_len)
276 ao_usb_ep0_in_len = ao_usb_setup.length;
281 /* End point 0 receives all of the control messages. */
287 ao_usb_ep0_state = AO_USB_EP0_IDLE;
289 __critical for (;;) {
290 if (ao_usb_iif & 1) {
294 ao_sleep(&ao_usb_task);
298 if (cs0 & USBCS0_SETUP_END) {
299 ao_usb_ep0_state = AO_USB_EP0_IDLE;
300 USBCS0 = USBCS0_CLR_SETUP_END;
302 if (cs0 & USBCS0_SENT_STALL) {
303 ao_usb_ep0_state = AO_USB_EP0_IDLE;
304 USBCS0 &= ~USBCS0_SENT_STALL;
306 if (ao_usb_ep0_state == AO_USB_EP0_DATA_IN &&
307 (cs0 & USBCS0_INPKT_RDY) == 0)
311 if (cs0 & USBCS0_OUTPKT_RDY) {
312 switch (ao_usb_ep0_state) {
313 case AO_USB_EP0_IDLE:
316 case AO_USB_EP0_DATA_OUT:
318 if (ao_usb_ep0_out_len == 0)
319 ao_usb_ep0_state = AO_USB_EP0_IDLE;
321 if (ao_usb_ep0_state == AO_USB_EP0_IDLE)
322 USBCS0 = USBCS0_CLR_OUTPKT_RDY | USBCS0_DATA_END;
324 USBCS0 = USBCS0_CLR_OUTPKT_RDY;
331 /* Wait for a free IN buffer */
336 USBINDEX = AO_USB_IN_EP;
337 if ((USBCSIL & USBCSIL_INPKT_RDY) == 0)
339 ao_sleep(&ao_usb_in_bytes);
343 /* Send the current IN packet */
347 USBINDEX = AO_USB_IN_EP;
348 USBCSIL |= USBCSIL_INPKT_RDY;
349 ao_usb_in_bytes_last = ao_usb_in_bytes;
354 ao_usb_flush(void) __critical
359 /* If there are pending bytes, or if the last packet was full,
360 * send another IN packet
362 if (ao_usb_in_bytes || (ao_usb_in_bytes_last == AO_USB_IN_SIZE)) {
369 ao_usb_putchar(char c) __critical __reentrant
376 /* Queue a byte, sending the packet when full */
377 USBFIFO[AO_USB_IN_EP << 1] = c;
378 if (++ao_usb_in_bytes == AO_USB_IN_SIZE)
383 ao_usb_pollchar(void) __critical
386 if (ao_usb_out_bytes == 0) {
387 USBINDEX = AO_USB_OUT_EP;
388 if ((USBCSOL & USBCSOL_OUTPKT_RDY) == 0)
389 return AO_READ_AGAIN;
390 ao_usb_out_bytes = (USBCNTH << 8) | USBCNTL;
391 if (ao_usb_out_bytes == 0) {
392 USBINDEX = AO_USB_OUT_EP;
393 USBCSOL &= ~USBCSOL_OUTPKT_RDY;
394 return AO_READ_AGAIN;
398 c = USBFIFO[AO_USB_OUT_EP << 1];
399 if (ao_usb_out_bytes == 0) {
400 USBINDEX = AO_USB_OUT_EP;
401 USBCSOL &= ~USBCSOL_OUTPKT_RDY;
407 ao_usb_getchar(void) __critical
411 while ((c = ao_usb_pollchar()) == AO_READ_AGAIN)
412 ao_sleep(&ao_stdin_ready);
419 /* Turn on the USB controller */
420 SLEEP |= SLEEP_USB_EN;
422 ao_usb_set_configuration();
424 ao_usb_set_interrupts();
426 /* enable USB interrupts */
429 /* Clear any pending interrupts */
438 /* Disable USB interrupts */
444 /* Clear any pending interrupts */
449 /* Turn off the USB controller */
450 SLEEP &= ~SLEEP_USB_EN;
458 ao_add_task(&ao_usb_task, ao_usb_ep0, "usb");
459 ao_add_stdio(ao_usb_pollchar, ao_usb_putchar, ao_usb_flush);