2 * Copyright © 2012 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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 #include "ao_product.h"
23 #ifndef AO_POWER_MANAGEMENT
24 #define AO_POWER_MANAGEMENT 0
27 #ifndef AO_USB_DEVICE_ID_SERIAL
28 #define AO_USB_DEVICE_ID_SERIAL 0
32 static struct ao_fifo ao_usb_rx_fifo;
36 #define AO_USB_OUT_SLEEP_ADDR (&ao_stdin_ready)
38 #define AO_USB_OUT_SLEEP_ADDR (&ao_usb_out_avail)
41 #define SAMD21_USB_ALIGN __attribute__ ((aligned(4)))
44 uint8_t dir_type_recip;
51 static uint8_t ao_usb_ep0_state;
53 struct samd21_usb_desc samd21_usb_desc[8] SAMD21_USB_ALIGN;
55 /* Pending EP0 IN data */
56 static uint8_t ao_usb_ep0_in_tmp[2]; /* small buffer */
57 static const uint8_t *ao_usb_ep0_in_data; /* Remaining data */
58 static uint8_t ao_usb_ep0_in_len; /* Remaining amount */
60 /* Pending EP0 OUT data */
61 static uint8_t *ao_usb_ep0_out_data;
62 static uint8_t ao_usb_ep0_out_len;
64 /* Endpoint 0 buffers */
65 static uint8_t ao_usb_ep0_in_buf[AO_USB_CONTROL_SIZE] SAMD21_USB_ALIGN;
66 static uint8_t ao_usb_ep0_out_buf[AO_USB_CONTROL_SIZE] SAMD21_USB_ALIGN;
69 /* Pointer to interrupt buffer in USB memory */
70 static uint8_t ao_usb_int_buf[AO_USB_INT_SIZE] SAMD21_USB_ALIGN;
75 static uint8_t ao_usb_in_tx_which;
76 static uint8_t ao_usb_tx_count;
77 static uint8_t ao_usb_in_buf[2][AO_USB_IN_SIZE] SAMD21_USB_ALIGN;
81 static uint8_t ao_usb_out_rx_which;
83 static uint8_t ao_usb_rx_count, ao_usb_rx_pos;
85 static uint8_t ao_usb_out_buf[2][AO_USB_OUT_SIZE] SAMD21_USB_ALIGN;
89 /* Marks when we don't need to send an IN packet.
90 * This happens only when the last IN packet is not full,
91 * otherwise the host will expect to keep seeing packets.
92 * Send a zero-length packet as required
94 static uint8_t ao_usb_in_flushed;
96 /* Marks when we have delivered an IN packet to the hardware
97 * and it has not been received yet. ao_sleep on this address
98 * to wait for it to be delivered.
100 static uint8_t ao_usb_in_pending;
102 /* Marks when an OUT packet has been received by the hardware
103 * but not pulled to the shadow buffer.
105 static uint8_t ao_usb_out_avail;
106 uint8_t ao_usb_running;
107 static uint8_t ao_usb_configuration;
109 #define AO_USB_EP0_GOT_SETUP 1
110 #define AO_USB_EP0_GOT_RX_DATA 2
111 #define AO_USB_EP0_GOT_TX_ACK 4
113 static uint8_t ao_usb_ep0_receive;
114 static uint8_t ao_usb_address;
115 static uint8_t ao_usb_address_pending;
118 * Set current device address and mark the
119 * interface as active
122 ao_usb_set_address(uint8_t address)
124 samd21_usb.dadd = (1 << SAMD21_USB_DADD_ADDEN) | (address << SAMD21_USB_DADD_DADD);
125 ao_usb_address_pending = 0;
129 * Initialize an entpoint
133 ao_usb_init_bank(struct samd21_usb_desc_bank *bank,
137 bank->addr = (uint32_t) buf;
139 uint32_t size_bits = 0;
141 case 8: size_bits = SAMD21_USB_DESC_PCKSIZE_SIZE_8; break;
142 case 16: size_bits = SAMD21_USB_DESC_PCKSIZE_SIZE_16; break;
143 case 32: size_bits = SAMD21_USB_DESC_PCKSIZE_SIZE_32; break;
144 case 64: size_bits = SAMD21_USB_DESC_PCKSIZE_SIZE_64; break;
145 case 128: size_bits = SAMD21_USB_DESC_PCKSIZE_SIZE_128; break;
146 case 256: size_bits = SAMD21_USB_DESC_PCKSIZE_SIZE_256; break;
147 case 512: size_bits = SAMD21_USB_DESC_PCKSIZE_SIZE_512; break;
148 case 1023: size_bits = SAMD21_USB_DESC_PCKSIZE_SIZE_1023; break;
150 bank->pcksize = ((0 << SAMD21_USB_DESC_PCKSIZE_BYTE_COUNT) |
151 (0 << SAMD21_USB_DESC_PCKSIZE_MULTI_PACKET_SIZE) |
152 (size_bits << SAMD21_USB_DESC_PCKSIZE_SIZE) |
153 (0 << SAMD21_USB_DESC_PCKSIZE_AUTO_ZLP));
157 ao_usb_init_ep(uint8_t ep,
158 uint8_t type_out, void *out_buf, uint16_t out_size,
159 uint8_t type_in, void *in_buf, uint16_t in_size)
161 /* set up descriptors */
162 ao_usb_init_bank(&samd21_usb_desc[ep].bank[0],
165 ao_usb_init_bank(&samd21_usb_desc[ep].bank[1],
168 samd21_usb.ep[ep].epcfg = (uint8_t) ((type_out << SAMD21_USB_EP_EPCFG_EP_TYPE_OUT) |
169 (type_in << SAMD21_USB_EP_EPCFG_EP_TYPE_IN));
171 /* Clear all status bits */
172 samd21_usb.ep[ep].epstatusclr = 0xff;
174 /* Select interrupts */
177 epinten |= (1 << SAMD21_USB_EP_EPINTFLAG_TRCPT0);
179 epinten |= (1 << SAMD21_USB_EP_EPINTFLAG_TRCPT1);
181 epinten |= (1 << SAMD21_USB_EP_EPINTFLAG_RXSTP);
182 samd21_usb.ep[ep].epintenset = epinten;
188 ao_usb_init_ep(AO_USB_CONTROL_EP,
189 SAMD21_USB_EP_EPCFG_EP_TYPE_OUT_CONTROL,
190 ao_usb_ep0_out_buf, AO_USB_CONTROL_SIZE,
191 SAMD21_USB_EP_EPCFG_EP_TYPE_IN_CONTROL,
192 ao_usb_ep0_in_buf, AO_USB_CONTROL_SIZE);
196 /* Reset our internal state
199 ao_usb_ep0_state = AO_USB_EP0_IDLE;
201 ao_usb_ep0_in_data = NULL;
202 ao_usb_ep0_in_len = 0;
204 ao_usb_ep0_out_data = NULL;
205 ao_usb_ep0_out_len = 0;
209 ao_usb_set_configuration(void)
212 ao_usb_init_ep(AO_USB_INT_EP,
213 SAMD21_USB_EP_EPCFG_EP_TYPE_OUT_DISABLED,
215 SAMD21_USB_EP_EPCFG_EP_TYPE_IN_INTERRUPT,
216 ao_usb_int_buf, AO_USB_INT_SIZE);
220 ao_usb_init_ep(AO_USB_OUT_EP,
221 SAMD21_USB_EP_EPCFG_EP_TYPE_OUT_BULK,
222 &ao_usb_out_buf[0][0], AO_USB_OUT_SIZE,
223 SAMD21_USB_EP_EPCFG_EP_TYPE_IN_DUAL_BANK,
224 &ao_usb_out_buf[1][0], AO_USB_OUT_SIZE);
226 /* At first receive, we'll flip this back to 0 */
227 ao_usb_out_rx_which = 1;
231 /* Set up the IN end point */
232 ao_usb_init_ep(AO_USB_IN_EP,
233 SAMD21_USB_EP_EPCFG_EP_TYPE_OUT_DUAL_BANK,
234 &ao_usb_in_buf[0][0], AO_USB_IN_SIZE,
235 SAMD21_USB_EP_EPCFG_EP_TYPE_IN_BULK,
236 &ao_usb_in_buf[1][0], AO_USB_IN_SIZE);
238 /* First transmit data goes to buffer 0 */
239 ao_usb_in_tx_which = 0;
242 ao_usb_in_flushed = 1;
243 ao_usb_in_pending = 0;
244 ao_wakeup(&ao_usb_in_pending);
246 ao_usb_out_avail = 0;
247 ao_usb_configuration = 0;
249 ao_wakeup(AO_USB_OUT_SLEEP_ADDR);
252 /* Send an IN data packet */
254 ao_usb_ep0_flush(void)
258 /* Check to see if the endpoint is still busy */
259 if ((samd21_usb.ep[0].epstatus & (1 << (SAMD21_USB_EP_EPSTATUS_BK1RDY))) != 0)
262 this_len = ao_usb_ep0_in_len;
263 if (this_len > AO_USB_CONTROL_SIZE)
264 this_len = AO_USB_CONTROL_SIZE;
266 if (this_len < AO_USB_CONTROL_SIZE)
267 ao_usb_ep0_state = AO_USB_EP0_IDLE;
269 ao_usb_ep0_in_len -= this_len;
271 memcpy(ao_usb_ep0_in_buf, ao_usb_ep0_in_data, this_len);
272 ao_usb_ep0_in_data += this_len;
274 /* Mark the endpoint as TX valid to send the packet */
275 samd21_usb_desc_set_byte_count(AO_USB_CONTROL_EP, 1, this_len);
276 samd21_usb_ep_set_ready(AO_USB_CONTROL_EP, 1);
279 /* Read data from the ep0 OUT fifo */
281 ao_usb_ep0_fill(void)
283 uint16_t len = samd21_usb_desc_get_byte_count(AO_USB_CONTROL_EP, 0);
285 if (len > ao_usb_ep0_out_len)
286 len = ao_usb_ep0_out_len;
287 ao_usb_ep0_out_len -= (uint8_t) len;
289 /* Pull all of the data out of the packet */
290 memcpy(ao_usb_ep0_out_data, ao_usb_ep0_out_buf, len);
291 ao_usb_ep0_out_data += len;
294 samd21_usb_ep_clr_ready(AO_USB_CONTROL_EP, 0);
298 ao_usb_ep0_in_reset(void)
300 ao_usb_ep0_in_data = ao_usb_ep0_in_tmp;
301 ao_usb_ep0_in_len = 0;
305 ao_usb_ep0_in_queue_byte(uint8_t a)
307 if (ao_usb_ep0_in_len < sizeof (ao_usb_ep0_in_tmp))
308 ao_usb_ep0_in_tmp[ao_usb_ep0_in_len++] = a;
312 ao_usb_ep0_in_set(const uint8_t *data, uint8_t len)
314 ao_usb_ep0_in_data = data;
315 ao_usb_ep0_in_len = len;
319 ao_usb_ep0_out_set(uint8_t *data, uint8_t len)
321 ao_usb_ep0_out_data = data;
322 ao_usb_ep0_out_len = len;
326 ao_usb_ep0_in_start(uint16_t max)
328 /* Don't send more than asked for */
329 if (ao_usb_ep0_in_len > max)
330 ao_usb_ep0_in_len = (uint8_t) max;
334 struct ao_usb_line_coding ao_usb_line_coding = {115200, 0, 0, 8};
336 #if AO_USB_DEVICE_ID_SERIAL
337 static uint8_t ao_usb_serial[2 + 64];
339 /* Convert a 32-bit value to 8 hexidecimal UCS2 characters */
341 hex_to_ucs2(uint32_t in, uint8_t *out)
345 for (i = 28; i >= 0; i -= 4) {
346 uint8_t bits = (in >> i) & 0xf;
347 *out++ = ((bits < 10) ? '0' : ('a' - 10)) + bits;
352 /* Encode the device ID (128 bits) in hexidecimal to use as a device
356 ao_usb_serial_init(void)
358 ao_usb_serial[0] = 66; /* length */
359 ao_usb_serial[1] = AO_USB_DESC_STRING;
360 hex_to_ucs2(samd21_serial.word0, ao_usb_serial + 2 + 0);
361 hex_to_ucs2(samd21_serial.word1, ao_usb_serial + 2 + 16);
362 hex_to_ucs2(samd21_serial.word2, ao_usb_serial + 2 + 32);
363 hex_to_ucs2(samd21_serial.word3, ao_usb_serial + 2 + 48);
367 /* Walk through the list of descriptors and find a match
370 ao_usb_get_descriptor(uint16_t value, uint16_t length)
372 const uint8_t *descriptor;
373 uint8_t type = (uint8_t) (value >> 8);
374 uint8_t index = (uint8_t) value;
376 descriptor = ao_usb_descriptors;
377 while (descriptor[0] != 0) {
378 if (descriptor[1] == type && index-- == 0) {
380 if (type == AO_USB_DESC_CONFIGURATION)
384 #if AO_USB_DEVICE_ID_SERIAL
385 /* Slightly hacky - the serial number is string 3 */
386 if (type == AO_USB_DESC_STRING && (value & 0xff) == 3) {
387 descriptor = ao_usb_serial;
388 len = sizeof (ao_usb_serial);
392 len = (uint8_t) length;
393 ao_usb_ep0_in_set(descriptor, len);
396 descriptor += descriptor[0];
401 ao_usb_ep0_setup(void)
403 /* Pull the setup packet out of the fifo */
404 ao_usb_ep0_out_set((uint8_t *) &ao_usb_setup, 8);
406 if (ao_usb_ep0_out_len != 0)
409 if ((ao_usb_setup.dir_type_recip & AO_USB_DIR_IN) || ao_usb_setup.length == 0)
410 ao_usb_ep0_state = AO_USB_EP0_DATA_IN;
412 ao_usb_ep0_state = AO_USB_EP0_DATA_OUT;
414 ao_usb_ep0_in_reset();
416 switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_TYPE_MASK) {
417 case AO_USB_TYPE_STANDARD:
418 switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_RECIP_MASK) {
419 case AO_USB_RECIP_DEVICE:
420 switch(ao_usb_setup.request) {
421 case AO_USB_REQ_GET_STATUS:
422 ao_usb_ep0_in_queue_byte(0);
423 ao_usb_ep0_in_queue_byte(0);
425 case AO_USB_REQ_SET_ADDRESS:
426 ao_usb_address = (uint8_t) ao_usb_setup.value;
427 ao_usb_address_pending = 1;
429 case AO_USB_REQ_GET_DESCRIPTOR:
430 ao_usb_get_descriptor(ao_usb_setup.value, ao_usb_setup.length);
432 case AO_USB_REQ_GET_CONFIGURATION:
433 ao_usb_ep0_in_queue_byte(ao_usb_configuration);
435 case AO_USB_REQ_SET_CONFIGURATION:
436 ao_usb_configuration = (uint8_t) ao_usb_setup.value;
437 ao_usb_set_configuration();
441 case AO_USB_RECIP_INTERFACE:
442 switch(ao_usb_setup.request) {
443 case AO_USB_REQ_GET_STATUS:
444 ao_usb_ep0_in_queue_byte(0);
445 ao_usb_ep0_in_queue_byte(0);
447 case AO_USB_REQ_GET_INTERFACE:
448 ao_usb_ep0_in_queue_byte(0);
450 case AO_USB_REQ_SET_INTERFACE:
454 case AO_USB_RECIP_ENDPOINT:
455 switch(ao_usb_setup.request) {
456 case AO_USB_REQ_GET_STATUS:
457 ao_usb_ep0_in_queue_byte(0);
458 ao_usb_ep0_in_queue_byte(0);
464 case AO_USB_TYPE_CLASS:
465 switch (ao_usb_setup.request) {
466 case AO_USB_SET_LINE_CODING:
467 ao_usb_ep0_out_set((uint8_t *) &ao_usb_line_coding, 7);
469 case AO_USB_GET_LINE_CODING:
470 ao_usb_ep0_in_set((const uint8_t *) &ao_usb_line_coding, 7);
472 case AO_USB_SET_CONTROL_LINE_STATE:
478 /* If we're not waiting to receive data from the host,
479 * queue an IN response
481 if (ao_usb_ep0_state == AO_USB_EP0_DATA_IN)
482 ao_usb_ep0_in_start(ao_usb_setup.length);
486 ao_usb_ep0_handle(uint8_t receive)
488 ao_usb_ep0_receive = 0;
489 if (receive & AO_USB_EP0_GOT_SETUP) {
492 if (receive & AO_USB_EP0_GOT_RX_DATA) {
493 if (ao_usb_ep0_state == AO_USB_EP0_DATA_OUT) {
495 if (ao_usb_ep0_out_len == 0) {
496 ao_usb_ep0_state = AO_USB_EP0_DATA_IN;
497 ao_usb_ep0_in_start(0);
501 if (receive & AO_USB_EP0_GOT_TX_ACK) {
503 #if HAS_FLIGHT && AO_USB_FORCE_IDLE
504 ao_flight_force_idle = 1;
506 if (ao_usb_ep0_state == AO_USB_EP0_DATA_IN)
508 /* Wait until the IN packet is received from addr 0
509 * before assigning our local address
511 if (ao_usb_address_pending)
512 ao_usb_set_address(ao_usb_address);
516 #if AO_POWER_MANAGEMENT
520 stm_usb.cntr |= (1 << STM_USB_CNTR_FSUSP);
522 stm_usb.cntr |= (1 << STM_USB_CNTR_LP_MODE);
530 stm_usb.cntr &= ~(1 << STM_USB_CNTR_FSUSP);
537 ao_usb_fifo_check(void)
539 uint8_t next_which = 1 - ao_usb_out_rx_which;
540 if (ao_usb_out_avail & (1 << next_which)) {
541 uint8_t *buf = ao_usb_out_buf[next_which];
542 uint16_t len = samd21_usb_desc_get_byte_count(AO_USB_OUT_EP, next_which);
544 if (ao_fifo_has_space(&ao_usb_rx_fifo, len)) {
547 for (i = 0; i < len; i++)
548 ao_fifo_insert(&ao_usb_rx_fifo, buf[i]);
549 samd21_usb_ep_clr_ready(AO_USB_OUT_EP, next_which);
550 ao_usb_out_avail &= ~(1 << next_which);
551 ao_usb_out_rx_which = next_which;
552 ao_wakeup(AO_USB_OUT_SLEEP_ADDR);
555 ao_usb_out_hook(buf, len);
564 uint16_t intflag = samd21_usb.intflag;
565 uint16_t epintsmry = samd21_usb.epintsmry;
567 samd21_usb.intflag = intflag;
569 if (epintsmry & (1 << 0)) {
570 uint8_t epintflag = samd21_usb.ep[0].epintflag;
571 samd21_usb.ep[0].epintflag = epintflag;
573 if (epintflag & (1 << SAMD21_USB_EP_EPINTFLAG_RXSTP))
574 ao_usb_ep0_receive |= AO_USB_EP0_GOT_SETUP;
575 else if (epintflag & (1 << SAMD21_USB_EP_EPINTFLAG_TRCPT0))
576 ao_usb_ep0_receive |= AO_USB_EP0_GOT_RX_DATA;
577 if (epintflag & (1 << SAMD21_USB_EP_EPINTFLAG_TRCPT1))
578 ao_usb_ep0_receive |= AO_USB_EP0_GOT_TX_ACK;
579 ao_usb_ep0_handle(ao_usb_ep0_receive);
582 if (epintsmry & (1 << AO_USB_OUT_EP)) {
583 uint8_t epintflag = samd21_usb.ep[AO_USB_OUT_EP].epintflag;
584 samd21_usb.ep[AO_USB_OUT_EP].epintflag = epintflag;
585 uint8_t avail = (epintflag >> SAMD21_USB_EP_EPINTFLAG_TRCPT0) & 3;
587 ao_usb_out_avail |= avail;
592 ao_wakeup(AO_USB_OUT_SLEEP_ADDR);
598 if (epintsmry & (1 << AO_USB_IN_EP)) {
599 uint8_t epintflag = samd21_usb.ep[AO_USB_IN_EP].epintflag;
600 samd21_usb.ep[AO_USB_IN_EP].epintflag = epintflag;
601 uint8_t done = (epintflag >> SAMD21_USB_EP_EPINTFLAG_TRCPT0) & 3;
603 ao_usb_in_pending &= (uint8_t) ~done;
604 ao_wakeup(&ao_usb_in_pending);
609 if (epintsmry & (1 << AO_USB_INT_EP)) {
612 if (intflag & (1 << SAMD21_USB_INTFLAG_EORST)) {
615 #if AO_POWER_MANAGEMENT
616 if (istr & (1 << STM_USB_ISTR_SUSP))
619 if (istr & (1 << STM_USB_ISTR_WKUP))
625 /* Queue the current IN buffer for transmission */
627 _ao_usb_in_send(void)
629 ao_usb_in_pending |= (uint8_t) (1 << ao_usb_in_tx_which);
630 if (ao_usb_tx_count != AO_USB_IN_SIZE)
631 ao_usb_in_flushed = 1;
633 samd21_usb_desc_set_byte_count(AO_USB_IN_EP, ao_usb_in_tx_which, ao_usb_tx_count);
634 samd21_usb_ep_set_ready(AO_USB_IN_EP, ao_usb_in_tx_which);
636 /* Toggle our usage */
637 ao_usb_in_tx_which = 1 - ao_usb_in_tx_which;
641 /* Wait for a free IN buffer. Interrupts are blocked */
643 _ao_usb_in_wait(void)
645 /* Wait for an IN buffer to be ready */
646 while ((ao_usb_in_pending & (1 << ao_usb_in_tx_which)) != 0)
647 ao_sleep(&ao_usb_in_pending);
656 /* Anytime we've sent a character since
657 * the last time we flushed, we'll need
658 * to send a packet -- the only other time
659 * we would send a packet is when that
660 * packet was full, in which case we now
661 * want to send an empty packet
663 ao_arch_block_interrupts();
664 if (!ao_usb_in_flushed) {
666 if (!ao_usb_in_flushed)
669 ao_arch_release_interrupts();
673 ao_usb_putchar(char c)
678 ao_arch_block_interrupts();
681 ao_usb_in_flushed = 0;
682 ao_usb_in_buf[ao_usb_in_tx_which][ao_usb_tx_count++] = c;
684 /* Send the packet when full */
685 if (ao_usb_tx_count == AO_USB_IN_SIZE)
688 ao_arch_release_interrupts();
697 _ao_usb_out_recv(void)
699 uint8_t next_which = 1 - ao_usb_out_rx_which;
701 if ((ao_usb_out_avail & (1 << next_which)) != 0) {
702 /* switch current buffer */
703 ao_usb_out_rx_which = next_which;
704 ao_usb_out_avail &= (uint8_t) ~(1 << ao_usb_out_rx_which);
706 ao_usb_rx_count = (uint8_t) samd21_usb_desc_get_byte_count(AO_USB_OUT_EP, ao_usb_out_rx_which);
715 _ao_usb_pollchar(void)
720 return AO_READ_AGAIN;
723 if (ao_fifo_empty(&ao_usb_rx_fifo))
724 return AO_READ_AGAIN;
725 c = ao_fifo_remove(&ao_usb_rx_fifo);
729 if (ao_usb_rx_pos != ao_usb_rx_count)
732 /* Check for packet */
733 if (!_ao_usb_out_recv())
734 return AO_READ_AGAIN;
737 /* Pull a character out of the fifo */
738 c = ao_usb_out_buf[ao_usb_out_rx_which][ao_usb_rx_pos++];
740 if (ao_usb_rx_pos == ao_usb_rx_count)
741 samd21_usb_ep_clr_ready(AO_USB_OUT_EP, ao_usb_out_rx_which);
752 ao_arch_block_interrupts();
753 while ((c = _ao_usb_pollchar()) == AO_READ_AGAIN)
754 ao_sleep(AO_USB_OUT_SLEEP_ADDR);
755 ao_arch_release_interrupts();
764 ao_arch_block_interrupts();
765 samd21_nvic_clear_enable(SAMD21_NVIC_ISR_USB_POS);
767 /* Disable USB pull-up */
768 samd21_usb.ctrlb |= (1 << SAMD21_USB_CTRLB_DETACH);
770 /* Switch off the device */
771 samd21_usb.ctrla &= (uint8_t) ~(1 << SAMD21_USB_CTRLA_ENABLE);
773 /* Disable the interface */
774 samd21_pm.apbbmask &= ~(1UL << SAMD21_PM_APBBMASK_USB);
775 ao_arch_release_interrupts();
783 ao_enable_port(&samd21_port_a);
785 /* Set up USB DM/DP pins */
786 samd21_port_pmux_set(&samd21_port_a, 24, SAMD21_PORT_PMUX_FUNC_G);
787 samd21_port_pmux_set(&samd21_port_a, 25, SAMD21_PORT_PMUX_FUNC_G);
789 /* Assign gclk 0 to USB reference */
790 samd21_gclk_clkctrl(AO_GCLK_USB, SAMD21_GCLK_CLKCTRL_ID_USB);
792 /* Enable USB clock */
793 samd21_pm.apbbmask |= (1 << SAMD21_PM_APBBMASK_USB);
795 /* Reset USB Device */
797 samd21_usb.ctrla |= (1 << SAMD21_USB_CTRLA_SWRST);
799 while ((samd21_usb.ctrla & (1 << SAMD21_USB_CTRLA_SWRST)) != 0)
802 while ((samd21_usb.syncbusy & (1 << SAMD21_USB_SYNCBUSY_SWRST)) != 0)
805 memset(&samd21_usb_desc, 0, sizeof (samd21_usb_desc));
808 samd21_usb.ctrlb |= (1 << SAMD21_USB_CTRLB_DETACH);
810 samd21_usb.descadd = (uint32_t) &samd21_usb_desc;
812 /* Load calibration values */
813 uint32_t transn = ((samd21_aux1.calibration >> SAMD21_AUX1_CALIBRATION_USB_TRANSN) &
814 SAMD21_AUX1_CALIBRATION_USB_TRANSN_MASK);
817 uint32_t transp = ((samd21_aux1.calibration >> SAMD21_AUX1_CALIBRATION_USB_TRANSP) &
818 SAMD21_AUX1_CALIBRATION_USB_TRANSP_MASK);
821 uint32_t trim = ((samd21_aux1.calibration >> SAMD21_AUX1_CALIBRATION_USB_TRIM) &
822 SAMD21_AUX1_CALIBRATION_USB_TRIM_MASK);
826 samd21_usb.padcal = (uint16_t) ((transn << SAMD21_USB_PADCAL_TRANSN) |
827 (transp << SAMD21_USB_PADCAL_TRANSP) |
828 (trim << SAMD21_USB_PADCAL_TRIM));
830 samd21_usb.qosctrl = ((3 << SAMD21_USB_QOSCTRL_CQOS) |
831 (3 << SAMD21_USB_QOSCTRL_DQOS));
833 ao_arch_block_interrupts();
836 samd21_usb.ctrlb = (SAMD21_USB_CTRLB_SPDCONF_FS << SAMD21_USB_CTRLB_SPDCONF);
838 /* Set device mode */
839 samd21_usb.ctrla = ((0 << SAMD21_USB_CTRLA_MODE) |
840 (1 << SAMD21_USB_CTRLA_RUNSTDBY) |
841 (1 << SAMD21_USB_CTRLA_ENABLE));
843 while ((samd21_usb.syncbusy & (1 << SAMD21_USB_SYNCBUSY_ENABLE)) != 0)
846 /* configure interrupts */
847 samd21_nvic_set_enable(SAMD21_NVIC_ISR_USB_POS);
848 samd21_nvic_set_priority(SAMD21_NVIC_ISR_USB_POS, 2);
850 samd21_usb.intenset = ((1 << SAMD21_USB_INTFLAG_EORST));
852 ao_arch_release_interrupts();
854 for (t = 0; t < 50000; t++)
858 samd21_usb.ctrlb &= (uint16_t) ~(1 << SAMD21_USB_CTRLB_DETACH);
866 #if AO_USB_DEVICE_ID_SERIAL
867 ao_usb_serial_init();
870 ao_usb_ep0_state = AO_USB_EP0_IDLE;
873 ao_add_stdio(_ao_usb_pollchar, ao_usb_putchar, ao_usb_flush);