Skip to content

Commit

Permalink
Use time instead of rtcc, add rtc-example
Browse files Browse the repository at this point in the history
  • Loading branch information
burrbull committed Feb 7, 2022
1 parent 7129ff1 commit 7953096
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 168 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Changed

- Use `time` for `Rtc` instead of `rtcc`, add `rtc` example [#436]
- Move `i2c` `embedded-hal` trait impls to `I2c` methods [#431]
- Reexport pins in `gpio` module
- Add channel events, make Event use bitflags (simplify interrupt handling) [#425]
Expand Down Expand Up @@ -38,6 +39,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
[#429]: https://github.com/stm32-rs/stm32f4xx-hal/pull/429
[#431]: https://github.com/stm32-rs/stm32f4xx-hal/pull/431
[#434]: https://github.com/stm32-rs/stm32f4xx-hal/pull/434
[#436]: https://github.com/stm32-rs/stm32f4xx-hal/pull/436

### Changed

Expand Down
15 changes: 14 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ cortex-m = "0.7.4"
cortex-m-rt = "0.7"
nb = "1"
rand_core = "0.6"
rtcc = "0.2"
stm32f4 = "0.14.0"
synopsys-usb-otg = { version = "0.2.0", features = ["cortex-m"], optional = true }
sdio-host = { version = "0.5.0", optional = true }
Expand All @@ -48,6 +47,10 @@ rtic-monotonic = { version = "1.0", optional = true }
bitflags = "1.3.2"
embedded-storage = "0.2"

[dependencies.time]
version = "0.3"
default-features = false

[dependencies.stm32_i2s_v12x]
version = "0.2.0"
optional = true
Expand All @@ -58,6 +61,7 @@ panic-semihosting = "0.5.3"
cortex-m-semihosting = "0.3.3"
heapless = "0.7.5"
panic-halt = "0.2.0"
panic-rtt-target = { version = "0.1.2", features = ["cortex-m"] }
ssd1306 = "0.7.0"
embedded-graphics = "0.7.1"
usb-device = "0.2.5"
Expand All @@ -73,6 +77,11 @@ ist7920 = "0.1.0"
smart-leds = "0.3.0"
ws2812-spi = { version = "0.4.0", features = [] }

[dev-dependencies.time]
version = "0.3"
default-features = false
features = ["macros"]

[features]
device-selected = []
rt = ["stm32f4/rt"]
Expand Down Expand Up @@ -457,3 +466,7 @@ required-features = ["rt", "stm32f411"]
name = "dynamic_gpio"
required-features = ["device-selected"]

[[example]]
name = "rtc"
required-features = ["device-selected"]

48 changes: 48 additions & 0 deletions examples/rtc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//! This example demonstrates how to use the RTC.
//! Note that the LSI can be quite inaccurate.
//! The tolerance is up to ±47% (Min 17 kHz, Typ 32 kHz, Max 47 kHz).

#![no_main]
#![no_std]

use cortex_m_rt::entry;
use panic_rtt_target as _;
use rtt_target::{rprintln, rtt_init_print};

use stm32f4xx_hal::{pac, prelude::*, rtc::Rtc};
use time::{
macros::{date, time},
PrimitiveDateTime,
};

#[entry]
fn main() -> ! {
rtt_init_print!();
let mut p = pac::Peripherals::take().unwrap();
let rcc = p.RCC.constrain();

let clocks = rcc.cfgr.freeze();

let mut rtc = Rtc::new(p.RTC, 249, 127, false, &mut p.PWR);
let mut delay = p.TIM5.delay_us(&clocks);

rtc.set_datetime(&PrimitiveDateTime::new(
date!(2022 - 02 - 07),
time!(23:59:50),
))
.unwrap();
// Alternatively:
// rtc.set_date(&date!(2022 - 02 - 07)).unwrap();
// rtc.set_time(&time!(23:59:50)).unwrap();
// Or:
// rtc.set_year(2022).unwrap();
// rtc.set_month(02).unwrap();
// rtc.set_day(07).unwrap();
// rtc.set_hours(23).unwrap();
// rtc.set_minutes(59).unwrap();
// rtc.set_seconds(50).unwrap();
loop {
rprintln!("{}", rtc.get_datetime());
delay.delay(500.millis()).unwrap();
}
}
Loading

0 comments on commit 7953096

Please sign in to comment.