@@ -93,6 +93,7 @@ const DeviceDescriptor USB_DeviceDescriptorA =
93
93
94
94
volatile u8 _usbConfiguration = 0 ;
95
95
volatile u8 _usbCurrentStatus = 0 ; // meaning of bits see usb_20.pdf, Figure 9-4. Information Returned by a GetStatus() Request to a Device
96
+ volatile u8 _usbSuspendState = 0 ; // copy of UDINT to check SUSPI and WAKEUPI bits
96
97
97
98
static inline void WaitIN (void )
98
99
{
@@ -614,11 +615,39 @@ void USB_Flush(u8 ep)
614
615
ReleaseTX ();
615
616
}
616
617
618
+ static inline void USB_ClockDisable ()
619
+ {
620
+ USBCON = (USBCON & ~(1 <<OTGPADE)) | (1 <<FRZCLK); // freeze clock and disable VBUS Pad
621
+ PLLCSR &= ~(1 <<PLLE); // stop PLL
622
+ }
623
+
624
+ static inline void USB_ClockEnable ()
625
+ {
626
+ UHWCON |= (1 <<UVREGE); // power internal reg
627
+ USBCON = (1 <<USBE) | (1 <<FRZCLK); // clock frozen, usb enabled
628
+ #if F_CPU == 16000000UL
629
+ PLLCSR = (1 <<PINDIV); // Need 16 MHz xtal
630
+ #elif F_CPU == 8000000UL
631
+ PLLCSR = 0x00 ; // Need 8 MHz xtal
632
+ #endif
633
+ PLLCSR |= (1 <<PLLE);
634
+ while (!(PLLCSR & (1 <<PLOCK))) // wait for lock pll
635
+ {
636
+ }
637
+
638
+ // Some tests on specific versions of macosx (10.7.3), reported some
639
+ // strange behaviuors when the board is reset using the serial
640
+ // port touch at 1200 bps. This delay fixes this behaviour.
641
+ delay (1 );
642
+ USBCON = (USBCON & ~(1 <<FRZCLK)) | (1 <<OTGPADE); // start USB clock, enable VBUS Pad
643
+ }
644
+
645
+
617
646
// General interrupt
618
647
ISR (USB_GEN_vect)
619
648
{
620
649
u8 udint = UDINT;
621
- UDINT = 0 ;
650
+ UDINT &= ~(( 1 <<EORSTI) | ( 1 <<SOFI)); // clear the IRQ flags for the IRQs which are handled here, except WAKEUPI and SUSPI (see below)
622
651
623
652
// End of Reset
624
653
if (udint & (1 <<EORSTI))
@@ -643,6 +672,32 @@ ISR(USB_GEN_vect)
643
672
if (RxLEDPulse && !(--RxLEDPulse))
644
673
RXLED0;
645
674
}
675
+
676
+ // the WAKEUPI interrupt is triggered as soon as there are non-idle patterns on the data
677
+ // lines. Thus, the WAKEUPI interrupt can occur even if the controller is not in the "suspend" mode.
678
+ // Therefore the we enable it only when USB is suspended
679
+ if (udint & (1 <<WAKEUPI))
680
+ {
681
+ UDIEN = (UDIEN & ~(1 <<WAKEUPE)) | (1 <<SUSPE); // Disable interrupts for WAKEUP and enable interrupts for SUSPEND
682
+
683
+ // TODO
684
+ // WAKEUPI shall be cleared by software (USB clock inputs must be enabled before).
685
+ // USB_ClockEnable();
686
+ UDINT &= ~(1 <<WAKEUPI);
687
+ _usbSuspendState = (1 <<WAKEUPI);
688
+ }
689
+ else if (udint & (1 <<SUSPI)) // only one of the WAKEUPI / SUSPI bits can be active at time
690
+ {
691
+ // disable SUSPEND interrupts, because the SUSPI IRQ flag is not cleared and would trigger end endless IRQ loop
692
+ // the SUSPI flag is needed to detect the current suspend state in wakeupHost
693
+ UDIEN = (UDIEN & ~(1 <<SUSPE)) | (1 <<WAKEUPE); // Disable interrupts for SUSPEND and enable interrupts for WAKEUP
694
+
695
+ // TODO
696
+ // USB_ClockDisable();
697
+
698
+ UDINT &= ~((1 <<WAKEUPI) | (1 <<SUSPI)); // clear any already pending WAKEUP IRQs and the SUSPI request
699
+ _usbSuspendState = (1 <<SUSPI);
700
+ }
646
701
}
647
702
648
703
// VBUS or counting frames
@@ -667,24 +722,10 @@ void USBDevice_::attach()
667
722
{
668
723
_usbConfiguration = 0 ;
669
724
_usbCurrentStatus = 0 ;
670
- UHWCON = 0x01 ; // power internal reg
671
- USBCON = (1 <<USBE)|(1 <<FRZCLK); // clock frozen, usb enabled
672
- #if F_CPU == 16000000UL
673
- PLLCSR = 0x12 ; // Need 16 MHz xtal
674
- #elif F_CPU == 8000000UL
675
- PLLCSR = 0x02 ; // Need 8 MHz xtal
676
- #endif
677
- while (!(PLLCSR & (1 <<PLOCK))) // wait for lock pll
678
- ;
679
-
680
- // Some tests on specific versions of macosx (10.7.3), reported some
681
- // strange behaviuors when the board is reset using the serial
682
- // port touch at 1200 bps. This delay fixes this behaviour.
683
- delay (1 );
725
+ USB_ClockEnable ();
684
726
685
- USBCON = ((1 <<USBE)|(1 <<OTGPADE)); // start USB clock
686
- UDIEN = (1 <<EORSTE)|(1 <<SOFE); // Enable interrupts for EOR (End of Reset) and SOF (start of frame)
687
- UDCON = 0 ; // enable attach resistor
727
+ UDIEN = (1 <<EORSTE) | (1 <<SOFE) | (1 <<SUSPE); // Enable interrupts for EOR (End of Reset), SOF (start of frame) and SUSPEND
728
+ UDCON &= ~((1 <<RSTCPU | (1 <<LSM) | (1 <<RMWKUP) | (1 <<DETACH))); // enable attach resistor, set full speed mode
688
729
689
730
TX_RX_LED_INIT;
690
731
}
@@ -711,10 +752,13 @@ bool USBDevice_::wakeupHost()
711
752
// e.g. because the host was not suspended at that time
712
753
UDCON &= ~(1 << RMWKUP);
713
754
714
- if (!(UDCON & (1 << RMWKUP)) && (_usbCurrentStatus & FEATURE_REMOTE_WAKEUP_ENABLED))
755
+ if (!(UDCON & (1 << RMWKUP))
756
+ && (_usbSuspendState & (1 <<SUSPI))
757
+ && (_usbCurrentStatus & FEATURE_REMOTE_WAKEUP_ENABLED))
715
758
{
716
759
// This short version will only work, when the device has not been suspended. Currently the
717
760
// Arduino core doesn't handle SUSPEND at all, so this is ok.
761
+ USB_ClockEnable ();
718
762
UDCON |= (1 << RMWKUP); // send the wakeup request
719
763
return true ;
720
764
}
0 commit comments