Skip to content

Commit

Permalink
Reviews + spi failed experiment
Browse files Browse the repository at this point in the history
  • Loading branch information
playfulFence committed Feb 5, 2025
1 parent 46bd259 commit 263187a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
43 changes: 38 additions & 5 deletions esp-hal/src/spi/master.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ pub enum ClockSource {
/// Defines how strictly the requested frequency must be met.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[instability::unstable]
pub enum FrequencyTolerance {
/// Accept the closest achievable baud rate without restriction.
#[default]
Expand Down Expand Up @@ -462,6 +463,7 @@ pub struct Config {

/// Determines how close to the desired baud rate value the driver should
/// set the baud rate.
#[builder_lite(skip_setter)]
frequency_tolerance: FrequencyTolerance,

/// The clock source
Expand Down Expand Up @@ -517,6 +519,23 @@ impl Config {
self
}

/// Set the frequency tolerance of the SPI configuration.
#[instability::unstable]
pub fn with_frequency_tolerance(mut self, tolerance: FrequencyTolerance) -> Self {
self.frequency_tolerance = match tolerance {
FrequencyTolerance::Exact => FrequencyTolerance::Exact,
FrequencyTolerance::Closest => FrequencyTolerance::Closest,
FrequencyTolerance::ErrorPercent(percentage) => {
assert!(percentage > 0 && percentage <= 100);
FrequencyTolerance::ErrorPercent(percentage)
}
};

self.reg = self.recalculate();

self
}

/// Set the clock source of the SPI bus.
#[instability::unstable]
pub fn with_clock_source(mut self, clock_source: ClockSource) -> Self {
Expand Down Expand Up @@ -546,6 +565,11 @@ impl Config {
// The value written to register is one lower than the used value.

if self.frequency > ((apb_clk_freq / 4) * 3) {
// If the user requests for `Exact` frequency, which is above 80Mhz
if self.frequency_tolerance == FrequencyTolerance::Exact && self.frequency > HertzU32::MHz(80){
return Err(ConfigError::UnsupportedFrequency)
}

// Using APB frequency directly will give us the best result here.
reg_val = 1 << 31;
} else {
Expand Down Expand Up @@ -612,7 +636,8 @@ impl Config {
let actual_freq = raw_apb_freq / (pre * n);

match self.frequency_tolerance {
FrequencyTolerance::Exact if actual_freq != raw_desired_freq => {
FrequencyTolerance::Exact => {
panic!("{} vs {}", actual_freq, raw_desired_freq);
return Err(ConfigError::UnsupportedFrequency)
}
FrequencyTolerance::ErrorPercent(percent) => {
Expand All @@ -635,10 +660,18 @@ impl Config {
}

fn validate(&self) -> Result<(), ConfigError> {
// Maximum supported frequency is 80Mhz, minimum is about 70khz.
if self.frequency < HertzU32::kHz(70) || self.frequency > HertzU32::MHz(80) {
return Err(ConfigError::UnsupportedFrequency);
}
// Maximum supported frequency is 80Mhz, minimum is about 80khz.
// cfg_if::cfg_if! {
// if #[cfg(esp32h2)] {
// if self.frequency < HertzU32::kHz(70) || self.frequency > HertzU32::MHz(48) {
// return Err(ConfigError::UnsupportedFrequency);
// }
// } else {
// if self.frequency < HertzU32::kHz(70) || self.frequency > HertzU32::MHz(80) {
// return Err(ConfigError::UnsupportedFrequency);
// }
// }
// }
Ok(())
}
}
Expand Down
2 changes: 2 additions & 0 deletions esp-hal/src/uart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ pub enum StopBits {
/// Defines how strictly the requested baud rate must be met.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[instability::unstable]
pub enum BaudrateTolerance {
/// Accept the closest achievable baud rate without restriction.
#[default]
Expand Down Expand Up @@ -349,6 +350,7 @@ impl Default for RxConfig {

impl Config {
/// Set the baudrate tolerance of the UART configuration.
#[instability::unstable]
pub fn with_baudrate_tolerance(mut self, tolerance: BaudrateTolerance) -> Self {
self.baudrate_tolerance = match tolerance {
BaudrateTolerance::Exact => BaudrateTolerance::Exact,
Expand Down

0 comments on commit 263187a

Please sign in to comment.