Simple Timer Interrupt Not Working

eowesi
Posts: 11
Joined: Sat Jun 09, 2018 8:55 am

Simple Timer Interrupt Not Working

Postby eowesi » Mon Nov 11, 2019 11:33 am

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?

chegewara
Posts: 2362
Joined: Wed Jun 14, 2017 9:00 pm

Re: Simple Timer Interrupt Not Working

Postby chegewara » 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

eowesi
Posts: 11
Joined: Sat Jun 09, 2018 8:55 am

Re: Simple Timer Interrupt Not Working

Postby eowesi » Mon Nov 11, 2019 12:09 pm

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.

User avatar
martinayotte
Posts: 141
Joined: Fri Nov 13, 2015 4:27 pm

Re: Simple Timer Interrupt Not Working

Postby martinayotte » 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;"

eowesi
Posts: 11
Joined: Sat Jun 09, 2018 8:55 am

Re: Simple Timer Interrupt Not Working

Postby eowesi » Tue Nov 12, 2019 4:52 am

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

chegewara
Posts: 2362
Joined: Wed Jun 14, 2017 9:00 pm

Re: Simple Timer Interrupt Not Working

Postby chegewara » 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.

eowesi
Posts: 11
Joined: Sat Jun 09, 2018 8:55 am

Re: Simple Timer Interrupt Not Working

Postby eowesi » Tue Nov 12, 2019 9:08 am

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"

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: Simple Timer Interrupt Not Working

Postby WiFive » Tue Nov 12, 2019 9:32 am

Your isr function is not in iram

eowesi
Posts: 11
Joined: Sat Jun 09, 2018 8:55 am

Re: Simple Timer Interrupt Not Working

Postby eowesi » Tue Nov 12, 2019 9:41 am

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.

Who is online

Users browsing this forum: No registered users and 93 guests