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;
87 if (cs0 & USBCS0_INPKT_RDY)
90 this_len = ao_usb_ep0_in_len;
91 if (this_len > AO_USB_CONTROL_SIZE)
92 this_len = AO_USB_CONTROL_SIZE;
93 cs0 = USBCS0_INPKT_RDY;
94 if (this_len != AO_USB_CONTROL_SIZE) {
95 cs0 = USBCS0_INPKT_RDY | USBCS0_DATA_END;
96 ao_usb_ep0_state = AO_USB_EP0_IDLE;
98 ao_usb_ep0_in_len -= this_len;
100 USBFIFO[0] = *ao_usb_ep0_in_data++;
105 __xdata static struct ao_usb_line_coding ao_usb_line_coding = {115200, 0, 0, 8};
107 /* Walk through the list of descriptors and find a match
110 ao_usb_get_descriptor(uint16_t value)
112 const uint8_t *__xdata descriptor;
113 __xdata uint8_t type = value >> 8;
114 __xdata uint8_t index = value;
116 descriptor = ao_usb_descriptors;
117 while (descriptor[0] != 0) {
118 if (descriptor[1] == type && index-- == 0) {
119 if (type == AO_USB_DESC_CONFIGURATION)
120 ao_usb_ep0_in_len = descriptor[2];
122 ao_usb_ep0_in_len = descriptor[0];
123 ao_usb_ep0_in_data = descriptor;
126 descriptor += descriptor[0];
130 /* Read data from the ep0 OUT fifo
133 ao_usb_ep0_fill(void)
139 if (len > ao_usb_ep0_out_len)
140 len = ao_usb_ep0_out_len;
141 ao_usb_ep0_out_len -= len;
143 *ao_usb_ep0_out_data++ = USBFIFO[0];
147 ao_usb_ep0_queue_byte(uint8_t a)
149 ao_usb_ep0_in_buf[ao_usb_ep0_in_len++] = a;
153 ao_usb_set_address(uint8_t address)
156 USBADDR = address | 0x80;
157 while (USBADDR & 0x80)
162 ao_usb_set_configuration(void)
164 /* Set the IN max packet size, double buffered */
165 USBINDEX = AO_USB_IN_EP;
166 USBMAXI = AO_USB_IN_SIZE >> 3;
167 USBCSIH |= USBCSIH_IN_DBL_BUF;
169 /* Set the OUT max packet size, double buffered */
170 USBINDEX = AO_USB_OUT_EP;
171 USBMAXO = AO_USB_OUT_SIZE >> 3;
172 USBCSOH = USBCSOH_OUT_DBL_BUF;
176 ao_usb_ep0_setup(void)
178 /* Pull the setup packet out of the fifo */
179 ao_usb_ep0_out_data = (__xdata uint8_t *) &ao_usb_setup;
180 ao_usb_ep0_out_len = 8;
182 if (ao_usb_ep0_out_len != 0)
185 /* Figure out how to ACK the setup packet */
186 if (ao_usb_setup.dir_type_recip & AO_USB_DIR_IN) {
187 if (ao_usb_setup.length)
188 ao_usb_ep0_state = AO_USB_EP0_DATA_IN;
190 ao_usb_ep0_state = AO_USB_EP0_IDLE;
192 if (ao_usb_setup.length)
193 ao_usb_ep0_state = AO_USB_EP0_DATA_OUT;
195 ao_usb_ep0_state = AO_USB_EP0_IDLE;
198 if (ao_usb_ep0_state == AO_USB_EP0_IDLE)
199 USBCS0 = USBCS0_CLR_OUTPKT_RDY | USBCS0_DATA_END;
201 USBCS0 = USBCS0_CLR_OUTPKT_RDY;
203 ao_usb_ep0_in_data = ao_usb_ep0_in_buf;
204 ao_usb_ep0_in_len = 0;
205 switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_TYPE_MASK) {
206 case AO_USB_TYPE_STANDARD:
207 switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_RECIP_MASK) {
208 case AO_USB_RECIP_DEVICE:
209 switch(ao_usb_setup.request) {
210 case AO_USB_REQ_GET_STATUS:
211 ao_usb_ep0_queue_byte(0);
212 ao_usb_ep0_queue_byte(0);
214 case AO_USB_REQ_SET_ADDRESS:
215 ao_usb_set_address(ao_usb_setup.value);
217 case AO_USB_REQ_GET_DESCRIPTOR:
218 ao_usb_get_descriptor(ao_usb_setup.value);
220 case AO_USB_REQ_GET_CONFIGURATION:
221 ao_usb_ep0_queue_byte(ao_usb_configuration);
223 case AO_USB_REQ_SET_CONFIGURATION:
224 ao_usb_configuration = ao_usb_setup.value;
225 ao_usb_set_configuration();
229 case AO_USB_RECIP_INTERFACE:
230 #pragma disable_warning 110
231 switch(ao_usb_setup.request) {
232 case AO_USB_REQ_GET_STATUS:
233 ao_usb_ep0_queue_byte(0);
234 ao_usb_ep0_queue_byte(0);
236 case AO_USB_REQ_GET_INTERFACE:
237 ao_usb_ep0_queue_byte(0);
239 case AO_USB_REQ_SET_INTERFACE:
243 case AO_USB_RECIP_ENDPOINT:
244 switch(ao_usb_setup.request) {
245 case AO_USB_REQ_GET_STATUS:
246 ao_usb_ep0_queue_byte(0);
247 ao_usb_ep0_queue_byte(0);
253 case AO_USB_TYPE_CLASS:
254 switch (ao_usb_setup.request) {
255 case SET_LINE_CODING:
256 ao_usb_ep0_out_len = 7;
257 ao_usb_ep0_out_data = (__xdata uint8_t *) &ao_usb_line_coding;
259 case GET_LINE_CODING:
260 ao_usb_ep0_in_len = 7;
261 ao_usb_ep0_in_data = (uint8_t *) &ao_usb_line_coding;
263 case SET_CONTROL_LINE_STATE:
268 if (ao_usb_ep0_state != AO_USB_EP0_DATA_OUT) {
269 if (ao_usb_setup.length < ao_usb_ep0_in_len)
270 ao_usb_ep0_in_len = ao_usb_setup.length;
275 /* End point 0 receives all of the control messages. */
281 ao_usb_ep0_state = AO_USB_EP0_IDLE;
283 __critical for (;;) {
284 if (ao_usb_iif & 1) {
288 ao_sleep(&ao_usb_task);
292 if (cs0 & USBCS0_SETUP_END) {
293 ao_usb_ep0_state = AO_USB_EP0_IDLE;
294 USBCS0 = USBCS0_CLR_SETUP_END;
296 if (cs0 & USBCS0_SENT_STALL) {
297 ao_usb_ep0_state = AO_USB_EP0_IDLE;
298 USBCS0 &= ~USBCS0_SENT_STALL;
300 if (ao_usb_ep0_state == AO_USB_EP0_DATA_IN &&
301 (cs0 & USBCS0_INPKT_RDY) == 0)
305 if (cs0 & USBCS0_OUTPKT_RDY) {
306 switch (ao_usb_ep0_state) {
307 case AO_USB_EP0_IDLE:
310 case AO_USB_EP0_DATA_OUT:
312 if (ao_usb_ep0_out_len == 0)
313 ao_usb_ep0_state = AO_USB_EP0_IDLE;
315 if (ao_usb_ep0_state == AO_USB_EP0_IDLE)
316 USBCS0 = USBCS0_CLR_OUTPKT_RDY | USBCS0_DATA_END;
318 USBCS0 = USBCS0_CLR_OUTPKT_RDY;
325 /* Wait for a free IN buffer */
330 USBINDEX = AO_USB_IN_EP;
331 if ((USBCSIL & USBCSIL_INPKT_RDY) == 0)
333 ao_sleep(&ao_usb_in_bytes);
337 /* Send the current IN packet */
341 USBINDEX = AO_USB_IN_EP;
342 USBCSIL |= USBCSIL_INPKT_RDY;
343 ao_usb_in_bytes_last = ao_usb_in_bytes;
348 ao_usb_flush(void) __critical
353 /* If there are pending bytes, or if the last packet was full,
354 * send another IN packet
356 if (ao_usb_in_bytes || (ao_usb_in_bytes_last == AO_USB_IN_SIZE)) {
363 ao_usb_putchar(char c) __critical __reentrant
370 /* Queue a byte, sending the packet when full */
371 USBFIFO[AO_USB_IN_EP << 1] = c;
372 if (++ao_usb_in_bytes == AO_USB_IN_SIZE)
377 ao_usb_pollchar(void) __critical
380 if (ao_usb_out_bytes == 0) {
381 USBINDEX = AO_USB_OUT_EP;
382 if ((USBCSOL & USBCSOL_OUTPKT_RDY) == 0)
383 return AO_READ_AGAIN;
384 ao_usb_out_bytes = (USBCNTH << 8) | USBCNTL;
385 if (ao_usb_out_bytes == 0)
386 return AO_READ_AGAIN;
389 c = USBFIFO[AO_USB_OUT_EP << 1];
390 if (ao_usb_out_bytes == 0) {
391 USBINDEX = AO_USB_OUT_EP;
392 USBCSOL &= ~USBCSOL_OUTPKT_RDY;
402 while ((c = ao_usb_pollchar()) == AO_READ_AGAIN)
403 ao_sleep(&ao_stdin_ready);
410 /* Turn on the USB controller */
411 SLEEP |= SLEEP_USB_EN;
413 ao_usb_set_configuration();
415 ao_usb_set_interrupts();
417 /* enable USB interrupts */
420 /* Clear any pending interrupts */
429 /* Disable USB interrupts */
435 /* Clear any pending interrupts */
440 /* Turn off the USB controller */
441 SLEEP &= ~SLEEP_USB_EN;
449 ao_add_task(&ao_usb_task, ao_usb_ep0, "usb");
450 ao_add_stdio(ao_usb_pollchar, ao_usb_putchar, ao_usb_flush);