Skip to content

Commit e87fc27

Browse files
authored
Merge pull request #213 from datdenkikniet/lp_timers
Fix incorrect mutability of references
2 parents 3ec6908 + 4df4d7b commit e87fc27

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

src/lptimer.rs

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -232,47 +232,46 @@ macro_rules! hal {
232232
self.enable();
233233
}
234234

235-
/// Check if the specified event has been triggered for this LPTIM.
235+
/// Check if the specified event has been triggered for this LowPowerTimer.
236236
///
237-
/// This function must be called if an event which this LPTIM listens to has
238-
/// generated an interrupt
239-
///
240-
/// If the event has occured, and `clear_interrupt` is true, the interrupt flag for this
241-
/// event will be cleared. Otherwise, the interrupt flag for this event will not
242-
/// be cleared.
243-
pub fn is_event_triggered(&self, event: Event, clear_interrupt: bool) -> bool {
237+
/// If this function returns `true` for an `Event` that this LowPowerTimer is listening for,
238+
/// [`LowPowerTimer::clear_event_flag`] must be called for that event to prevent the
239+
/// interrupt from looping eternally. This is not done in a single function to
240+
/// avoid using a mutable reference for an operation that does not require it.
241+
pub fn is_event_triggered(&self, event: Event) -> bool {
244242
let reg_val = self.lptim.isr.read();
245-
let bit_is_set = match event {
243+
match event {
246244
Event::CompareMatch => reg_val.cmpm().bit_is_set(),
247245
Event::AutoReloadMatch => reg_val.arrm().bit_is_set(),
248-
};
249-
if bit_is_set && clear_interrupt {
250-
self.lptim.icr.write(|w| match event {
251-
Event::CompareMatch => w.cmpmcf().set_bit(),
252-
Event::AutoReloadMatch => w.arrmcf().set_bit(),
253-
});
254246
}
255-
bit_is_set
256247
}
257248

258-
/// Set the compare match field for this LPTIM
249+
/// Clear the interrupt flag for the specified event
250+
pub fn clear_event_flag(&mut self, event: Event) {
251+
self.lptim.icr.write(|w| match event {
252+
Event::CompareMatch => w.cmpmcf().set_bit(),
253+
Event::AutoReloadMatch => w.arrmcf().set_bit(),
254+
});
255+
}
256+
257+
/// Set the compare match field for this LowPowerTimer
259258
#[inline]
260259
pub fn set_compare_match(&mut self, value: u16) {
261260
// This operation is sound as compare_value is a u16, and there are 16 writeable bits
262261
// Additionally, the LPTIM peripheral will always be in the enabled state when this code is called
263262
self.lptim.cmp.write(|w| unsafe { w.bits(value as u32) });
264263
}
265264

266-
/// Get the current counter value for this LPTIM
265+
/// Get the current counter value for this LowPowerTimer
267266
#[inline]
268-
pub fn get_counter(&mut self) -> u16 {
267+
pub fn get_counter(&self) -> u16 {
269268
self.lptim.cnt.read().bits() as u16
270269
}
271270

272-
/// Get the value of the LPTIM_ARR register for this
273-
/// LPTIM
271+
/// Get the value of the ARR register for this
272+
/// LowPowerTimer
274273
#[inline]
275-
pub fn get_arr(&mut self) -> u16 {
274+
pub fn get_arr(&self) -> u16 {
276275
self.lptim.arr.read().bits() as u16
277276
}
278277
}

0 commit comments

Comments
 (0)