Questions about FreeRTOS and RTC on nrf52832

Hi all,

I am having an issue with FreeRTOS on the NRF52832 (SDK 17.1.0).

I need precise time so I use the io to measure the time. the code like this:

    nrf_gpio_cfg_output(18);
    PRINTF("nrf_gpio_cfg_output 18.");
    while(1)
    {
        nrf_gpio_pin_write(18, 1);
        vTaskDelay(1);
        nrf_gpio_pin_write(18, 0);
        vTaskDelay(1);
        nrf_gpio_pin_write(18, 1);
        nrf_delay_us(500);
        nrf_gpio_pin_write(18, 0);
        nrf_delay_us(500);
        nrf_delay_ms(10);
        PRINTF("while loop.");
    }

#define configTICK_RATE_HZ        1024

But the oscilloscope waveform shows that the interval is abnormal.

The first pullup interval is 738 us, and the first pulldown interval is 910us, but the second pullup interval is exact time 500us.

That means delay time on rtos is not accurate, but nrf_delay is accurate.

Did I set something wrong about freertos?

The config file is here.

8508.FreeRTOSConfig.h

5432.sdk_config.h

Hope you can give me some suggestions.

Best regards

Lurn

Parents
  • Hi,

    The time resolution of vtaskdelay is limited by the configTICK_RATE_HZ from the RTC. For short accurate delays I would recommend using busy waits (nrf_delay_us()). When you factor in the processing overhead associated with context switching, I don't think the power consumption difference will be significant. Another alternative is to use a TIMER instance.

    Best regards,

    Vidar

Reply
  • Hi,

    The time resolution of vtaskdelay is limited by the configTICK_RATE_HZ from the RTC. For short accurate delays I would recommend using busy waits (nrf_delay_us()). When you factor in the processing overhead associated with context switching, I don't think the power consumption difference will be significant. Another alternative is to use a TIMER instance.

    Best regards,

    Vidar

Children
  • Hi Vidar,

    If I want to use the function vTaskDelayUntil how to handle it. Beacuse I want to ensure a constant execution frequency and the interval is 2ms/ 1ms.

    And I used TIMER to test, but I get something crash, but in thread while loop is ok, probably due to some interrupt issues, but I haven't checked in detail now.

    Update:

    As you said I use timer to test the interval, every interval is 1.82ms.

    Should I use the nrf timer APP_TIMER_DEF() ?

    xTimerCreate("DataTimer",
                pdMS_TO_TICKS(2),
                pdTRUE,
                0,
                TimerFunc);
    
    void TimerFunc(TimerHandle_t xTimers)
    {
        static uint8_t flag = 0;
        nrf_gpio_pin_write(18, flag);
        if(flag)
            flag = 0;
        else
            flag = 1;
    }

    Update 2:

    when I use two timer to change the IO state.

    I found that the two timers always set the IO at same time. so I print the tick, and it shows the count is same.I want to set one of it at odd numbers, another at even number. How to do this?

        // start timer 
        xTimerStart(testTimer, 0);
        nrf_delay_ms(1);
        xTimerStart(testTimer2, 0);
        
        
        // in timer func
        nrf_gpio_pin_write(18, 0);
        TickType_t tetsttick = xTaskGetTickCount();
        printf("pulldown tick = %d.", tetsttick);

    Best regards,

    Lurn

  • Hi Vidar,

    Another qusetions.

    1.

    I would recommend using busy waits (nrf_delay_us())

    If i use the nrf_delay_ms(time1); Can Freertos schedule tasks at this time? I need rtos to schedule tasks in this time. 

    2. I saw in this link @Edvin said the PPI is very accurate, and in my project, since I'm using ESB and UARTE, I'm also using HFCLK.

     Can I also use the PPI in FreeRTOS? Can it trigger the ESB write payload and send it? Can I use it to sync time between two devices?

    Best regards,

    Lurn

  • Hi Vidar,

    Actually, what i want to do is that I have 1 PRX and 2 PTX, and I want to synchronize the timer of the 3 devices via ESB.

    Then make sure that the time points when the two TXs send data are separated to ensure that the data does not conflict(due to I need real-time data, I can't use the esb retransmit). And The sending interval of each TX is 2MS. It's looks like the sending time of TX 1 to be at an odd time, another at even time.

    So I want to do it with clock synchronization, but now I can't achieve precise 2ms time synchronization.

    Maybe what I'm doing is not right, because I'm just trying, so I should need to say what I want to do so that you can correctly understand whether I'm trying to go in the right direction.

    So did PPI can help me to do this?

    In this link, @haakonsh said it can use a propriatary 2.4GHz protocol to synchronize nordic devices, but the link he shared is lost, can you give me a new link about it?

    And will it solve my problem? If it doesn't work out, can you give me a better suggestion?

    Update:

    Can I use the timeslot to do this? If I can use it, can you give me a example about it. my project didn't use the SoftDevice, 

    Best regards,

    Lurn

  • Hi Lurn,

    The time resolution with vtaskdelay is limited by the RTC clock source (32768/1024) so you should use another timer source if you need more accuracy. Preferably with PPI if possible.

    Here is the link:

     Wireless timer synchronization among nRF5 devices  

    Best regards,

    Vidar

  • Hi Vidar,

    After communicating the requirements, my colleagues and I believe that the focus of the problem should be on how to synchronize the three devices. Of course, time accuracy is also important, but it should be placed after synchronization. 

    In this link, it shows how to sync the clock, and I will learn how to implement it, but I also have a question, after sync the time, How can I confirm that the two TX devices are separate? Like TX A at an odd time, TX B at even time.

    Best regards,

    Lurn

Related