Checking time difference between 2 gpiote events (when chip is in sleep between events) - code get stuck

Hi

I am trying to run this code:

void in_pin_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
{
current_event_timestamp = get_current_time_ms();

// Calculate the time difference in milliseconds
uint32_t time_diff_ms = (current_event_timestamp >= last_event_timestamp)
? (current_event_timestamp - last_event_timestamp)
: (UINT32_MAX - last_event_timestamp + current_event_timestamp + 1);

// Update the last event timestamp
last_event_timestamp = current_event_timestamp;
// Ignore the event if it occurred too soon (less than 100ms) after the last event
if (time_diff_ms > 100)
{

if (counter<50)
counter++;
else
counter=0;

sd_ble_gap_adv_stop(m_adv_handle);
advertising_init();
}
}

But it get stuck. What is the problem?

Parents
  • I tried also this:

    void in_pin_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
    {
    current_event_timestamp = get_current_time_ms();
    uint32_t time_diff_ms;

    // Calculate the time difference in milliseconds
    if (current_event_timestamp >= last_event_timestamp)
    {
    time_diff_ms = current_event_timestamp - last_event_timestamp;
    // Update the last event timestamp
    last_event_timestamp = current_event_timestamp;
    // Ignore the event if it occurred too soon (less than 100ms) after the last event
    if (time_diff_ms > 100)
    {

    if (counter<50)
    counter++;
    else
    counter=0;

    sd_ble_gap_adv_stop(m_adv_handle);
    advertising_init();
    }
    }
    }

    And I see that it still gets stuck

    If I remove the timestamp comparison it works.

  • There is nothing in your code that loops, so please explain what you mean by "stuck"? In a microcontroller applicaition there is app error check loops, hardfaults or loops that the app inserted. There is no other stuck state.

    Avi said:
    If I remove the timestamp comparison it works.

    Removing which exact line makes it work? maybe there is a hardfault in your case which you presume to be stuck. Do you have any logs in enabled in the application? if so add some logs to see if some errors are thrown out.

  • My mistake, I forgot to include: advertising_start inside the handle.

    Yet, it is not solving my main problem, 

    I want to detect if a switch is open or closed. If it changes state too fast and it is back to the same level I want to ignore. For example, It was closed and then it was opened and closed within 100msec. I want to ignore it and say it is still closed.

    I thought about reading the GPIN state, but looks like I cannot since I am using it also for GPIOTE

    How can I do it?

  • OK, done it:

    void in_pin_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
    {
    nrf_drv_gpiote_in_event_disable(PIN_IN);
    uint32_t previous_pin_state = nrf_gpio_pin_read(PIN_IN);
    nrf_delay_ms(500);
    uint32_t current_pin_state = nrf_gpio_pin_read(PIN_IN);
    if (current_pin_state == previous_pin_state)
    {

    if (counter<50)
    counter++;
    else
    counter=0;

    sd_ble_gap_adv_stop(m_adv_handle);
    advertising_init();
    advertising_start();
    }
    nrf_drv_gpiote_in_event_enable(PIN_IN,true);
    }

Reply
  • OK, done it:

    void in_pin_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
    {
    nrf_drv_gpiote_in_event_disable(PIN_IN);
    uint32_t previous_pin_state = nrf_gpio_pin_read(PIN_IN);
    nrf_delay_ms(500);
    uint32_t current_pin_state = nrf_gpio_pin_read(PIN_IN);
    if (current_pin_state == previous_pin_state)
    {

    if (counter<50)
    counter++;
    else
    counter=0;

    sd_ble_gap_adv_stop(m_adv_handle);
    advertising_init();
    advertising_start();
    }
    nrf_drv_gpiote_in_event_enable(PIN_IN,true);
    }

Children
No Data
Related