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

change clock speed nrf52

I have some troubles to understand the system clock functionality. I am using a Module from Rigado which uses the nRF52 and has an external 32Mhz osc. on board.

In the nrv_drv_config.h file follwing value is set for the HFCLK controller.

#define CLOCK_CONFIG_XTAL_FREQ          NRF_CLOCK_XTALFREQ_Default

If I understand it correctly this should equal a system clock of 64 Mhz. However, in my main loop I have nothing else than nrf_gpio_pin_toggle(LED_4);

No interrupts are used and no Softdevice is running.

The frequenzy I get on the LED pin is about 300 Khz.

Can anyone explain why it is only 300 KHz I would have expected something around a few MHz.

I run into this problem because I was debugging a problem where the processing of COMP interrupts took to long and I wanted to make sure that the nRF52 is running as fast as possible.

so two questions:

  1. Why is the pin toggle frequency so low ?
  2. How do I change the system clock from ?

thanks in advanced best, Timur

  • You can't change the system clock, it's 64MHz and that's it.

    How are you measuring 300kHz on the LED pin? I just compiled nrf_gpio_pin_toggle() and counted the instructions and it's about 30, call it 32, so if all you have is that in a loop there's no way you can be getting 300kHz, should be 6 times that, that is really all your code,

    while(1)
        nrf_gpio_pin_toggle( LED_4);
    
  • This all my code: err_code = nrf_drv_clock_init(); APP_ERROR_CHECK(err_code);

    while(1)
    {
    	nrf_gpio_pin_toggle(LED_4);
    }
    

    And I am measuring the frequency with an Tektronix oscilloscope. I had on button poll left which I have deleted now and now I am getting close to 500 Khz.

    The gcc optimaziation is off, how can I see the instructions count.

    image description

  • ok so 500kHz, that's the time for a full cycle so you're getting one toggle every 1us, so that's closer. With the clock running at 64MHz that's about 64 instructions per toggle. I just eyeballed the compiled code and rough-counted the instructions as something like 32, that includes all the entry/exit code for the routine as well as the actual toggle.

    So if my count's right, we're about a factor of 2 off. But you're running from flash, that might introduce a wait state. I'm not convinced by that statement partly because there's cache RAM on the chip to deal with that exact thing. That simple code should prime the cache and get it running with 0 wait states. If you have however an average of 1 wait state, there's your factor of two.

    Hmm .. checks manual. I think the cache isn't enabled by default. Try that one thing, NRF_NVMC->ICACHECNF = 0x01. See what happens.

  • Great that explains what I am experiencing.

    Since I am sure now that the nRF52 is running on full speed. I can focus on making my code faster in order to handle the interrupts.

Related