Page 1 of 1

Simple Timer Interrupt Not Working

Posted: Mon Nov 11, 2019 11:33 am
by eowesi
I am trying to run timer interrupt with below code. But I can't see the print (printf("timer_group0_isr OK!\n")) I must to see.

Code: Select all

static void timer_group0_isr(void* arg) {

    printf("timer_group0_isr OK!\n");
	
    TIMERG0.int_clr_timers.t0 = 1;
    TIMERG0.hw_timer[0].config.alarm_en = 1;
}

void timer_tg0_initialise (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");
}
AppMain:

Code: Select all

timer_tg0_initialise(TIMER_0);
Do I need to make a setting in Menuconfig?

Re: Simple Timer Interrupt Not Working

Posted: Mon Nov 11, 2019 12:03 pm
by chegewara
At the beginning you can't use standard logging from isr function, when timer will trigger it it will crash, but you can use ets_printf.

Here is very good example how to start:
https://github.com/espressif/esp-idf/bl ... st_timer.c

Re: Simple Timer Interrupt Not Working

Posted: Mon Nov 11, 2019 12:09 pm
by eowesi
chegewara wrote:
Mon Nov 11, 2019 12:03 pm
At the beginning you can't use standard logging from isr function, when timer will trigger it it will crash, but you can use ets_printf.

Here is very good example how to start:
https://github.com/espressif/esp-idf/bl ... st_timer.c
I changed my isr to :

Code: Select all

uint32_t c = 0;
static void timer_group0_isr(void* arg) {
    c++;
    TIMERG0.int_clr_timers.t0 = 1;
    TIMERG0.hw_timer[0].config.alarm_en = 1;
}
in app_main():

Code: Select all

    vTaskDelay(2000 / portTICK_PERIOD_MS);
    timer_tg0_initialise(TIMER_0);

    while(1) {
        vTaskDelay(500 / portTICK_PERIOD_MS);
        printf("c :   %d\n", c);
    }
The display shows "c : 0" continuously.

Re: Simple Timer Interrupt Not Working

Posted: Mon Nov 11, 2019 3:11 pm
by martinayotte
eowesi wrote:
Mon Nov 11, 2019 12:09 pm
The display shows "c : 0" continuously.
You need to have your declaration "uint32_t c = 0;" to be volatile, ie "volatile uint32_t c = 0;"

Re: Simple Timer Interrupt Not Working

Posted: Tue Nov 12, 2019 4:52 am
by eowesi
martinayotte wrote:
Mon Nov 11, 2019 3:11 pm
eowesi wrote:
Mon Nov 11, 2019 12:09 pm
The display shows "c : 0" continuously.
You need to have your declaration "uint32_t c = 0;" to be volatile, ie "volatile uint32_t c = 0;"
not working

Re: Simple Timer Interrupt Not Working

Posted: Tue Nov 12, 2019 7:44 am
by chegewara
Did you check timer's value after start?
Did you check that all commands return ESP_OK?

I'm guessing timer is setup to very long time and did not trigger yet.

Re: Simple Timer Interrupt Not Working

Posted: Tue Nov 12, 2019 9:08 am
by eowesi
chegewara wrote:
Tue Nov 12, 2019 7:44 am
Did you check timer's value after start?
Did you check that all commands return ESP_OK?

I'm guessing timer is setup to very long time and did not trigger yet.
timer_isr_register function returns "ESP_ERR_INVALID_ARG"

Re: Simple Timer Interrupt Not Working

Posted: Tue Nov 12, 2019 9:32 am
by WiFive
Your isr function is not in iram

Re: Simple Timer Interrupt Not Working

Posted: Tue Nov 12, 2019 9:41 am
by eowesi
When I changed the function from
static void timer_group0_isr(void* arg)
to
static void IRAM_ATTR timer_group0_isr(void* arg)

my problem solved. Thank you to everyone who help.