-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
239 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"); | ||
} | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.