Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deepSleep() incompatibility with STM32duino USB support #114

Open
rayozzie opened this issue Oct 4, 2024 · 0 comments
Open

deepSleep() incompatibility with STM32duino USB support #114

rayozzie opened this issue Oct 4, 2024 · 0 comments
Labels

Comments

@rayozzie
Copy link

rayozzie commented Oct 4, 2024

My sketch is using Arduino IDE 2.3.3 with STM32duino version 2.8.1 with a Blues Cygnet (STM32L433) mcu which has an LSE.

As many do, I always initialize my sketch with:

#define debug Serial
    debug.begin(115200);
    bool serialInitialized = false;
    if (waitForSerialInit) {
	uint64_t expiresMs = millis64() + 2500;
        while (millis64() < expiresMs) {
            if (debug) {
                serialInitialized = true;
                break;
            }
        }
    }

I then initialize the low power package with

    rtc.setClockSource(STM32RTC::LSE_CLOCK);
    rtc.setBinaryMode(STM32RTC::MODE_BCD);
    rtc.begin(true);
    LowPower.begin();

In my loop, when I'd like to go into deep sleep mode I do

    LowPower.deepSleep(sleepMs);

This works wonderfully, and sleeps for sleepMs when the above "serialInitialized" returns 'true'.

The LowPower.deepSleep() call returns in just one or two milliseconds, regardless of sleepMs, when the above "serialInitialized" returns 'false'.

The core problem turns out to be the fact that, when using Arduino's
USB Support: CDC (generic 'Serial' supersede U(S)ART)
...IF AND ONLY IF the USB is unplugged at the time of setup(). And if indeed the USB is unplugged at the time of setup(), (as helpfully indicated by serialInitialized being false) deepSleep() will fail.

If the USB is plugged in during setup(), OR if USB Support is set to 'None', serialInitialized returns 'true' and deepSleep() works fine.

The workaround in my device (which happens to have a screen) is to use that serialInitialized flag to warn the user to rebuild the sketch without USB support, however it would be best if deepSleep() were enhanced so that it does not immediately wake if USB support is configured but unplugged.

Thank you for a great library and for your consideration.

@rayozzie rayozzie added the bug label Oct 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant