ESP32 simple timer setup
Posted: Mon Sep 20, 2021 11:43 am
Hello. I am working on ESP32 and trying to setup the most simple timer example. I have found some timer_queue example code in the esp examples folder but this example is way too complicated and overwhelming. All I want is to setup a simple timer and trigger my callback function lets say every 20ms.
See my .c file
my header file
As you can see in my TIMER_INIT function I have set the divider to 80 which means that the timer frequency should be 80mhz(apb frequency) / 80 = 1Mhz
I have set my counter value to 0 and my alarm value to 20000.
So from what I understand, the callback function is only going to trigger when the alarm value reaches 20000 so it is going to take 20000 timer ticks which results in 50hz frequency is that right? So I am expecting my timer callback function be executed at 50hz.
However, I cannot get my program to work, the cpu just constantly crashing:
My questions:
1. Can someone help me understand the example timer_queue code. Why would I need to use freertos queues for a simple timer application? I have tried to play with the esp-idf example by changing the intervals but just couldnt get my head around it. If I want to get a timer with a frequency of 1khz and in the callback function I want to increment counter, what do I need to modify in the example code?
2. In my own custom example. I use:
to register the callback function, but the example code uses
I cant really seem to understand what exactly are the differences.
Please advise me how to develop timers for esp32
See my .c file
Code: Select all
#include "timer_custom.h"
static void IRAM_ATTR timer_group0_isr(void* arg) {
printf("TIMER CALLBACK\n");
//TIMERG0.int_clr_timers.t0 = 1;
//TIMERG0.hw_timer[0].config.alarm_en = 1;
}
void TIMER_INIT(int timer_idx) {
timer_config_t config =
{
.alarm_en = true,
.counter_en = false,
.intr_type = TIMER_INTR_LEVEL,
.counter_dir = TIMER_COUNT_UP,
.auto_reload = true,
.divider = 80
};
timer_init(TIMER_GROUP_0, timer_idx, &config);
timer_set_counter_value(TIMER_GROUP_0, timer_idx, 0);
timer_set_alarm_value(TIMER_GROUP_0, timer_idx, 10000);
timer_enable_intr(TIMER_GROUP_0, timer_idx);
timer_isr_register(TIMER_GROUP_0, timer_idx, timer_group0_isr, (void *) timer_idx, ESP_INTR_FLAG_IRAM, NULL);
timer_start(TIMER_GROUP_0, timer_idx);
printf("timer initialzied!!!!\n");
}
my header file
Code: Select all
#ifndef TIMER_CUSTOM_H
#define TIMER_CUSTOM_H
#include "driver/timer.h"
#include "stdio.h"
void TIMER_INIT (int timer_idx);
#endif
As you can see in my TIMER_INIT function I have set the divider to 80 which means that the timer frequency should be 80mhz(apb frequency) / 80 = 1Mhz
I have set my counter value to 0 and my alarm value to 20000.
So from what I understand, the callback function is only going to trigger when the alarm value reaches 20000 so it is going to take 20000 timer ticks which results in 50hz frequency is that right? So I am expecting my timer callback function be executed at 50hz.
However, I cannot get my program to work, the cpu just constantly crashing:
Code: Select all
timer initialzied!!!!
abort() was called at PC 0x4008265f on core 0
My questions:
1. Can someone help me understand the example timer_queue code. Why would I need to use freertos queues for a simple timer application? I have tried to play with the esp-idf example by changing the intervals but just couldnt get my head around it. If I want to get a timer with a frequency of 1khz and in the callback function I want to increment counter, what do I need to modify in the example code?
2. In my own custom example. I use:
Code: Select all
timer_isr_register(TIMER_GROUP_0, timer_idx, timer_group0_isr, (void *) timer_idx, ESP_INTR_FLAG_IRAM, NULL);
Code: Select all
timer_isr_callback_add(TIMER_GROUP_0, timer_idx, timer_group0_isr, NULL, 0);
Please advise me how to develop timers for esp32