Skip to content

Commit 5b45315

Browse files
authored
Merge pull request #314 from Sh3Rm4n/rtcc
Upgrade rtcc
2 parents a56e130 + 4f20e73 commit 5b45315

File tree

3 files changed

+80
-74
lines changed

3 files changed

+80
-74
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
3838

3939
- Make `rtc` an optional feature. Without that feature `rtcc` as a dependency is
4040
not needed. ([#283])
41+
- Update `rtcc` to `v0.3.0`. For a detailed changelog, go [here].
4142
- Enable `rt`, `usb`, `can`, `rtc` and `ld` feature by default.
4243
To disable that behavior, set `default-features = false`. ([#283])
4344
- The MSRV was bumped to 1.54 ([#308])
@@ -76,6 +77,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
7677
- Support every possible ADC channel.
7778
- Add interrupt support.
7879

80+
[here]: https://github.com/eldruin/rtcc-rs/blob/master/CHANGELOG.md#030---2022-02-19
81+
7982
## [v0.8.2] - 2021-12-14
8083

8184
- Add missing SPI impls for the `gpio-f303` device groups (e.g. stm32f303vc) ([#304])
@@ -547,6 +550,7 @@ let clocks = rcc
547550
[defmt]: https://github.com/knurling-rs/defmt
548551
[filter]: https://defmt.ferrous-systems.com/filtering.html
549552

553+
[#314]: https://github.com/stm32-rs/stm32f3xx-hal/pull/314
550554
[#309]: https://github.com/stm32-rs/stm32f3xx-hal/pull/309
551555
[#308]: https://github.com/stm32-rs/stm32f3xx-hal/pull/308
552556
[#304]: https://github.com/stm32-rs/stm32f3xx-hal/pull/304

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ embedded-hal = { version = "0.2.5", features = ["unproven"] }
4343
embedded-time = "0.12.0"
4444
nb = "1.0.0"
4545
paste = "1.0.5"
46-
rtcc = { version = "0.2.1", optional = true }
46+
rtcc = { version = "0.3.0", optional = true }
4747
stm32f3 = { version = "0.14.0", default-features = false }
4848
bxcan = { version = "0.6.2", optional = true }
4949
stm32-usbd = { version = "0.6.0", optional = true }

src/rtc.rs

Lines changed: 75 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::pac::{PWR, RTC};
99
use crate::rcc::{Enable, APB1, BDCR};
1010
use core::convert::TryInto;
1111
use core::fmt;
12-
use rtcc::{Datelike, Hours, NaiveDate, NaiveDateTime, NaiveTime, Rtcc, Timelike};
12+
use rtcc::{DateTimeAccess, Datelike, Hours, NaiveDate, NaiveDateTime, NaiveTime, Rtcc, Timelike};
1313

1414
/// RTC error type
1515
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
@@ -128,9 +128,67 @@ impl Rtc {
128128
}
129129
}
130130

131-
impl Rtcc for Rtc {
131+
impl DateTimeAccess for Rtc {
132132
type Error = Error;
133133

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 {
134192
/// set time using NaiveTime (ISO 8601 time without timezone)
135193
/// Hour format is 24h
136194
fn set_time(&mut self, time: &NaiveTime) -> Result<(), Self::Error> {
@@ -245,55 +303,19 @@ impl Rtcc for Rtc {
245303
Ok(())
246304
}
247305

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> {
285307
let tr = self.rtc.tr.read();
286308
let seconds = bcd2_decode(tr.st().bits(), tr.su().bits());
287309
Ok(seconds as u8)
288310
}
289311

290-
fn get_minutes(&mut self) -> Result<u8, Self::Error> {
312+
fn minutes(&mut self) -> Result<u8, Self::Error> {
291313
let tr = self.rtc.tr.read();
292314
let minutes = bcd2_decode(tr.mnt().bits(), tr.mnu().bits());
293315
Ok(minutes as u8)
294316
}
295317

296-
fn get_hours(&mut self) -> Result<Hours, Self::Error> {
318+
fn hours(&mut self) -> Result<Hours, Self::Error> {
297319
let tr = self.rtc.tr.read();
298320
let hours = bcd2_decode(tr.ht().bits(), tr.hu().bits());
299321
if self.is_24h_fmt() {
@@ -305,11 +327,11 @@ impl Rtcc for Rtc {
305327
Ok(Hours::PM(hours as u8))
306328
}
307329

308-
fn get_time(&mut self) -> Result<NaiveTime, Self::Error> {
330+
fn time(&mut self) -> Result<NaiveTime, Self::Error> {
309331
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()?)?;
313335

314336
Ok(NaiveTime::from_hms(
315337
hours.into(),
@@ -318,58 +340,38 @@ impl Rtcc for Rtc {
318340
))
319341
}
320342

321-
fn get_weekday(&mut self) -> Result<u8, Self::Error> {
343+
fn weekday(&mut self) -> Result<u8, Self::Error> {
322344
let dr = self.rtc.dr.read();
323345
let weekday = bcd2_decode(dr.wdu().bits(), 0x00);
324346
Ok(weekday as u8)
325347
}
326348

327-
fn get_day(&mut self) -> Result<u8, Self::Error> {
349+
fn day(&mut self) -> Result<u8, Self::Error> {
328350
let dr = self.rtc.dr.read();
329351
let day = bcd2_decode(dr.dt().bits(), dr.du().bits());
330352
Ok(day as u8)
331353
}
332354

333-
fn get_month(&mut self) -> Result<u8, Self::Error> {
355+
fn month(&mut self) -> Result<u8, Self::Error> {
334356
let dr = self.rtc.dr.read();
335357
let mt: u8 = if dr.mt().bit() { 1 } else { 0 };
336358
let month = bcd2_decode(mt, dr.mu().bits());
337359
Ok(month as u8)
338360
}
339361

340-
fn get_year(&mut self) -> Result<u16, Self::Error> {
362+
fn year(&mut self) -> Result<u16, Self::Error> {
341363
let dr = self.rtc.dr.read();
342364
let year = bcd2_decode(dr.yt().bits(), dr.yu().bits());
343365
Ok(year as u16)
344366
}
345367

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()?;
350372

351373
Ok(NaiveDate::from_ymd(year.into(), month.into(), day.into()))
352374
}
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-
}
373375
}
374376

375377
// Two 32-bit registers (RTC_TR and RTC_DR) contain the seconds, minutes, hours (12- or 24-hour format), day (day

0 commit comments

Comments
 (0)