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

LFCLK (32kHz) on a GPIO pin for precision measurement?

Dear Nordic Developer Zone,

Is there a way to set the LFCLK (32kHz) on a GPIO for precision measurement?

Best regards

Parents
  • Hi Sebastien,

    yes, you can connect the LFCLK to a GPIO using the RTC and GPIOTE:

    For example: RTC tick event can be configured to trigger a GPIOTE task which toggles a pin. See code example below which starts up the internal 32 kHz RC and configures RTC Tick event to toggle gpio 0:

    // Start 32 kHz clock
    NRF_CLOCK->TASKS_LFCLKSTART = 1;
    
    // Configure GPIOTE to toggle gpio 0
    NRF_GPIOTE->CONFIG[0] = (GPIOTE_CONFIG_MODE_Task       << GPIOTE_CONFIG_MODE_Pos)     | 
                            (GPIOTE_CONFIG_OUTINIT_Low     << GPIOTE_CONFIG_OUTINIT_Pos)  |
                            (GPIOTE_CONFIG_POLARITY_Toggle << GPIOTE_CONFIG_POLARITY_Pos) | 
                            (0                             << GPIOTE_CONFIG_PSEL_Pos);
    
    // Configure RTC1 to generate tick event
    NRF_RTC1->PRESCALER = 0;
    NRF_RTC1->EVTENSET  = RTC_EVTENSET_TICK_Msk;
    
    // Connect RTC1 tick event to GPIOTE toggle task
    NRF_PPI->CH[0].EEP = (uint32_t) &NRF_RTC1->EVENTS_TICK;
    NRF_PPI->CH[0].TEP = (uint32_t) &NRF_GPIOTE->TASKS_OUT[0];
    NRF_PPI->CHEN      = PPI_CHENCLR_CH0_Msk;
                         
    // Start RTC1
    NRF_RTC1->TASKS_START = 1;
    
    while(1)
    {
    }
    

    The pulse width of the signal measured on gpio 0 corresponds to LFCLK frequency (1 / 32768 seconds).

    Note that the internal RC needs to be calibrated (using calibration task NRF_CLOCK->TASKS_CAL) to achieve 250 ppm accuracy. External 16 MHz crystal should be running during calibration for best results.

Reply
  • Hi Sebastien,

    yes, you can connect the LFCLK to a GPIO using the RTC and GPIOTE:

    For example: RTC tick event can be configured to trigger a GPIOTE task which toggles a pin. See code example below which starts up the internal 32 kHz RC and configures RTC Tick event to toggle gpio 0:

    // Start 32 kHz clock
    NRF_CLOCK->TASKS_LFCLKSTART = 1;
    
    // Configure GPIOTE to toggle gpio 0
    NRF_GPIOTE->CONFIG[0] = (GPIOTE_CONFIG_MODE_Task       << GPIOTE_CONFIG_MODE_Pos)     | 
                            (GPIOTE_CONFIG_OUTINIT_Low     << GPIOTE_CONFIG_OUTINIT_Pos)  |
                            (GPIOTE_CONFIG_POLARITY_Toggle << GPIOTE_CONFIG_POLARITY_Pos) | 
                            (0                             << GPIOTE_CONFIG_PSEL_Pos);
    
    // Configure RTC1 to generate tick event
    NRF_RTC1->PRESCALER = 0;
    NRF_RTC1->EVTENSET  = RTC_EVTENSET_TICK_Msk;
    
    // Connect RTC1 tick event to GPIOTE toggle task
    NRF_PPI->CH[0].EEP = (uint32_t) &NRF_RTC1->EVENTS_TICK;
    NRF_PPI->CH[0].TEP = (uint32_t) &NRF_GPIOTE->TASKS_OUT[0];
    NRF_PPI->CHEN      = PPI_CHENCLR_CH0_Msk;
                         
    // Start RTC1
    NRF_RTC1->TASKS_START = 1;
    
    while(1)
    {
    }
    

    The pulse width of the signal measured on gpio 0 corresponds to LFCLK frequency (1 / 32768 seconds).

    Note that the internal RC needs to be calibrated (using calibration task NRF_CLOCK->TASKS_CAL) to achieve 250 ppm accuracy. External 16 MHz crystal should be running during calibration for best results.

Children
Related