Skip to content

Commit b907431

Browse files
committed
Added first part of SUSPEND / WAKEUP handling; store SUSPI in _usbSuspendState
1 parent 966748f commit b907431

File tree

1 file changed

+63
-19
lines changed

1 file changed

+63
-19
lines changed

hardware/arduino/cores/arduino/USBCore.cpp

Lines changed: 63 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ const DeviceDescriptor USB_DeviceDescriptorA =
9393

9494
volatile u8 _usbConfiguration = 0;
9595
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
9697

9798
static inline void WaitIN(void)
9899
{
@@ -614,11 +615,39 @@ void USB_Flush(u8 ep)
614615
ReleaseTX();
615616
}
616617

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+
617646
// General interrupt
618647
ISR(USB_GEN_vect)
619648
{
620649
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)
622651

623652
// End of Reset
624653
if (udint & (1<<EORSTI))
@@ -643,6 +672,32 @@ ISR(USB_GEN_vect)
643672
if (RxLEDPulse && !(--RxLEDPulse))
644673
RXLED0;
645674
}
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+
}
646701
}
647702

648703
// VBUS or counting frames
@@ -667,24 +722,10 @@ void USBDevice_::attach()
667722
{
668723
_usbConfiguration = 0;
669724
_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();
684726

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
688729

689730
TX_RX_LED_INIT;
690731
}
@@ -711,10 +752,13 @@ bool USBDevice_::wakeupHost()
711752
// e.g. because the host was not suspended at that time
712753
UDCON &= ~(1 << RMWKUP);
713754

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))
715758
{
716759
// This short version will only work, when the device has not been suspended. Currently the
717760
// Arduino core doesn't handle SUSPEND at all, so this is ok.
761+
USB_ClockEnable();
718762
UDCON |= (1 << RMWKUP); // send the wakeup request
719763
return true;
720764
}

0 commit comments

Comments
 (0)