How do I stop a timer?

HyperUniverse
Posts: 33
Joined: Fri Oct 29, 2021 11:20 pm

How do I stop a timer?

Postby HyperUniverse » Tue Nov 02, 2021 11:54 pm

I'm trying this example project

Code: Select all

/* Timer group-hardware timer example

   This example code is in the Public Domain (or CC0 licensed, at your option.)

   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
*/
and there is no information on how to stop a timer.

I have found in the online documentation:

Code: Select all

esp_err_t timer_pause(timer_group_t group_num, timer_idx_t timer_num)

    Pause the counter of hardware timer.
So I tried:

Code: Select all

esp_err_t timer_pause(TIMER_GROUP_0, TIMER_1);
or

Code: Select all

esp_err_t timer_pause(timer_group_t TIMER_GROUP_0, timer_idx_t TIMER_1) ;
but none stoped the timer.

How do I do it?
Are the above commands pausing or stopping a timer?
I mean, if I resart the timer later will it restart from zero, or from the value where I paused it?

Thanks

User avatar
WardMas
Posts: 73
Joined: Fri Jun 19, 2020 9:09 am

Re: How do I stop a timer?

Postby WardMas » Wed Nov 03, 2021 8:05 am

Hi,
First have a look at your timer configuration. Here is the the configuration I use:

Code: Select all

timer_config_t config;							
	config.alarm_en = TIMER_ALARM_EN;				//Alarm is the value that the timer will count to before rolling over
	config.counter_dir = TIMER_COUNT_UP;
	config.auto_reload = TIMER_AUTORELOAD_EN;
	config.counter_en = TIMER_PAUSE;				//timer_set_counter_value
	config.intr_type = TIMER_INTR_LEVEL;
	config.divider = TIMER_DIVIDER;
	timer_init(TimerGroup, TimerIdx, &config);
	timer_set_counter_value(TimerGroup, TimerIdx, 0x00000000ULL);
	timer_set_alarm_value(TimerGroup, TimerIdx, TIMER_PERIODE);
	timer_enable_intr(TimerGroup, TimerIdx);
	timer_isr_register(TimerGroup, TimerIdx, timer_group0_isr,
       (void *) TimerIdx, Interrupt_Level, NULL);
and when I want to stop the timer I do the following:

Code: Select all

timer_pause(TIMER_GROUP_0, TIMER_1);							//Pause Timer
timer_set_counter_value(TIMER_GROUP_0, TIMER_1, 0x00000000ULL)				//Reset Timer value
You can always visit my YouTube channel for embedded systems related tutorials
https://youtube.com/user/wardzx1

HyperUniverse
Posts: 33
Joined: Fri Oct 29, 2021 11:20 pm

Re: How do I stop a timer?

Postby HyperUniverse » Wed Nov 03, 2021 9:41 am

Thank you WardMas

It works perfectly.

And I suppose to restart the same timer later when I need it, I'll do:

Code: Select all

timer_start(TIMER_GROUP_0, TIMER_1);
right?
*******
Any idea why the documentation says:

Code: Select all

esp_err_t timer_pause(TIMER_GROUP_0, TIMER_1);
but in fact we don't need this esp_err_t to make it work?

What is this esp_err_t supposed to do?
I've seen it a lot around, but I can't understand it.

Thanks again

Victoria Nope
Posts: 75
Joined: Fri Dec 04, 2020 9:56 pm

Re: How do I stop a timer?

Postby Victoria Nope » Wed Nov 03, 2021 10:21 pm

HyperUniverse wrote:
Wed Nov 03, 2021 9:41 am
And I suppose to restart the same timer later when I need it, I'll do:

Code: Select all

timer_start(TIMER_GROUP_0, TIMER_1);
right?

That's correct. You may find both methods used e.g. in this example.

HyperUniverse wrote: Any idea why the documentation says:

Code: Select all

esp_err_t timer_pause(TIMER_GROUP_0, TIMER_1);
but in fact we don't need this esp_err_t to make it work?

What is this esp_err_t supposed to do?
I've seen it a lot around, but I can't understand it.

That esp_err_t is the error data type which is in ESP IDF mainly used as a function return data type. From the mentioned timer_pause function reference you can read that this function call will return one of the values described in the Return paragraph (that is either ESP_OK or ESP_ERR_INVALID_ARG).

Whether to check every function return value is a subject to flame and endless war.

For the mentioned function, if you are sure that you will always pass valid parameters to its call, you will never update IDF for possible API changes, earth gravity won't rapidly change or the world won't be attacked by evil aliens, you should be fine to ignore the returned value...

Well, I'd personally check the returned value even in this case (call with valid, constant parameters), even for the price of just wasted CPU ticks for its comparison (which in turn consumes more pointless power and may cause global warming issue even worse).

HyperUniverse
Posts: 33
Joined: Fri Oct 29, 2021 11:20 pm

Re: How do I stop a timer?

Postby HyperUniverse » Thu Nov 04, 2021 8:42 am

Victoria Nope wrote:
Wed Nov 03, 2021 10:21 pm
Thanks for the detailed answer Victoria.

I'll save the planet, as I've been so much indoctrinated by the media, that I've almost started believing it.

Who is online

Users browsing this forum: No registered users and 351 guests