2 * Copyright © 2011 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.
20 struct ao_task __xdata ao_usb_task;
22 static __xdata uint16_t ao_usb_in_bytes;
23 static __xdata uint16_t ao_usb_in_bytes_last;
24 static __xdata uint16_t ao_usb_out_bytes;
25 static __xdata uint8_t ao_usb_iif;
26 static __xdata uint8_t ao_usb_running;
29 ao_usb_set_interrupts(void)
31 /* IN interrupts on the control an IN endpoints */
32 USBIIE = (1 << AO_USB_CONTROL_EP) | (1 << AO_USB_IN_EP);
34 /* OUT interrupts on the OUT endpoint */
35 USBOIE = (1 << AO_USB_OUT_EP);
37 /* Only care about reset */
38 USBCIE = USBCIE_RSTIE;
42 ao_usb_set_address(uint8_t address)
44 UDADD = (0 << ADDEN) | address;
48 #define EP_SIZE(s) ((s) == 64 ? 0x30 : \
56 /* Set the CONTROL max packet size, single buffered */
58 UECONX = (1 << EPEN); /* Enable */
60 UECFG0X = ((0 << EPTYPE0) | /* Control */
61 (0 << EPDIR)); /* Out (ish) */
63 UECFG1X = ((EP_SIZE(AO_USB_CONTROL_SIZE) << EPSIZE0) | /* Size */
64 (0 << EPBK)); /* Single bank */
66 UEIENX = ((1 << RXSTPE) | /* Enable SETUP interrupt */
67 (1 << RXOUTE) | /* Enable OUT interrupt */
68 (1 << TXINE)); /* Enable IN complete interrupt */
72 ao_usb_set_configuration(void)
74 /* Set the IN max packet size, double buffered */
76 UECONX = (1 << EPEN); /* Enable */
78 UECFG0X = ((2 << EPTYPE0) | /* Bulk */
79 (1 << EPDIR)); /* In */
81 UECFG1X = ((EP_SIZE(AO_USB_IN_SIZE) << EPSIZE0) | /* Size */
82 (1 << EPBK) | /* Double bank */
83 (1 << ALLOC)); /* Allocate */
85 /* Set the OUT max packet size, double buffered */
86 UENUM = AO_USB_OUT_EP;
87 UECONX |= (1 << EPEN); /* Enable */
89 UECFG0X = ((2 << EPTYPE0) | /* Bulk */
90 (0 << EPDIR)); /* Out */
92 UECFG1X = ((EP_SIZE(AO_USB_OUT_SIZE) << EPSIZE0) | /* Size */
93 (1 << EPBK) | /* Double bank */
94 (1 << ALLOC)); /* Allocate */
103 if (intbits & (1<<EORSTI)) {
105 usb_configuration = 0;
107 if (intbits & (1<<SOFI)) {
108 if (usb_configuration) {
109 t = transmit_flush_timer;
111 transmit_flush_timer = --t;
113 UENUM = CDC_TX_ENDPOINT;
121 struct ao_usb_setup {
122 uint8_t dir_type_recip;
127 } __xdata ao_usb_setup;
129 __xdata uint8_t ao_usb_ep0_state;
130 uint8_t * __xdata ao_usb_ep0_in_data;
131 __xdata uint8_t ao_usb_ep0_in_len;
132 __xdata uint8_t ao_usb_ep0_in_buf[2];
133 __xdata uint8_t ao_usb_ep0_out_len;
134 __xdata uint8_t *__xdata ao_usb_ep0_out_data;
135 __xdata uint8_t ao_usb_configuration;
137 /* Send an IN data packet */
139 ao_usb_ep0_flush(void)
141 __xdata uint8_t this_len;
144 /* If the IN packet hasn't been picked up, just return */
146 if (!(UEINTX & (1 << TXINI)))
149 this_len = ao_usb_ep0_in_len;
150 if (this_len > AO_USB_CONTROL_SIZE)
151 this_len = AO_USB_CONTROL_SIZE;
153 ao_usb_ep0_in_len -= this_len;
155 UEDATX = *ao_usb_ep0_in_data++;
157 /* Clear the TXINI bit to send the packet */
158 UEINTX &= ~(1 << TXINI);
161 __xdata static struct ao_usb_line_coding ao_usb_line_coding = {115200, 0, 0, 8};
163 /* Walk through the list of descriptors and find a match
166 ao_usb_get_descriptor(uint16_t value)
168 const uint8_t *__xdata descriptor;
169 __xdata uint8_t type = value >> 8;
170 __xdata uint8_t index = value;
172 descriptor = ao_usb_descriptors;
173 while (descriptor[0] != 0) {
174 if (descriptor[1] == type && index-- == 0) {
175 if (type == AO_USB_DESC_CONFIGURATION)
176 ao_usb_ep0_in_len = descriptor[2];
178 ao_usb_ep0_in_len = descriptor[0];
179 ao_usb_ep0_in_data = descriptor;
182 descriptor += descriptor[0];
186 /* Read data from the ep0 OUT fifo
189 ao_usb_ep0_fill(void)
194 len = UEBCLX; /* read length */
195 if (len > ao_usb_ep0_out_len)
196 len = ao_usb_ep0_out_len;
197 ao_usb_ep0_out_len -= len;
199 /* Pull all of the data out of the packet */
201 *ao_usb_ep0_out_data++ = UEDATX;
205 ao_usb_ep0_queue_byte(uint8_t a)
207 ao_usb_ep0_in_buf[ao_usb_ep0_in_len++] = a;
211 ao_usb_ep0_setup(void)
213 /* Pull the setup packet out of the fifo */
214 ao_usb_ep0_out_data = (__xdata uint8_t *) &ao_usb_setup;
215 ao_usb_ep0_out_len = 8;
218 UEINTX &= ~(1 << RXSTPI);
219 if (ao_usb_ep0_out_len != 0)
222 /* Figure out how to ACK the setup packet */
223 if (ao_usb_setup.dir_type_recip & AO_USB_DIR_IN) {
224 if (ao_usb_setup.length)
225 ao_usb_ep0_state = AO_USB_EP0_DATA_IN;
227 ao_usb_ep0_state = AO_USB_EP0_IDLE;
229 if (ao_usb_setup.length)
230 ao_usb_ep0_state = AO_USB_EP0_DATA_OUT;
232 ao_usb_ep0_state = AO_USB_EP0_IDLE;
236 if (ao_usb_ep0_state == AO_USB_EP0_IDLE)
237 USBCS0 = USBCS0_CLR_OUTPKT_RDY | USBCS0_DATA_END;
239 USBCS0 = USBCS0_CLR_OUTPKT_RDY;
242 ao_usb_ep0_in_data = ao_usb_ep0_in_buf;
243 ao_usb_ep0_in_len = 0;
244 switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_TYPE_MASK) {
245 case AO_USB_TYPE_STANDARD:
246 switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_RECIP_MASK) {
247 case AO_USB_RECIP_DEVICE:
248 switch(ao_usb_setup.request) {
249 case AO_USB_REQ_GET_STATUS:
250 ao_usb_ep0_queue_byte(0);
251 ao_usb_ep0_queue_byte(0);
253 case AO_USB_REQ_SET_ADDRESS:
254 ao_usb_set_address(ao_usb_setup.value);
256 case AO_USB_REQ_GET_DESCRIPTOR:
257 ao_usb_get_descriptor(ao_usb_setup.value);
259 case AO_USB_REQ_GET_CONFIGURATION:
260 ao_usb_ep0_queue_byte(ao_usb_configuration);
262 case AO_USB_REQ_SET_CONFIGURATION:
263 ao_usb_configuration = ao_usb_setup.value;
264 ao_usb_set_configuration();
268 case AO_USB_RECIP_INTERFACE:
269 #pragma disable_warning 110
270 switch(ao_usb_setup.request) {
271 case AO_USB_REQ_GET_STATUS:
272 ao_usb_ep0_queue_byte(0);
273 ao_usb_ep0_queue_byte(0);
275 case AO_USB_REQ_GET_INTERFACE:
276 ao_usb_ep0_queue_byte(0);
278 case AO_USB_REQ_SET_INTERFACE:
282 case AO_USB_RECIP_ENDPOINT:
283 switch(ao_usb_setup.request) {
284 case AO_USB_REQ_GET_STATUS:
285 ao_usb_ep0_queue_byte(0);
286 ao_usb_ep0_queue_byte(0);
292 case AO_USB_TYPE_CLASS:
293 switch (ao_usb_setup.request) {
294 case SET_LINE_CODING:
295 ao_usb_ep0_out_len = 7;
296 ao_usb_ep0_out_data = (__xdata uint8_t *) &ao_usb_line_coding;
298 case GET_LINE_CODING:
299 ao_usb_ep0_in_len = 7;
300 ao_usb_ep0_in_data = (uint8_t *) &ao_usb_line_coding;
302 case SET_CONTROL_LINE_STATE:
307 if (ao_usb_ep0_state != AO_USB_EP0_DATA_OUT) {
308 if (ao_usb_setup.length < ao_usb_ep0_in_len)
309 ao_usb_ep0_in_len = ao_usb_setup.length;
314 /* End point 0 receives all of the control messages. */
318 __xdata uint8_t intx;
320 ao_usb_ep0_state = AO_USB_EP0_IDLE;
326 if (intx & ((1 << RXOUTI) | (1 <<RXSTPI)))
328 ao_sleep(&ao_usb_task);
331 if (intx & (1 << RXSTPI)) {
334 UEINTX &= ~(1 << RXSTPI);
336 if (intx & (1 << RXOUTI)) {
339 UEINTX &= ~(1 << RXOUTI);
344 /* Wait for a free IN buffer */
349 UENUM = AO_USB_IN_EP;
350 if ((UEINTX & (1 << RWAL)))
352 ao_sleep(&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 UENUM = AO_USB_IN_EP;
382 if (++ao_usb_in_bytes == AO_USB_IN_SIZE)
387 ao_usb_pollchar(void) __critical
390 if (ao_usb_out_bytes == 0) {
391 /* Check to see if a packet has arrived */
392 UENUM = AO_USB_OUT_EP;
393 if ((UEINTX & (1 << RXOUTI)) == 0)
394 return AO_READ_AGAIN;
395 ao_usb_out_bytes = (UEBCHX << 8) | UEBCLX;
396 if (ao_usb_out_bytes == 0) {
397 UEINTX &= ~(1 << RXOUTI);
398 return AO_READ_AGAIN;
402 UENUM = AO_USB_OUT_EP;
404 if (ao_usb_out_bytes == 0) {
405 UENUM = AO_USB_OUT_EP;
406 UEINTX &= ~(1 << RXOUTI);
412 ao_usb_getchar(void) __critical
416 while ((c = ao_usb_pollchar()) == AO_READ_AGAIN)
417 ao_sleep(&ao_stdin_ready);
421 // Misc functions to wait for ready and send/receive packets
422 static inline void usb_wait_in_ready(void)
424 while (!(UEINTX & (1<<TXINI))) ;
426 static inline void usb_send_in(void)
428 UEINTX = ~(1<<TXINI);
430 static inline void usb_wait_receive_out(void)
432 while (!(UEINTX & (1<<RXOUTI))) ;
434 static inline void usb_ack_out(void)
436 UEINTX = ~(1<<RXOUTI);
439 /* Endpoint 0 interrupt */
443 ao_wakeup(&ao_usb_task);
447 #define AO_PAD_REGULATOR_INIT (1 << UVREGE) /* Turn on pad regulator */
450 #define AO_PAD_REGULATOR_INIT 0 /* Turn off pad regulator */
453 #if AVR_CLOCK == 16000000UL
454 #define AO_USB_PLL_INPUT_PRESCALER (1 << PINDIV) /* Divide 16MHz clock by 2 */
456 #if AVR_CLOCK == 8000000UL
457 #define AO_USB_PLL_INPUT_PRESCALER 0 /* Don't divide clock */
463 /* Unplug from the bus */
464 UDCON = (1 << DETACH);
465 ao_usb_detatch_interface();
467 /* Disable the interface */
470 /* Disable the PLL */
473 /* Turn off the pad regulator */
477 #define AO_USB_CON ((1 << USBE) | /* USB enable */ \
478 (0 << RSTCPU) | /* do not reset CPU */ \
479 (0 << LSM) | /* Full speed mode */ \
480 (0 << RMWKUP)) /* no remote wake-up */ \
485 /* Configure pad regulator */
486 UHWCON = AO_PAD_REGULATOR_INIT;
488 /* Enable USB device, but freeze the clocks until initialized */
489 USBCON = AO_USB_CON | (1 <<FRZCLK);
491 /* Enable PLL with appropriate divider */
492 PLLCSR = AO_USB_PLL_INPUT_PRESCALER | (1 << PLLE);
494 /* Wait for PLL to lock */
495 loop_until_bit_is_set(PLLCSR, (1 << PLOCK));
497 /* Enable USB, enable the VBUS pad */
498 USBCON = AO_USB_CON | (1 << OTGPADE);
500 UDCON = (0 << DETACH); /* Clear the DETACH bit to plug into the bus */
502 usb_configuration = 0;
510 ao_add_task(&ao_usb_task, ao_usb_ep0, "usb");
511 ao_add_stdio(ao_usb_pollchar, ao_usb_putchar, ao_usb_flush);