kmilo17pet / QuarkTS

An open-source OS for embedded applications that supports prioritized cooperative scheduling, time control, inter-task communications primitives, hierarchical state machines and CoRoutines.
MIT License
253 stars 44 forks source link

Timing may not be correct when using floating-point timing-base #7

Closed HpLightcorner closed 4 years ago

HpLightcorner commented 4 years ago

Issue: When initialising the OS in the standard-mode using floating points (eg. 0.001s as base-time, 1s as periodic-task time) the task becomes alive every 0.999s instead of exactly one second.

Problem: I tracked down the issue to be in the function qClock_t qClock_Convert2Clock(const qTime_t t) - where following conversion from floating point qTime_t to qClock_t is done in the return statement:

qTime_t epochs;
epochs = (t / TimmingBase) + QFLT_TIME_FIX_VALUE;
return (qClock_t)epochs;

so, epochs might become something like 999.99999998 when diving for example 1over 0.001 in the given example. The correct rounding to unsigned int for qClock_t type should be 1000 mS - but instead the conversion ends up with 999 mS - causing the task-offset from 1 mS.

Solution: To correctly get back 1000 mS, one could add a round in the conversion - with adding a hint in the manual what is going to happen on fractional numbers of a periodic task and the Timing-Base:

qTime_t epochs;
epochs = (t / TimmingBase) + QFLT_TIME_FIX_VALUE;
return (qClock_t)round(epochs);

What do you think? Kind regards.

HpLightcorner commented 4 years ago

Ah, I see that there was already a similar issue - not sure if we can fix that one.

At least I would suggest that adding a specific remark in the manual (or I have overseen it?) would be a good idea regarding this topic.

kmilo17pet commented 4 years ago

Yes, Duplicate of #2 Fixed in the last commit. Thanks.