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();
63 uint8_t dir_type_recip;
68 } __xdata ao_usb_setup;
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;
78 /* Send an IN data packet */
80 ao_usb_ep0_flush(void)
82 __xdata uint8_t this_len;
85 /* If the IN packet hasn't been picked up, just return */
88 if (cs0 & USBCS0_INPKT_RDY)
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;
99 ao_usb_ep0_in_len -= this_len;
101 USBFIFO[0] = *ao_usb_ep0_in_data++;
106 __xdata static struct ao_usb_line_coding ao_usb_line_coding = {115200, 0, 0, 8};
108 /* Walk through the list of descriptors and find a match
111 ao_usb_get_descriptor(uint16_t value)
113 const uint8_t *__xdata descriptor;
114 __xdata uint8_t type = value >> 8;
115 __xdata uint8_t index = value;
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];
123 ao_usb_ep0_in_len = descriptor[0];
124 ao_usb_ep0_in_data = descriptor;
127 descriptor += descriptor[0];
131 /* Read data from the ep0 OUT fifo
134 ao_usb_ep0_fill(void)
140 if (len > ao_usb_ep0_out_len)
141 len = ao_usb_ep0_out_len;
142 ao_usb_ep0_out_len -= len;
144 *ao_usb_ep0_out_data++ = USBFIFO[0];
148 ao_usb_ep0_queue_byte(uint8_t a)
150 ao_usb_ep0_in_buf[ao_usb_ep0_in_len++] = a;
154 ao_usb_set_address(uint8_t address)
157 USBADDR = address | 0x80;
158 while (USBADDR & 0x80)
163 ao_usb_set_configuration(void)
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;
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;
177 ao_usb_ep0_setup(void)
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;
183 if (ao_usb_ep0_out_len != 0)
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;
191 ao_usb_ep0_state = AO_USB_EP0_IDLE;
193 if (ao_usb_setup.length)
194 ao_usb_ep0_state = AO_USB_EP0_DATA_OUT;
196 ao_usb_ep0_state = AO_USB_EP0_IDLE;
199 if (ao_usb_ep0_state == AO_USB_EP0_IDLE)
200 USBCS0 = USBCS0_CLR_OUTPKT_RDY | USBCS0_DATA_END;
202 USBCS0 = USBCS0_CLR_OUTPKT_RDY;
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);
215 case AO_USB_REQ_SET_ADDRESS:
216 ao_usb_set_address(ao_usb_setup.value);
218 case AO_USB_REQ_GET_DESCRIPTOR:
219 ao_usb_get_descriptor(ao_usb_setup.value);
221 case AO_USB_REQ_GET_CONFIGURATION:
222 ao_usb_ep0_queue_byte(ao_usb_configuration);
224 case AO_USB_REQ_SET_CONFIGURATION:
225 ao_usb_configuration = ao_usb_setup.value;
226 ao_usb_set_configuration();
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);
237 case AO_USB_REQ_GET_INTERFACE:
238 ao_usb_ep0_queue_byte(0);
240 case AO_USB_REQ_SET_INTERFACE:
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);
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;
260 case GET_LINE_CODING:
261 ao_usb_ep0_in_len = 7;
262 ao_usb_ep0_in_data = (uint8_t *) &ao_usb_line_coding;
264 case SET_CONTROL_LINE_STATE:
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;
276 /* End point 0 receives all of the control messages. */
282 ao_usb_ep0_state = AO_USB_EP0_IDLE;
284 __critical for (;;) {
285 if (ao_usb_iif & 1) {
289 ao_sleep(&ao_usb_task);
293 if (cs0 & USBCS0_SETUP_END) {
294 ao_usb_ep0_state = AO_USB_EP0_IDLE;
295 USBCS0 = USBCS0_CLR_SETUP_END;
297 if (cs0 & USBCS0_SENT_STALL) {
298 ao_usb_ep0_state = AO_USB_EP0_IDLE;
299 USBCS0 &= ~USBCS0_SENT_STALL;
301 if (ao_usb_ep0_state == AO_USB_EP0_DATA_IN &&
302 (cs0 & USBCS0_INPKT_RDY) == 0)
306 if (cs0 & USBCS0_OUTPKT_RDY) {
307 switch (ao_usb_ep0_state) {
308 case AO_USB_EP0_IDLE:
311 case AO_USB_EP0_DATA_OUT:
313 if (ao_usb_ep0_out_len == 0)
314 ao_usb_ep0_state = AO_USB_EP0_IDLE;
316 if (ao_usb_ep0_state == AO_USB_EP0_IDLE)
317 USBCS0 = USBCS0_CLR_OUTPKT_RDY | USBCS0_DATA_END;
319 USBCS0 = USBCS0_CLR_OUTPKT_RDY;
326 /* Wait for a free IN buffer */
331 USBINDEX = AO_USB_IN_EP;
332 if ((USBCSIL & USBCSIL_INPKT_RDY) == 0)
334 ao_sleep(&ao_usb_in_bytes);
338 /* Send the current IN packet */
342 USBINDEX = AO_USB_IN_EP;
343 USBCSIL |= USBCSIL_INPKT_RDY;
344 ao_usb_in_bytes_last = ao_usb_in_bytes;
349 ao_usb_flush(void) __critical
354 /* If there are pending bytes, or if the last packet was full,
355 * send another IN packet
357 if (ao_usb_in_bytes || (ao_usb_in_bytes_last == AO_USB_IN_SIZE)) {
364 ao_usb_putchar(char c) __critical __reentrant
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)
378 ao_usb_pollchar(void) __critical
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;
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;
402 ao_usb_getchar(void) __critical
406 while ((c = ao_usb_pollchar()) == AO_READ_AGAIN)
407 ao_sleep(&ao_stdin_ready);
414 /* Turn on the USB controller */
415 SLEEP |= SLEEP_USB_EN;
417 ao_usb_set_configuration();
419 ao_usb_set_interrupts();
421 /* enable USB interrupts */
424 /* Clear any pending interrupts */
433 /* Disable USB interrupts */
439 /* Clear any pending interrupts */
444 /* Turn off the USB controller */
445 SLEEP &= ~SLEEP_USB_EN;
453 ao_add_task(&ao_usb_task, ao_usb_ep0, "usb");
454 ao_add_stdio(ao_usb_pollchar, ao_usb_putchar, ao_usb_flush);