Problem periodic timer

marius.cezar18
Posts: 8
Joined: Mon May 16, 2022 10:41 am

Problem periodic timer

Postby marius.cezar18 » Mon May 16, 2022 10:52 am

I have a problem with a periodic timer not running properly, in parallel I have several tasks and I want a timer to run every 52us.
This timer is weird, where do I go wrong? I also attached 2 pictures with the logic analyzer on the output pin.
Thanks in advance!

Code: Select all

static void periodic_timer_callback(void *arg) {
	if (toggle2 == 0) {
		toggle2 = 1;
	} else {
		toggle2 = 0;
	}
	gpio_set_level(GPIO_OUTPUT_IO_2, toggle2);
}

Code: Select all

void InitTimer() {
	const esp_timer_create_args_t periodic_timer_args = { .callback =
			&periodic_timer_callback,
	// name is optional, but may help identify the timer when debugging
			.name = "periodic" };

	ESP_ERROR_CHECK(esp_timer_create(&periodic_timer_args, &periodic_timer));
	// The timer has been created but is not running yet
	// Start the timers
	ESP_ERROR_CHECK(esp_timer_start_periodic(periodic_timer, 52));
	}
Attachments
t2.png
t2.png (71.54 KiB) Viewed 2803 times
t1.png
t1.png (67.71 KiB) Viewed 2803 times

ESP_Sprite
Posts: 9713
Joined: Thu Nov 26, 2015 4:08 am

Re: Problem periodic timer

Postby ESP_Sprite » Tue May 17, 2022 2:47 am

In this particular case, it's likely because esp_timer only uses a single task to handle all the timer callbacks, which means that if something else (e.g. WiFi or BT) schedules something, your GPIO toggling routine can be 'stuck behind it'. I'd suggest using a hardware timer for something like this, or even better, see if you can use a peripheral to generate the signal you need without using software intervention.

marius.cezar18
Posts: 8
Joined: Mon May 16, 2022 10:41 am

Re: Problem periodic timer

Postby marius.cezar18 » Tue May 17, 2022 5:43 am

When you say hardware timer what you mean? An example please!

User avatar
mbratch
Posts: 303
Joined: Fri Jun 11, 2021 1:51 pm

Re: Problem periodic timer

Postby mbratch » Tue May 17, 2022 7:18 pm


marius.cezar18
Posts: 8
Joined: Mon May 16, 2022 10:41 am

Re: Problem periodic timer

Postby marius.cezar18 » Thu May 26, 2022 1:56 pm

Thanks!!!

Who is online

Users browsing this forum: acsoprana, Majestic-12 [Bot] and 57 guests