Page 1 of 1

ESP32 Timer

Posted: Wed Nov 30, 2022 8:55 am
by ketan vadodariya
Hi,

I am using ESP32 for development purpose. I have application which is counting the counter value at every-second for the period. I am looking for the Timer where I can count the value at every-second. I am looking some for timer interrupt of 1 ms period that mean Interrupt should be trigger 1 ms period. I did some research regarding this and found some article of TIMER group 0.

Following Question I have regarding the timer 0
How to use the timer of ESP32 at every 1 ms of tick period. ?
How to calculate the Tick Value for Timer 0 using APB (I think default clock would be APB) ?

Re: ESP32 Timer

Posted: Wed Nov 30, 2022 1:26 pm
by sparta
Did you check esp-idf\examples\peripherals\timer_group example?

Re: ESP32 Timer

Posted: Thu Dec 01, 2022 5:09 am
by ketan vadodariya
Hi,

Thank you for the reply.

Yes I checked the example. Interrupt is working fine but I am not able to calculate the 1 ms period of tick value.

How to calculate the 1 ms tick value ?

Below code is for the timer group 0

Code: Select all

static bool IRAM_ATTR timer_group_isr_callback(void *args)
{
	BaseType_t high_task_awoken = pdFALSE;

	MSD_DeviceTimer.deviceRunTimer.val++;

	return high_task_awoken == pdTRUE; // return whether we need to yield at the end of ISR
}

static void timer_core(int group, int timer, bool auto_reload, int tick)
{
	/* Select and initialize basic parameters of the timer */
	timer_config_t config = {
			.divider = TIMER_DIVIDER,
			.counter_dir = TIMER_COUNT_UP,
			.counter_en = TIMER_PAUSE,
			.alarm_en = TIMER_ALARM_EN,
			.auto_reload = auto_reload,
	}; // default clock source is APB
	timer_init(group, timer, &config);

	/* Timer's counter will initially start from value below.
	 Also, if auto_reload is set, this value will be automatically reload on alarm */
	timer_set_counter_value(group, timer, 0);

	/* Configure the alarm value and the interrupt on alarm. */
	timer_set_alarm_value(group, timer, tick * TIMER_SCALE);
	timer_enable_intr(group, timer);

	example_timer_info_t*timer_info = calloc(1, sizeof(example_timer_info_t));
	timer_info->timer_group = group;
	timer_info->timer_idx = timer;
	timer_info->auto_reload = auto_reload;
	timer_info->alarm_interval = tick;
	timer_isr_callback_add(group, timer, timer_group_isr_callback, timer_info, 0);

	timer_start(group, timer);
}

void timer_initialization(void)
{
	timer_core(TIMER_GROUP_0, TIMER_0, true, 1);
}