Skip to content

Commit f8ff86f

Browse files
committed
set_alarm
1 parent dddd787 commit f8ff86f

File tree

4 files changed

+239
-57
lines changed

4 files changed

+239
-57
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,10 @@ required-features = ["rng"] # stm32f407
406406
name = "rtc"
407407
required-features = ["device-selected"]
408408

409+
[[example]]
410+
name = "rtc_alarm"
411+
required-features = ["stm32f411"]
412+
409413
[[example]]
410414
name = "rtic-adc-dma"
411415
required-features = ["device-selected", "rtic"] # stm32f401

examples/rtc_alarm.rs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
//! Sets an RTC alarm
2+
3+
#![no_std]
4+
#![no_main]
5+
6+
use stm32f4xx_hal as hal;
7+
8+
use panic_halt as _;
9+
use rtt_target::{rprintln, rtt_init_print};
10+
11+
use crate::hal::prelude::*;
12+
use crate::hal::rtc::{Alarm, AlarmDay, Event, Rtc};
13+
use cortex_m::interrupt::{free, Mutex};
14+
use fugit::ExtU32;
15+
use time::{
16+
macros::{date, time},
17+
PrimitiveDateTime,
18+
};
19+
20+
use core::{cell::RefCell, ops::DerefMut};
21+
use hal::interrupt;
22+
use hal::pac;
23+
use pac::NVIC;
24+
25+
static RTC: Mutex<RefCell<Option<Rtc>>> = Mutex::new(RefCell::new(None));
26+
27+
use cortex_m_rt::entry;
28+
29+
#[entry]
30+
fn main() -> ! {
31+
rtt_init_print!();
32+
33+
let mut p = hal::pac::Peripherals::take().unwrap();
34+
35+
let rcc = p.RCC.constrain();
36+
let _clocks = rcc.cfgr.freeze();
37+
let mut rtc = Rtc::new(p.RTC, &mut p.PWR);
38+
39+
let today = date!(2023 - 05 - 28);
40+
rtc.set_datetime(&PrimitiveDateTime::new(today, time!(21:57:32)))
41+
.unwrap();
42+
43+
// Set alarm A for 1 minute
44+
rtc.set_alarm(Alarm::AlarmA, AlarmDay::Date(today), time!(21:58:32))
45+
.unwrap();
46+
rtc.enable_wakeup(8.secs());
47+
rtc.listen(&mut p.EXTI, Event::AlarmA);
48+
rtc.listen(&mut p.EXTI, Event::Wakeup);
49+
50+
rprintln!("Hello, world!");
51+
52+
unsafe {
53+
NVIC::unmask(pac::Interrupt::RTC_ALARM);
54+
NVIC::unmask(pac::Interrupt::RTC_WKUP);
55+
}
56+
57+
free(|cs| {
58+
RTC.borrow(cs).replace(Some(rtc));
59+
});
60+
61+
loop {
62+
continue;
63+
}
64+
}
65+
66+
#[interrupt]
67+
fn RTC_ALARM() {
68+
free(|cs| {
69+
let mut rtc_ref = RTC.borrow(cs).borrow_mut();
70+
if let Some(rtc) = rtc_ref.deref_mut() {
71+
if rtc.is_pending(Event::AlarmA) {
72+
rtc.clear_interrupt(Event::AlarmA);
73+
rprintln!("RTC Alaaaaarm!");
74+
}
75+
}
76+
});
77+
}
78+
79+
#[interrupt]
80+
fn RTC_WKUP() {
81+
free(|cs| {
82+
let mut rtc_ref = RTC.borrow(cs).borrow_mut();
83+
if let Some(rtc) = rtc_ref.deref_mut() {
84+
if rtc.is_pending(Event::Wakeup) {
85+
rtc.clear_interrupt(Event::Wakeup);
86+
rprintln!("RTC Wakeup!");
87+
}
88+
}
89+
});
90+
}

src/i2s.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,14 @@ impl<I: DualInstance> DualI2s<I> {
372372
/// $i2s: module containing the Ws pin definition. (example: i2s1).
373373
/// $clock: The name of the Clocks function that returns the frequency of the I2S clock input
374374
/// to this SPI peripheral (i2s_cl, i2s_apb1_clk, or i2s2_apb_clk)
375+
#[cfg(any(
376+
feature = "gpio-f401",
377+
feature = "gpio-f411",
378+
feature = "gpio-f412",
379+
feature = "gpio-f417",
380+
feature = "gpio-f427",
381+
feature = "gpio-f469",
382+
))]
375383
macro_rules! dual_i2s {
376384
($SPI:ty,$I2SEXT:ty, $DualI2s:ident, $i2s:ident, $clock:ident) => {
377385
pub type $DualI2s = DualI2s<$SPI>;

0 commit comments

Comments
 (0)