RTOS SDK cannot start timer from interrupt handler

gustavo
Posts: 24
Joined: Thu Apr 28, 2016 6:01 pm

RTOS SDK cannot start timer from interrupt handler

Postby gustavo » Thu Jun 02, 2016 11:08 pm

Hi,
I'm trying to implement debounce when the user clicks a button.

I need to start the debounce timer from within the gpio interrupt. However this leads to a crash.

My code is as follows.

Code: Select all

#include "gpio.h"
#include "esp_common.h"
#include "esp_timer.h"

static os_timer_t some_timer;

static void timer_handler(void* args) {
   
    printf("timer");
}

static void intr_handler() {
   
    portENTER_CRITICAL();

    os_timer_disarm(&some_timer);
    os_timer_setfn(&some_timer, (os_timer_func_t *)timer_handler, NULL);
    os_timer_arm(&some_timer, 3, 1);

    portEXIT_CRITICAL();
}

void user_init(void)
{
    GPIO_ConfigTypeDef gpio_in_cfg12;
    gpio_in_cfg12.GPIO_Pin  = GPIO_Pin_12;
    gpio_in_cfg12.GPIO_IntrType = GPIO_PIN_INTR_NEGEDGE;
    gpio_in_cfg12.GPIO_Mode = GPIO_Mode_Input;
    gpio_in_cfg12.GPIO_Pullup = GPIO_PullUp_EN;
    gpio_config(&gpio_in_cfg12);

    gpio_intr_handler_register(intr_handler, NULL);
    _xt_isr_unmask(1 << ETS_GPIO_INUM);
}


The only output I get is:

Code: Select all

ShowCritical:1


Edit: If I start the timer from user_init, everything works as planned.

ESP_Faye
Posts: 1646
Joined: Mon Oct 27, 2014 11:08 am

Re: RTOS SDK cannot start timer from interrupt handler

Postby ESP_Faye » Tue Jun 07, 2016 7:40 pm

Hi,

Please try this

Code: Select all

#include "esp_common.h"
#include "esp_timer.h"
#include "gpio.h"
static os_timer_t some_timer;

static void timer_handler(void* args) {
   
    printf("timer");
}


static void intr_handler() {
   
    u32 gpio_status = GPIO_REG_READ(GPIO_STATUS_ADDRESS);
    gpio_pin_intr_state_set(GPIO_ID_PIN(12) , GPIO_PIN_INTR_DISABLE);
    GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, gpio_status & BIT(12) );

    //printf("a,");   
    portENTER_CRITICAL();
   
    //printf("b,");
    os_timer_disarm(&some_timer);
    os_timer_setfn(&some_timer, (os_timer_func_t *)timer_handler, NULL);
    os_timer_arm(&some_timer, 3, 1);
    //printf("c,");
    portEXIT_CRITICAL();
    //printf("d\n");

        gpio_pin_intr_state_set(GPIO_ID_PIN(12) ,GPIO_PIN_INTR_NEGEDGE);
}

void user_init(void)
{
    GPIO_ConfigTypeDef gpio_in_cfg12;
    gpio_in_cfg12.GPIO_Pin  = GPIO_Pin_12;
    gpio_in_cfg12.GPIO_IntrType = GPIO_PIN_INTR_NEGEDGE;
    gpio_in_cfg12.GPIO_Mode = GPIO_Mode_Input;
    gpio_in_cfg12.GPIO_Pullup = GPIO_PullUp_EN;
    gpio_config(&gpio_in_cfg12);

    gpio_intr_handler_register(intr_handler, NULL);
    _xt_isr_unmask(1 << ETS_GPIO_INUM);
}


If your problem is still unsolved, please feel free to let us know.

gustavo
Posts: 24
Joined: Thu Apr 28, 2016 6:01 pm

Re: RTOS SDK cannot start timer from interrupt handler

Postby gustavo » Wed Jun 08, 2016 7:05 pm

Shoudn't portENTER_CRITICAL() disable interrupts?

Either way, your code works.

ESP_Faye
Posts: 1646
Joined: Mon Oct 27, 2014 11:08 am

Re: RTOS SDK cannot start timer from interrupt handler

Postby ESP_Faye » Sun Jun 12, 2016 10:29 am

Hi,

You need to disable the GPIO interrupt as the sample code we gave.

Thanks for your interest in ESP8266 !

Who is online

Users browsing this forum: No registered users and 5 guests