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
  • Thank's for link. I solved that problem using drivers:

    Initialisation:

    	nrf_drv_timer_t timer1 = NRF_DRV_TIMER_INSTANCE(2);
    
    	nrf_ppi_channel_t ppi_channel1;
    
    	uint32_t capture_frenq(){
    		uint32_t frenq_ = nrf_drv_timer_capture(
    					&timer1,
    					NRF_TIMER_CC_CHANNEL0
    			);
    			
    		timer1.p_reg->TASKS_CLEAR = 1;
    		timer1.p_reg->TASKS_START = 1;
    		return frenq_;
    	}
    
    	// Void handler
    	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){}
    
    
    	//Counter only
    	void counter_init(void)
    	{	
    		
    		nrf_drv_timer_config_t conf;
    		   conf.mode = TIMER_MODE_MODE_Counter;
    		   conf.bit_width = TIMER_BITMODE_BITMODE_16Bit;
    		   conf.p_context = &timer1;
    		   
    		APP_ERROR_CHECK(
    				   nrf_drv_timer_init(
    						   &timer1,
    						   &conf,
    						   timer_void_handler
    		   ));
    		   
    		nrf_drv_timer_enable(&timer1);
    	}
    
    	void gpiote_init(nrf_drv_gpiote_pin_t frenq_counter_pin){
    		nrf_drv_gpiote_init();
    		
    		nrf_drv_gpiote_in_config_t config = GPIOTE_CONFIG_IN_SENSE_HITOLO(true);
    		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);
    	}
    
    
    	void ppi_init(nrf_drv_gpiote_pin_t frenq_counter_pin){
    		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);
    	}
    
    
    	void speedometer_init(nrf_drv_gpiote_pin_t pin){
    		counter_init();
    		gpiote_init(pin);
    		ppi_init(pin);
    	}
    

    Application timer for frequency capturing:

    void timers_init(void)
    {
    	uint32_t err_code;
    	APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, false);
    	
    	err_code = app_timer_create(&speed_timer_id,
    								APP_TIMER_MODE_REPEATED,
    								speed_meas_handler);
    	APP_ERROR_CHECK(err_code);
    
    }
    
    void speed_meas_handler(void * p_context){
    	freq_ = capture_freq();
    	
    }
    
Reply
  • Thank's for link. I solved that problem using drivers:

    Initialisation:

    	nrf_drv_timer_t timer1 = NRF_DRV_TIMER_INSTANCE(2);
    
    	nrf_ppi_channel_t ppi_channel1;
    
    	uint32_t capture_frenq(){
    		uint32_t frenq_ = nrf_drv_timer_capture(
    					&timer1,
    					NRF_TIMER_CC_CHANNEL0
    			);
    			
    		timer1.p_reg->TASKS_CLEAR = 1;
    		timer1.p_reg->TASKS_START = 1;
    		return frenq_;
    	}
    
    	// Void handler
    	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){}
    
    
    	//Counter only
    	void counter_init(void)
    	{	
    		
    		nrf_drv_timer_config_t conf;
    		   conf.mode = TIMER_MODE_MODE_Counter;
    		   conf.bit_width = TIMER_BITMODE_BITMODE_16Bit;
    		   conf.p_context = &timer1;
    		   
    		APP_ERROR_CHECK(
    				   nrf_drv_timer_init(
    						   &timer1,
    						   &conf,
    						   timer_void_handler
    		   ));
    		   
    		nrf_drv_timer_enable(&timer1);
    	}
    
    	void gpiote_init(nrf_drv_gpiote_pin_t frenq_counter_pin){
    		nrf_drv_gpiote_init();
    		
    		nrf_drv_gpiote_in_config_t config = GPIOTE_CONFIG_IN_SENSE_HITOLO(true);
    		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);
    	}
    
    
    	void ppi_init(nrf_drv_gpiote_pin_t frenq_counter_pin){
    		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);
    	}
    
    
    	void speedometer_init(nrf_drv_gpiote_pin_t pin){
    		counter_init();
    		gpiote_init(pin);
    		ppi_init(pin);
    	}
    

    Application timer for frequency capturing:

    void timers_init(void)
    {
    	uint32_t err_code;
    	APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, false);
    	
    	err_code = app_timer_create(&speed_timer_id,
    								APP_TIMER_MODE_REPEATED,
    								speed_meas_handler);
    	APP_ERROR_CHECK(err_code);
    
    }
    
    void speed_meas_handler(void * p_context){
    	freq_ = capture_freq();
    	
    }
    
Children
Related