Skip to content

Commit

Permalink
add support for 26mhz esp32
Browse files Browse the repository at this point in the history
  • Loading branch information
RepeatedRoot committed Feb 16, 2023
1 parent 5284fc0 commit 912ab30
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 3 deletions.
3 changes: 3 additions & 0 deletions esp-hal-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ esp32c3 = ["esp32c3/rt", "riscv", "procmacros/esp32c3"]
esp32s2 = ["esp32s2/rt", "xtensa", "xtensa-lx/esp32s2", "xtensa-lx-rt/esp32s2", "esp-synopsys-usb-otg", "usb-device", "procmacros/esp32s2"]
esp32s3 = ["esp32s3/rt", "xtensa", "xtensa-lx/esp32s3", "xtensa-lx-rt/esp32s3", "lock_api", "esp-synopsys-usb-otg", "usb-device", "procmacros/esp32s3"]

esp32_40mhz = []
esp32_26mhz = []

esp32c2_40mhz = []
esp32c2_26mhz = []

Expand Down
3 changes: 3 additions & 0 deletions esp-hal-common/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ fn main() {
n => panic!("Exactly 1 chip must be enabled via its Cargo feature, {n} provided"),
}

if cfg!(feature = "esp32") && cfg!(feature = "esp32_40mhz") && cfg!(feature = "esp32_26mhz") {
panic!("Only one xtal speed feature can be selected");
}
if cfg!(feature = "esp32c2")
&& cfg!(feature = "esp32c2_40mhz")
&& cfg!(feature = "esp32c2_26mhz")
Expand Down
20 changes: 18 additions & 2 deletions esp-hal-common/src/clock/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ impl<'d> ClockControl<'d> {
pub fn boot_defaults(
clock_control: impl Peripheral<P = SystemClockControl> + 'd,
) -> ClockControl<'d> {
ClockControl {
#[cfg(feature = "esp32_40mhz")]
return ClockControl {
_private: clock_control.into_ref(),
desired_rates: RawClocks {
cpu_clock: HertzU32::MHz(80),
Expand All @@ -194,7 +195,19 @@ impl<'d> ClockControl<'d> {
i2c_clock: HertzU32::MHz(80),
pwm_clock: HertzU32::MHz(160),
},
}
};

#[cfg(feature = "esp32_26mhz")]
return ClockControl {
_private: clock_control.into_ref(),
desired_rates: RawClocks {
cpu_clock: HertzU32::MHz(80),
apb_clock: HertzU32::MHz(80),
xtal_clock: HertzU32::MHz(26),
i2c_clock: HertzU32::MHz(80),
pwm_clock: HertzU32::MHz(160),
},
};
}

/// Configure the CPU clock speed.
Expand All @@ -205,7 +218,10 @@ impl<'d> ClockControl<'d> {
) -> ClockControl<'d> {
// like NuttX use 40M hardcoded - if it turns out to be a problem
// we will take care then
#[cfg(feature = "esp32_40mhz")]
let xtal_freq = XtalClock::RtcXtalFreq40M;
#[cfg(feature = "esp32_26mhz")]
let xtal_freq = XtalClock::RtcXtalFreq26M;
let pll_freq = match cpu_clock_speed {
CpuClock::Clock80MHz => PllClock::Pll320MHz,
CpuClock::Clock160MHz => PllClock::Pll320MHz,
Expand Down
6 changes: 6 additions & 0 deletions esp-hal-common/src/rtc_cntl/rtc/esp32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@ use crate::{
pub(crate) fn init() {}

pub(crate) fn configure_clock() {
#[cfg(feature = "esp32_40mhz")]
assert!(matches!(
RtcClock::get_xtal_freq(),
XtalClock::RtcXtalFreq40M
));
#[cfg(feature = "esp32_26mhz")]
assert!(
matches!(RtcClock::get_xtal_freq(), XtalClock::RtcXtalFreq26M),
"Did you flash the right bootloader configured for 26Mhz xtal?"
);

RtcClock::set_fast_freq(RtcFastClock::RtcFastClock8m);

Expand Down
4 changes: 3 additions & 1 deletion esp32-hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ static_cell = "1.0.0"
aes = "0.8.2"

[features]
default = ["rt", "vectored"]
default = ["rt", "vectored", "xtal40mhz"]
bluetooth = []
eh1 = ["esp-hal-common/eh1", "dep:embedded-hal-1", "dep:embedded-hal-nb"]
rt = []
Expand All @@ -55,6 +55,8 @@ vectored = ["esp-hal-common/vectored"]
async = ["esp-hal-common/async", "embedded-hal-async"]
embassy = ["esp-hal-common/embassy"]
embassy-time-timg0 = ["esp-hal-common/embassy-time-timg0", "embassy-time/tick-hz-1_000_000"]
xtal40mhz = ["esp-hal-common/esp32_40mhz"]
xtal26mhz = ["esp-hal-common/esp32_26mhz"]

[[example]]
name = "hello_rgb"
Expand Down
7 changes: 7 additions & 0 deletions esp32-hal/build.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{env, fs::File, io::Write, path::PathBuf};

fn main() {
check_features();
// Put the linker script somewhere the linker can find it
let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap());
File::create(out.join("memory.x"))
Expand Down Expand Up @@ -68,3 +69,9 @@ fn generate_memory_extras() -> Vec<u8> {
.as_bytes()
.to_vec()
}

fn check_features() {
if cfg!(feature = "esp32_40mhz") && cfg!(feature = "esp32_26mhz") {
panic!("Only one xtal speed feature can be selected");
}
}

0 comments on commit 912ab30

Please sign in to comment.