Page 1 of 1

Problem periodic timer

Posted: Mon May 16, 2022 10:52 am
by marius.cezar18
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));
	}

Re: Problem periodic timer

Posted: Tue May 17, 2022 2:47 am
by ESP_Sprite
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.

Re: Problem periodic timer

Posted: Tue May 17, 2022 5:43 am
by marius.cezar18
When you say hardware timer what you mean? An example please!

Re: Problem periodic timer

Posted: Tue May 17, 2022 7:18 pm
by mbratch

Re: Problem periodic timer

Posted: Thu May 26, 2022 1:56 pm
by marius.cezar18
Thanks!!!