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

NRF_TIMER2 error?

I want to create a 1MHz timer using TIMER2.

I try to start the timer with the following settings, but the device probably hangs.

I also use app_timer, is it a problem to use it at the same time?

What I want to do is read the counter value of the 1MHz timer when the button is interrupted.

    NRF_TIMER1->TASKS_STOP  = 1;
    NRF_TIMER2->MODE        = TIMER_MODE_MODE_Timer;
    NRF_TIMER2->BITMODE     = TIMER_BITMODE_BITMODE_16Bit;
    NRF_TIMER2->PRESCALER   = 6;
    NRF_TIMER2->INTENSET    = TIMER_INTENSET_COMPARE1_Enabled << TIMER_INTENSET_COMPARE1_Pos;//CC[1]  
    NRF_TIMER2->INTENSET    = TIMER_INTENSET_COMPARE2_Enabled << TIMER_INTENSET_COMPARE2_Pos;//CC[1]
    NVIC_EnableIRQ(TIMER2_IRQn);
    NRF_TIMER2->TASKS_START = 1;

Parents
  • Hi,

    The app_timer use the RTC and 32.768 kHz clock and so does not overlap with the TIMER peripheral in any way.

    Regarding what is the actual problem here I do not see enough of your code to know what happens, but with this I expect the timer should be continuously counting. In what way does it not work? (you write it "probably hangs" - what do you mean precisely?). I so not see your code that should trigger capture on button press. Perhaps there is an issue there?

Reply
  • Hi,

    The app_timer use the RTC and 32.768 kHz clock and so does not overlap with the TIMER peripheral in any way.

    Regarding what is the actual problem here I do not see enough of your code to know what happens, but with this I expect the timer should be continuously counting. In what way does it not work? (you write it "probably hangs" - what do you mean precisely?). I so not see your code that should trigger capture on button press. Perhaps there is an issue there?

Children
  • I try to start TIMER in main, but it doesn't work.
    It seems to stop working at hfclk_timer_init() and then no connection is made.
    If I don't run hfclk_timer_init() everything works fine.

    What I want to do is to run a timer with higher frequency than 32kHz RTC to get timestamp.

    static void hfclk_timer_init(void)
    {
        NRF_TIMER2->TASKS_STOP  = 1;
        NRF_TIMER2->MODE        = TIMER_MODE_MODE_Timer;
        NRF_TIMER2->BITMODE     = TIMER_BITMODE_BITMODE_16Bit;
        NRF_TIMER2->PRESCALER   = 6;
        NRF_TIMER2->INTENSET    = TIMER_INTENSET_COMPARE1_Enabled << TIMER_INTENSET_COMPARE1_Pos;//CC[1]  
        NRF_TIMER2->INTENSET    = TIMER_INTENSET_COMPARE2_Enabled << TIMER_INTENSET_COMPARE2_Pos;//CC[1]
        NVIC_EnableIRQ(TIMER2_IRQn);
        NRF_TIMER2->TASKS_START = 1;
    
        //NRF_LOG_INFO("TIMER START");
    }
    
    int main(void)
    {
        bool erase_bonds;
        // Initialize.
        log_init();
        timer_init();
        leds_init(); 
        original_gpio_init();
        //buttons_init();
        buttons_leds_init(&erase_bonds);
        power_management_init();
        ble_stack_init();
    
        pa_lna_init();
        gatt_init();
        db_discovery_init();
        original_c_init();
        hfclk_timer_init();
        //rtc_config();
        ble_conn_state_init();
        scan_init();
    
        // Start execution.
        NRF_LOG_INFO("Multilink example started.");
        scan_start();
    
        for (;;)
        {
            idle_state_handle();
        }
    }

  • This is not enough information for me to understand the situation, unfortunately. Can you explain in detail what the expected behavior is and how it fails, and complete code so that I can understand wat is going on (seeing your main function does not help much as I do not know what you do in any of the functions you call from there)?

  • TIMER_ENABLED of legacy layer was not enabled in sdk_config.h.
    It is now working properly.

    this answer was helphul.

    Thanks for your help.

  • TIMER is now working, but interrupts are not working properly.
    When the interrupt timing specified in CC[] is reached, the IRQHadler is not processed.
    Are there any possible causes?

    I also have one more question.
    I heard information that 32Bit cannot be specified for TIMER2, but I can get the timer value with BITMODE_332Bit and it seems to work.
    Does this cause any problems?

  • Hi,

    hrms said:
    TIMER is now working, but interrupts are not working properly.
    When the interrupt timing specified in CC[] is reached, the IRQHadler is not processed.
    Are there any possible causes?

    As before I do not know enough about your code to say anything with certainty. Please share your code. Without that, I can make some guesses. I am surprised that you need to enable TIMER_ENABLED in sdk_config.h as you have shared code that use the TIMER peripheral directly, not via the driver. When the driver is used, it will implement the interrupt routine (nrfx_timer_x_irq_handler). Is that the one that is being used and not your own? (you would get a compiler warning if multiple are used, though).

    hrms said:
    I also have one more question.
    I heard information that 32Bit cannot be specified for TIMER2, but I can get the timer value with BITMODE_332Bit and it seems to work.
    Does this cause any problems?

    All TIMER instances on all nRF52 variants are 32 bit. That was not the case for nRF51 though, so perhaps that is where you have this information from.

Related