@@ -9,7 +9,7 @@ use crate::pac::{PWR, RTC};
9
9
use crate :: rcc:: { Enable , APB1 , BDCR } ;
10
10
use core:: convert:: TryInto ;
11
11
use core:: fmt;
12
- use rtcc:: { Datelike , Hours , NaiveDate , NaiveDateTime , NaiveTime , Rtcc , Timelike } ;
12
+ use rtcc:: { DateTimeAccess , Datelike , Hours , NaiveDate , NaiveDateTime , NaiveTime , Rtcc , Timelike } ;
13
13
14
14
/// RTC error type
15
15
#[ derive( Copy , Clone , PartialEq , Eq , Debug ) ]
@@ -128,9 +128,67 @@ impl Rtc {
128
128
}
129
129
}
130
130
131
- impl Rtcc for Rtc {
131
+ impl DateTimeAccess for Rtc {
132
132
type Error = Error ;
133
133
134
+ fn set_datetime ( & mut self , date : & NaiveDateTime ) -> Result < ( ) , Self :: Error > {
135
+ if date. year ( ) < 1970 {
136
+ return Err ( Error :: InvalidInputData ) ;
137
+ }
138
+
139
+ self . set_24h_fmt ( ) ;
140
+ let ( yt, yu) = bcd2_encode ( ( date. year ( ) - 1970 ) as u32 ) ?;
141
+ let ( mt, mu) = bcd2_encode ( date. month ( ) ) ?;
142
+ let ( dt, du) = bcd2_encode ( date. day ( ) ) ?;
143
+
144
+ let ( ht, hu) = bcd2_encode ( date. hour ( ) ) ?;
145
+ let ( mnt, mnu) = bcd2_encode ( date. minute ( ) ) ?;
146
+ let ( st, su) = bcd2_encode ( date. second ( ) ) ?;
147
+
148
+ self . rtc . dr . write ( |w| {
149
+ w. dt ( ) . bits ( dt) ;
150
+ w. du ( ) . bits ( du) ;
151
+ w. mt ( ) . bit ( mt > 0 ) ;
152
+ w. mu ( ) . bits ( mu) ;
153
+ w. yt ( ) . bits ( yt) ;
154
+ w. yu ( ) . bits ( yu)
155
+ } ) ;
156
+
157
+ self . rtc . tr . write ( |w| {
158
+ w. ht ( ) . bits ( ht) ;
159
+ w. hu ( ) . bits ( hu) ;
160
+ w. mnt ( ) . bits ( mnt) ;
161
+ w. mnu ( ) . bits ( mnu) ;
162
+ w. st ( ) . bits ( st) ;
163
+ w. su ( ) . bits ( su) ;
164
+ w. pm ( ) . clear_bit ( )
165
+ } ) ;
166
+
167
+ Ok ( ( ) )
168
+ }
169
+
170
+ fn datetime ( & mut self ) -> Result < NaiveDateTime , Self :: Error > {
171
+ self . set_24h_fmt ( ) ;
172
+
173
+ let day = self . day ( ) ?;
174
+ let month = self . month ( ) ?;
175
+ let year = self . year ( ) ?;
176
+
177
+ let seconds = self . seconds ( ) ?;
178
+ let minutes = self . minutes ( ) ?;
179
+ let hours = hours_to_u8 ( self . hours ( ) ?) ?;
180
+
181
+ Ok (
182
+ NaiveDate :: from_ymd ( year. into ( ) , month. into ( ) , day. into ( ) ) . and_hms (
183
+ hours. into ( ) ,
184
+ minutes. into ( ) ,
185
+ seconds. into ( ) ,
186
+ ) ,
187
+ )
188
+ }
189
+ }
190
+
191
+ impl Rtcc for Rtc {
134
192
/// set time using NaiveTime (ISO 8601 time without timezone)
135
193
/// Hour format is 24h
136
194
fn set_time ( & mut self , time : & NaiveTime ) -> Result < ( ) , Self :: Error > {
@@ -245,55 +303,19 @@ impl Rtcc for Rtc {
245
303
Ok ( ( ) )
246
304
}
247
305
248
- fn set_datetime ( & mut self , date : & NaiveDateTime ) -> Result < ( ) , Self :: Error > {
249
- if date. year ( ) < 1970 {
250
- return Err ( Error :: InvalidInputData ) ;
251
- }
252
-
253
- self . set_24h_fmt ( ) ;
254
- let ( yt, yu) = bcd2_encode ( ( date. year ( ) - 1970 ) as u32 ) ?;
255
- let ( mt, mu) = bcd2_encode ( date. month ( ) ) ?;
256
- let ( dt, du) = bcd2_encode ( date. day ( ) ) ?;
257
-
258
- let ( ht, hu) = bcd2_encode ( date. hour ( ) ) ?;
259
- let ( mnt, mnu) = bcd2_encode ( date. minute ( ) ) ?;
260
- let ( st, su) = bcd2_encode ( date. second ( ) ) ?;
261
-
262
- self . rtc . dr . write ( |w| {
263
- w. dt ( ) . bits ( dt) ;
264
- w. du ( ) . bits ( du) ;
265
- w. mt ( ) . bit ( mt > 0 ) ;
266
- w. mu ( ) . bits ( mu) ;
267
- w. yt ( ) . bits ( yt) ;
268
- w. yu ( ) . bits ( yu)
269
- } ) ;
270
-
271
- self . rtc . tr . write ( |w| {
272
- w. ht ( ) . bits ( ht) ;
273
- w. hu ( ) . bits ( hu) ;
274
- w. mnt ( ) . bits ( mnt) ;
275
- w. mnu ( ) . bits ( mnu) ;
276
- w. st ( ) . bits ( st) ;
277
- w. su ( ) . bits ( su) ;
278
- w. pm ( ) . clear_bit ( )
279
- } ) ;
280
-
281
- Ok ( ( ) )
282
- }
283
-
284
- fn get_seconds ( & mut self ) -> Result < u8 , Self :: Error > {
306
+ fn seconds ( & mut self ) -> Result < u8 , Self :: Error > {
285
307
let tr = self . rtc . tr . read ( ) ;
286
308
let seconds = bcd2_decode ( tr. st ( ) . bits ( ) , tr. su ( ) . bits ( ) ) ;
287
309
Ok ( seconds as u8 )
288
310
}
289
311
290
- fn get_minutes ( & mut self ) -> Result < u8 , Self :: Error > {
312
+ fn minutes ( & mut self ) -> Result < u8 , Self :: Error > {
291
313
let tr = self . rtc . tr . read ( ) ;
292
314
let minutes = bcd2_decode ( tr. mnt ( ) . bits ( ) , tr. mnu ( ) . bits ( ) ) ;
293
315
Ok ( minutes as u8 )
294
316
}
295
317
296
- fn get_hours ( & mut self ) -> Result < Hours , Self :: Error > {
318
+ fn hours ( & mut self ) -> Result < Hours , Self :: Error > {
297
319
let tr = self . rtc . tr . read ( ) ;
298
320
let hours = bcd2_decode ( tr. ht ( ) . bits ( ) , tr. hu ( ) . bits ( ) ) ;
299
321
if self . is_24h_fmt ( ) {
@@ -305,11 +327,11 @@ impl Rtcc for Rtc {
305
327
Ok ( Hours :: PM ( hours as u8 ) )
306
328
}
307
329
308
- fn get_time ( & mut self ) -> Result < NaiveTime , Self :: Error > {
330
+ fn time ( & mut self ) -> Result < NaiveTime , Self :: Error > {
309
331
self . set_24h_fmt ( ) ;
310
- let seconds = self . get_seconds ( ) ?;
311
- let minutes = self . get_minutes ( ) ?;
312
- let hours = hours_to_u8 ( self . get_hours ( ) ?) ?;
332
+ let seconds = self . seconds ( ) ?;
333
+ let minutes = self . minutes ( ) ?;
334
+ let hours = hours_to_u8 ( self . hours ( ) ?) ?;
313
335
314
336
Ok ( NaiveTime :: from_hms (
315
337
hours. into ( ) ,
@@ -318,58 +340,38 @@ impl Rtcc for Rtc {
318
340
) )
319
341
}
320
342
321
- fn get_weekday ( & mut self ) -> Result < u8 , Self :: Error > {
343
+ fn weekday ( & mut self ) -> Result < u8 , Self :: Error > {
322
344
let dr = self . rtc . dr . read ( ) ;
323
345
let weekday = bcd2_decode ( dr. wdu ( ) . bits ( ) , 0x00 ) ;
324
346
Ok ( weekday as u8 )
325
347
}
326
348
327
- fn get_day ( & mut self ) -> Result < u8 , Self :: Error > {
349
+ fn day ( & mut self ) -> Result < u8 , Self :: Error > {
328
350
let dr = self . rtc . dr . read ( ) ;
329
351
let day = bcd2_decode ( dr. dt ( ) . bits ( ) , dr. du ( ) . bits ( ) ) ;
330
352
Ok ( day as u8 )
331
353
}
332
354
333
- fn get_month ( & mut self ) -> Result < u8 , Self :: Error > {
355
+ fn month ( & mut self ) -> Result < u8 , Self :: Error > {
334
356
let dr = self . rtc . dr . read ( ) ;
335
357
let mt: u8 = if dr. mt ( ) . bit ( ) { 1 } else { 0 } ;
336
358
let month = bcd2_decode ( mt, dr. mu ( ) . bits ( ) ) ;
337
359
Ok ( month as u8 )
338
360
}
339
361
340
- fn get_year ( & mut self ) -> Result < u16 , Self :: Error > {
362
+ fn year ( & mut self ) -> Result < u16 , Self :: Error > {
341
363
let dr = self . rtc . dr . read ( ) ;
342
364
let year = bcd2_decode ( dr. yt ( ) . bits ( ) , dr. yu ( ) . bits ( ) ) ;
343
365
Ok ( year as u16 )
344
366
}
345
367
346
- fn get_date ( & mut self ) -> Result < NaiveDate , Self :: Error > {
347
- let day = self . get_day ( ) ?;
348
- let month = self . get_month ( ) ?;
349
- let year = self . get_year ( ) ?;
368
+ fn date ( & mut self ) -> Result < NaiveDate , Self :: Error > {
369
+ let day = self . day ( ) ?;
370
+ let month = self . month ( ) ?;
371
+ let year = self . year ( ) ?;
350
372
351
373
Ok ( NaiveDate :: from_ymd ( year. into ( ) , month. into ( ) , day. into ( ) ) )
352
374
}
353
-
354
- fn get_datetime ( & mut self ) -> Result < NaiveDateTime , Self :: Error > {
355
- self . set_24h_fmt ( ) ;
356
-
357
- let day = self . get_day ( ) ?;
358
- let month = self . get_month ( ) ?;
359
- let year = self . get_year ( ) ?;
360
-
361
- let seconds = self . get_seconds ( ) ?;
362
- let minutes = self . get_minutes ( ) ?;
363
- let hours = hours_to_u8 ( self . get_hours ( ) ?) ?;
364
-
365
- Ok (
366
- NaiveDate :: from_ymd ( year. into ( ) , month. into ( ) , day. into ( ) ) . and_hms (
367
- hours. into ( ) ,
368
- minutes. into ( ) ,
369
- seconds. into ( ) ,
370
- ) ,
371
- )
372
- }
373
375
}
374
376
375
377
// Two 32-bit registers (RTC_TR and RTC_DR) contain the seconds, minutes, hours (12- or 24-hour format), day (day
0 commit comments