-
Notifications
You must be signed in to change notification settings - Fork 7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
drivers: counter: Extend set channel alarm flags (late-to-set) #16252
drivers: counter: Extend set channel alarm flags (late-to-set) #16252
Conversation
1292ba3
to
90044c6
Compare
90044c6
to
2db668a
Compare
2db668a
to
7cc325e
Compare
5d973e9
to
79570ac
Compare
Thanks for working this up. With some clarification it looks like it could handle the use cases I had in mind when I opened #12626. "Lateness" is really only meaningful in the context of absolute alarms. A late relative alarm is easily identified: it's one where the offset is non-positive. That this was the intent in the original counter API is indicated by the decision to express a relative alarm as a value that can't be negative. (How to interpret a relative alarm where I see that "lateness" in this proposal is defined in terms of I don't see a rigorous definition of counter terminology anywhere. So here's my attempt:
With all that in place, the motivation for #12626 was in the context of counters where
The generalization of this to arbitrary
Assume B=0, T=15, so S=16. With
Note that
None of this addresses issues like Nordic's RTC where there's a hardware requirement that Alternatively everything above could be ignored, and the meaning of "late" could mean something like attempting to set a relative alarm with a value that is not less than I guess what this all comes down to is the API looks fine, but the concept of "late" is not adequately defined or agreed upon, so this is still premature. |
79570ac
to
cd68ea4
Compare
If i understand correctly, your proposal divides full range in half and half of the range is the upper alarm limit. I was having configurable limit (like in #15510 ) that could be read using |
@pabigot I'm planning to join todays API call to discuss this and other counter API PR's. |
Again, late-to-set only makes sense for absolute alarms: you can't even request a late relative alarm because the If you want lateness for absolute alarms to have a configurable boundary instead of the half-span I could accept that even though I personally don't have a case where a different value is useful. Changing the domain model so However, whatever API is used to provide |
cd68ea4
to
9bfe6e2
Compare
All checks are passing now. Review history of this comment for details about previous failed status. |
9bfe6e2
to
61d0323
Compare
added long description before late set flags where i tried to explained the concept. |
61d0323
to
94acb0d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I entirely support switching from the single bool absolute
to a set of flags that control various counter options.
However, as long as there's no clearly defined and accepted counter model that can be used to describe and assess lateness policies and against which vendors can implement, I don't find this feature, or the counter API as a whole, to be robust and useful.
For example, I believe that as long as a relative alarm is specified by a non-negative value lateness should be clearly and universally understood: an offset strictly less than an inspectable minimum advance required to guarantee a compare event is always late; and any offset greater than that minimum advance is never late. This must hold regardless of any latency between the point the application tries to set the alarm and the point where the driver sets the compare value. It's the responsibility of the implementation to comply with this, which for Nordic might mean that the minimum advance would be 3 rather than 2. There is no need for threshold configurability in relative alarms.
Lateness for absolute alarms then reduces to checking the derived relative offset at the point the compare hardware is configured, which is trivial but suggests another behavior option that I won't get into here.
PR #14794, which I have not looked at closely, also appears to be weakened by lack of a counter model to use to assess proposed API.
I stand by my earlier comments, and will neither approve nor reject this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good with some minor comments. I think we need to rebase this PR on the latest master. There are two new drivers in the tree counter_gecko_rtcc.c
, counter_sam0_tc32.c
which will not work with the new COUNTER_ALARM_CFG_ABSOLUTE
flag this PR is introducing.
include/counter.h
Outdated
* converted to microseconds (see @ref counter_ticks_to_us). | ||
* Alternatively, counter implementation may count asynchronous | ||
* events. | ||
* @param ticks Ticks that triggers the alarm. It can be relative (to now) or |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like suggestion from @anangl hasn't been implemented.
9a842b0
to
a8107e6
Compare
@mnkp |
a8107e6
to
a639579
Compare
drivers/counter/counter_handlers.c
Outdated
@@ -23,3 +23,5 @@ COUNTER_HANDLER(stop) | |||
COUNTER_HANDLER(start) | |||
COUNTER_HANDLER(get_top_value) | |||
COUNTER_HANDLER(get_max_relative_alarm) | |||
COUNTER_HANDLER(get_guard_period) | |||
COUNTER_HANDLER(set_guard_period) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As the description of COUNTER_HANDLER
says, it is "for those APIs that just take one argument which is a counter driver instance and return an integral value". So both these lines are incorrect.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
right, i've tried to change it but i'm not familiar with user space macros so please look again.
include/counter.h
Outdated
* @brief Identifies guard period needed for detection of late setting of | ||
* absolute alarm (see @ref counter_set_channel_alarm). | ||
*/ | ||
#define COUNTER_GUARD_PERIOD_LATE_ABS BIT(0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not convinced about this name, especially this _ABS
part. What about _LATE_TO_SET
or _LATE_DETECTION
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 for COUNTER_GUARD_PERIOD_LATE_TO_SET
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed to COUNTER_GUARD_PERIOD_LATE_TO_SET, wanted to stress that it is related to absolute alarms.
include/counter.h
Outdated
/** @brief Alarm callback | ||
* | ||
* @param dev Pointer to the device structure for the driver instance. | ||
* @param chan_id Channel ID. | ||
* @param ticks Counter value that triggered the callback. | ||
* @param ticks Counter value that triggers the alarm. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the past tense was good in this description as it is for the parameter passed to the callback.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
include/counter.h
Outdated
@@ -460,6 +501,71 @@ static inline u32_t z_impl_counter_get_max_relative_alarm(struct device *dev) | |||
return api->get_max_relative_alarm(dev); | |||
} | |||
|
|||
/** | |||
* @brief Set guard period in ticks. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"counter ticks", to avoid confusion with kernel ticks. Or maybe it would be better to omit "in ticks" here (it is stated in the parameter description).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
include/counter.h
Outdated
* @param dev Pointer to the device structure for the driver instance. | ||
* @param flags See @ref COUNTER_GUARD_PERIOD_FLAGS. | ||
* | ||
* @return Guard period given in ticks. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"counter ticks", to avoid confusion with kernel ticks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
include/counter.h
Outdated
* | ||
* @retval 0 if successful. | ||
* @retval -ENOTSUP if not supported. | ||
* @retval -EINVAL if invalid guard. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These descriptions are too ambiguous. Which one of these error values should be returned when an unknown flag is supplied?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed to:
-ENOTSUP if function is not supported.
-EINVAL if invalid flags provided.
include/counter.h
Outdated
* See @ref counter_set_guard_period. | ||
* | ||
* @param dev Pointer to the device structure for the driver instance. | ||
* @param flags See @ref COUNTER_GUARD_PERIOD_FLAGS. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the expected behavior when given flag is not supported? I assume it is safe to return 0, as when the function is not implemented. But maybe it would be better to state it clearly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed to Guard period given in counter ticks or 0 if function or flags are not supported.
include/counter.h
Outdated
* @brief Identifies guard period needed for detection of late setting of | ||
* absolute alarm (see @ref counter_set_channel_alarm). | ||
*/ | ||
#define COUNTER_GUARD_PERIOD_LATE_ABS BIT(0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 for COUNTER_GUARD_PERIOD_LATE_TO_SET
include/counter.h
Outdated
* After initialization guard period is set to 0 and late detection is disabled. | ||
* | ||
* @param dev Pointer to the device structure for the driver instance. | ||
* @param guard Guard period in counter ticks. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we are going to update this PR anyway, what about using period
or guard_period
instead of guard
as a parameter name. Guard seems too ambiguous/misleading.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what about using
period
orguard_period
instead ofguard
as a parameter name.
Or maybe ticks
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed to ticks
a639579
to
61f8ece
Compare
include/drivers/counter.h
Outdated
@@ -145,6 +180,9 @@ typedef int (*counter_api_set_top_value)(struct device *dev, | |||
typedef u32_t (*counter_api_get_pending_int)(struct device *dev); | |||
typedef u32_t (*counter_api_get_top_value)(struct device *dev); | |||
typedef u32_t (*counter_api_get_max_relative_alarm)(struct device *dev); | |||
typedef u32_t (*counter_api_get_guard_period)(struct device *dev, u32_t flags); | |||
typedef int (*counter_api_set_guard_period)(struct device *dev, u32_t guard, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
guard
-> ticks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bingo
61f8ece
to
6f91167
Compare
include/drivers/counter.h
Outdated
* | ||
* @retval 0 if successful. | ||
* @retval -ENOTSUP if function is not supported. | ||
* @retval -EINVAL if invalid flags provided. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that for both these cases -ENOTSUP
should be returned, and -EINVAL
should rather indicate an attempt to set an invalid guard period (> top_value
is not valid in any case, I'm not sure if for some drivers some more limitation won't be needed).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
* @brief Set guard period in counter ticks. | ||
* | ||
* Setting non-zero guard period enables detection of setting absolute alarm | ||
* too late. It limits how far in the future absolute alarm can be set. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps it would be good to explicitly state that setting the guard period to 0 disables the detection, or is it obvious?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Setting non-zero guard period enables detection of setting absolute alarm too late.
i feel like stating that setting 0 means disabling is redundant.
6f91167
to
3415466
Compare
drivers/counter/counter_gecko_rtcc.c
Outdated
@@ -172,7 +172,7 @@ static int counter_gecko_set_alarm(struct device *dev, u8_t chan_id, | |||
return -EBUSY; | |||
} | |||
|
|||
if (alarm_cfg->absolute) { | |||
if (alarm_cfg->flags & COUNTER_ALARM_CFG_ABSOLUTE) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Misra-C pendantry: need to write this as ((alarm_cfg->flags & COUNTER_ALARM_CFG_ABSOLUTE) != 0)
, here and elsewhere
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done for all drivers.
const struct counter_driver_api *api = | ||
(struct counter_driver_api *)dev->driver_api; | ||
|
||
if (!api->set_guard_period) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't typical of Zephyr driver subsystems, normally if an API isn't implemented we don't do a NULL check. We might want to consider a consistent system-wide strategy on this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this approach allows extending api without breaking existing drivers. It's not the first place it is used though i'm not sure how consistent zephyr is in that matter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'm not sure how consistent zephyr is in that matter.
It's not, AFAICT.
Fine with this as-is, can address kernel-wide later which direction to go (and codify somewhere)
cc0aa77
to
333ca0c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please rebase and fix the merge conflict
Flags in alarm configuration structure will allow further extention without breaking API. Initially, existing absolute flag was added as the only flag. Signed-off-by: Krzysztof Chruscinski <[email protected]>
Added flags for controling detection of setting alarm to late. Updated drivers to return -ENOTSUP when new option is requested. Signed-off-by: Krzysztof Chruscinski <[email protected]>
333ca0c
to
e76fbfe
Compare
@ioannisg @carlescufi can you merge that one? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
Added flags for controling detection of setting alarm to late.
Updated drivers to return -ENOTSUP when new option is requested.