-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft: Dynamic Clock Speed #5
Draft
dwbrite
wants to merge
3
commits into
irevoire:master
Choose a base branch
from
dwbrite:feat/dynamic-mcg-modes
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Cargo.lock | ||
target/ | ||
.cargo/ | ||
.github/ | ||
.idea/ |
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 |
---|---|---|
|
@@ -5,12 +5,14 @@ categories = ["embedded", "no-std"] | |
authors = [ | ||
"Thomas Campistron <[email protected]>", | ||
"Debilausaure", | ||
"Devin Brite <[email protected]>" | ||
] | ||
edition = "2018" | ||
|
||
[dependencies] | ||
volatile = "*" | ||
bit_field = "*" | ||
r0 = "0.2.2" | ||
|
||
[features] | ||
default = [] | ||
|
Binary file not shown.
Empty file.
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,33 @@ | ||
#![feature(stdsimd)] | ||
#![no_std] | ||
#![no_main] | ||
|
||
use teensy::mcg::{CpuFreq, Mcg}; | ||
use teensy::sim::Sim; | ||
use teensy::*; | ||
|
||
define_panic! {blink} | ||
|
||
#[no_mangle] | ||
fn main() { | ||
let mut led = unsafe { make_pin!(led).make_gpio().with_output() }; | ||
|
||
loop { | ||
change_clocks(CpuFreq::High); | ||
led.toggle(); | ||
sleep::delay(72_000_000 * 5); // 5s at 72MHz -> 3.75s at 96MHz | ||
led.toggle(); | ||
sleep::delay(72_000_000 * 5); // 5s at 72MHz -> 3.75s at 96MHz | ||
|
||
change_clocks(CpuFreq::Reduced); | ||
led.toggle(); | ||
sleep::delay(72_000_000 * 5); // 5s at 72MHz -> 7.5s at 48MHz | ||
led.toggle(); | ||
sleep::delay(72_000_000 * 5); // 5s at 72MHz -> 7.5s at 48MHz | ||
} | ||
} | ||
|
||
fn change_clocks(freq: mcg::CpuFreq) { | ||
let (sim, mcg): (&mut Sim, &mut Mcg) = unsafe { (sim::Sim::new(), mcg::Mcg::new()) }; | ||
mcg.set_clocks(freq, sim); | ||
} |
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
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 |
---|---|---|
@@ -1,10 +1,10 @@ | ||
use core::mem; | ||
|
||
use crate::sim; | ||
use bit_field::BitField; | ||
use volatile::Volatile; | ||
|
||
/// TODO This value should not be hardcoded and should change depending on the choosen frequency | ||
pub const F_CPU: u32 = 72_000_000; | ||
pub static mut F_CPU: u32 = 72_000_000; | ||
|
||
#[repr(C, packed)] | ||
pub struct Mcg { | ||
|
@@ -42,6 +42,14 @@ enum OscSource { | |
External = 2, | ||
} | ||
|
||
pub enum CpuFreq { | ||
/// Core/Bus/Flash speeds | ||
High, // 96/48/24 (96MHz PLL) | ||
Default, // 72/36/24 (72MHz PLL) | ||
Reduced, // 48/48/24 (96MHz PLL) | ||
Low, // 24/24/24 (96MHz PLL) | ||
} | ||
|
||
pub struct Fei { | ||
mcg: &'static mut Mcg, | ||
} | ||
|
@@ -107,6 +115,8 @@ pub struct Fbe { | |
} | ||
|
||
impl Fbe { | ||
// numerator / denominator * xtal (16MHz) = clock speed?? | ||
// enable phase-locked loop | ||
pub fn enable_pll(self, numerator: u8, denominator: u8) -> Pbe { | ||
if numerator < 24 || numerator > 55 { | ||
panic!("Invalid PLL VCO divide factor: {}", numerator); | ||
|
@@ -117,11 +127,15 @@ impl Fbe { | |
} | ||
|
||
self.mcg.c5.update(|c5| { | ||
// set PLL external reference divide factor | ||
c5.set_bits(0..5, denominator - 1); | ||
// Subtract 1 here to make math easier | ||
}); | ||
|
||
self.mcg.c6.update(|c6| { | ||
// set VCO divider | ||
c6.set_bits(0..5, numerator - 24); | ||
// select PLL | ||
c6.set_bit(6, true); | ||
}); | ||
|
||
|
@@ -139,7 +153,7 @@ pub struct Pbe { | |
} | ||
|
||
impl Pbe { | ||
pub fn use_pll(self) { | ||
pub fn use_pll(self) -> Pee { | ||
self.mcg.c1.update(|c1| { | ||
c1.set_bits(6..8, OscSource::LockedLoop as u8); | ||
}); | ||
|
@@ -151,13 +165,50 @@ impl Pbe { | |
// which would be invalid to set, we just check for the known | ||
// value "3" here. | ||
while self.mcg.s.read().get_bits(2..4) != 3 {} | ||
Pee { mcg: self.mcg } | ||
} | ||
|
||
pub fn disable_pll(self) -> Fbe { | ||
self.mcg.c6.update(|c6| { | ||
// select FLL | ||
c6.set_bit(6, false); | ||
}); | ||
|
||
// Wait for PLL to be disabled | ||
while self.mcg.s.read().get_bit(5) {} | ||
// Wait for the PLL to be "unlocked" | ||
while self.mcg.s.read().get_bit(6) {} | ||
|
||
Fbe { mcg: self.mcg } | ||
} | ||
} | ||
|
||
pub struct Pee { | ||
mcg: &'static mut Mcg, | ||
} | ||
|
||
impl Pee { | ||
fn use_external(self) -> Pbe { | ||
self.mcg.c1.update(|c1| { | ||
c1.set_bits(6..8, OscSource::External as u8); | ||
}); | ||
|
||
// mcg.c1 and mcg.s have slightly different behaviors. In c1, | ||
// we use one value to indicate "Use whichever LL is | ||
// enabled". In s, it is differentiated between the FLL at 0, | ||
// and the PLL at 3. Instead of adding a value to OscSource | ||
// which would be invalid to set, we just check for the known | ||
// value "0" here. | ||
while self.mcg.s.read().get_bits(2..4) != 2 {} | ||
return Pbe { mcg: self.mcg }; | ||
} | ||
} | ||
|
||
pub enum Clock { | ||
Fei(Fei), | ||
Fbe(Fbe), | ||
Pbe(Pbe), | ||
Pee(Pee), | ||
} | ||
|
||
impl Mcg { | ||
|
@@ -166,11 +217,82 @@ impl Mcg { | |
let fll_internal = self.c1.read().get_bit(2); | ||
let pll_enabled = self.c6.read().get_bit(6); | ||
|
||
// TODO: match all possible MCG clock modes before panic | ||
match (fll_internal, pll_enabled, source) { | ||
(true, false, OscSource::LockedLoop) => Clock::Fei(Fei { mcg: self }), | ||
(false, false, OscSource::External) => Clock::Fbe(Fbe { mcg: self }), | ||
(_, true, OscSource::External) => Clock::Pbe(Pbe { mcg: self }), | ||
(_, true, OscSource::LockedLoop) => Clock::Pee(Pee { mcg: self }), | ||
_ => panic!("The current clock mode cannot be represented as a known struct"), | ||
} | ||
} | ||
|
||
pub fn set_clocks(&'static mut self, clock: CpuFreq, sim: &mut sim::Sim) -> Pee { | ||
match clock { | ||
CpuFreq::High => { | ||
unsafe { | ||
F_CPU = 96_000_000; | ||
} | ||
// Set our clocks: 96/48/24 | ||
sim.set_dividers(1, 2, 4); | ||
// We would also set the USB divider here if we wanted to use it. | ||
let fbe = self.mode_to_fbe(); | ||
let pbe = fbe.enable_pll(24, 4); // 16MHz / 4 * 24 = 96MHz | ||
pbe.use_pll() | ||
} | ||
CpuFreq::Default => { | ||
unsafe { | ||
F_CPU = 72_000_000; | ||
} | ||
// Set our clocks: 72/36/24 | ||
sim.set_dividers(1, 2, 3); | ||
// We would also set the USB divider here if we wanted to use it. | ||
let fbe = self.mode_to_fbe(); | ||
let pbe = fbe.enable_pll(27, 6); // 16MHz / 6 * 27 = 72MHz | ||
pbe.use_pll() | ||
} | ||
CpuFreq::Reduced => { | ||
unsafe { | ||
F_CPU = 48_000_000; | ||
} | ||
// Set our clocks: 48/48/24 | ||
sim.set_dividers(2, 2, 4); | ||
// We would also set the USB divider here if we wanted to use it. | ||
let fbe = self.mode_to_fbe(); | ||
let pbe = fbe.enable_pll(24, 4); // 16MHz / 4 * 24 = 96MHz | ||
pbe.use_pll() | ||
} | ||
CpuFreq::Low => { | ||
unsafe { | ||
F_CPU = 24_000_000; | ||
} | ||
// Set our clocks: 24/24/24 | ||
sim.set_dividers(4, 4, 4); | ||
// We would also set the USB divider here if we wanted to use it. | ||
let fbe = self.mode_to_fbe(); | ||
let pbe = fbe.enable_pll(24, 4); // 16MHz / 4 * 24 = 96MHz | ||
pbe.use_pll() | ||
} | ||
} | ||
} | ||
|
||
pub fn mode_to_fbe(&'static mut self) -> Fbe { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this function feels... ehh? Maybe just the name is bad. |
||
return match self.clock() { | ||
Clock::Fei(mut fei) => { | ||
// Our 16MHz xtal is "very fast", and needs to be divided | ||
// by 512 to be in the acceptable FLL range. | ||
// 31.25 kHz to 39.0625 kHz | ||
fei.enable_xtal(OscRange::VeryHigh); | ||
// (literally the only valid divisor on teensy) | ||
fei.use_external(512) // 16MHz/512 = 31.25KHz | ||
} | ||
Clock::Fbe(fbe) => fbe, | ||
Clock::Pbe(pbe) => pbe.disable_pll(), | ||
Clock::Pee(pee) => { | ||
let pbe = pee.use_external(); | ||
let fbe = pbe.disable_pll(); | ||
return fbe; | ||
} | ||
}; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -62,7 +62,7 @@ macro_rules! blink_panic { | |
|
||
loop { | ||
led.toggle(); | ||
sleep::sleep_ms(500); | ||
sleep::sleep_ms(50); | ||
} | ||
} | ||
}; | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm uncertain on how much I like the values of this enum, but it does well enough for now.