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

  • 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

Reply
  • 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

Children
No Data
Related