@@ -5,7 +5,9 @@ mod datetime;
55mod low_power;
66
77#[ cfg( feature = "low-power" ) ]
8- use core:: cell:: Cell ;
8+ use core:: cell:: { Cell , RefCell , RefMut } ;
9+ #[ cfg( feature = "low-power" ) ]
10+ use core:: ops;
911
1012#[ cfg( feature = "low-power" ) ]
1113use critical_section:: CriticalSection ;
@@ -52,9 +54,8 @@ pub struct RtcTimeProvider {
5254}
5355
5456impl RtcTimeProvider {
55- #[ cfg( feature = "low-power" ) ]
5657 /// Create a new RTC time provider instance.
57- pub fn new ( _rtc : Peri < ' static , RTC > ) -> Self {
58+ pub ( self ) const fn new ( ) -> Self {
5859 Self { _private : ( ) }
5960 }
6061
@@ -115,6 +116,50 @@ impl RtcTimeProvider {
115116 }
116117}
117118
119+ #[ cfg( feature = "low-power" ) ]
120+ /// Contains an RTC driver.
121+ pub struct RtcContainer {
122+ pub ( self ) mutex : & ' static Mutex < CriticalSectionRawMutex , RefCell < Option < Rtc > > > ,
123+ }
124+
125+ #[ cfg( feature = "low-power" ) ]
126+ impl RtcContainer {
127+ pub ( self ) const fn new ( ) -> Self {
128+ Self {
129+ mutex : & crate :: time_driver:: get_driver ( ) . rtc ,
130+ }
131+ }
132+
133+ /// Acquire an RTC borrow.
134+ pub fn borrow_mut < ' a > ( & self , cs : CriticalSection < ' a > ) -> RtcBorrow < ' a > {
135+ RtcBorrow {
136+ ref_mut : self . mutex . borrow ( cs) . borrow_mut ( ) ,
137+ }
138+ }
139+ }
140+
141+ #[ cfg( feature = "low-power" ) ]
142+ /// Contains an RTC borrow.
143+ pub struct RtcBorrow < ' a > {
144+ pub ( self ) ref_mut : RefMut < ' a , Option < Rtc > > ,
145+ }
146+
147+ #[ cfg( feature = "low-power" ) ]
148+ impl < ' a > ops:: Deref for RtcBorrow < ' a > {
149+ type Target = Rtc ;
150+
151+ fn deref ( & self ) -> & Self :: Target {
152+ self . ref_mut . as_ref ( ) . unwrap ( )
153+ }
154+ }
155+
156+ #[ cfg( feature = "low-power" ) ]
157+ impl < ' a > ops:: DerefMut for RtcBorrow < ' a > {
158+ fn deref_mut ( & mut self ) -> & mut Self :: Target {
159+ self . ref_mut . as_mut ( ) . unwrap ( )
160+ }
161+ }
162+
118163/// RTC driver.
119164pub struct Rtc {
120165 #[ cfg( feature = "low-power" ) ]
@@ -156,8 +201,14 @@ pub enum RtcCalibrationCyclePeriod {
156201impl Rtc {
157202 #[ cfg( not( feature = "low-power" ) ) ]
158203 /// Create a new RTC instance.
159- pub fn new ( _rtc : Peri < ' static , RTC > , rtc_config : RtcConfig ) -> Self {
160- Self :: new_inner ( rtc_config)
204+ pub fn new ( _rtc : Peri < ' static , RTC > , rtc_config : RtcConfig ) -> ( Self , RtcTimeProvider ) {
205+ ( Self :: new_inner ( rtc_config) , RtcTimeProvider :: new ( ) )
206+ }
207+
208+ #[ cfg( feature = "low-power" ) ]
209+ /// Create a new RTC instance.
210+ pub fn new ( _rtc : Peri < ' static , RTC > ) -> ( RtcContainer , RtcTimeProvider ) {
211+ ( RtcContainer :: new ( ) , RtcTimeProvider :: new ( ) )
161212 }
162213
163214 pub ( self ) fn new_inner ( rtc_config : RtcConfig ) -> Self {
@@ -179,8 +230,8 @@ impl Rtc {
179230 // Wait for the clock to update after initialization
180231 #[ cfg( not( rtc_v2_f2) ) ]
181232 {
182- let now = this . time_provider ( ) . read ( |_, _, ss| Ok ( ss) ) . unwrap ( ) ;
183- while now == this . time_provider ( ) . read ( |_, _, ss| Ok ( ss) ) . unwrap ( ) { }
233+ let now = RtcTimeProvider :: new ( ) . read ( |_, _, ss| Ok ( ss) ) . unwrap ( ) ;
234+ while now == RtcTimeProvider :: new ( ) . read ( |_, _, ss| Ok ( ss) ) . unwrap ( ) { }
184235 }
185236
186237 #[ cfg( feature = "low-power" ) ]
@@ -194,11 +245,6 @@ impl Rtc {
194245 freqs. rtc . to_hertz ( ) . unwrap ( )
195246 }
196247
197- /// Acquire a [`RtcTimeProvider`] instance.
198- pub const fn time_provider ( & self ) -> RtcTimeProvider {
199- RtcTimeProvider { _private : ( ) }
200- }
201-
202248 /// Set the datetime to a new value.
203249 ///
204250 /// # Errors
@@ -242,15 +288,6 @@ impl Rtc {
242288 Ok ( ( ) )
243289 }
244290
245- /// Return the current datetime.
246- ///
247- /// # Errors
248- ///
249- /// Will return an `RtcError::InvalidDateTime` if the stored value in the system is not a valid [`DayOfWeek`].
250- pub fn now ( & self ) -> Result < DateTime , RtcError > {
251- self . time_provider ( ) . now ( )
252- }
253-
254291 /// Check if daylight savings time is active.
255292 pub fn get_daylight_savings ( & self ) -> bool {
256293 let cr = RTC :: regs ( ) . cr ( ) . read ( ) ;
0 commit comments