Skip to content

Commit

Permalink
Try to avoid pointer read in time conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Feb 4, 2025
1 parent fe53061 commit 5e86590
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 11 deletions.
1 change: 1 addition & 0 deletions esp-hal/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- UART: Add separate config for Rx and Tx (#2965)
- Added accessor methods to config structs (#3011)
- Async support for ADC oneshot reads for ESP32C2, ESP32C3, ESP32C6 and ESP32H2 (#2925, #3082)
- `ESP_HAL_CONFIG_XTAL_FREQUENCY` configuration. For now, chips other than ESP32 and ESP32-C2 have a single option only. (#3054)

### Changed

Expand Down
1 change: 1 addition & 0 deletions esp-hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ embedded-io = { version = "0.6.1", optional = true }
embedded-io-async = { version = "0.6.1", optional = true }
enumset = "1.1.5"
esp-build = { version = "0.2.0", path = "../esp-build" }
esp-config = { version = "0.3.0", path = "../esp-config" }
esp-synopsys-usb-otg = { version = "0.4.2", optional = true, features = ["fs", "esp32sx"] }
fugit = "0.3.7"
instability = "0.3.7"
Expand Down
24 changes: 23 additions & 1 deletion esp-hal/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,28 @@ fn main() -> Result<(), Box<dyn Error>> {
Value::Bool(false),
None
),
// Ideally, we should be able to set any clock frequency for any chip. However, currently
// only the 32 and C2 implements any sort of configurability, and the rest have a fixed
// clock frequeny.
// TODO: only show this configuration for chips that have multiple valid options.
(
"xtal-frequency",
"The frequency of the crystal oscillator, in MHz. Set to `auto` to automatically detect the frequency. `auto` may not be able to identify the clock frequency in some cases. Also, configuring a specific frequency may increase performance slightly.",
Value::String(match device_name {
"esp32" | "esp32c2" => String::from("auto"),
// The rest has only one option
"esp32c3" | "esp32c6" | "esp32s2" | "esp32s3" => String::from("40"),
"esp32h2" => String::from("32"),
_ => unreachable!(),
}),
Some(Validator::Enumeration(match device_name {
"esp32" | "esp32c2" => vec![String::from("auto"), String::from("26"), String::from("40")],
// The rest has only one option
"esp32c3" | "esp32c6" | "esp32s2" | "esp32s3" => vec![String::from("40")],
"esp32h2" => vec![String::from("32")],
_ => unreachable!(),
})),
),
// ideally we should only offer this for ESP32 but the config system doesn't
// support per target configs, yet
(
Expand All @@ -98,7 +120,7 @@ fn main() -> Result<(), Box<dyn Error>> {
"(ESP32, ESP32-S2 and ESP32-S3 only, `octal` is only supported for ESP32-S3) SPIRAM chip mode",
Value::String(String::from("quad")),
Some(Validator::Enumeration(
vec![String::from("quad"), String::from("octal")]
vec![String::from("quad"), String::from("octal")]
)),
)
], true);
Expand Down
44 changes: 34 additions & 10 deletions esp-hal/src/clock/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,21 +319,34 @@ impl Clocks {
/// [`crate::init()`].
#[cfg(systimer)]
pub(crate) fn xtal_freq() -> HertzU32 {
if let Some(clocks) = Self::try_get() {
clocks.xtal_clock
} else {
Self::measure_xtal_frequency().frequency()
if esp_config::esp_config_str!("ESP_HAL_CONFIG_XTAL_FREQUENCY") == "auto" {
if let Some(clocks) = Self::try_get() {
return clocks.xtal_clock;
}
}

Self::measure_xtal_frequency().frequency()
}
}

#[cfg(esp32)]
impl Clocks {
fn measure_xtal_frequency() -> XtalClock {
if RtcClock::estimate_xtal_frequency() > 33 {
XtalClock::_40M
if esp_config::esp_config_str!("ESP_HAL_CONFIG_XTAL_FREQUENCY") == "auto" {
if RtcClock::estimate_xtal_frequency() > 33 {
XtalClock::_40M
} else {
XtalClock::_26M
}
} else {
XtalClock::_26M
const {
match esp_config::esp_config_str!("ESP_HAL_CONFIG_XTAL_FREQUENCY").as_bytes() {
b"auto" => XtalClock::Other(0),
b"26" => XtalClock::_26M,
b"40" => XtalClock::_40M,
other => XtalClock::Other(esp_config::esp_config_int_parse!(u32, other)),
}
}
}
}

Expand Down Expand Up @@ -370,10 +383,21 @@ impl Clocks {
#[cfg(esp32c2)]
impl Clocks {
fn measure_xtal_frequency() -> XtalClock {
if RtcClock::estimate_xtal_frequency() > 33 {
XtalClock::_40M
if esp_config::esp_config_str!("ESP_HAL_CONFIG_XTAL_FREQUENCY") == "auto" {
if RtcClock::estimate_xtal_frequency() > 33 {
XtalClock::_40M
} else {
XtalClock::_26M
}
} else {
XtalClock::_26M
const {
match esp_config::esp_config_str!("ESP_HAL_CONFIG_XTAL_FREQUENCY").as_bytes() {
b"auto" => XtalClock::Other(0),
b"26" => XtalClock::_26M,
b"40" => XtalClock::_40M,
other => XtalClock::Other(esp_config::esp_config_int_parse!(u32, other)),
}
}
}
}

Expand Down

0 comments on commit 5e86590

Please sign in to comment.