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

Counting edges with PPI and GPIOTE

I've used the example in examples/peripheral/gpiote to count edges of a WATCHDOG pin from a different MCU:

After starting the timer I want to periodically get the current count value with:

But it always gives me a constant value. If I use a gpiote interrupt and increment a counter in the interrupt routine it runs fine which tells me that the pin

is working fine. But somehow the counter or PPI does not work as expected. So how can I make a counter count on a PIN toggle and get and reset the

count from within the main context.

Thanks in advance.

C.W.

Parents
  • Hi,

    Check the code attached at the end of this post (inside the gpiote_timer_ppi.zip)

  • Thanks for the reply. The issue is that in the example there are two interrupt routines. But I want to solve it without any interrupt routine. Why do I have to enter a timer_handler and a gpiote handler?
    This consumes CPU without optimization.

Reply
  • Thanks for the reply. The issue is that in the example there are two interrupt routines. But I want to solve it without any interrupt routine. Why do I have to enter a timer_handler and a gpiote handler?
    This consumes CPU without optimization.

Children
  • The gpiote interrupt is not enabled, only the event is enabled, i.e. last argument in nrf_drv_gpiote_in_event_enable(PIN_IN, false) is set to false.

    Same applies to the timer used for counting, the function nrf_drv_timer_compare_int_enable() and nrf_drv_timer_extended_compare() is not used to enable any interrupt for that timer.

    In the example, the m_timer_read interrupt is only used to periodically print the counter value.

    Also, the example I linked to does by default not put the CPU to sleep. Something like the snippet below should be added to main() while-loop in order to do that.

  • Hi Sigurd,

    okay that sound reasonable. But I am still not getting any count when I call

    toggle_count = nrfx_timer_capture_get(&m_timer, NRF_TIMER_CC_CHANNEL0);

    It only gives permanent 0. Although the line the toggling. What Am I missing?

  • Please check the return codes from nrfx_timer_init() and the nrfx_ppi_x functions, and make sure that they return NRFX_SUCCESS (=0). If you are not able to find the root cause of the issue, please post your main.c file and/or a full example that shows the issue.

  • Okay I've solved it: The issue is that you can not read the current count of the timer without triggering a capture. I solved it with:

        NRF_TIMER4->TASKS_CAPTURE[2] = 1;
        toggle_count = NRF_TIMER4->CC[2];

    Maybe adding this information to the documentation or exampel is worth it.

    Thanks