Skip to content
This repository was archived by the owner on Feb 13, 2019. It is now read-only.

Commit 41c421f

Browse files
committed
Add some flag clearing functions and fix initialization
1 parent 8d3e256 commit 41c421f

File tree

1 file changed

+49
-9
lines changed

1 file changed

+49
-9
lines changed

src/rtc.rs

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,26 @@ impl Rtc {
6363
}
6464

6565
pub fn set_alarm(&mut self, time_seconds: u32) {
66+
// Reset counter
6667
self.perform_write(|s| {
67-
// Reset counter
6868
s.regs.cnth.write(|w| unsafe{w.bits(0)});
69+
});
70+
self.perform_write(|s| {
6971
s.regs.cntl.write(|w| unsafe{w.bits(0)});
72+
});
7073

71-
// Set alarm time
72-
s.regs.alrh.write(|w| unsafe{w.alrh().bits((time_seconds >> 16) as u16)});
73-
s.regs.alrl.write(|w| unsafe{w.alrl().bits((time_seconds & 0x0000ffff) as u16)});
74+
// Set alarm time
75+
// See section 18.3.5 for explanation
76+
let alarm_value = time_seconds - 1;
77+
self.perform_write(|s| {
78+
s.regs.alrh.write(|w| unsafe{w.alrh().bits((alarm_value >> 16) as u16)});
79+
});
80+
self.perform_write(|s| {
81+
s.regs.alrl.write(|w| unsafe{w.alrl().bits((alarm_value & 0x0000ffff) as u16)});
82+
});
7483

75-
// Enable alarm interrupt
84+
// Enable alarm interrupt
85+
self.perform_write(|s| {
7686
s.regs.crh.modify(|_, w| w.alrie().set_bit());
7787
})
7888
}
@@ -84,6 +94,40 @@ impl Rtc {
8494
((self.regs.cnth.read().bits() << 16) as u32) + (self.regs.cntl.read().bits() as u32)
8595
}
8696

97+
/**
98+
Enables the RTC second interrupt
99+
*/
100+
pub fn listen_seconds(&mut self) {
101+
self.perform_write(|s| {
102+
s.regs.crh.modify(|_, w| w.secie().set_bit())
103+
})
104+
}
105+
/**
106+
Disables the RTC second interrupt
107+
*/
108+
pub fn unlisten_seconds(&mut self) {
109+
self.perform_write(|s| {
110+
s.regs.crh.modify(|_, w| w.secie().clear_bit())
111+
})
112+
}
113+
/**
114+
Clears the RTC second interrupt flag
115+
*/
116+
pub fn clear_second_flag(&mut self) {
117+
self.perform_write(|s| {
118+
s.regs.crl.modify(|_, w| w.secf().clear_bit())
119+
})
120+
}
121+
122+
/**
123+
Clears the RTC alarm interrupt flag
124+
*/
125+
pub fn clear_alarm_flag(&mut self) {
126+
self.perform_write(|s| {
127+
s.regs.crl.modify(|_, w| w.alrf().clear_bit())
128+
})
129+
}
130+
87131

88132
fn perform_write(&mut self, func: impl Fn(&mut Self)) {
89133
// This process is documented on page 485 of the stm32f103 manual
@@ -100,8 +144,4 @@ impl Rtc {
100144
// Wait for the write to be done
101145
while self.regs.crl.read().rtoff().bit() == false {}
102146
}
103-
104-
fn clear_alarm_flag(&mut self) {
105-
self.perform_write(|s| s.regs.crl.modify(|_, w| w.alrf().clear_bit()))
106-
}
107147
}

0 commit comments

Comments
 (0)