I want to measure the accuracy of the 32.768 kHz oscillator.
Is there a way to output it on a port-pin, even when it comes out via a divider?
I can't touch the crystal as it would change the loading.
I want to measure the accuracy of the 32.768 kHz oscillator.
Is there a way to output it on a port-pin, even when it comes out via a divider?
I can't touch the crystal as it would change the loading.
Try something like the code below. It should output 512Hz square wave on pin CLKOUT_PIN. To change frequency, adjust TICKS and PRESCALER. If there is no softdevice and no OS running there should be no jitter.
#define CC 0
#define PRESCALER 31
#define TICKS 1
#define CLKOUT_PIN 15
static nrf_drv_rtc_config_t m_rtc_config = NRF_DRV_RTC_DEFAULT_CONFIG;
// RTC0 is used by softdevice, RTC1 is used by RTOS
static nrf_drv_rtc_t const m_rtc = NRF_DRV_RTC_INSTANCE(2);
static void rtc_handler(nrf_drv_rtc_int_type_t int_type)
{
nrf_gpio_pin_toggle(CLKOUT_PIN);
uint32_t err_code = nrf_drv_rtc_cc_set(
&m_rtc,
CC,
(nrf_rtc_cc_get(m_rtc.p_reg, CC) + TICKS) & RTC_COUNTER_COUNTER_Msk,
true);
APP_ERROR_CHECK(err_code);
}
void rtc_init(void)
{
ret_code_t err_code;
m_rtc_config.prescaler = PRESCALER; // prescaler 31 for 1024 ticks per second
err_code = nrf_drv_rtc_init(&m_rtc, &m_rtc_config, rtc_handler);
APP_ERROR_CHECK(err_code);
err_code = nrf_drv_rtc_cc_set(&m_rtc, CC, TICKS, true);
APP_ERROR_CHECK(err_code);
nrf_drv_rtc_enable(&m_rtc);
}
int main(void)
{
ret_code_t err_code = nrf_drv_clock_init();
APP_ERROR_CHECK(err_code);
nrf_gpio_cfg_output(CLKOUT_PIN);
rtc_init();
while (1) {};
}
On a similar note: we have to measure the 32KHz crystal accuracy on every product (nRF5x or not). What we do is have a 1 second interrupt handler driven by the RTC or similar feature on other processors. That interrupt handler pulses a unused GPIO every time it fires. This lets our HW folks measure the accuracy of the crystal over the temperature range of the product, and we can apply calibration for temperature and offset if needed based on what we learn in the HW testing.
On a similar note: we have to measure the 32KHz crystal accuracy on every product (nRF5x or not). What we do is have a 1 second interrupt handler driven by the RTC or similar feature on other processors. That interrupt handler pulses a unused GPIO every time it fires. This lets our HW folks measure the accuracy of the crystal over the temperature range of the product, and we can apply calibration for temperature and offset if needed based on what we learn in the HW testing.