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

How to config the gpiote and timer capture mode to count the external pulses on a GPIO?

Hi,

I am new to NORDIC dev and I find its ppi is really useful but also difficult to understand.

Now my device may be connected the truck engine to count the engine speed(rpm) with a gpio. The engine generates a pulse on the GPIO and the device needs to count it.

I want to use the gpiote + ppi + timer capture mode to implement it. so i coded it like this

static void rpm_init(void)
{
nrf_ppi_channel_t ppi_channel;
uint32_t gpiote_in_evt_addr;
uint32_t capture_task_addr;
ret_code_t err_code;

nrf_drv_gpiote_in_config_t config = NRFX_GPIOTE_CONFIG_IN_SENSE_TOGGLE(false);
err_code = nrf_drv_gpiote_in_init(PIN_DIN3_RPM, &config, rpm_event_handler);
APP_ERROR_CHECK(err_code);

// Check TIMER2 configuration for details.
nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;
err_code = nrf_drv_timer_init(&m_timer2, &timer_cfg, empty_timer_handler);
APP_ERROR_CHECK(err_code);

gpiote_in_evt_addr = nrfx_gpiote_in_event_addr_get(PIN_DIN3_RPM);
capture_task_addr = nrfx_timer_capture_task_address_get(&m_timer2, NRF_TIMER_CC_CHANNEL0);

err_code = nrf_drv_ppi_channel_alloc(&ppi_channel);
APP_ERROR_CHECK(err_code);
err_code = nrf_drv_ppi_channel_assign(ppi_channel, gpiote_in_evt_addr, capture_task_addr);
APP_ERROR_CHECK(err_code);
err_code = nrf_drv_ppi_channel_enable(ppi_channel);
APP_ERROR_CHECK(err_code);

nrfx_gpiote_in_event_enable(PIN_DIN3_RPM, true);

app_timer_start(rpm_timer_id, APP_TIMER_TICKS(1000), NULL);
}

the app timer rpm_timer_id is used to read the CC[0] of TIMER2. How ever, it seems the CC[0] is always 0.

Is there any wrong with my code?

Thank you

  • Hi,

    I notice a few problems:

    • You use the default timer configuration NRF_DRV_TIMER_DEFAULT_CONFIG as is, without doing any changes. Unless you have made changes in your sdk_config.h file, the default configuration for the timer driver is to use the timer in timer mode, not counter mode, which is what you want.
    • You use NRF_TIMER_CC_CHANNEL0, but you should use the COUNT task to increment the counter in the timer (NRF_TIMER_TASK_COUNT).

    Looking quickly fixing those two issues should work. You could also refer to this example, which demonstrates exactly what you want to do (see the last part of timer_counter_with_gpio() here.

Related