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 __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)
169 ao_usb_set_configuration(void)
171 /* Set the IN max packet size, double buffered */
172 USBINDEX = AO_USB_IN_EP;
173 USBMAXI = AO_USB_IN_SIZE >> 3;
174 USBCSIH |= USBCSIH_IN_DBL_BUF;
176 /* Set the OUT max packet size, double buffered */
177 USBINDEX = AO_USB_OUT_EP;
178 USBMAXO = AO_USB_OUT_SIZE >> 3;
179 USBCSOH = USBCSOH_OUT_DBL_BUF;
183 ao_usb_ep0_setup(void)
185 /* Pull the setup packet out of the fifo */
186 ao_usb_ep0_out_data = (__xdata uint8_t *) &ao_usb_setup;
187 ao_usb_ep0_out_len = 8;
189 if (ao_usb_ep0_out_len != 0)
192 ao_usb_ep0_in_data = ao_usb_ep0_in_buf;
193 ao_usb_ep0_in_len = 0;
194 switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_TYPE_MASK) {
195 case AO_USB_TYPE_STANDARD:
196 switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_RECIP_MASK) {
197 case AO_USB_RECIP_DEVICE:
198 switch(ao_usb_setup.request) {
199 case AO_USB_REQ_GET_STATUS:
200 ao_usb_ep0_queue_byte(0);
201 ao_usb_ep0_queue_byte(0);
203 case AO_USB_REQ_SET_ADDRESS:
204 #if USB_FORCE_FLIGHT_IDLE
205 /* Go to idle mode if USB is connected
207 ao_flight_force_idle = 1;
209 ao_usb_set_address(ao_usb_setup.value);
211 case AO_USB_REQ_GET_DESCRIPTOR:
212 ao_usb_get_descriptor(ao_usb_setup.value);
214 case AO_USB_REQ_GET_CONFIGURATION:
215 ao_usb_ep0_queue_byte(ao_usb_configuration);
217 case AO_USB_REQ_SET_CONFIGURATION:
218 ao_usb_configuration = ao_usb_setup.value;
219 ao_usb_set_configuration();
223 case AO_USB_RECIP_INTERFACE:
224 #pragma disable_warning 110
225 switch(ao_usb_setup.request) {
226 case AO_USB_REQ_GET_STATUS:
227 ao_usb_ep0_queue_byte(0);
228 ao_usb_ep0_queue_byte(0);
230 case AO_USB_REQ_GET_INTERFACE:
231 ao_usb_ep0_queue_byte(0);
233 case AO_USB_REQ_SET_INTERFACE:
237 case AO_USB_RECIP_ENDPOINT:
238 switch(ao_usb_setup.request) {
239 case AO_USB_REQ_GET_STATUS:
240 ao_usb_ep0_queue_byte(0);
241 ao_usb_ep0_queue_byte(0);
247 case AO_USB_TYPE_CLASS:
248 switch (ao_usb_setup.request) {
249 case AO_USB_SET_LINE_CODING:
250 ao_usb_ep0_out_len = 7;
251 ao_usb_ep0_out_data = (__xdata uint8_t *) &ao_usb_line_coding;
253 case AO_USB_GET_LINE_CODING:
254 ao_usb_ep0_in_len = 7;
255 ao_usb_ep0_in_data = (uint8_t *) &ao_usb_line_coding;
257 case AO_USB_SET_CONTROL_LINE_STATE:
263 /* Figure out how to ACK the setup packet and the
267 if (ao_usb_ep0_in_len) {
269 /* Sending data back to the host
271 ao_usb_ep0_state = AO_USB_EP0_DATA_IN;
272 USBCS0 = USBCS0_CLR_OUTPKT_RDY;
273 if (ao_usb_setup.length < ao_usb_ep0_in_len)
274 ao_usb_ep0_in_len = ao_usb_setup.length;
276 } else if (ao_usb_ep0_out_len) {
278 /* Receiving data from the host
280 ao_usb_ep0_state = AO_USB_EP0_DATA_OUT;
281 USBCS0 = USBCS0_CLR_OUTPKT_RDY;
282 } else if (ao_usb_setup.length) {
284 /* Uh-oh, the host expected to send or receive data
285 * and we don't know what to do.
287 ao_usb_ep0_state = AO_USB_EP0_STALL;
288 USBCS0 = USBCS0_CLR_OUTPKT_RDY | USBCS0_SEND_STALL;
291 /* Simple setup packet with no data
293 ao_usb_ep0_state = AO_USB_EP0_IDLE;
294 USBCS0 = USBCS0_CLR_OUTPKT_RDY | USBCS0_DATA_END;
298 /* End point 0 receives all of the control messages. */
304 ao_usb_ep0_state = AO_USB_EP0_IDLE;
306 __critical for (;;) {
307 if (ao_usb_iif & 1) {
311 ao_sleep(&ao_usb_task);
315 if (cs0 & USBCS0_SETUP_END) {
316 USBCS0 = USBCS0_CLR_SETUP_END;
317 ao_usb_ep0_state = AO_USB_EP0_IDLE;
319 if (cs0 & USBCS0_SENT_STALL) {
321 ao_usb_ep0_state = AO_USB_EP0_IDLE;
323 if (ao_usb_ep0_state == AO_USB_EP0_DATA_IN &&
324 (cs0 & USBCS0_INPKT_RDY) == 0)
328 if (cs0 & USBCS0_OUTPKT_RDY) {
329 switch (ao_usb_ep0_state) {
330 case AO_USB_EP0_IDLE:
333 case AO_USB_EP0_DATA_OUT:
336 if (ao_usb_ep0_out_len == 0) {
337 ao_usb_ep0_state = AO_USB_EP0_IDLE;
338 USBCS0 = USBCS0_CLR_OUTPKT_RDY | USBCS0_DATA_END;
340 USBCS0 = USBCS0_CLR_OUTPKT_RDY;
347 /* Wait for a free IN buffer */
352 USBINDEX = AO_USB_IN_EP;
353 if ((USBCSIL & USBCSIL_INPKT_RDY) == 0)
355 ao_sleep(&ao_usb_in_bytes);
359 /* Send the current IN packet */
363 USBINDEX = AO_USB_IN_EP;
364 USBCSIL |= USBCSIL_INPKT_RDY;
365 ao_usb_in_bytes_last = ao_usb_in_bytes;
370 ao_usb_flush(void) __critical
375 /* If there are pending bytes, or if the last packet was full,
376 * send another IN packet
378 if (ao_usb_in_bytes || (ao_usb_in_bytes_last == AO_USB_IN_SIZE)) {
385 ao_usb_putchar(char c) __critical __reentrant
392 /* Queue a byte, sending the packet when full */
393 USBFIFO[AO_USB_IN_EP << 1] = c;
394 if (++ao_usb_in_bytes == AO_USB_IN_SIZE)
399 _ao_usb_pollchar(void)
402 if (ao_usb_out_bytes == 0) {
403 USBINDEX = AO_USB_OUT_EP;
404 if ((USBCSOL & USBCSOL_OUTPKT_RDY) == 0)
405 return AO_READ_AGAIN;
406 ao_usb_out_bytes = (USBCNTH << 8) | USBCNTL;
407 if (ao_usb_out_bytes == 0) {
408 USBINDEX = AO_USB_OUT_EP;
409 USBCSOL &= ~USBCSOL_OUTPKT_RDY;
410 return AO_READ_AGAIN;
414 c = USBFIFO[AO_USB_OUT_EP << 1];
415 if (ao_usb_out_bytes == 0) {
416 USBINDEX = AO_USB_OUT_EP;
417 USBCSOL &= ~USBCSOL_OUTPKT_RDY;
427 ao_arch_block_interrupts();
428 while ((c = _ao_usb_pollchar()) == AO_READ_AGAIN)
429 ao_sleep(&ao_stdin_ready);
430 ao_arch_release_interrupts();
437 /* Turn on the USB controller */
438 SLEEP |= SLEEP_USB_EN;
440 ao_usb_set_configuration();
442 ao_usb_set_interrupts();
444 /* enable USB interrupts */
447 /* Clear any pending interrupts */
452 ao_gpio_set(AO_USB_PULLUP_PORT, AO_USB_PULLUP_PIN, AO_USB_PULLUP, 0);
459 /* Disable USB interrupts */
466 ao_gpio_set(AO_USB_PULLUP_PORT, AO_USB_PULLUP_PIN, AO_USB_PULLUP, 0);
469 /* Clear any pending interrupts */
474 /* Turn off the USB controller */
475 SLEEP &= ~SLEEP_USB_EN;
482 ao_enable_output(AO_USB_PULLUP_PORT, AO_USB_PULLUP_PIN, AO_USB_PULLUP, 0);
486 ao_add_task(&ao_usb_task, ao_usb_ep0, "usb");
487 ao_add_stdio(_ao_usb_pollchar, ao_usb_putchar, ao_usb_flush);