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 __pdata uint16_t ao_usb_in_bytes_last;
25 static __xdata uint16_t ao_usb_out_bytes;
26 static __pdata uint8_t ao_usb_iif;
27 static __pdata 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();
71 uint8_t dir_type_recip;
76 } __xdata ao_usb_setup;
78 __pdata uint8_t ao_usb_ep0_state;
79 uint8_t * __pdata ao_usb_ep0_in_data;
80 __pdata uint8_t ao_usb_ep0_in_len;
81 __pdata uint8_t ao_usb_ep0_in_buf[2];
82 __pdata uint8_t ao_usb_ep0_out_len;
83 __xdata uint8_t *__pdata ao_usb_ep0_out_data;
84 __pdata uint8_t ao_usb_configuration;
86 /* Send an IN data packet */
88 ao_usb_ep0_flush(void)
90 __pdata uint8_t this_len;
93 /* If the IN packet hasn't been picked up, just return */
96 if (cs0 & USBCS0_INPKT_RDY)
99 this_len = ao_usb_ep0_in_len;
100 if (this_len > AO_USB_CONTROL_SIZE)
101 this_len = AO_USB_CONTROL_SIZE;
102 cs0 = USBCS0_INPKT_RDY;
103 if (this_len != AO_USB_CONTROL_SIZE) {
104 cs0 = USBCS0_INPKT_RDY | USBCS0_DATA_END;
105 ao_usb_ep0_state = AO_USB_EP0_IDLE;
107 ao_usb_ep0_in_len -= this_len;
109 USBFIFO[0] = *ao_usb_ep0_in_data++;
114 __xdata static struct ao_usb_line_coding ao_usb_line_coding = {115200, 0, 0, 8};
116 /* Walk through the list of descriptors and find a match
119 ao_usb_get_descriptor(uint16_t value)
121 __code uint8_t *__pdata descriptor;
122 __pdata uint8_t type = value >> 8;
123 __pdata uint8_t index = value;
125 descriptor = ao_usb_descriptors;
126 while (descriptor[0] != 0) {
127 if (descriptor[1] == type && index-- == 0) {
128 if (type == AO_USB_DESC_CONFIGURATION)
129 ao_usb_ep0_in_len = descriptor[2];
131 ao_usb_ep0_in_len = descriptor[0];
132 ao_usb_ep0_in_data = descriptor;
135 descriptor += descriptor[0];
139 /* Read data from the ep0 OUT fifo
142 ao_usb_ep0_fill(void)
148 if (len > ao_usb_ep0_out_len)
149 len = ao_usb_ep0_out_len;
150 ao_usb_ep0_out_len -= len;
152 *ao_usb_ep0_out_data++ = USBFIFO[0];
156 ao_usb_ep0_queue_byte(uint8_t a)
158 ao_usb_ep0_in_buf[ao_usb_ep0_in_len++] = a;
162 ao_usb_set_address(uint8_t address)
165 USBADDR = address | 0x80;
166 while (USBADDR & 0x80)
171 ao_usb_set_configuration(void)
173 /* Set the IN max packet size, double buffered */
174 USBINDEX = AO_USB_IN_EP;
175 USBMAXI = AO_USB_IN_SIZE >> 3;
176 USBCSIH |= USBCSIH_IN_DBL_BUF;
178 /* Set the OUT max packet size, double buffered */
179 USBINDEX = AO_USB_OUT_EP;
180 USBMAXO = AO_USB_OUT_SIZE >> 3;
181 USBCSOH = USBCSOH_OUT_DBL_BUF;
185 ao_usb_ep0_setup(void)
187 /* Pull the setup packet out of the fifo */
188 ao_usb_ep0_out_data = (__xdata uint8_t *) &ao_usb_setup;
189 ao_usb_ep0_out_len = 8;
191 if (ao_usb_ep0_out_len != 0)
194 /* Figure out how to ACK the setup packet */
195 if (ao_usb_setup.dir_type_recip & AO_USB_DIR_IN) {
196 if (ao_usb_setup.length)
197 ao_usb_ep0_state = AO_USB_EP0_DATA_IN;
199 ao_usb_ep0_state = AO_USB_EP0_IDLE;
201 if (ao_usb_setup.length)
202 ao_usb_ep0_state = AO_USB_EP0_DATA_OUT;
204 ao_usb_ep0_state = AO_USB_EP0_IDLE;
207 if (ao_usb_ep0_state == AO_USB_EP0_IDLE)
208 USBCS0 = USBCS0_CLR_OUTPKT_RDY | USBCS0_DATA_END;
210 USBCS0 = USBCS0_CLR_OUTPKT_RDY;
212 ao_usb_ep0_in_data = ao_usb_ep0_in_buf;
213 ao_usb_ep0_in_len = 0;
214 switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_TYPE_MASK) {
215 case AO_USB_TYPE_STANDARD:
216 switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_RECIP_MASK) {
217 case AO_USB_RECIP_DEVICE:
218 switch(ao_usb_setup.request) {
219 case AO_USB_REQ_GET_STATUS:
220 ao_usb_ep0_queue_byte(0);
221 ao_usb_ep0_queue_byte(0);
223 case AO_USB_REQ_SET_ADDRESS:
224 ao_usb_set_address(ao_usb_setup.value);
226 case AO_USB_REQ_GET_DESCRIPTOR:
227 ao_usb_get_descriptor(ao_usb_setup.value);
229 case AO_USB_REQ_GET_CONFIGURATION:
230 ao_usb_ep0_queue_byte(ao_usb_configuration);
232 case AO_USB_REQ_SET_CONFIGURATION:
233 ao_usb_configuration = ao_usb_setup.value;
234 ao_usb_set_configuration();
238 case AO_USB_RECIP_INTERFACE:
239 #pragma disable_warning 110
240 switch(ao_usb_setup.request) {
241 case AO_USB_REQ_GET_STATUS:
242 ao_usb_ep0_queue_byte(0);
243 ao_usb_ep0_queue_byte(0);
245 case AO_USB_REQ_GET_INTERFACE:
246 ao_usb_ep0_queue_byte(0);
248 case AO_USB_REQ_SET_INTERFACE:
252 case AO_USB_RECIP_ENDPOINT:
253 switch(ao_usb_setup.request) {
254 case AO_USB_REQ_GET_STATUS:
255 ao_usb_ep0_queue_byte(0);
256 ao_usb_ep0_queue_byte(0);
262 case AO_USB_TYPE_CLASS:
263 switch (ao_usb_setup.request) {
264 case AO_USB_SET_LINE_CODING:
265 ao_usb_ep0_out_len = 7;
266 ao_usb_ep0_out_data = (__xdata uint8_t *) &ao_usb_line_coding;
268 case AO_USB_GET_LINE_CODING:
269 ao_usb_ep0_in_len = 7;
270 ao_usb_ep0_in_data = (uint8_t *) &ao_usb_line_coding;
272 case AO_USB_SET_CONTROL_LINE_STATE:
277 if (ao_usb_ep0_state != AO_USB_EP0_DATA_OUT) {
278 if (ao_usb_setup.length < ao_usb_ep0_in_len)
279 ao_usb_ep0_in_len = ao_usb_setup.length;
284 /* End point 0 receives all of the control messages. */
290 ao_usb_ep0_state = AO_USB_EP0_IDLE;
292 __critical for (;;) {
293 if (ao_usb_iif & 1) {
297 ao_sleep(&ao_usb_task);
301 if (cs0 & USBCS0_SETUP_END) {
302 ao_usb_ep0_state = AO_USB_EP0_IDLE;
303 USBCS0 = USBCS0_CLR_SETUP_END;
305 if (cs0 & USBCS0_SENT_STALL) {
306 ao_usb_ep0_state = AO_USB_EP0_IDLE;
307 USBCS0 &= ~USBCS0_SENT_STALL;
309 if (ao_usb_ep0_state == AO_USB_EP0_DATA_IN &&
310 (cs0 & USBCS0_INPKT_RDY) == 0)
314 if (cs0 & USBCS0_OUTPKT_RDY) {
315 switch (ao_usb_ep0_state) {
316 case AO_USB_EP0_IDLE:
319 case AO_USB_EP0_DATA_OUT:
321 if (ao_usb_ep0_out_len == 0)
322 ao_usb_ep0_state = AO_USB_EP0_IDLE;
324 if (ao_usb_ep0_state == AO_USB_EP0_IDLE)
325 USBCS0 = USBCS0_CLR_OUTPKT_RDY | USBCS0_DATA_END;
327 USBCS0 = USBCS0_CLR_OUTPKT_RDY;
334 /* Wait for a free IN buffer */
339 USBINDEX = AO_USB_IN_EP;
340 if ((USBCSIL & USBCSIL_INPKT_RDY) == 0)
342 ao_sleep(&ao_usb_in_bytes);
346 /* Send the current IN packet */
350 USBINDEX = AO_USB_IN_EP;
351 USBCSIL |= USBCSIL_INPKT_RDY;
352 ao_usb_in_bytes_last = ao_usb_in_bytes;
357 ao_usb_flush(void) __critical
362 /* If there are pending bytes, or if the last packet was full,
363 * send another IN packet
365 if (ao_usb_in_bytes || (ao_usb_in_bytes_last == AO_USB_IN_SIZE)) {
372 ao_usb_putchar(char c) __critical __reentrant
379 /* Queue a byte, sending the packet when full */
380 USBFIFO[AO_USB_IN_EP << 1] = c;
381 if (++ao_usb_in_bytes == AO_USB_IN_SIZE)
386 ao_usb_pollchar(void) __critical
389 if (ao_usb_out_bytes == 0) {
390 USBINDEX = AO_USB_OUT_EP;
391 if ((USBCSOL & USBCSOL_OUTPKT_RDY) == 0)
392 return AO_READ_AGAIN;
393 ao_usb_out_bytes = (USBCNTH << 8) | USBCNTL;
394 if (ao_usb_out_bytes == 0) {
395 USBINDEX = AO_USB_OUT_EP;
396 USBCSOL &= ~USBCSOL_OUTPKT_RDY;
397 return AO_READ_AGAIN;
401 c = USBFIFO[AO_USB_OUT_EP << 1];
402 if (ao_usb_out_bytes == 0) {
403 USBINDEX = AO_USB_OUT_EP;
404 USBCSOL &= ~USBCSOL_OUTPKT_RDY;
410 ao_usb_getchar(void) __critical
414 while ((c = ao_usb_pollchar()) == AO_READ_AGAIN)
415 ao_sleep(&ao_stdin_ready);
422 /* Turn on the USB controller */
423 SLEEP |= SLEEP_USB_EN;
425 ao_usb_set_configuration();
427 ao_usb_set_interrupts();
429 /* enable USB interrupts */
432 /* Clear any pending interrupts */
441 /* Disable USB interrupts */
447 /* Clear any pending interrupts */
452 /* Turn off the USB controller */
453 SLEEP &= ~SLEEP_USB_EN;
461 ao_add_task(&ao_usb_task, ao_usb_ep0, "usb");
462 ao_add_stdio(ao_usb_pollchar, ao_usb_putchar, ao_usb_flush);