Skip to content

Standard Arduino practices cause WDT to reset esp8266 #540

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

Closed
Makuna opened this issue Jul 12, 2015 · 13 comments
Closed

Standard Arduino practices cause WDT to reset esp8266 #540

Makuna opened this issue Jul 12, 2015 · 13 comments

Comments

@Makuna
Copy link
Collaborator

Makuna commented Jul 12, 2015

One standard practice in many examples is to wait for serial input in a tight loop like ...

while (!Serial.available());

This will cause the WDT to trigger.

Can we insert a yield inside the available() when it would return false so that this common code fragment would not cause a processor reset?

@me-no-dev
Copy link
Collaborator

@Makuna doing it is not a bad idea, but it might put the current code to wait when that is not expected.
Any use cases that would have a problem come to mind?
Can't we find a better way? Maybe find out before wdt triggers and feed it? Do you know of any relevant interrupt vector?

@skandragon
Copy link

Does the standard Arduino even have a WDT enabled by default? I also see no mention of a WDT in this project's README, other than this little bit:

ESP-specific APIs

APIs related to deep sleep and watchdog timer are available in the ESP object,
only available in Alpha version.

Also, no mention how to find and use this "alpha" version (which I assume is based on master?)

@Makuna
Copy link
Collaborator Author

Makuna commented Jul 12, 2015

The standard Arduino (AVR) does NOT have the WDT turned on by default. AVR does support a WDT in the processor (I use it) but its uncommon to find it being used.
The esp8266 does due to the WiFi that runs in the background.
This is also why I believe most of the available() methods should contain a yield() so that the WiFi will continue to function as expected. Note that a few of the available() methods on libraries already have a yield() in them, but not all.
I have the changes and will be testing it shortly.

@Makuna
Copy link
Collaborator Author

Makuna commented Jul 12, 2015

This is what is done in some available() methods, and the proposal is to replicate something similar to all of them.

    static uint32_t lastPollTime = 0;

    if (lastPollTime > esp_micros_at_task_start())
        yield();

@me-no-dev
Copy link
Collaborator

reding the source behind, I come to conclusion that yield will be called every next time, but first, inside the same loop() call. Am i getting this right?
esp_micros_at_task_start is set before calling each loop()

@Makuna
Copy link
Collaborator Author

Makuna commented Jul 12, 2015

I think that code is flawed, you are correct in one way, but it has a problem, if the loop is entered when micros is near the top of the range, then wraps, this test will calculate true faster than it normally will.
The concept should be, if a specific amount of time has passed since the last yield (while start of the loop is ok also), then yield.

Here is my working example, removing the static variable completely. It also only yields when there is nothing available, with the thought that if there is something available, the caller will want to get to it quickly.

    if (!isAvailable && (micros() - esp_micros_at_task_start()) > 10000)
    {
        yield();
    }

@me-no-dev
Copy link
Collaborator

calling yield after the first time makes perfect sense when we are talking about network streams, because after consuming the first available, we need to yield to get the next packet in the buffer.
When talking about Serial, maybe yielding on nothing available or time close to wdt is better idea.

@me-no-dev
Copy link
Collaborator

you are correct on the flawed comparison

@Makuna
Copy link
Collaborator Author

Makuna commented Jul 12, 2015

What I list above will only yield if there is no data available, but it will yield often after 10ms since the last loop start.

@me-no-dev
Copy link
Collaborator

looks great

@Makuna Makuna mentioned this issue Jul 12, 2015
@Makuna
Copy link
Collaborator Author

Makuna commented Jul 13, 2015

new pull to help solve this #551

@Makuna
Copy link
Collaborator Author

Makuna commented Jul 15, 2015

please review the new pull, it is cleaner and provides a consistent experiences.
Further, the latest improvement will also never yield within an ISR.

@Makuna
Copy link
Collaborator Author

Makuna commented Jul 16, 2015

1154545 Merged

@Makuna Makuna closed this as completed Jul 16, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants