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

Peripheral Timer Example - Timing inconsistent across different boards

Greetings,

I'm working on the TIMER example ( ⁨nRF5_SDK_15.2.0_9412b96⁩ ▸ ⁨examples⁩ ▸ ⁨peripheral⁩ ▸ ⁨timer⁩ ) on a nRF52832 with no changes to the code.  Across 5 different boards - Rigado BMD-300-EVAL (3 boards), Rigado BMD-350-EVAL (1 board), and a Nordic nRF52 DK (1 board), I see different timing of the LED output (P0.17) on each one. Worse fast case is 1.985 seconds. Worse slow case is 2.030 seconds. Each individual board shows consistent timing from run to run. Measurements were done with a Saleae Logic analyzer.

I figure there would be some interrupt overhead, so I'd expect all of them to be some value over 2 seconds. The one that's 15ms under 2 seconds is confusing me. Is this expected? How is this one shorter?

Is PPI / GPIOTE a better choice?

Thanks,

Bob

Parents
  • Hi,

    There is nothing else going on in this example, so the timing is deterministic (in clock cycles) even if you don't use PPI. The problem is likely that you are using the internal RC oscillator instead of the crystal oscillator, as that is not started in the TIMER example.

    The cleanest way to start it is via the Clock driver, as that can be used regardless of whether you use the SoftDevice or not. You can modify the Timer example from SDK 15.2 as shown by this diff:

    diff --git a/examples/peripheral/timer/main.c b/examples/peripheral/timer/main.c
    index e2e80b0..c7be4bb 100644
    --- a/examples/peripheral/timer/main.c
    +++ b/examples/peripheral/timer/main.c
    @@ -53,6 +53,7 @@
     #include "nrf_drv_timer.h"
     #include "bsp.h"
     #include "app_error.h"
    +#include "nrf_drv_clock.h"
     
     const nrf_drv_timer_t TIMER_LED = NRF_DRV_TIMER_INSTANCE(0);
     
    @@ -89,6 +90,10 @@ int main(void)
         //Configure all leds on board.
         bsp_board_init(BSP_INIT_LEDS);
     
    +    // Enable HF crystall oscillator
    +    err_code = nrf_drv_clock_init();
    +    nrf_drv_clock_hfclk_request(NULL);
    +
         //Configure TIMER_LED for generating simple light effect - leds on board will invert his state one after the other.
         nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;
         err_code = nrf_drv_timer_init(&TIMER_LED, &timer_cfg, timer_led_event_handler);
    

    Note that you also need to update the sdk_config.h file as shown below and add nrf_drv_clock.c and nrfx_clock.c to the project in order to use the clock driver.

    diff --git a/examples/peripheral/timer/pca10040/blank/config/sdk_config.h b/examples/peripheral/timer/pca10040/blank/config/sdk_config.h
    index 9a2140e..6dc2fc1 100644
    --- a/examples/peripheral/timer/pca10040/blank/config/sdk_config.h
    +++ b/examples/peripheral/timer/pca10040/blank/config/sdk_config.h
    @@ -48,6 +48,129 @@
     #endif
     // <h> nRF_Drivers 
     
    +
    +
    +// <e> NRFX_CLOCK_ENABLED - nrfx_clock - CLOCK peripheral driver
    +//==========================================================
    +#ifndef NRFX_CLOCK_ENABLED
    +#define NRFX_CLOCK_ENABLED 1
    +#endif
    +// <o> NRFX_CLOCK_CONFIG_LF_SRC  - LF Clock Source
    + 
    +// <0=> RC 
    +// <1=> XTAL 
    +// <2=> Synth 
    +// <131073=> External Low Swing 
    +// <196609=> External Full Swing 
    +
    +#ifndef NRFX_CLOCK_CONFIG_LF_SRC
    +#define NRFX_CLOCK_CONFIG_LF_SRC 1
    +#endif
    +
    +// <o> NRFX_CLOCK_CONFIG_IRQ_PRIORITY  - Interrupt priority
    + 
    +// <0=> 0 (highest) 
    +// <1=> 1 
    +// <2=> 2 
    +// <3=> 3 
    +// <4=> 4 
    +// <5=> 5 
    +// <6=> 6 
    +// <7=> 7 
    +
    +#ifndef NRFX_CLOCK_CONFIG_IRQ_PRIORITY
    +#define NRFX_CLOCK_CONFIG_IRQ_PRIORITY 6
    +#endif
    +
    +// <e> NRFX_CLOCK_CONFIG_LOG_ENABLED - Enables logging in the module.
    +//==========================================================
    +#ifndef NRFX_CLOCK_CONFIG_LOG_ENABLED
    +#define NRFX_CLOCK_CONFIG_LOG_ENABLED 0
    +#endif
    +// <o> NRFX_CLOCK_CONFIG_LOG_LEVEL  - Default Severity level
    + 
    +// <0=> Off 
    +// <1=> Error 
    +// <2=> Warning 
    +// <3=> Info 
    +// <4=> Debug 
    +
    +#ifndef NRFX_CLOCK_CONFIG_LOG_LEVEL
    +#define NRFX_CLOCK_CONFIG_LOG_LEVEL 3
    +#endif
    +
    +// <o> NRFX_CLOCK_CONFIG_INFO_COLOR  - ANSI escape code prefix.
    + 
    +// <0=> Default 
    +// <1=> Black 
    +// <2=> Red 
    +// <3=> Green 
    +// <4=> Yellow 
    +// <5=> Blue 
    +// <6=> Magenta 
    +// <7=> Cyan 
    +// <8=> White 
    +
    +#ifndef NRFX_CLOCK_CONFIG_INFO_COLOR
    +#define NRFX_CLOCK_CONFIG_INFO_COLOR 0
    +#endif
    +
    +// <o> NRFX_CLOCK_CONFIG_DEBUG_COLOR  - ANSI escape code prefix.
    + 
    +// <0=> Default 
    +// <1=> Black 
    +// <2=> Red 
    +// <3=> Green 
    +// <4=> Yellow 
    +// <5=> Blue 
    +// <6=> Magenta 
    +// <7=> Cyan 
    +// <8=> White 
    +
    +#ifndef NRFX_CLOCK_CONFIG_DEBUG_COLOR
    +#define NRFX_CLOCK_CONFIG_DEBUG_COLOR 0
    +#endif
    +
    +// </e>
    +
    +// </e>
    +
    +// <e> NRF_CLOCK_ENABLED - nrf_drv_clock - CLOCK peripheral driver - legacy layer
    +//==========================================================
    +#ifndef NRF_CLOCK_ENABLED
    +#define NRF_CLOCK_ENABLED 1
    +#endif
    +// <o> CLOCK_CONFIG_LF_SRC  - LF Clock Source
    + 
    +// <0=> RC 
    +// <1=> XTAL 
    +// <2=> Synth 
    +// <131073=> External Low Swing 
    +// <196609=> External Full Swing 
    +
    +#ifndef CLOCK_CONFIG_LF_SRC
    +#define CLOCK_CONFIG_LF_SRC 1
    +#endif
    +
    +// <o> CLOCK_CONFIG_IRQ_PRIORITY  - Interrupt priority
    + 
    +
    +// <i> Priorities 0,2 (nRF51) and 0,1,4,5 (nRF52) are reserved for SoftDevice
    +// <0=> 0 (highest) 
    +// <1=> 1 
    +// <2=> 2 
    +// <3=> 3 
    +// <4=> 4 
    +// <5=> 5 
    +// <6=> 6 
    +// <7=> 7 
    +
    +#ifndef CLOCK_CONFIG_IRQ_PRIORITY
    +#define CLOCK_CONFIG_IRQ_PRIORITY 6
    +#endif
    +
    +// </e>
    +
     //==========================================================
     // <e> NRFX_TIMER_ENABLED - nrfx_timer - TIMER periperal driver
     //==========================================================
    

Reply
  • Hi,

    There is nothing else going on in this example, so the timing is deterministic (in clock cycles) even if you don't use PPI. The problem is likely that you are using the internal RC oscillator instead of the crystal oscillator, as that is not started in the TIMER example.

    The cleanest way to start it is via the Clock driver, as that can be used regardless of whether you use the SoftDevice or not. You can modify the Timer example from SDK 15.2 as shown by this diff:

    diff --git a/examples/peripheral/timer/main.c b/examples/peripheral/timer/main.c
    index e2e80b0..c7be4bb 100644
    --- a/examples/peripheral/timer/main.c
    +++ b/examples/peripheral/timer/main.c
    @@ -53,6 +53,7 @@
     #include "nrf_drv_timer.h"
     #include "bsp.h"
     #include "app_error.h"
    +#include "nrf_drv_clock.h"
     
     const nrf_drv_timer_t TIMER_LED = NRF_DRV_TIMER_INSTANCE(0);
     
    @@ -89,6 +90,10 @@ int main(void)
         //Configure all leds on board.
         bsp_board_init(BSP_INIT_LEDS);
     
    +    // Enable HF crystall oscillator
    +    err_code = nrf_drv_clock_init();
    +    nrf_drv_clock_hfclk_request(NULL);
    +
         //Configure TIMER_LED for generating simple light effect - leds on board will invert his state one after the other.
         nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;
         err_code = nrf_drv_timer_init(&TIMER_LED, &timer_cfg, timer_led_event_handler);
    

    Note that you also need to update the sdk_config.h file as shown below and add nrf_drv_clock.c and nrfx_clock.c to the project in order to use the clock driver.

    diff --git a/examples/peripheral/timer/pca10040/blank/config/sdk_config.h b/examples/peripheral/timer/pca10040/blank/config/sdk_config.h
    index 9a2140e..6dc2fc1 100644
    --- a/examples/peripheral/timer/pca10040/blank/config/sdk_config.h
    +++ b/examples/peripheral/timer/pca10040/blank/config/sdk_config.h
    @@ -48,6 +48,129 @@
     #endif
     // <h> nRF_Drivers 
     
    +
    +
    +// <e> NRFX_CLOCK_ENABLED - nrfx_clock - CLOCK peripheral driver
    +//==========================================================
    +#ifndef NRFX_CLOCK_ENABLED
    +#define NRFX_CLOCK_ENABLED 1
    +#endif
    +// <o> NRFX_CLOCK_CONFIG_LF_SRC  - LF Clock Source
    + 
    +// <0=> RC 
    +// <1=> XTAL 
    +// <2=> Synth 
    +// <131073=> External Low Swing 
    +// <196609=> External Full Swing 
    +
    +#ifndef NRFX_CLOCK_CONFIG_LF_SRC
    +#define NRFX_CLOCK_CONFIG_LF_SRC 1
    +#endif
    +
    +// <o> NRFX_CLOCK_CONFIG_IRQ_PRIORITY  - Interrupt priority
    + 
    +// <0=> 0 (highest) 
    +// <1=> 1 
    +// <2=> 2 
    +// <3=> 3 
    +// <4=> 4 
    +// <5=> 5 
    +// <6=> 6 
    +// <7=> 7 
    +
    +#ifndef NRFX_CLOCK_CONFIG_IRQ_PRIORITY
    +#define NRFX_CLOCK_CONFIG_IRQ_PRIORITY 6
    +#endif
    +
    +// <e> NRFX_CLOCK_CONFIG_LOG_ENABLED - Enables logging in the module.
    +//==========================================================
    +#ifndef NRFX_CLOCK_CONFIG_LOG_ENABLED
    +#define NRFX_CLOCK_CONFIG_LOG_ENABLED 0
    +#endif
    +// <o> NRFX_CLOCK_CONFIG_LOG_LEVEL  - Default Severity level
    + 
    +// <0=> Off 
    +// <1=> Error 
    +// <2=> Warning 
    +// <3=> Info 
    +// <4=> Debug 
    +
    +#ifndef NRFX_CLOCK_CONFIG_LOG_LEVEL
    +#define NRFX_CLOCK_CONFIG_LOG_LEVEL 3
    +#endif
    +
    +// <o> NRFX_CLOCK_CONFIG_INFO_COLOR  - ANSI escape code prefix.
    + 
    +// <0=> Default 
    +// <1=> Black 
    +// <2=> Red 
    +// <3=> Green 
    +// <4=> Yellow 
    +// <5=> Blue 
    +// <6=> Magenta 
    +// <7=> Cyan 
    +// <8=> White 
    +
    +#ifndef NRFX_CLOCK_CONFIG_INFO_COLOR
    +#define NRFX_CLOCK_CONFIG_INFO_COLOR 0
    +#endif
    +
    +// <o> NRFX_CLOCK_CONFIG_DEBUG_COLOR  - ANSI escape code prefix.
    + 
    +// <0=> Default 
    +// <1=> Black 
    +// <2=> Red 
    +// <3=> Green 
    +// <4=> Yellow 
    +// <5=> Blue 
    +// <6=> Magenta 
    +// <7=> Cyan 
    +// <8=> White 
    +
    +#ifndef NRFX_CLOCK_CONFIG_DEBUG_COLOR
    +#define NRFX_CLOCK_CONFIG_DEBUG_COLOR 0
    +#endif
    +
    +// </e>
    +
    +// </e>
    +
    +// <e> NRF_CLOCK_ENABLED - nrf_drv_clock - CLOCK peripheral driver - legacy layer
    +//==========================================================
    +#ifndef NRF_CLOCK_ENABLED
    +#define NRF_CLOCK_ENABLED 1
    +#endif
    +// <o> CLOCK_CONFIG_LF_SRC  - LF Clock Source
    + 
    +// <0=> RC 
    +// <1=> XTAL 
    +// <2=> Synth 
    +// <131073=> External Low Swing 
    +// <196609=> External Full Swing 
    +
    +#ifndef CLOCK_CONFIG_LF_SRC
    +#define CLOCK_CONFIG_LF_SRC 1
    +#endif
    +
    +// <o> CLOCK_CONFIG_IRQ_PRIORITY  - Interrupt priority
    + 
    +
    +// <i> Priorities 0,2 (nRF51) and 0,1,4,5 (nRF52) are reserved for SoftDevice
    +// <0=> 0 (highest) 
    +// <1=> 1 
    +// <2=> 2 
    +// <3=> 3 
    +// <4=> 4 
    +// <5=> 5 
    +// <6=> 6 
    +// <7=> 7 
    +
    +#ifndef CLOCK_CONFIG_IRQ_PRIORITY
    +#define CLOCK_CONFIG_IRQ_PRIORITY 6
    +#endif
    +
    +// </e>
    +
     //==========================================================
     // <e> NRFX_TIMER_ENABLED - nrfx_timer - TIMER periperal driver
     //==========================================================
    

Children
Related