0

I am using the STM32f7 Nucleo board for one of my projects. I need to make the program wait for the exact duration which is specified by the variable I am using. The waiting time is in terms of nanoseconds so I cannot use HAL_Delay() method.

The Timer frequency is 216 Mhz.I am using TIM3 which is ticking at 1us and TIM2 which is ticking at 1 milisecond.

However, I am not able to achieve the exact waiting time for the program.

while (ptpTimeCompare(configTime, currentTime) >= 0) {
    ptpTime temp;

    temp = ptpTimeDiff(configTime, currentTime);
    configStartWaitingTime = (uint64_t) (ptpTimetoCycleTime(&temp) *
        1000000000.0);

    if (temp.tv_sec > 0) {
        HAL_TIM_Base_Start(&htim2);
        delay_sec(temp.tv_sec * 1000);
        getCurrentTime(Scheduler);

        if (configTime >= currentTime) {
            HAL_TIM_Base_Start(&htim3);
            delay_usec(temp.tv_nsec / 100000);
            getCurrentTime(Scheduler);
        }
        else
            getCurrentTime(qbvScheduler);
    }
}

But, In the end the currentTime is always greater than the configTime.

Is there any way so that both currentTime & configTime are exactly equal so that the execution of the code begins exactly at the configTime ?

both the times are stored in a structure

currentTime{
tv_sec;
tv_nsec
};

Any help in this regard is highly appreciated

Thanks in Advance

Craig Estey
  • 30,627
  • 4
  • 24
  • 48
Gaurav1234
  • 43
  • 7
  • _But, In the end the currentTime is always greater than the configTime_ By how much? Due to delays in the ISR for timer interrupts, even if the interrupt hits the mark exactly, the time that base level gets control of will be greater. – Craig Estey Feb 07 '21 at 23:50
  • @Craig Estey - It's around 3 - 3.5 secs – Gaurav1234 Feb 08 '21 at 00:07
  • What do you mean by the waiting time is in terms of nanoseconds? Maybe you could make a counting loop and see if that can be tuned, but I don't think you can be that precise – Jack Lilhammers Feb 08 '21 at 00:25
  • `delay_usec(temp.tv_nsec / 100000);` -> `delay_usec(temp.tv_nsec / 1000);` ??? – Craig Estey Feb 08 '21 at 01:11
  • I downloaded freertos source from github. Various functions (e.g. `ptpTimeCompare`, `getCurrentTime`, ...), don't seem to be a part of it. So, what/where are these? – Craig Estey Feb 08 '21 at 01:14
  • How are you configuring the timers? Are they free running or do are they countdown [with interrupt]? Are they periodic [self rearming]? Can you change the countdown time on the fly? AFAICT, your code's doesn't interact with them directly. What is the source and granularity of `getCurrentTime`? – Craig Estey Feb 08 '21 at 01:18
  • `if (temp.tv_sec > 0)` might need to be `if ((temp.tv_sec > 0) || (temp.tv_nsec > 0))` – Craig Estey Feb 08 '21 at 01:22
  • @Gaurav1234 - relevant parts of your program code are missing. Please add them to the post or we cannot help you. See [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) – Tarmo Feb 08 '21 at 08:25

0 Answers0