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

Can I use Timer1 and RTC on the same app?

Hi all,

I would like someone give me a suggestion about timers.

in my current approvement I am using an RTC timer with 32k crystal to control my sleep times it is power efficient and accurate, but limited if I want to use micro seconds interruptions.

I need to generate delays for 5us for a custom protocol to transfer data, I could use nrf_delay_us function, but it will keep the CPU running. it worth implement a TIMER for such delay? 

I also tried to port the code from Timer example to my code (it uses softdevice) I am using the TIMER1 as far I see. but it is not working properly

#include <stdbool.h>
#include <stdint.h>
#include "nrf.h"
#include "nrf_drv_timer.h"
#include "bsp.h"
#include "app_error.h"

const nrf_drv_timer_t TIMER_LED = NRF_DRV_TIMER_INSTANCE(1);

void timer_led_event_handler(nrf_timer_event_t event_type, void* p_context)
{
    static uint32_t i;
    uint32_t led_to_invert = ((i++) % LEDS_NUMBER);

    switch (event_type)
    {
        case NRF_TIMER_EVENT_COMPARE0:
            bsp_board_led_invert(led_to_invert);
            break;

        default:
            //Do nothing.
            break;
    }
}

void Start_timer(void)
{
    uint32_t time_ms = 500; //Time(in miliseconds) between consecutive compare events.
    uint32_t time_ticks;
    uint32_t err_code = NRF_SUCCESS;

    //Configure all leds on board.
    bsp_board_leds_init();

    //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);
    APP_ERROR_CHECK(err_code);

    time_ticks = nrf_drv_timer_ms_to_ticks(&TIMER_LED, time_ms);

    nrf_drv_timer_extended_compare(
         &TIMER_LED, NRF_TIMER_CC_CHANNEL0, time_ticks, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, true);

    nrf_drv_timer_enable(&TIMER_LED);

}

talking in efficiency what is better nrf_delay_us(5) vs use TIMER1 for 5us delay

I also was wondering if is possible set a value to the RTC counter to control when it overflow, for example if it is working at 8 bit, set 250, ant it will overflow every 5 ticks

Thanks

Parents Reply Children
Related