-
Notifications
You must be signed in to change notification settings - Fork 7.6k
timerRestart does not restart timer #2944
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
Comments
It is not a heavily used piece of the code. If it works for you, it can be submitted as a PR. @me-no-dev is the only one that really knows what the author was thinking... |
In my usage of the timer hal I always used it as:
In the ISR code I would call timerAlarmWrite(), timerWrite() and timerAlarmEnable() to reset the timer period. I agree the timerRestart doesn't look correctly implemented, I'd suggest a slight adjustment to your proposal:
|
Really pleased to find this discussion as I was getting very frustrated with things not working.
My original plan was to have a timer that runs once, that can be re-triggered by an event. I thought I could use Any suggestions? |
Did you try the Ticker library that comes with the ESP32-Arduino framework? Works for me. It has a I use it to quite often. Here is an example with retriggering for a blinking LED that blinks every 500ms for 10ms #include <Ticker.h>
// Ticker called every 500ms
Ticker statusLED;
// Ticker called to switch off the green LED after 10ms
Ticker greenOff;
// Ticker called to switch off the redLED after 10ms
Ticker redOff;
// Forward declaration of function
void blinkLED(void);
// Called by Ticker greenOff
void switchGreenOff(void)
{
digitalWrite(STAT_BLUE, LED_OFF);
}
// Called by Ticker redOff
void switchRedOff(void)
{
digitalWrite(STAT_RED, LED_OFF);
}
// Initialize the blinking and GPIOs
void initLED(void)
{
pinMode(STAT_RED, OUTPUT);
pinMode(STAT_BLUE, OUTPUT);
digitalWrite(STAT_RED, LED_ON);
digitalWrite(STAT_BLUE, LED_ON);
statusLED.attach_ms(500, blinkLED);
}
// Called every 500ms by Ticker statusLED
void blinkLED(void)
{
digitalWrite(STAT_RED, LED_ON);
digitalWrite(STAT_BLUE, LED_ON);
redOff.once_ms(10, switchRedOff);
greenOff.once_ms(10, switchGreenOff);
} |
Thanks beegee-tokyo Ticker looks like a simpler way to do it. I am using a card reader - when read successfully (matched with a known card) it triggers a relay output for 6 seconds. My existing code ran using millis(), with 4 separate procedures called, 3 of these kept track of timing. My intention is to have the list of cards configurable using a web interface which the ESP32 is ideally suited for. As I want to vary the LED on and off periods independently, I will need to experiment, to see if I can do it with a single timer called once, which then gets called again repeatedly with alternating durations. The challenge may be that I will need to change to a shorter delay, while it is part way through a long time period... |
[STALE_SET] This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs. Thank you for your contributions. |
I use the timer as a watchdog. So the timer needs to be reset every time the 'watched' function is called. Replacing timerReset(timer); with timerWrite(timer, 0); is a workaround for the bug in timerReset(); No need to call any other function (assumed the timer is created to restart automatically) Tnx for fix! :) |
[STALE_CLR] This issue has been removed from the stale queue. Please ensure activity to keep it openin the future. |
I am just using timerAlarmWrite(timer, 100, true) when I need to reset the timer. |
Thankyou |
The implementation of timerRestart in esp32-hal-timers.c is as follows:
However, according to the ESP32 reference manual it appears this will have no net effect:
Clearing and resetting the timer enable bit as implemented above will have the effect of pausing the timer for a few cycles. This might be what is intended but I suspect not! My expectation was that calling timerRestart would cause the timer to restart, i.e. to start counting from the beginning. It appears from the reference manual that this is accomplished by reloading the timer instead:
Which would imply the following code for timerRestart might be more appropriate:
Am I missing something here, or is this a bug? I need this functionality to implement a timeout.
The text was updated successfully, but these errors were encountered: