3 /* Copyright (c) 2010, Peter Barrett
4 ** Sleep/Wakeup support added by Michael Dreher
6 ** Permission to use, copy, modify, and/or distribute this software for
7 ** any purpose with or without fee is hereby granted, provided that the
8 ** above copyright notice and this permission notice appear in all copies.
10 ** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11 ** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12 ** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
13 ** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
14 ** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
15 ** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
16 ** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
21 #include "PluggableUSB.h"
26 /** Pulse generation counters to keep track of the number of milliseconds remaining for each pulse type */
27 #define TX_RX_LED_PULSE_MS 100
28 volatile u8 TxLEDPulse; /**< Milliseconds remaining for data Tx LED pulse */
29 volatile u8 RxLEDPulse; /**< Milliseconds remaining for data Rx LED pulse */
31 //==================================================================
32 //==================================================================
34 extern const u16 STRING_LANGUAGE[] PROGMEM;
35 extern const u8 STRING_PRODUCT[] PROGMEM;
36 extern const u8 STRING_MANUFACTURER[] PROGMEM;
37 extern const DeviceDescriptor USB_DeviceDescriptorIAD PROGMEM;
39 const u16 STRING_LANGUAGE[2] = {
45 // If no product is provided, use USB IO Board
46 #define USB_PRODUCT "USB IO Board"
49 const u8 STRING_PRODUCT[] PROGMEM = USB_PRODUCT;
52 # if defined(USB_MANUFACTURER)
53 # undef USB_MANUFACTURER
55 # define USB_MANUFACTURER "Arduino LLC"
56 #elif USB_VID == 0x1b4f
57 # if defined(USB_MANUFACTURER)
58 # undef USB_MANUFACTURER
60 # define USB_MANUFACTURER "SparkFun"
61 #elif !defined(USB_MANUFACTURER)
62 // Fall through to unknown if no manufacturer name was provided in a macro
63 # define USB_MANUFACTURER "Unknown"
66 const u8 STRING_MANUFACTURER[] PROGMEM = USB_MANUFACTURER;
69 #define DEVICE_CLASS 0x02
72 const DeviceDescriptor USB_DeviceDescriptorIAD =
73 D_DEVICE(0xEF,0x02,0x01,64,USB_VID,USB_PID,0x100,IMANUFACTURER,IPRODUCT,ISERIAL,1);
75 //==================================================================
76 //==================================================================
78 volatile u8 _usbConfiguration = 0;
79 volatile u8 _usbCurrentStatus = 0; // meaning of bits see usb_20.pdf, Figure 9-4. Information Returned by a GetStatus() Request to a Device
80 volatile u8 _usbSuspendState = 0; // copy of UDINT to check SUSPI and WAKEUPI bits
82 static inline void WaitIN(void)
84 while (!(UEINTX & (1<<TXINI)))
88 static inline void ClearIN(void)
93 static inline void WaitOUT(void)
95 while (!(UEINTX & (1<<RXOUTI)))
99 static inline u8 WaitForINOrOUT()
101 while (!(UEINTX & ((1<<TXINI)|(1<<RXOUTI))))
103 return (UEINTX & (1<<RXOUTI)) == 0;
106 static inline void ClearOUT(void)
108 UEINTX = ~(1<<RXOUTI);
111 static inline void Recv(volatile u8* data, u8 count)
116 RXLED1; // light the RX LED
117 RxLEDPulse = TX_RX_LED_PULSE_MS;
120 static inline u8 Recv8()
122 RXLED1; // light the RX LED
123 RxLEDPulse = TX_RX_LED_PULSE_MS;
128 static inline void Send8(u8 d)
133 static inline void SetEP(u8 ep)
138 static inline u8 FifoByteCount()
143 static inline u8 ReceivedSetupInt()
145 return UEINTX & (1<<RXSTPI);
148 static inline void ClearSetupInt()
150 UEINTX = ~((1<<RXSTPI) | (1<<RXOUTI) | (1<<TXINI));
153 static inline void Stall()
155 UECONX = (1<<STALLRQ) | (1<<EPEN);
158 static inline u8 ReadWriteAllowed()
160 return UEINTX & (1<<RWAL);
163 static inline u8 Stalled()
165 return UEINTX & (1<<STALLEDI);
168 static inline u8 FifoFree()
170 return UEINTX & (1<<FIFOCON);
173 static inline void ReleaseRX()
175 UEINTX = 0x6B; // FIFOCON=0 NAKINI=1 RWAL=1 NAKOUTI=0 RXSTPI=1 RXOUTI=0 STALLEDI=1 TXINI=1
178 static inline void ReleaseTX()
180 UEINTX = 0x3A; // FIFOCON=0 NAKINI=0 RWAL=1 NAKOUTI=1 RXSTPI=1 RXOUTI=0 STALLEDI=1 TXINI=0
183 static inline u8 FrameNumber()
188 //==================================================================
189 //==================================================================
191 u8 USBGetConfiguration(void)
193 return _usbConfiguration;
196 #define USB_RECV_TIMEOUT
201 LockEP(u8 ep) : _sreg(SREG)
212 // Number of bytes, assumes a rx endpoint
213 u8 USB_Available(u8 ep)
216 return FifoByteCount();
219 // Non Blocking receive
220 // Return number of bytes read
221 int USB_Recv(u8 ep, void* d, int len)
223 if (!_usbConfiguration || len < 0)
227 u8 n = FifoByteCount();
233 if (len && !FifoByteCount()) // release empty buffer
239 // Recv 1 byte if ready
243 if (USB_Recv(ep,&c,1) != 1)
249 u8 USB_SendSpace(u8 ep)
252 if (!ReadWriteAllowed())
254 return USB_EP_SIZE - FifoByteCount();
257 // Blocking Send of data to an endpoint
258 int USB_Send(u8 ep, const void* d, int len)
260 if (!_usbConfiguration)
263 if (_usbSuspendState & (1<<SUSPI)) {
264 //send a remote wakeup
265 UDCON |= (1 << RMWKUP);
269 const u8* data = (const u8*)d;
270 u8 timeout = 250; // 250ms timeout on send? TODO
271 bool sendZlp = false;
273 while (len || sendZlp)
275 u8 n = USB_SendSpace(ep);
290 // Frame may have been released by the SOF interrupt handler
291 if (!ReadWriteAllowed())
295 if (ep & TRANSFER_ZERO)
300 else if (ep & TRANSFER_PGM)
303 Send8(pgm_read_byte(data++));
314 } else if (!ReadWriteAllowed()) { // ...release if buffer is full...
316 if (len == 0) sendZlp = true;
317 } else if ((len == 0) && (ep & TRANSFER_RELEASE)) { // ...or if forced with TRANSFER_RELEASE
318 // XXX: TRANSFER_RELEASE is never used can be removed?
323 TXLED1; // light the TX LED
324 TxLEDPulse = TX_RX_LED_PULSE_MS;
328 u8 _initEndpoints[USB_ENDPOINTS] =
330 0, // Control Endpoint
332 EP_TYPE_INTERRUPT_IN, // CDC_ENDPOINT_ACM
333 EP_TYPE_BULK_OUT, // CDC_ENDPOINT_OUT
334 EP_TYPE_BULK_IN, // CDC_ENDPOINT_IN
336 // Following endpoints are automatically initialized to 0
339 #define EP_SINGLE_64 0x32 // EP0
340 #define EP_DOUBLE_64 0x36 // Other endpoints
341 #define EP_SINGLE_16 0x12
344 void InitEP(u8 index, u8 type, u8 size)
355 for (u8 i = 1; i < sizeof(_initEndpoints) && _initEndpoints[i] != 0; i++)
359 UECFG0X = _initEndpoints[i];
360 #if USB_EP_SIZE == 16
361 UECFG1X = EP_SINGLE_16;
362 #elif USB_EP_SIZE == 64
363 UECFG1X = EP_DOUBLE_64;
365 #error Unsupported value for USB_EP_SIZE
368 UERST = 0x7E; // And reset them
372 // Handle CLASS_INTERFACE requests
374 bool ClassInterfaceRequest(USBSetup& setup)
378 if (CDC_ACM_INTERFACE == i)
379 return CDC_Setup(setup);
381 #ifdef PLUGGABLE_USB_ENABLED
382 return PluggableUSB().setup(setup);
389 void InitControl(int end)
397 bool SendControl(u8 d)
401 if (!WaitForINOrOUT())
404 if (!((_cmark + 1) & 0x3F))
405 ClearIN(); // Fifo is full, release this packet
411 // Clipped by _cmark/_cend
412 int USB_SendControl(u8 flags, const void* d, int len)
415 const u8* data = (const u8*)d;
416 bool pgm = flags & TRANSFER_PGM;
419 u8 c = pgm ? pgm_read_byte(data++) : *data++;
426 // Send a USB descriptor string. The string is stored in PROGMEM as a
427 // plain ASCII string but is sent out as UTF-16 with the correct 2-byte
429 static bool USB_SendStringDescriptor(const u8*string_P, u8 string_len, uint8_t flags) {
430 SendControl(2 + string_len * 2);
432 bool pgm = flags & TRANSFER_PGM;
433 for(u8 i = 0; i < string_len; i++) {
434 bool r = SendControl(pgm ? pgm_read_byte(&string_P[i]) : string_P[i]);
435 r &= SendControl(0); // high byte
443 // Does not timeout or cross fifo boundaries
444 int USB_RecvControl(void* d, int len)
449 // Dont receive more than the USB Control EP has to offer
450 // Use fixed 64 because control EP always have 64 bytes even on 16u2.
451 auto recvLength = length;
456 // Write data to fit to the end (not the beginning) of the array
458 Recv((u8*)d + len - length, recvLength);
460 length -= recvLength;
465 static u8 SendInterfaces()
469 CDC_GetInterface(&interfaces);
471 #ifdef PLUGGABLE_USB_ENABLED
472 PluggableUSB().getInterface(&interfaces);
478 // Construct a dynamic configuration descriptor
479 // This really needs dynamic endpoint allocation etc
482 bool SendConfiguration(int maxlen)
484 // Count and measure interfaces
486 u8 interfaces = SendInterfaces();
487 ConfigDescriptor config = D_CONFIG(_cmark + sizeof(ConfigDescriptor),interfaces);
491 USB_SendControl(0,&config,sizeof(ConfigDescriptor));
497 bool SendDescriptor(USBSetup& setup)
499 u8 t = setup.wValueH;
500 if (USB_CONFIGURATION_DESCRIPTOR_TYPE == t)
501 return SendConfiguration(setup.wLength);
503 InitControl(setup.wLength);
504 #ifdef PLUGGABLE_USB_ENABLED
505 int ret = PluggableUSB().getDescriptor(setup);
507 return (ret > 0 ? true : false);
511 const u8* desc_addr = 0;
512 if (USB_DEVICE_DESCRIPTOR_TYPE == t)
514 desc_addr = (const u8*)&USB_DeviceDescriptorIAD;
516 else if (USB_STRING_DESCRIPTOR_TYPE == t)
518 if (setup.wValueL == 0) {
519 desc_addr = (const u8*)&STRING_LANGUAGE;
521 else if (setup.wValueL == IPRODUCT) {
522 return USB_SendStringDescriptor(STRING_PRODUCT, strlen(USB_PRODUCT), TRANSFER_PGM);
524 else if (setup.wValueL == IMANUFACTURER) {
525 return USB_SendStringDescriptor(STRING_MANUFACTURER, strlen(USB_MANUFACTURER), TRANSFER_PGM);
527 else if (setup.wValueL == ISERIAL) {
528 #ifdef PLUGGABLE_USB_ENABLED
529 char name[ISERIAL_MAX_LEN];
530 PluggableUSB().getShortName(name);
531 return USB_SendStringDescriptor((uint8_t*)name, strlen(name), 0);
540 u8 desc_length = pgm_read_byte(desc_addr);
542 USB_SendControl(TRANSFER_PGM,desc_addr,desc_length);
546 // Endpoint 0 interrupt
550 if (!ReceivedSetupInt())
557 u8 requestType = setup.bmRequestType;
558 if (requestType & REQUEST_DEVICETOHOST)
564 if (REQUEST_STANDARD == (requestType & REQUEST_TYPE))
567 u8 r = setup.bRequest;
568 u16 wValue = setup.wValueL | (setup.wValueH << 8);
571 if (requestType == (REQUEST_DEVICETOHOST | REQUEST_STANDARD | REQUEST_DEVICE))
573 Send8(_usbCurrentStatus);
578 // TODO: handle the HALT state of an endpoint here
579 // see "Figure 9-6. Information Returned by a GetStatus() Request to an Endpoint" in usb_20.pdf for more information
584 else if (CLEAR_FEATURE == r)
586 if((requestType == (REQUEST_HOSTTODEVICE | REQUEST_STANDARD | REQUEST_DEVICE))
587 && (wValue == DEVICE_REMOTE_WAKEUP))
589 _usbCurrentStatus &= ~FEATURE_REMOTE_WAKEUP_ENABLED;
592 else if (SET_FEATURE == r)
594 if((requestType == (REQUEST_HOSTTODEVICE | REQUEST_STANDARD | REQUEST_DEVICE))
595 && (wValue == DEVICE_REMOTE_WAKEUP))
597 _usbCurrentStatus |= FEATURE_REMOTE_WAKEUP_ENABLED;
600 else if (SET_ADDRESS == r)
603 UDADDR = setup.wValueL | (1<<ADDEN);
605 else if (GET_DESCRIPTOR == r)
607 ok = SendDescriptor(setup);
609 else if (SET_DESCRIPTOR == r)
613 else if (GET_CONFIGURATION == r)
617 else if (SET_CONFIGURATION == r)
619 if (REQUEST_DEVICE == (requestType & REQUEST_RECIPIENT))
622 _usbConfiguration = setup.wValueL;
626 else if (GET_INTERFACE == r)
629 else if (SET_INTERFACE == r)
635 InitControl(setup.wLength); // Max length of transfer
636 ok = ClassInterfaceRequest(setup);
647 void USB_Flush(u8 ep)
654 static inline void USB_ClockDisable()
657 USBCON = (USBCON & ~(1<<OTGPADE)) | (1<<FRZCLK); // freeze clock and disable VBUS Pad
659 USBCON = (1 << FRZCLK); // freeze clock
661 PLLCSR &= ~(1<<PLLE); // stop PLL
664 static inline void USB_ClockEnable()
667 UHWCON |= (1<<UVREGE); // power internal reg
669 USBCON = (1<<USBE) | (1<<FRZCLK); // clock frozen, usb enabled
673 #if F_CPU == 16000000UL
674 PLLCSR |= (1<<PINDIV); // Need 16 MHz xtal
675 #elif F_CPU == 8000000UL
676 PLLCSR &= ~(1<<PINDIV); // Need 8 MHz xtal
678 #error "Clock rate of F_CPU not supported"
681 #elif defined(__AVR_AT90USB82__) || defined(__AVR_AT90USB162__) || defined(__AVR_ATmega32U2__) || defined(__AVR_ATmega16U2__) || defined(__AVR_ATmega8U2__)
682 // for the u2 Series the datasheet is confusing. On page 40 its called PINDIV and on page 290 its called PLLP0
683 #if F_CPU == 16000000UL
685 PLLCSR |= (1 << PLLP0);
686 #elif F_CPU == 8000000UL
688 PLLCSR &= ~(1 << PLLP0);
691 // AT90USB646, AT90USB647, AT90USB1286, AT90USB1287
693 #if F_CPU == 16000000UL
694 #if defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__)
695 // For Atmel AT90USB128x only. Do not use with Atmel AT90USB64x.
696 PLLCSR = (PLLCSR & ~(1<<PLLP1)) | ((1<<PLLP2) | (1<<PLLP0)); // Need 16 MHz xtal
697 #elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB647__)
698 // For AT90USB64x only. Do not use with AT90USB128x.
699 PLLCSR = (PLLCSR & ~(1<<PLLP0)) | ((1<<PLLP2) | (1<<PLLP1)); // Need 16 MHz xtal
701 #error "USB Chip not supported, please defined method of USB PLL initialization"
703 #elif F_CPU == 8000000UL
704 // for Atmel AT90USB128x and AT90USB64x
705 PLLCSR = (PLLCSR & ~(1<<PLLP2)) | ((1<<PLLP1) | (1<<PLLP0)); // Need 8 MHz xtal
707 #error "Clock rate of F_CPU not supported"
710 #error "USB Chip not supported, please defined method of USB PLL initialization"
714 while (!(PLLCSR & (1<<PLOCK))) // wait for lock pll
718 // Some tests on specific versions of macosx (10.7.3), reported some
719 // strange behaviors when the board is reset using the serial
720 // port touch at 1200 bps. This delay fixes this behavior.
723 USBCON = (USBCON & ~(1<<FRZCLK)) | (1<<OTGPADE); // start USB clock, enable VBUS Pad
725 USBCON &= ~(1 << FRZCLK); // start USB clock
730 UDCON &= ~((1<<RSTCPU) | (1<<LSM) | (1<<RMWKUP) | (1<<DETACH)); // enable attach resistor, set full speed mode
732 UDCON &= ~((1 << RSTCPU) | (1 << RMWKUP) | (1 << DETACH)); // enable attach resistor, set full speed mode
735 // AT90USB64x and AT90USB128x don't have RSTCPU
736 UDCON &= ~((1<<LSM) | (1<<RMWKUP) | (1<<DETACH)); // enable attach resistor, set full speed mode
744 UDINT &= ~((1<<EORSTI) | (1<<SOFI)); // clear the IRQ flags for the IRQs which are handled here, except WAKEUPI and SUSPI (see below)
747 if (udint & (1<<EORSTI))
749 InitEP(0,EP_TYPE_CONTROL,EP_SINGLE_64); // init ep0
750 _usbConfiguration = 0; // not configured yet
751 UEIENX = 1 << RXSTPE; // Enable interrupts for ep0
754 // Start of Frame - happens every millisecond so we use it for TX and RX LED one-shot timing, too
755 if (udint & (1<<SOFI))
757 USB_Flush(CDC_TX); // Send a tx frame if found
759 // check whether the one-shot period has elapsed. if so, turn off the LED
760 if (TxLEDPulse && !(--TxLEDPulse))
762 if (RxLEDPulse && !(--RxLEDPulse))
766 // the WAKEUPI interrupt is triggered as soon as there are non-idle patterns on the data
767 // lines. Thus, the WAKEUPI interrupt can occur even if the controller is not in the "suspend" mode.
768 // Therefore the we enable it only when USB is suspended
769 if (udint & (1<<WAKEUPI))
771 UDIEN = (UDIEN & ~(1<<WAKEUPE)) | (1<<SUSPE); // Disable interrupts for WAKEUP and enable interrupts for SUSPEND
774 // WAKEUPI shall be cleared by software (USB clock inputs must be enabled before).
776 UDINT &= ~(1<<WAKEUPI);
777 _usbSuspendState = (_usbSuspendState & ~(1<<SUSPI)) | (1<<WAKEUPI);
779 else if (udint & (1<<SUSPI)) // only one of the WAKEUPI / SUSPI bits can be active at time
781 UDIEN = (UDIEN & ~(1<<SUSPE)) | (1<<WAKEUPE); // Disable interrupts for SUSPEND and enable interrupts for WAKEUP
784 //USB_ClockDisable();
786 UDINT &= ~((1<<WAKEUPI) | (1<<SUSPI)); // clear any already pending WAKEUP IRQs and the SUSPI request
787 _usbSuspendState = (_usbSuspendState & ~(1<<WAKEUPI)) | (1<<SUSPI);
791 // VBUS or counting frames
792 // Any frame counting?
800 //=======================================================================
801 //=======================================================================
803 USBDevice_ USBDevice;
805 USBDevice_::USBDevice_()
809 void USBDevice_::attach()
811 _usbConfiguration = 0;
812 _usbCurrentStatus = 0;
813 _usbSuspendState = 0;
816 UDINT &= ~((1<<WAKEUPI) | (1<<SUSPI)); // clear already pending WAKEUP / SUSPEND requests
817 UDIEN = (1<<EORSTE) | (1<<SOFE) | (1<<SUSPE); // Enable interrupts for EOR (End of Reset), SOF (start of frame) and SUSPEND
822 void USBDevice_::detach()
826 // Check for interrupts
827 // TODO: VBUS detection
828 bool USBDevice_::configured()
830 return _usbConfiguration;
833 void USBDevice_::poll()
837 bool USBDevice_::wakeupHost()
839 // clear any previous wakeup request which might have been set but could be processed at that time
840 // e.g. because the host was not suspended at that time
841 UDCON &= ~(1 << RMWKUP);
843 if(!(UDCON & (1 << RMWKUP))
844 && (_usbSuspendState & (1<<SUSPI))
845 && (_usbCurrentStatus & FEATURE_REMOTE_WAKEUP_ENABLED))
847 // This short version will only work, when the device has not been suspended. Currently the
848 // Arduino core doesn't handle SUSPEND at all, so this is ok.
850 UDCON |= (1 << RMWKUP); // send the wakeup request
857 bool USBDevice_::isSuspended()
859 return (_usbSuspendState & (1 << SUSPI));
863 #endif /* if defined(USBCON) */