This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Starting timer disables gpiote button

Hi,

I am developing a code in which, among other things, when a button is pressed, a led turns on. In the same button handler, a timer is started to turn the led off a bit later. 

It works perfectly well in the beginning, but after pushing the button some times (between 3 and 10) it responds no more. The rest of the application will continue acting normally, even its timers.

I've already checked that  APP_TIMER_CONFIG_OP_QUEUE_SIZE is big enough, and also changing the button handler for a flag and handle the event in the main loop. It solved nothing. The only way it works is by changing the timer by a delay, but it is useless if I want the led to be on for some seconds. 

Button initialization and handler:

void button_handler(nrf_drv_gpiote_pin_t pin_no, nrf_gpiote_polarity_t button_action)
{
    app_timer_stop(led_off_timer);
    nrf_gpio_pin_clear(LED_G);
    nrf_gpio_pin_clear(LED_B);
    uint32_t err_code = app_timer_start(led_off_timer, APP_TIMER_TICKS(1000), NULL);
    APP_ERROR_CHECK(err_code);
}


static void init_button(void)
{
	uint32_t err_code;
	
	err_code = nrf_drv_gpiote_init();
	APP_ERROR_CHECK(err_code);
	
	nrf_drv_gpiote_in_config_t in_config = GPIOTE_CONFIG_IN_SENSE_LOTOHI(true);
	in_config.pull = NRF_GPIO_PIN_NOPULL;
	
	err_code = nrf_drv_gpiote_in_init(BUTTON1, &in_config, button_handler);
	APP_ERROR_CHECK(err_code);
	nrf_drv_gpiote_in_event_enable(BUTTON1, true);
}

Timer handler:

void led_off_handler(void * p_context)
{
	nrf_gpio_pin_set(LED_G);
	nrf_gpio_pin_set(LED_B);
}

I am using a nrf52832 with SDK 14.2.0 and softdevice s132 v5.1.

Any clue of why it is happening or how can be solved will be very welcome. Thanks.

  • Hi 114r5,

      It seems like you have a debouncing issue. 

     

    I would recommend you to check out the "Button handling library" and especially how this is done in the BSP example.

     

  • No luck.

    I tried doing the same action using the bsp functions as it is done in the bsp example and the result is the same. At the third push, the led fades fastly and the button responds no more. No error is fired. 

    void bsp_evt_handler(bsp_event_t evt)
    {
    	uint32_t err_code;
    	if(evt == BSP_EVENT_KEY_0)
    	{
    		actual_state = BSP_INDICATE_BUTTON_PRESSED;
    		err_code = bsp_indication_set(actual_state);
    		APP_ERROR_CHECK(err_code);
    	}
    }
    
    void leds_and_buttons_init(void)
    {
    	uint32_t err_code;
    	err_code = bsp_init(BSP_INIT_LED | BSP_INIT_BUTTONS, bsp_evt_handler);
    	APP_ERROR_CHECK(err_code);
    }

    static uint32_t bsp_led_indication(bsp_indication_t indicate)
    {
        uint32_t err_code   = NRF_SUCCESS;
        uint32_t next_delay = 0;
    
        if (m_leds_clear)
        {
            m_leds_clear = false;
            leds_off();
        }
    
        switch (indicate)
        {
            case BSP_INDICATE_IDLE:
                leds_off();
                m_stable_state = indicate;
                break;
    				
    		case BSP_INDICATE_INIT:
    				bsp_board_led_on(1);
    				err_code = app_timer_start(m_leds_timer_id, APP_TIMER_TICKS(3500), NULL);
    			break;
    			
    		case BSP_INDICATE_BUTTON_PRESSED:
    				bsp_board_led_on(1);
    				bsp_board_led_on(2);
    				err_code = app_timer_start(m_leds_timer_id, APP_TIMER_TICKS(1000), NULL);
    			break;
    
    		case BSP_INDICATE_ALIVE:
    				bsp_board_led_on(2);
    				err_code = app_timer_start(m_leds_timer_id, APP_TIMER_TICKS(50), NULL);
    			break;
    					
            default:
                break;
        }
    
        return err_code;
    }

  • Oh, I think I found the problem. 

    One day, in app_timer_init(), I changed m_op_queue.size = APP_TIMER_CONFIG_OP_QUEUE_SIZE+1 for m_op_queue.size = APP_TIMER_CONFIG_OP_QUEUE_SIZE+2. (sorry for that silly change, don't ask me why)

    Now I turned back to its original state and seems to work well. How does it affect to the code? Why a change like this may cause that kind of problems?

  • Hi 114r5,

    I am glad to hear that you found the issue and solved it.

    You should not change things in "app_timer.c" , that can cause unwanted behaviour as you saw yourself. 

     

    If you want to increase the queue size you do this in the "sdk_config.h" file.

     

Related