This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Can't set up timer in couner mode with ppi

Hi. I am trying to set up timer in counter mode to count up signals from gpio through the ppi module, but it's not working. In main cycle I am testing the timer using led :) . Code:

#define frenq_counter_pin 13
#define test_counter_pin 18
#define GPIOTE_CHANNEL_NUMBER 0;

const nrf_drv_timer_t timer1 = NRF_DRV_TIMER_INSTANCE(1);

nrf_ppi_channel_t ppi_channel1;

void timer_void_handler(nrf_timer_event_t event_type, void * p_context){}
void pin_event_void_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action){}

void counter_init(void) {

	nrf_drv_timer_config_t conf;
	conf.mode = TIMER_MODE_MODE_Counter;
    conf.bit_width = TIMER_BITMODE_BITMODE_16Bit;


    APP_ERROR_CHECK(
		nrf_drv_timer_init(
            &timer1,
            &conf,
            timer_void_handler
    ));

nrf_drv_timer_enable(&timer1);

}

void gpiote_init(){

    APP_GPIOTE_INIT(APP_GPIOTE_MAX_USERS);

    nrf_drv_gpiote_in_config_t config = GPIOTE_CONFIG_IN_SENSE_HITOLO(false);
    config.pull = NRF_GPIO_PIN_PULLUP;

    nrf_drv_gpiote_in_init(frenq_counter_pin, &config, pin_event_void_handler);
    nrf_drv_gpiote_in_event_enable(frenq_counter_pin, true);

    nrf_gpio_cfg_output(test_counter_pin);
    nrf_gpio_pin_set(test_counter_pin);
}

void ppi_init(){

    uint32_t err_code = NRF_SUCCESS;

    err_code = nrf_drv_ppi_init();
    APP_ERROR_CHECK(err_code);

    err_code = nrf_drv_ppi_channel_alloc(&ppi_channel1);
    APP_ERROR_CHECK(err_code);
    err_code = nrf_drv_ppi_channel_assign(ppi_channel1,
                                      nrf_drv_gpiote_in_event_addr_get(frenq_counter_pin),
                                      nrf_drv_timer_task_address_get(&timer1, NRF_TIMER_TASK_COUNT));
    APP_ERROR_CHECK(err_code);

    nrf_drv_ppi_channel_enable(ppi_channel1);
}

int main(void) {
    counter_init();
    gpiote_init();
    ppi_init();

    for (;;) {
	
        if (((int)nrf_drv_timer_capture(&timer1,NRF_TIMER_CC_CHANNEL0)) >= 10) {
            nrf_gpio_pin_toggle(test_counter_pin);
            NRF_TIMER1->CC[0] = 0;
        }
    }

}

Thank you for help!

Parents
  • I haven't tried it using drivers but here is the post that did this without using drivers. Note that TIMER1 and PPI next to it used to stop the TIMER2 after 1 second. In your case you just need to configure GPIOTE->PPI->TIMER2 in counter mode. @adriand has updated the thread with his code.

Reply
  • I haven't tried it using drivers but here is the post that did this without using drivers. Note that TIMER1 and PPI next to it used to stop the TIMER2 after 1 second. In your case you just need to configure GPIOTE->PPI->TIMER2 in counter mode. @adriand has updated the thread with his code.

Children
No Data
Related