Skip to content

Commit

Permalink
set_alarm
Browse files Browse the repository at this point in the history
  • Loading branch information
burrbull committed May 28, 2023
1 parent dddd787 commit f8ff86f
Show file tree
Hide file tree
Showing 4 changed files with 239 additions and 57 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,10 @@ required-features = ["rng"] # stm32f407
name = "rtc"
required-features = ["device-selected"]

[[example]]
name = "rtc_alarm"
required-features = ["stm32f411"]

[[example]]
name = "rtic-adc-dma"
required-features = ["device-selected", "rtic"] # stm32f401
Expand Down
90 changes: 90 additions & 0 deletions examples/rtc_alarm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
//! Sets an RTC alarm

#![no_std]
#![no_main]

use stm32f4xx_hal as hal;

use panic_halt as _;
use rtt_target::{rprintln, rtt_init_print};

use crate::hal::prelude::*;
use crate::hal::rtc::{Alarm, AlarmDay, Event, Rtc};
use cortex_m::interrupt::{free, Mutex};
use fugit::ExtU32;
use time::{
macros::{date, time},
PrimitiveDateTime,
};

use core::{cell::RefCell, ops::DerefMut};
use hal::interrupt;
use hal::pac;
use pac::NVIC;

static RTC: Mutex<RefCell<Option<Rtc>>> = Mutex::new(RefCell::new(None));

use cortex_m_rt::entry;

#[entry]
fn main() -> ! {
rtt_init_print!();

let mut p = hal::pac::Peripherals::take().unwrap();

let rcc = p.RCC.constrain();
let _clocks = rcc.cfgr.freeze();
let mut rtc = Rtc::new(p.RTC, &mut p.PWR);

let today = date!(2023 - 05 - 28);
rtc.set_datetime(&PrimitiveDateTime::new(today, time!(21:57:32)))
.unwrap();

// Set alarm A for 1 minute
rtc.set_alarm(Alarm::AlarmA, AlarmDay::Date(today), time!(21:58:32))
.unwrap();
rtc.enable_wakeup(8.secs());
rtc.listen(&mut p.EXTI, Event::AlarmA);
rtc.listen(&mut p.EXTI, Event::Wakeup);

rprintln!("Hello, world!");

unsafe {
NVIC::unmask(pac::Interrupt::RTC_ALARM);
NVIC::unmask(pac::Interrupt::RTC_WKUP);
}

free(|cs| {
RTC.borrow(cs).replace(Some(rtc));
});

loop {
continue;
}
}

#[interrupt]
fn RTC_ALARM() {
free(|cs| {
let mut rtc_ref = RTC.borrow(cs).borrow_mut();
if let Some(rtc) = rtc_ref.deref_mut() {
if rtc.is_pending(Event::AlarmA) {
rtc.clear_interrupt(Event::AlarmA);
rprintln!("RTC Alaaaaarm!");
}
}
});
}

#[interrupt]
fn RTC_WKUP() {
free(|cs| {
let mut rtc_ref = RTC.borrow(cs).borrow_mut();
if let Some(rtc) = rtc_ref.deref_mut() {
if rtc.is_pending(Event::Wakeup) {
rtc.clear_interrupt(Event::Wakeup);
rprintln!("RTC Wakeup!");
}
}
});
}
8 changes: 8 additions & 0 deletions src/i2s.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,14 @@ impl<I: DualInstance> DualI2s<I> {
/// $i2s: module containing the Ws pin definition. (example: i2s1).
/// $clock: The name of the Clocks function that returns the frequency of the I2S clock input
/// to this SPI peripheral (i2s_cl, i2s_apb1_clk, or i2s2_apb_clk)
#[cfg(any(
feature = "gpio-f401",
feature = "gpio-f411",
feature = "gpio-f412",
feature = "gpio-f417",
feature = "gpio-f427",
feature = "gpio-f469",
))]
macro_rules! dual_i2s {
($SPI:ty,$I2SEXT:ty, $DualI2s:ident, $i2s:ident, $clock:ident) => {
pub type $DualI2s = DualI2s<$SPI>;
Expand Down
Loading

0 comments on commit f8ff86f

Please sign in to comment.