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

nRF52, 1-4 MHz clock needed

I am using the nRF52 Preview board, PCA 10036. I need to provide a high-frequency clock signal to a peripheral. Anything between 1 and 4 MHz will work. However, I don't need and don't want the CPU to receive an interrupt on every clock pulse.

I think that the GPIOTE example in the SDK is relevant to my needs. (I'm using SDK 0.9.2.) However: the code has a function called timer_dummy_handler(). While this function does nothing, the code does connect it to a timer event, with a call to nrf_drv_timer_init(). The documentation for nrf_drv_timer_init() says that an event handler function is a required argument.

The GPIOTE example code has a timer frequency of 200 milliseconds. Calling a quick and useless function five times/second is harmless. Calling that same function 1-4 million times / second would be a nightmare.

Is that function call actually used?

Can I avoid attaching a function call to a timer event, if I program at a lower level?

Parents
  • Hi,

    I wrote some code for this earlier, for 4 MHz output on pin 18 set NRF_TIMER1->CC[0] = 2;.

    int main(void)
    {
        // Set up GPIO as output
        nrf_gpio_range_cfg_output(17, 18);
        nrf_gpio_pin_clear(17); // Light LED 1 to indicate that the code is running
        
        // Start high frequency clock
        NRF_CLOCK->TASKS_HFCLKSTART = 1;
        while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0)
        {
            // Wait for clock to start
        }
        NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
        
        // Configure GPIOTE to toggle pin 18 
        NRF_GPIOTE->CONFIG[0] = GPIOTE_CONFIG_MODE_Task << GPIOTE_CONFIG_MODE_Pos |
                                GPIOTE_CONFIG_POLARITY_Toggle << GPIOTE_CONFIG_POLARITY_Pos |
                                18 << GPIOTE_CONFIG_PSEL_Pos | 
                                GPIOTE_CONFIG_OUTINIT_Low << GPIOTE_CONFIG_OUTINIT_Pos;
                                
        // Set up timer
        NRF_TIMER1->PRESCALER = 0;
        NRF_TIMER1->CC[0] = 2; // Adjust the output frequency by adjusting the CC.
        NRF_TIMER1->SHORTS = TIMER_SHORTS_COMPARE0_CLEAR_Enabled << TIMER_SHORTS_COMPARE0_CLEAR_Pos;
        NRF_TIMER1->TASKS_START = 1;
            
        // Set up PPI to connect the timer compare event with the GPIOTE toggle task
        NRF_PPI->CH[0].EEP = (uint32_t) &NRF_TIMER1->EVENTS_COMPARE[0];
        NRF_PPI->CH[0].TEP = (uint32_t) &NRF_GPIOTE->TASKS_OUT[0];
        
        NRF_PPI->CHENSET = PPI_CHENSET_CH0_Enabled << PPI_CHENSET_CH0_Pos;
        
        while (true)
        {
           
        }
    }
    

    The principle is well described by RKs post. You can find relevant documentation in the infocenter:

    Best regards,

    Øyvind

  • 18 lines - I guessed 20 - I must be slipping :)

Reply Children
No Data
Related