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

NRF52832 - TIMER0 - GPIOE -PPI, Clock is inaccurate.

I have clock drift issue, should I calibrate anything? I test this simple code to get 1second or 100ms signal both of them are inaccurate. 

here is my code for 100ms; I tested it with a 16mhz timer clock but the result was similar.

I am measuring with a simple logic analyzer and I also tested a 1-second signal with a stopwatch, it drifts 1 second for every 2 minutes.

#include <stdbool.h>
#include <stdint.h>
#include "nrf.h"
#include "nrf_gpiote.h"
#include "nrf_gpio.h"
#include "boards.h"
#include "nrf_drv_ppi.h"
#include "nrf_drv_timer.h"
#include "nrf_drv_gpiote.h"
#include "app_error.h"

#ifdef BSP_LED_0
    #define GPIO_OUTPUT_PIN_NUMBER 15 //BSP_LED_0  /**< Pin number for output. */
#endif
#ifndef GPIO_OUTPUT_PIN_NUMBER
    #error "Please indicate output pin"
#endif

static nrf_drv_timer_t timer = NRF_DRV_TIMER_INSTANCE(0);

void timer_dummy_handler(nrf_timer_event_t event_type, void * p_context){}

static void led_blinking_setup()
{
    uint32_t compare_evt_addr;
    uint32_t gpiote_task_addr;
    nrf_ppi_channel_t ppi_channel;
    ret_code_t err_code;
    nrf_drv_gpiote_out_config_t config = GPIOTE_CONFIG_OUT_TASK_TOGGLE(false);

    err_code = nrf_drv_gpiote_out_init(GPIO_OUTPUT_PIN_NUMBER, &config);
    APP_ERROR_CHECK(err_code);



    nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;
    timer_cfg.frequency = NRF_TIMER_FREQ_31250Hz;
    timer_cfg.bit_width = NRF_TIMER_BIT_WIDTH_32;
    err_code = nrf_drv_timer_init(&timer, &timer_cfg, timer_dummy_handler);
    APP_ERROR_CHECK(err_code);
    nrf_drv_timer_extended_compare(&timer, (nrf_timer_cc_channel_t)0,3125, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, false);

    err_code = nrf_drv_ppi_channel_alloc(&ppi_channel);
    APP_ERROR_CHECK(err_code);

    compare_evt_addr = nrf_drv_timer_event_address_get(&timer, NRF_TIMER_EVENT_COMPARE0);
    gpiote_task_addr = nrf_drv_gpiote_out_task_addr_get(GPIO_OUTPUT_PIN_NUMBER);

    err_code = nrf_drv_ppi_channel_assign(ppi_channel, compare_evt_addr, gpiote_task_addr);
    APP_ERROR_CHECK(err_code);

    err_code = nrf_drv_gpiote_out_init(BSP_LED_1, &config);
    APP_ERROR_CHECK(err_code);
    gpiote_task_addr = nrf_drv_gpiote_out_task_addr_get(BSP_LED_1);
    err_code = nrfx_ppi_channel_fork_assign(ppi_channel, gpiote_task_addr);
    APP_ERROR_CHECK(err_code);


    err_code = nrf_drv_ppi_channel_enable(ppi_channel);
    APP_ERROR_CHECK(err_code);

    nrf_drv_gpiote_out_task_enable(GPIO_OUTPUT_PIN_NUMBER);
    nrf_drv_gpiote_out_task_enable(BSP_LED_1);
}

/**
 * @brief Function for application main entry.
 */
int main(void)
{
    ret_code_t err_code;
    err_code = nrf_drv_ppi_init();
    APP_ERROR_CHECK(err_code);
    err_code = nrf_drv_gpiote_init();
    APP_ERROR_CHECK(err_code);
    led_blinking_setup();
    nrf_drv_timer_enable(&timer);
    while (true) {}
}

Related