Skip to content

Commit

Permalink
Fixes. Now finally working!
Browse files Browse the repository at this point in the history
  • Loading branch information
Finomnis committed Nov 3, 2023
1 parent cbf1d31 commit e862d71
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions rtic-monotonics/src/imxrt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ macro_rules! make_timer {
FRR: 1, // Free-Run mode
);

// Reset period
$period.store(0, Ordering::Relaxed);

// Prescaler
ral::modify_reg!(ral::gpt, gpt, PR,
PRESCALER: (prescaler - 1), // Scale to our desired clock rate
Expand Down Expand Up @@ -247,21 +250,22 @@ macro_rules! make_timer {
}

fn set_compare(instant: Self::Instant) {
// let now = Self::now();
// let max_ticks = $bits::MAX as u64;
let gpt = unsafe{ $timer::instance() };

// // Since the timer may or may not overflow based on the requested compare val, we check how many ticks are left.
// let val = match instant.checked_duration_since(now) {
// None => 0, // In the past
// Some(x) if x.ticks() <= max_ticks => instant.duration_since_epoch().ticks() as $bits, // Will not overflow
// Some(_x) => $timer.cnt().read().cnt().wrapping_add($bits::MAX - 1), // Will overflow
// };
// Set the timer regardless of whether it is multiple periods in the future,
// or even already in the past.
// The worst thing that can happen is a spurious wakeup, and with a timer
// period of half an hour, this is hardly a problem.

// $timer.ccr(1).write(|r| r.set_ccr(val));
let ticks = instant.duration_since_epoch().ticks();
let ticks_wrapped = ticks as u32;

ral::write_reg!(ral::gpt, gpt, OCR[1], ticks_wrapped);
}

fn clear_compare_flag() {
// $timer.sr().modify(|r| r.set_ccif(1, false));
let gpt = unsafe{ $timer::instance() };
ral::write_reg!(ral::gpt, gpt, SR, OF2: 1);
}

fn pend_interrupt() {
Expand All @@ -273,12 +277,15 @@ macro_rules! make_timer {

let (rollover, half_rollover) = ral::read_reg!(ral::gpt, gpt, SR, ROV, OF1);

if((rollover != 0) || (half_rollover != 0)){
if rollover != 0 {
$period.fetch_add(1, Ordering::Relaxed);
ral::write_reg!(ral::gpt, gpt, SR, ROV: 1);
}

// Clear all status registers.
ral::write_reg!(ral::gpt, gpt, SR, 0b11_1111);
if half_rollover != 0 {
$period.fetch_add(1, Ordering::Relaxed);
ral::write_reg!(ral::gpt, gpt, SR, OF1: 1);
}
}
}
};
Expand Down

0 comments on commit e862d71

Please sign in to comment.