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();
67 uint8_t dir_type_recip;
72 } __xdata ao_usb_setup;
74 __xdata uint8_t ao_usb_ep0_state;
75 uint8_t * __xdata ao_usb_ep0_in_data;
76 __xdata uint8_t ao_usb_ep0_in_len;
77 __xdata uint8_t ao_usb_ep0_in_buf[2];
78 __xdata uint8_t ao_usb_ep0_out_len;
79 __xdata uint8_t *__xdata ao_usb_ep0_out_data;
80 __xdata uint8_t ao_usb_configuration;
82 /* Send an IN data packet */
84 ao_usb_ep0_flush(void)
86 __xdata uint8_t this_len;
89 /* If the IN packet hasn't been picked up, just return */
92 if (cs0 & USBCS0_INPKT_RDY)
95 this_len = ao_usb_ep0_in_len;
96 if (this_len > AO_USB_CONTROL_SIZE)
97 this_len = AO_USB_CONTROL_SIZE;
98 cs0 = USBCS0_INPKT_RDY;
99 if (this_len != AO_USB_CONTROL_SIZE) {
100 cs0 = USBCS0_INPKT_RDY | USBCS0_DATA_END;
101 ao_usb_ep0_state = AO_USB_EP0_IDLE;
103 ao_usb_ep0_in_len -= this_len;
105 USBFIFO[0] = *ao_usb_ep0_in_data++;
110 __xdata static struct ao_usb_line_coding ao_usb_line_coding = {115200, 0, 0, 8};
112 /* Walk through the list of descriptors and find a match
115 ao_usb_get_descriptor(uint16_t value)
117 const uint8_t *__xdata descriptor;
118 __xdata uint8_t type = value >> 8;
119 __xdata uint8_t index = value;
121 descriptor = ao_usb_descriptors;
122 while (descriptor[0] != 0) {
123 if (descriptor[1] == type && index-- == 0) {
124 if (type == AO_USB_DESC_CONFIGURATION)
125 ao_usb_ep0_in_len = descriptor[2];
127 ao_usb_ep0_in_len = descriptor[0];
128 ao_usb_ep0_in_data = descriptor;
131 descriptor += descriptor[0];
135 /* Read data from the ep0 OUT fifo
138 ao_usb_ep0_fill(void)
144 if (len > ao_usb_ep0_out_len)
145 len = ao_usb_ep0_out_len;
146 ao_usb_ep0_out_len -= len;
148 *ao_usb_ep0_out_data++ = USBFIFO[0];
152 ao_usb_ep0_queue_byte(uint8_t a)
154 ao_usb_ep0_in_buf[ao_usb_ep0_in_len++] = a;
158 ao_usb_set_address(uint8_t address)
161 USBADDR = address | 0x80;
162 while (USBADDR & 0x80)
167 ao_usb_set_configuration(void)
169 /* Set the IN max packet size, double buffered */
170 USBINDEX = AO_USB_IN_EP;
171 USBMAXI = AO_USB_IN_SIZE >> 3;
172 USBCSIH |= USBCSIH_IN_DBL_BUF;
174 /* Set the OUT max packet size, double buffered */
175 USBINDEX = AO_USB_OUT_EP;
176 USBMAXO = AO_USB_OUT_SIZE >> 3;
177 USBCSOH = USBCSOH_OUT_DBL_BUF;
181 ao_usb_ep0_setup(void)
183 /* Pull the setup packet out of the fifo */
184 ao_usb_ep0_out_data = (__xdata uint8_t *) &ao_usb_setup;
185 ao_usb_ep0_out_len = 8;
187 if (ao_usb_ep0_out_len != 0)
190 /* Figure out how to ACK the setup packet */
191 if (ao_usb_setup.dir_type_recip & AO_USB_DIR_IN) {
192 if (ao_usb_setup.length)
193 ao_usb_ep0_state = AO_USB_EP0_DATA_IN;
195 ao_usb_ep0_state = AO_USB_EP0_IDLE;
197 if (ao_usb_setup.length)
198 ao_usb_ep0_state = AO_USB_EP0_DATA_OUT;
200 ao_usb_ep0_state = AO_USB_EP0_IDLE;
203 if (ao_usb_ep0_state == AO_USB_EP0_IDLE)
204 USBCS0 = USBCS0_CLR_OUTPKT_RDY | USBCS0_DATA_END;
206 USBCS0 = USBCS0_CLR_OUTPKT_RDY;
208 ao_usb_ep0_in_data = ao_usb_ep0_in_buf;
209 ao_usb_ep0_in_len = 0;
210 switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_TYPE_MASK) {
211 case AO_USB_TYPE_STANDARD:
212 switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_RECIP_MASK) {
213 case AO_USB_RECIP_DEVICE:
214 switch(ao_usb_setup.request) {
215 case AO_USB_REQ_GET_STATUS:
216 ao_usb_ep0_queue_byte(0);
217 ao_usb_ep0_queue_byte(0);
219 case AO_USB_REQ_SET_ADDRESS:
220 ao_usb_set_address(ao_usb_setup.value);
222 case AO_USB_REQ_GET_DESCRIPTOR:
223 ao_usb_get_descriptor(ao_usb_setup.value);
225 case AO_USB_REQ_GET_CONFIGURATION:
226 ao_usb_ep0_queue_byte(ao_usb_configuration);
228 case AO_USB_REQ_SET_CONFIGURATION:
229 ao_usb_configuration = ao_usb_setup.value;
230 ao_usb_set_configuration();
234 case AO_USB_RECIP_INTERFACE:
235 #pragma disable_warning 110
236 switch(ao_usb_setup.request) {
237 case AO_USB_REQ_GET_STATUS:
238 ao_usb_ep0_queue_byte(0);
239 ao_usb_ep0_queue_byte(0);
241 case AO_USB_REQ_GET_INTERFACE:
242 ao_usb_ep0_queue_byte(0);
244 case AO_USB_REQ_SET_INTERFACE:
248 case AO_USB_RECIP_ENDPOINT:
249 switch(ao_usb_setup.request) {
250 case AO_USB_REQ_GET_STATUS:
251 ao_usb_ep0_queue_byte(0);
252 ao_usb_ep0_queue_byte(0);
258 case AO_USB_TYPE_CLASS:
259 switch (ao_usb_setup.request) {
260 case SET_LINE_CODING:
261 ao_usb_ep0_out_len = 7;
262 ao_usb_ep0_out_data = (__xdata uint8_t *) &ao_usb_line_coding;
264 case GET_LINE_CODING:
265 ao_usb_ep0_in_len = 7;
266 ao_usb_ep0_in_data = (uint8_t *) &ao_usb_line_coding;
268 case SET_CONTROL_LINE_STATE:
273 if (ao_usb_ep0_state != AO_USB_EP0_DATA_OUT) {
274 if (ao_usb_setup.length < ao_usb_ep0_in_len)
275 ao_usb_ep0_in_len = ao_usb_setup.length;
280 /* End point 0 receives all of the control messages. */
286 ao_usb_ep0_state = AO_USB_EP0_IDLE;
288 __critical for (;;) {
289 if (ao_usb_iif & 1) {
293 ao_sleep(&ao_usb_task);
297 if (cs0 & USBCS0_SETUP_END) {
298 ao_usb_ep0_state = AO_USB_EP0_IDLE;
299 USBCS0 = USBCS0_CLR_SETUP_END;
301 if (cs0 & USBCS0_SENT_STALL) {
302 ao_usb_ep0_state = AO_USB_EP0_IDLE;
303 USBCS0 &= ~USBCS0_SENT_STALL;
305 if (ao_usb_ep0_state == AO_USB_EP0_DATA_IN &&
306 (cs0 & USBCS0_INPKT_RDY) == 0)
310 if (cs0 & USBCS0_OUTPKT_RDY) {
311 switch (ao_usb_ep0_state) {
312 case AO_USB_EP0_IDLE:
315 case AO_USB_EP0_DATA_OUT:
317 if (ao_usb_ep0_out_len == 0)
318 ao_usb_ep0_state = AO_USB_EP0_IDLE;
320 if (ao_usb_ep0_state == AO_USB_EP0_IDLE)
321 USBCS0 = USBCS0_CLR_OUTPKT_RDY | USBCS0_DATA_END;
323 USBCS0 = USBCS0_CLR_OUTPKT_RDY;
330 /* Wait for a free IN buffer */
335 USBINDEX = AO_USB_IN_EP;
336 if ((USBCSIL & USBCSIL_INPKT_RDY) == 0)
338 ao_sleep(&ao_usb_in_bytes);
342 /* Send the current IN packet */
346 USBINDEX = AO_USB_IN_EP;
347 USBCSIL |= USBCSIL_INPKT_RDY;
348 ao_usb_in_bytes_last = ao_usb_in_bytes;
353 ao_usb_flush(void) __critical
358 /* If there are pending bytes, or if the last packet was full,
359 * send another IN packet
361 if (ao_usb_in_bytes || (ao_usb_in_bytes_last == AO_USB_IN_SIZE)) {
368 ao_usb_putchar(char c) __critical __reentrant
375 /* Queue a byte, sending the packet when full */
376 USBFIFO[AO_USB_IN_EP << 1] = c;
377 if (++ao_usb_in_bytes == AO_USB_IN_SIZE)
382 ao_usb_pollchar(void) __critical
385 if (ao_usb_out_bytes == 0) {
386 USBINDEX = AO_USB_OUT_EP;
387 if ((USBCSOL & USBCSOL_OUTPKT_RDY) == 0)
388 return AO_READ_AGAIN;
389 ao_usb_out_bytes = (USBCNTH << 8) | USBCNTL;
390 if (ao_usb_out_bytes == 0) {
391 USBINDEX = AO_USB_OUT_EP;
392 USBCSOL &= ~USBCSOL_OUTPKT_RDY;
393 return AO_READ_AGAIN;
397 c = USBFIFO[AO_USB_OUT_EP << 1];
398 if (ao_usb_out_bytes == 0) {
399 USBINDEX = AO_USB_OUT_EP;
400 USBCSOL &= ~USBCSOL_OUTPKT_RDY;
406 ao_usb_getchar(void) __critical
410 while ((c = ao_usb_pollchar()) == AO_READ_AGAIN)
411 ao_sleep(&ao_stdin_ready);
418 /* Turn on the USB controller */
419 SLEEP |= SLEEP_USB_EN;
421 ao_usb_set_configuration();
423 ao_usb_set_interrupts();
425 /* enable USB interrupts */
428 /* Clear any pending interrupts */
437 /* Disable USB interrupts */
443 /* Clear any pending interrupts */
448 /* Turn off the USB controller */
449 SLEEP &= ~SLEEP_USB_EN;
457 ao_add_task(&ao_usb_task, ao_usb_ep0, "usb");
458 ao_add_stdio(ao_usb_pollchar, ao_usb_putchar, ao_usb_flush);