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

How to access the TIMER3 in wireless timer sync?

Hello,

I found this great blog entry about synchronizing timers on different devices: https://devzone.nordicsemi.com/nordic/short-range-guides/b/bluetooth-low-energy/posts/wireless-timer-synchronization-among-nrf5-devices

It works just fine on a nRF52840DK and a nRF52DK. Timers on multiple nRF5 devices are synced by one device (master) sending sync packets containing captured timer values to other devices (nodes). (TIMER3 is free running, TIMER2 counts TIMER3 overflows).

At the moment I am struggeling to access this synchronized TIMER3 on the two boards. I am familiar with setting up a TIMER without wireless timer sync. 

So here are my questions:

1. How can I define CC, Prescaler, Mode, Bitmode, the interrupt handler and so forth when using wireless timer sync?

2. Can anybody provide me some advice how to access this synchronized TIMER3 properly? 

Thank you very much in advance.

  • Hello Audun,

    thank you very much for your detailed answer.

    You already helped me a lot.

    Can you please answer my following questions:

    1. The ts_timestamp_get_ticks() function ist defined as 

    uint32_t ts_timestamp_get_ticks_u32(uint8_t ppi_chn);

    At the moment I call this function with ppi_chn = 0. With time_sync activated the return values on central and peripheral look equal. Is it correct to use ppi_chn = 0?

    2. Maybe a silly question, but I still do not really know how to handle the overflow when 2^32 is reached (After ~268s). I need a counter that runs up to 8 hours without overflow. Do you have any suggestions how to handle this?

    3. Where can I configure the sync paket transmit rate? (1Hz or lower would be enough)

    you can calculate the timestamp by n * 10 ms + timer value, where n is the connection event count.

    I will give that a try too. If I do this on peripheral and central, I should get also a synchronized time base? I already worked with radio_notifications and they can be a powerful tool.

    However, in the near future, there will be up to 4 peripherals and one central in this project. So I think the wireless timer sync is the approach, which is more reliable and futureproof. 

    4. Do you have any information or experience when the wireless timer sync is used with one central and more than one peripheral?

    Thank you very much in advance.

    Michael

  • No problem!

    I'll try to answer these questions:

    1) I recommend using the latest version of the time_sync code. Among other improvements, the timestamp functions are updated to not have this parameter: https://github.com/nordic-auko/nRF5-ble-timesync-demo/blob/master/nRF5_SDK_16.0.0_98a08e2/examples/common/time_sync.h#L216 . If not, you can use the nrfx_ppi_channel_alloc() function to allocate an unused PPI channel, and use this channel when calling the old version of the timestamp function.

    2) It would be easiest to use the 64-bit version of the timestamp function in this case, I think. This way you wont have to count overflows. Something along the lines of:

    uint64_t timestamp_ticks;
    uint32_t timestamp_ms;
    
    timestamp_ticks = ts_timestamp_get_ticks_u64();
    timestamp_ms = (uint32_t) ROUNDED_DIV(timestamp_ticks, 16000);

    3) Use ts_tx_start(1) to set the lowest possible rate. As this API is in Hz, you cannot go below 1 Hz here

    Regarding using the connection event to calculate a shared time, this does get more complicated with multiple connections. I was thinking maybe this function might be more useful than the radio notifications: 

    https://infocenter.nordicsemi.com/topic/com.nordic.infocenter.s140.api.v7.2.0/group___b_l_e___g_a_p___f_u_n_c_t_i_o_n_s.html?cp=4_7_4_1_2_1_3_10#ga6a2d8c4cd863ac538eb0a51d594e0dd6

    You could have the softdevice trigger a timer capture for each link.

    Either way, the time_sync code should work fine too. In this topology you can have any number of timing receivers (the receivers only listen, they never reply to the timing transmitter).

    4) You can have any number of receivers with the time_sync code. The transmitter will transmit at the rate you start it with, and the receivers will listen for the sync packets from the transmitter. Note that in its current version the receivers will run the radio as much as possible in receive mode, which is not great for power consumption. An improved implementation would have the receivers only turn on the radio when it expects the transmitter to send a packet.

  • Hello Audun,

    thank you for your great answer. Sorry for my late reply, I was out of office.

    I continued my work on this and I figured something weird out.

    I tried to add a continuous data transmission to the ble_app_uart example of the timesync-demo. To achieve this I implemented a timer which calls the ble_nus_data_send function every 40ms in its timer handler. So I added the following code to the ble_app_uart example: 

    static const nrfx_timer_t   m_timer = NRFX_TIMER_INSTANCE(4);
    
    static uint32_t counter = 0;
    static uint8_t array[200];
    
    void timer_handler(nrf_timer_event_t event_type, void* p_context)
    {
        ret_code_t err_code;
        counter++;
        uint16_t length_array = 200;
    
        err_code = ble_nus_data_send(&m_nus, array, &length_array, m_conn_handle);
        if ((err_code != NRF_ERROR_INVALID_STATE) &&
            (err_code != NRF_ERROR_RESOURCES) &&
            (err_code != NRF_ERROR_NOT_FOUND))
        {
            APP_ERROR_CHECK(err_code);
        }
    }
    
    void event_timer_init(void)
    {
        ret_code_t err_code;
        
        nrfx_timer_config_t timer_config = NRFX_TIMER_DEFAULT_CONFIG;
        timer_config.frequency = NRF_TIMER_FREQ_125kHz;
        err_code = nrfx_timer_init(&m_timer, &timer_config, timer_handler);
        APP_ERROR_CHECK(err_code);
    
        uint32_t ticks = nrfx_timer_ms_to_ticks(&m_timer,40);
        nrfx_timer_extended_compare(&m_timer, NRF_TIMER_CC_CHANNEL0, ticks, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, true);
        nrfx_timer_enable(&m_timer);
    }
    
    void init_array (void)
    {
        int i;
        for(i = 0; i < 200; i++)
        {
            array[i] = 33;
        }
    }
    
    int main(void)
    {
        ...
        event_timer_init();
        init_array();
        ...
    }

    I use TIMER4. 

    Here is my problem: The timer handler (and the ble_nus_data_send() function) gets called every ~12s instead of every 40ms.

    If I add the code above into the normal ble_app_uart example without timer sync it works like it should.

    Here are my questions:

    1. Do you know what causes this weird behaviour?

    2. Is TIMER4 available in general? (TIMER0 is Softdevice, TIMER2 and TIMER3 for timesync)

    3. Can you please provide me some proper guidance how to fix this issue?

    Thank you very much in advance.

    #Update:

    It looks like If I use TIMER1 it works like it should. Can you please still answer my questions. Thank you very much.

  • Hi Michael,

    I looked at the code again, and noticed that different timers are used in the time_sync examples depending on SDK version. Could there be a conflict here with the version you are using?

    SDK 14.2.0 version:

        ts_params.high_freq_timer[0] = NRF_TIMER3;
        ts_params.high_freq_timer[1] = NRF_TIMER2;

    SDK 16.0.0 version:

        ts_init_t init_ts =
        {
            .high_freq_timer[0] = NRF_TIMER3,
            .high_freq_timer[1] = NRF_TIMER4,

  • Hi Audun, 

    yes, there was an conflict. Now I use TIMER1 for my application and it works.

    Thank you.

    Kind regards,

    Michael

Related