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

Using BSP long push event to enter system off mode

Hi,

I am trying to use BSP button event to take the system into sleep mode. A long push turns off IO and calls power management shutdown and the same button is configured for wakeup. However the system immediately restarts after entering system off mode. It is in system off as long the button is held. A release of the button triggers restart. Is there a way to ignore the release event or should the release event be used to enter system off mode as the hardware has only one button?

I see that there was a similar question on the forum about the same. But it is unclear how the situation was resolved. 

Thanks.

Parents
  • HI,

    You can do this code. Create 2 app_timer as below.

            err_code = app_timer_create(&m_app_shutdown_timer_id,
                                        APP_TIMER_MODE_SINGLE_SHOT,
                                        long_press_button_shutdown_timeout_handler);
            APP_ERROR_CHECK(err_code);
            
            err_code = app_timer_create(&m_button_long_press_timer_id,
                                        APP_TIMER_MODE_REPEATED,
                                        button_timer_handler);
            APP_ERROR_CHECK(err_code);
            
    static void long_press_button_shutdown_timeout_handler(void *unused)
    {
            uint32_t err_code;
            UNUSED_PARAMETER(unused);
    
            drv_ssd1306_power_off();
    
            err_code = app_timer_stop_all();
            APP_ERROR_CHECK(err_code);
    
            delay_ms(10);
    
            nrf_gpio_cfg_sense_input(CINETIC_BUTTON_PIN, NRF_GPIO_PIN_PULLUP, NRF_GPIO_PIN_SENSE_LOW);
    
            nrf_pwr_mgmt_shutdown(NRF_PWR_MGMT_SHUTDOWN_GOTO_SYSOFF);
    }
    
    static void button_timer_handler(void * p_context)
    {
            uint32_t err_code;
    
            UNUSED_PARAMETER(p_context);
            NRF_LOG_INFO("Long Press button count %d", m_button_ticks_count);
            m_button_ticks_count++;
    
            if (m_button_ticks_count > LONG_PRESS_BUTTON_COUNT)
            {
                    m_shutdown_is_request = true;
    
                    app_button_disable();
                    err_code = app_timer_stop(m_button_long_press_timer_id);
                    APP_ERROR_CHECK(err_code);
    
                    NRF_LOG_INFO("Prepare to shutdown the device");
                    err_code = app_timer_start(m_app_shutdown_timer_id, LONG_PRESS_BUTTON_SHUTDOWN_TIMER_INTERVAL, NULL);
                    APP_ERROR_CHECK(err_code);
            }
    }
    
    static void button_evt_handler(uint8_t pin_no, uint8_t button_action)
    {
            uint32_t err_code;
            static uint8_t current_long_push_pin_no;
    
            switch (pin_no) {
            case KEY_BUTTON_PIN:
            {
                    switch (button_action)
                    {
                    case APP_BUTTON_PUSH:
                            m_press_is_button = true;
                            if (m_button_ticks_count == 0)
                            {
                                    err_code = app_timer_start(m_button_long_press_timer_id, LONG_PRESS_BUTTON_TIMER_INTERVAL, NULL);
                                    APP_ERROR_CHECK(err_code);
                                    m_button_ticks_count++;
                            }
    

Reply
  • HI,

    You can do this code. Create 2 app_timer as below.

            err_code = app_timer_create(&m_app_shutdown_timer_id,
                                        APP_TIMER_MODE_SINGLE_SHOT,
                                        long_press_button_shutdown_timeout_handler);
            APP_ERROR_CHECK(err_code);
            
            err_code = app_timer_create(&m_button_long_press_timer_id,
                                        APP_TIMER_MODE_REPEATED,
                                        button_timer_handler);
            APP_ERROR_CHECK(err_code);
            
    static void long_press_button_shutdown_timeout_handler(void *unused)
    {
            uint32_t err_code;
            UNUSED_PARAMETER(unused);
    
            drv_ssd1306_power_off();
    
            err_code = app_timer_stop_all();
            APP_ERROR_CHECK(err_code);
    
            delay_ms(10);
    
            nrf_gpio_cfg_sense_input(CINETIC_BUTTON_PIN, NRF_GPIO_PIN_PULLUP, NRF_GPIO_PIN_SENSE_LOW);
    
            nrf_pwr_mgmt_shutdown(NRF_PWR_MGMT_SHUTDOWN_GOTO_SYSOFF);
    }
    
    static void button_timer_handler(void * p_context)
    {
            uint32_t err_code;
    
            UNUSED_PARAMETER(p_context);
            NRF_LOG_INFO("Long Press button count %d", m_button_ticks_count);
            m_button_ticks_count++;
    
            if (m_button_ticks_count > LONG_PRESS_BUTTON_COUNT)
            {
                    m_shutdown_is_request = true;
    
                    app_button_disable();
                    err_code = app_timer_stop(m_button_long_press_timer_id);
                    APP_ERROR_CHECK(err_code);
    
                    NRF_LOG_INFO("Prepare to shutdown the device");
                    err_code = app_timer_start(m_app_shutdown_timer_id, LONG_PRESS_BUTTON_SHUTDOWN_TIMER_INTERVAL, NULL);
                    APP_ERROR_CHECK(err_code);
            }
    }
    
    static void button_evt_handler(uint8_t pin_no, uint8_t button_action)
    {
            uint32_t err_code;
            static uint8_t current_long_push_pin_no;
    
            switch (pin_no) {
            case KEY_BUTTON_PIN:
            {
                    switch (button_action)
                    {
                    case APP_BUTTON_PUSH:
                            m_press_is_button = true;
                            if (m_button_ticks_count == 0)
                            {
                                    err_code = app_timer_start(m_button_long_press_timer_id, LONG_PRESS_BUTTON_TIMER_INTERVAL, NULL);
                                    APP_ERROR_CHECK(err_code);
                                    m_button_ticks_count++;
                            }
    

Children
No Data
Related