@@ -78,6 +78,8 @@ const DeviceDescriptor USB_DeviceDescriptorB =
78
78
// ==================================================================
79
79
80
80
volatile u8 _usbConfiguration = 0 ;
81
+ volatile u8 _usbCurrentStatus = 0 ; // meaning of bits see usb_20.pdf, Figure 9-4. Information Returned by a GetStatus() Request to a Device
82
+ volatile u8 _usbSuspendState = 0 ; // copy of UDINT to check SUSPI and WAKEUPI bits
81
83
82
84
static inline void WaitIN (void )
83
85
{
@@ -329,7 +331,7 @@ static
329
331
void InitEP (u8 index, u8 type, u8 size)
330
332
{
331
333
UENUM = index ;
332
- UECONX = 1 ;
334
+ UECONX = ( 1 <<EPEN) ;
333
335
UECFG0X = type;
334
336
UECFG1X = size;
335
337
}
@@ -340,7 +342,7 @@ void InitEndpoints()
340
342
for (u8 i = 1 ; i < sizeof (_initEndpoints) && _initEndpoints[i] != 0 ; i++)
341
343
{
342
344
UENUM = i;
343
- UECONX = 1 ;
345
+ UECONX = ( 1 <<EPEN) ;
344
346
UECFG0X = _initEndpoints[i];
345
347
UECFG1X = EP_DOUBLE_64;
346
348
}
@@ -530,16 +532,37 @@ ISR(USB_COM_vect)
530
532
{
531
533
// Standard Requests
532
534
u8 r = setup.bRequest ;
535
+ u16 wValue = setup.wValueL | (setup.wValueH << 8 );
533
536
if (GET_STATUS == r)
534
537
{
535
- Send8 (0 ); // TODO
536
- Send8 (0 );
538
+ if (requestType == (REQUEST_DEVICETOHOST | REQUEST_STANDARD | REQUEST_DEVICE))
539
+ {
540
+ Send8 (_usbCurrentStatus);
541
+ Send8 (0 );
542
+ }
543
+ else
544
+ {
545
+ // TODO: handle the HALT state of an endpoint here
546
+ // see "Figure 9-6. Information Returned by a GetStatus() Request to an Endpoint" in usb_20.pdf for more information
547
+ Send8 (0 );
548
+ Send8 (0 );
549
+ }
537
550
}
538
551
else if (CLEAR_FEATURE == r)
539
552
{
553
+ if ((requestType == (REQUEST_HOSTTODEVICE | REQUEST_STANDARD | REQUEST_DEVICE))
554
+ && (wValue == DEVICE_REMOTE_WAKEUP))
555
+ {
556
+ _usbCurrentStatus &= ~FEATURE_REMOTE_WAKEUP_ENABLED;
557
+ }
540
558
}
541
559
else if (SET_FEATURE == r)
542
560
{
561
+ if ((requestType == (REQUEST_HOSTTODEVICE | REQUEST_STANDARD | REQUEST_DEVICE))
562
+ && (wValue == DEVICE_REMOTE_WAKEUP))
563
+ {
564
+ _usbCurrentStatus |= FEATURE_REMOTE_WAKEUP_ENABLED;
565
+ }
543
566
}
544
567
else if (SET_ADDRESS == r)
545
568
{
@@ -595,11 +618,73 @@ void USB_Flush(u8 ep)
595
618
ReleaseTX ();
596
619
}
597
620
621
+ static inline void USB_ClockDisable ()
622
+ {
623
+ USBCON = (USBCON & ~(1 <<OTGPADE)) | (1 <<FRZCLK); // freeze clock and disable VBUS Pad
624
+ PLLCSR &= ~(1 <<PLLE); // stop PLL
625
+ }
626
+
627
+ static inline void USB_ClockEnable ()
628
+ {
629
+ UHWCON |= (1 <<UVREGE); // power internal reg
630
+ USBCON = (1 <<USBE) | (1 <<FRZCLK); // clock frozen, usb enabled
631
+
632
+ // ATmega32U4
633
+ #if defined(PINDIV)
634
+ #if F_CPU == 16000000UL
635
+ PLLCSR |= (1 <<PINDIV); // Need 16 MHz xtal
636
+ #elif F_CPU == 8000000UL
637
+ PLLCSR &= ~(1 <<PINDIV); // Need 8 MHz xtal
638
+ #else
639
+ #error "Clock rate of F_CPU not supported"
640
+ #endif
641
+
642
+ // AT90USB646, AT90USB647, AT90USB1286, AT90USB1287
643
+ #elif defined(PLLP2)
644
+ #if F_CPU == 16000000UL
645
+ #if defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__)
646
+ // For Atmel AT90USB128x only. Do not use with Atmel AT90USB64x.
647
+ PLLCSR = (PLLCSR & ~(1 <<PLLP1)) | ((1 <<PLLP2) | (1 <<PLLP0)); // Need 16 MHz xtal
648
+ #elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB647__)
649
+ // For AT90USB64x only. Do not use with AT90USB128x.
650
+ PLLCSR = (PLLCSR & ~(1 <<PLLP0)) | ((1 <<PLLP2) | (1 <<PLLP1)); // Need 16 MHz xtal
651
+ #else
652
+ #error "USB Chip not supported, please defined method of USB PLL initialization"
653
+ #endif
654
+ #elif F_CPU == 8000000UL
655
+ // for Atmel AT90USB128x and AT90USB64x
656
+ PLLCSR = (PLLCSR & ~(1 <<PLLP2)) | ((1 <<PLLP1) | (1 <<PLLP0)); // Need 8 MHz xtal
657
+ #else
658
+ #error "Clock rate of F_CPU not supported"
659
+ #endif
660
+ #else
661
+ #error "USB Chip not supported, please defined method of USB PLL initialization"
662
+ #endif
663
+
664
+ PLLCSR |= (1 <<PLLE);
665
+ while (!(PLLCSR & (1 <<PLOCK))) // wait for lock pll
666
+ {
667
+ }
668
+
669
+ // Some tests on specific versions of macosx (10.7.3), reported some
670
+ // strange behaviors when the board is reset using the serial
671
+ // port touch at 1200 bps. This delay fixes this behavior.
672
+ delay (1 );
673
+ USBCON = (USBCON & ~(1 <<FRZCLK)) | (1 <<OTGPADE); // start USB clock, enable VBUS Pad
674
+
675
+ #if defined(RSTCPU)
676
+ UDCON &= ~((1 <<RSTCPU) | (1 <<LSM) | (1 <<RMWKUP) | (1 <<DETACH)); // enable attach resistor, set full speed mode
677
+ #else
678
+ // AT90USB64x and AT90USB128x don't have RSTCPU
679
+ UDCON &= ~((1 <<LSM) | (1 <<RMWKUP) | (1 <<DETACH)); // enable attach resistor, set full speed mode
680
+ #endif
681
+ }
682
+
598
683
// General interrupt
599
684
ISR (USB_GEN_vect)
600
685
{
601
686
u8 udint = UDINT;
602
- UDINT = 0 ;
687
+ UDINT = UDINT &= ~(( 1 <<EORSTI) | ( 1 <<SOFI)); // clear the IRQ flags for the IRQs which are handled here, except WAKEUPI and SUSPI (see below)
603
688
604
689
// End of Reset
605
690
if (udint & (1 <<EORSTI))
@@ -620,6 +705,30 @@ ISR(USB_GEN_vect)
620
705
if (RxLEDPulse && !(--RxLEDPulse))
621
706
RXLED0;
622
707
}
708
+
709
+ // the WAKEUPI interrupt is triggered as soon as there are non-idle patterns on the data
710
+ // lines. Thus, the WAKEUPI interrupt can occur even if the controller is not in the "suspend" mode.
711
+ // Therefore the we enable it only when USB is suspended
712
+ if (udint & (1 <<WAKEUPI))
713
+ {
714
+ UDIEN = (UDIEN & ~(1 <<WAKEUPE)) | (1 <<SUSPE); // Disable interrupts for WAKEUP and enable interrupts for SUSPEND
715
+
716
+ // TODO
717
+ // WAKEUPI shall be cleared by software (USB clock inputs must be enabled before).
718
+ // USB_ClockEnable();
719
+ UDINT &= ~(1 <<WAKEUPI);
720
+ _usbSuspendState = (_usbSuspendState & ~(1 <<SUSPI)) | (1 <<WAKEUPI);
721
+ }
722
+ else if (udint & (1 <<SUSPI)) // only one of the WAKEUPI / SUSPI bits can be active at time
723
+ {
724
+ UDIEN = (UDIEN & ~(1 <<SUSPE)) | (1 <<WAKEUPE); // Disable interrupts for SUSPEND and enable interrupts for WAKEUP
725
+
726
+ // TODO
727
+ // USB_ClockDisable();
728
+
729
+ UDINT &= ~((1 <<WAKEUPI) | (1 <<SUSPI)); // clear any already pending WAKEUP IRQs and the SUSPI request
730
+ _usbSuspendState = (_usbSuspendState & ~(1 <<WAKEUPI)) | (1 <<SUSPI);
731
+ }
623
732
}
624
733
625
734
// VBUS or counting frames
@@ -643,24 +752,12 @@ USBDevice_::USBDevice_()
643
752
void USBDevice_::attach ()
644
753
{
645
754
_usbConfiguration = 0 ;
646
- UHWCON = 0x01 ; // power internal reg
647
- USBCON = (1 <<USBE)|(1 <<FRZCLK); // clock frozen, usb enabled
648
- #if F_CPU == 16000000UL
649
- PLLCSR = 0x12 ; // Need 16 MHz xtal
650
- #elif F_CPU == 8000000UL
651
- PLLCSR = 0x02 ; // Need 8 MHz xtal
652
- #endif
653
- while (!(PLLCSR & (1 <<PLOCK))) // wait for lock pll
654
- ;
755
+ _usbCurrentStatus = 0 ;
756
+ _usbSuspendState = 0 ;
757
+ USB_ClockEnable ();
655
758
656
- // Some tests on specific versions of macosx (10.7.3), reported some
657
- // strange behaviuors when the board is reset using the serial
658
- // port touch at 1200 bps. This delay fixes this behaviour.
659
- delay (1 );
660
-
661
- USBCON = ((1 <<USBE)|(1 <<OTGPADE)); // start USB clock
662
- UDIEN = (1 <<EORSTE)|(1 <<SOFE); // Enable interrupts for EOR (End of Reset) and SOF (start of frame)
663
- UDCON = 0 ; // enable attach resistor
759
+ UDINT &= ~((1 <<WAKEUPI) | (1 <<SUSPI)); // clear already pending WAKEUP / SUSPEND requests
760
+ UDIEN = (1 <<EORSTE) | (1 <<SOFE) | (1 <<SUSPE); // Enable interrupts for EOR (End of Reset), SOF (start of frame) and SUSPEND
664
761
665
762
TX_RX_LED_INIT;
666
763
}
@@ -680,4 +777,24 @@ void USBDevice_::poll()
680
777
{
681
778
}
682
779
780
+ bool USBDevice_::wakeupHost ()
781
+ {
782
+ // clear any previous wakeup request which might have been set but could be processed at that time
783
+ // e.g. because the host was not suspended at that time
784
+ UDCON &= ~(1 << RMWKUP);
785
+
786
+ if (!(UDCON & (1 << RMWKUP))
787
+ && (_usbSuspendState & (1 <<SUSPI))
788
+ && (_usbCurrentStatus & FEATURE_REMOTE_WAKEUP_ENABLED))
789
+ {
790
+ // This short version will only work, when the device has not been suspended. Currently the
791
+ // Arduino core doesn't handle SUSPEND at all, so this is ok.
792
+ USB_ClockEnable ();
793
+ UDCON |= (1 << RMWKUP); // send the wakeup request
794
+ return true ;
795
+ }
796
+
797
+ return false ;
798
+ }
799
+
683
800
#endif /* if defined(USBCON) */
0 commit comments