Skip to content

Commit d983a3d

Browse files
bors[bot]luojia65Luo Jia
authored
Merge #222
222: Improve watchdog API design using move semantics r=therealprof a=luojia65 This pull request improved watchdog API design. When starting watchdog, we may convert it into another type. We may implement different functions for this type. Or downstream developers can implement `Watchdog` for only an enabled type, to prevent feed to disabled watchdogs or forget to enable before feeding. If we are able to stop this watchdog, it can be converted into the former type. If current design still need a same type after the watchdog is enabled, they may use `Target = Self`. In this way we create a fallback for earlier designs. A simple proof of concept: [here](https://github.com/gd32v-rust/gd32vf103-hal/blob/new-watchdog-design/src/wdog.rs#L155-L169) (L120-L153 for its `Enable` implementation, and there is `Disable` implementation) Related issue: #98 Earlier discussion: #76 (comment) Co-authored-by: luojia65 <[email protected]> Co-authored-by: Luo Jia <[email protected]>
2 parents b46d300 + 599d44f commit d983a3d

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
99
## [Unreleased]
1010

1111
### Changed
12+
1213
- The method `try_write` from the trait `blocking::i2c::WriteIter` trait
1314
has been renamed `try_write_iter` for consistency.
1415
- Updated `nb` dependency to version `1`.
16+
- The watchdog API now uses move semantics. See [PR](https://github.com/rust-embedded/embedded-hal/pull/222).
1517

1618
## [v1.0.0-alpha.1] - 2020-06-16
1719

src/prelude.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ pub use crate::spi::FullDuplex as _embedded_hal_spi_FullDuplex;
3434
pub use crate::timer::Cancel as _embedded_hal_timer_Cancel;
3535
pub use crate::timer::CountDown as _embedded_hal_timer_CountDown;
3636
pub use crate::timer::Periodic as _embedded_hal_timer_Periodic;
37+
pub use crate::watchdog::Disable as _embedded_hal_watchdog_Disable;
38+
pub use crate::watchdog::Enable as _embedded_hal_watchdog_Enable;
3739
pub use crate::watchdog::Watchdog as _embedded_hal_watchdog_Watchdog;
38-
pub use crate::watchdog::WatchdogDisable as _embedded_hal_watchdog_WatchdogDisable;
39-
pub use crate::watchdog::WatchdogEnable as _embedded_hal_watchdog_WatchdogEnable;

src/watchdog.rs

+27-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Traits for interactions with a processors watchdog timer.
22
33
/// Feeds an existing watchdog to ensure the processor isn't reset. Sometimes
4-
/// commonly referred to as "kicking" or "refreshing".
4+
/// the "feeding" operation is also referred to as "refreshing".
55
pub trait Watchdog {
66
/// An enumeration of `Watchdog` errors.
77
///
@@ -15,29 +15,46 @@ pub trait Watchdog {
1515

1616
/// Enables A watchdog timer to reset the processor if software is frozen or
1717
/// stalled.
18-
pub trait WatchdogEnable {
19-
/// An enumeration of `WatchdogEnable` errors.
18+
pub trait Enable {
19+
/// An enumeration of `Enable` errors.
2020
///
2121
/// For infallible implementations, will be `Infallible`
2222
type Error;
2323

24-
/// Unit of time used by the watchdog
24+
/// Unit of time used by the watchdog.
2525
type Time;
2626

27+
/// The started watchdog that should be `feed()`.
28+
type Target: Watchdog;
29+
2730
/// Starts the watchdog with a given period, typically once this is done
28-
/// the watchdog needs to be kicked periodically or the processor is reset.
29-
fn try_start<T>(&mut self, period: T) -> Result<(), Self::Error>
31+
/// the watchdog needs to be `feed()` periodically, or the processor would be
32+
/// reset.
33+
///
34+
/// This consumes the value and returns the `Watchdog` trait that you must
35+
/// `feed()`.
36+
fn try_start<T>(self, period: T) -> Result<Self::Target, Self::Error>
3037
where
3138
T: Into<Self::Time>;
3239
}
3340

3441
/// Disables a running watchdog timer so the processor won't be reset.
35-
pub trait WatchdogDisable {
36-
/// An enumeration of `WatchdogDisable` errors.
42+
///
43+
/// Not all watchdog timers support disable operation after they've been enabled.
44+
/// In this case, hardware support libraries would not implement this trait
45+
/// and hardware-agnostic libraries should consider not requiring it.
46+
pub trait Disable {
47+
/// An enumeration of `Disable` errors.
3748
///
3849
/// For infallible implementations, will be `Infallible`
3950
type Error;
4051

41-
/// Disables the watchdog
42-
fn try_disable(&mut self) -> Result<(), Self::Error>;
52+
/// Disabled watchdog instance that can be enabled.
53+
type Target: Enable;
54+
55+
/// Disables the watchdog.
56+
///
57+
/// This stops the watchdog and returns an instance implementing the
58+
/// `Enable` trait so that it can be started again.
59+
fn try_disable(self) -> Result<Self::Target, Self::Error>;
4360
}

0 commit comments

Comments
 (0)