Skip to content

Commit

Permalink
move rtc_clk_xtal_freq_get to esp-hal
Browse files Browse the repository at this point in the history
  • Loading branch information
MabezDev committed Sep 18, 2024
1 parent e6007c7 commit 19c430b
Show file tree
Hide file tree
Showing 14 changed files with 40 additions and 48 deletions.
8 changes: 8 additions & 0 deletions esp-hal/src/rtc_cntl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1116,3 +1116,11 @@ pub fn get_wakeup_cause() -> SleepSource {

SleepSource::Undefined
}

// libphy.a can pull this in on some chips, we provide it here in the hal
// so that either ieee or esp-wifi gets it for free without duplicating in both
#[no_mangle]
extern "C" fn rtc_clk_xtal_freq_get() -> i32 {
let xtal = RtcClock::get_xtal_freq();
xtal.mhz() as i32
}
8 changes: 4 additions & 4 deletions esp-hal/src/rtc_cntl/rtc/esp32c6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1372,7 +1372,7 @@ impl Clock for RtcFastClock {
#[allow(clippy::enum_variant_names)]
#[derive(Debug, Clone, Copy, PartialEq)]
/// RTC SLOW_CLK frequency values
pub(crate) enum RtcSlowClock {
pub enum RtcSlowClock {
/// Select RC_SLOW_CLK as RTC_SLOW_CLK source
RtcSlowClockRcSlow = 0,
/// Select XTAL32K_CLK as RTC_SLOW_CLK source
Expand Down Expand Up @@ -1426,7 +1426,7 @@ impl RtcClock {
const CAL_FRACT: u32 = 19;

// rtc_clk_xtal_freq_get
fn get_xtal_freq_mhz() -> u32 {
pub(crate) fn get_xtal_freq_mhz() -> u32 {
let xtal_freq_reg = unsafe { lp_aon().store4().read().bits() };

// Values of RTC_XTAL_FREQ_REG and RTC_APB_FREQ_REG are stored as two copies in
Expand All @@ -1447,15 +1447,15 @@ impl RtcClock {
/// Get main XTAL frequency
/// This is the value stored in RTC register RTC_XTAL_FREQ_REG by the
/// bootloader, as passed to rtc_clk_init function.
fn get_xtal_freq() -> XtalClock {
pub fn get_xtal_freq() -> XtalClock {
match Self::get_xtal_freq_mhz() {
40 => XtalClock::RtcXtalFreq40M,
other => XtalClock::RtcXtalFreqOther(other),
}
}

/// Get the RTC_SLOW_CLK source
pub(crate) fn get_slow_freq() -> RtcSlowClock {
pub fn get_slow_freq() -> RtcSlowClock {
let lp_clrst = unsafe { lp_clkrst() };

let slow_freq = lp_clrst.lp_clk_conf().read().slow_clk_sel().bits();
Expand Down
13 changes: 8 additions & 5 deletions esp-hal/src/rtc_cntl/rtc/esp32h2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,9 @@ impl Clock for RtcFastClock {

/// RTC SLOW_CLK frequency values
#[allow(clippy::enum_variant_names)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub(crate) enum RtcSlowClock {
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub enum RtcSlowClock {
/// Select RC_SLOW_CLK as RTC_SLOW_CLK source
RtcSlowClockRcSlow = 0,
/// Select XTAL32K_CLK as RTC_SLOW_CLK source
Expand Down Expand Up @@ -270,8 +271,10 @@ pub struct RtcClock;
impl RtcClock {
const CAL_FRACT: u32 = 19;

/// Calculate the necessary RTC_SLOW_CLK cycles to complete 1 millisecond.
fn get_xtal_freq() -> XtalClock {
/// Get main XTAL frequency.
/// This is the value stored in RTC register RTC_XTAL_FREQ_REG by the
/// bootloader, as passed to rtc_clk_init function.
pub fn get_xtal_freq() -> XtalClock {
let xtal_freq_reg = unsafe { &*LP_AON::PTR }.store4().read().bits();

// Values of RTC_XTAL_FREQ_REG and RTC_APB_FREQ_REG are stored as two copies in
Expand Down Expand Up @@ -324,7 +327,7 @@ impl RtcClock {
}

/// Get the RTC_SLOW_CLK source
pub(crate) fn get_slow_freq() -> RtcSlowClock {
pub fn get_slow_freq() -> RtcSlowClock {
let lp_clrst = unsafe { &*LPWR::ptr() };

let slow_freq = lp_clrst.lp_clk_conf().read().slow_clk_sel().bits();
Expand Down
2 changes: 1 addition & 1 deletion esp-ieee802154/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ byte = "0.2.7"
critical-section = "1.1.3"
document-features = "0.2.10"
esp-hal = { version = "0.20.0", path = "../esp-hal" }
esp-wifi-sys = { version = "0.5.0", git = "https://github.com/esp-rs/esp-wifi-sys", rev = "e64adcf" }
esp-wifi-sys = { version = "0.5.0", git = "https://github.com/esp-rs/esp-wifi-sys", rev = "661e948" }
heapless = "0.8.0"
ieee802154 = "0.6.1"
log = "0.4.22"
Expand Down
4 changes: 2 additions & 2 deletions esp-wifi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ heapless = { version = "0.8.0", default-features = false, features = [
num-derive = { version = "0.4.2" }
num-traits = { version = "0.2.19", default-features = false }
no-std-net = { version = "0.6.0", optional = true }
esp-wifi-sys = { version = "0.5.0", git = "https://github.com/esp-rs/esp-wifi-sys", rev = "e64adcf" }
esp-wifi-sys = { version = "0.5.0", git = "https://github.com/esp-rs/esp-wifi-sys", rev = "661e948" }
embassy-sync = { version = "0.6.0", optional = true }
embassy-futures = { version = "0.1.1", optional = true }
embassy-net-driver = { version = "0.2.0", optional = true }
Expand Down Expand Up @@ -127,7 +127,7 @@ embassy-net = ["dep:embassy-net-driver", "async"]
coex = []

## Logs the WiFi logs from the driver at log level info (needs a nightly-compiler)
binary-logs = []
binary-logs = ["esp-wifi-sys/binary-logs"]

## Dumps packet info at log level info
dump-packets = []
Expand Down
7 changes: 0 additions & 7 deletions esp-wifi/src/common_adapter/common_adapter_esp32c2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,3 @@ pub(crate) unsafe fn phy_disable_clock() {

trace!("phy_disable_clock done!");
}

#[no_mangle]
pub extern "C" fn rtc_clk_xtal_freq_get() -> i32 {
use crate::hal::clock::Clock;
let xtal = crate::hal::rtc_cntl::RtcClock::get_xtal_freq();
xtal.mhz() as i32
}
6 changes: 0 additions & 6 deletions esp-wifi/src/common_adapter/common_adapter_esp32c6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,3 @@ pub(crate) unsafe fn phy_disable_clock() {
unwrap!(RADIO_CLOCKS.as_mut()).disable(RadioPeripherals::Phy);
trace!("phy_disable_clock done!");
}

#[no_mangle]
pub extern "C" fn rtc_clk_xtal_freq_get() -> i32 {
// JUST SUPPORT 40MHz XTAL for now
40
}
6 changes: 0 additions & 6 deletions esp-wifi/src/common_adapter/common_adapter_esp32h2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,3 @@ pub(crate) unsafe fn phy_disable_clock() {
unwrap!(RADIO_CLOCKS.as_mut()).disable(RadioPeripherals::Phy);
trace!("phy_disable_clock done!");
}

#[no_mangle]
pub extern "C" fn rtc_clk_xtal_freq_get() -> i32 {
// JUST SUPPORT 32MHz XTAL for now
32
}
14 changes: 7 additions & 7 deletions esp-wifi/src/wifi/os_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1449,8 +1449,7 @@ pub unsafe extern "C" fn log_write(
format: *const crate::binary::c_types::c_char,
args: ...
) {
#[allow(clippy::missing_transmute_annotations)]
crate::binary::log::syslog(level, format as *const u8, core::mem::transmute(args));
crate::binary::log::syslog(level, format as *const u8, args);
}

/// **************************************************************************
Expand All @@ -1475,12 +1474,13 @@ pub unsafe extern "C" fn log_writev(
level: u32,
_tag: *const crate::binary::c_types::c_char,
format: *const crate::binary::c_types::c_char,
args: esp_wifi_sys::include::va_list,
args: crate::binary::include::va_list,
) {
// annotations on transmute here would require different types for RISC-V and
// Xtensa - so let's allow `missing_transmute_annotations` in this case
#[allow(clippy::missing_transmute_annotations)]
crate::binary::log::syslog(level, format as *const u8, core::mem::transmute(args));
crate::binary::log::syslog(
level,
format as *const u8,
core::mem::transmute::<crate::binary::include::va_list, core::ffi::VaListImpl<'_>>(args),
);
}

/// **************************************************************************
Expand Down
4 changes: 2 additions & 2 deletions examples/src/bin/ieee802154_receive_all_frames.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use esp_println::println;

#[entry]
fn main() -> ! {
let mut peripherals = esp_hal::init(esp_hal::Config::default());
let mut ieee802154 = Ieee802154::new(peripherals.IEEE802154, &mut peripherals.RADIO_CLK);
let peripherals = esp_hal::init(esp_hal::Config::default());
let mut ieee802154 = Ieee802154::new(peripherals.IEEE802154, peripherals.RADIO_CLK);

ieee802154.set_config(Config {
channel: 15,
Expand Down
4 changes: 2 additions & 2 deletions examples/src/bin/ieee802154_receive_frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use esp_println::println;

#[entry]
fn main() -> ! {
let mut peripherals = esp_hal::init(esp_hal::Config::default());
let mut ieee802154 = Ieee802154::new(peripherals.IEEE802154, &mut peripherals.RADIO_CLK);
let peripherals = esp_hal::init(esp_hal::Config::default());
let mut ieee802154 = Ieee802154::new(peripherals.IEEE802154, peripherals.RADIO_CLK);

ieee802154.set_config(Config {
channel: 15,
Expand Down
4 changes: 2 additions & 2 deletions examples/src/bin/ieee802154_send_broadcast_frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ use ieee802154::mac::{

#[entry]
fn main() -> ! {
let mut peripherals = esp_hal::init(esp_hal::Config::default());
let peripherals = esp_hal::init(esp_hal::Config::default());

let delay = Delay::new();

let mut ieee802154 = Ieee802154::new(peripherals.IEEE802154, &mut peripherals.RADIO_CLK);
let mut ieee802154 = Ieee802154::new(peripherals.IEEE802154, peripherals.RADIO_CLK);

ieee802154.set_config(Config {
channel: 15,
Expand Down
4 changes: 2 additions & 2 deletions examples/src/bin/ieee802154_send_frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ use ieee802154::mac::{

#[entry]
fn main() -> ! {
let mut peripherals = esp_hal::init(esp_hal::Config::default());
let peripherals = esp_hal::init(esp_hal::Config::default());

let delay = Delay::new();

let mut ieee802154 = Ieee802154::new(peripherals.IEEE802154, &mut peripherals.RADIO_CLK);
let mut ieee802154 = Ieee802154::new(peripherals.IEEE802154, peripherals.RADIO_CLK);

ieee802154.set_config(Config {
channel: 15,
Expand Down
4 changes: 2 additions & 2 deletions examples/src/bin/ieee802154_sniffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use esp_println::println;

#[entry]
fn main() -> ! {
let mut peripherals = esp_hal::init(esp_hal::Config::default());
let peripherals = esp_hal::init(esp_hal::Config::default());

let io = Io::new(peripherals.GPIO, peripherals.IO_MUX);

Expand Down Expand Up @@ -50,7 +50,7 @@ fn main() -> ! {
.unwrap();

let radio = peripherals.IEEE802154;
let mut ieee802154 = Ieee802154::new(radio, &mut peripherals.RADIO_CLK);
let mut ieee802154 = Ieee802154::new(radio, peripherals.RADIO_CLK);

ieee802154.set_config(Config {
channel,
Expand Down

0 comments on commit 19c430b

Please sign in to comment.