Skip to content

Commit

Permalink
move more things to init
Browse files Browse the repository at this point in the history
  • Loading branch information
MabezDev committed Sep 5, 2024
1 parent e785ac3 commit befe0e0
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 115 deletions.
6 changes: 3 additions & 3 deletions esp-hal-embassy/src/time_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use embassy_time_driver::{AlarmHandle, Driver};
use esp_hal::{
interrupt::{InterruptHandler, Priority},
prelude::*,
time::current_time,
time::uptime,
timer::{ErasedTimer, OneShotTimer},
};

Expand Down Expand Up @@ -119,7 +119,7 @@ impl EmbassyTimer {
}

fn arm(timer: &mut Timer, timestamp: u64) {
let now = current_time().duration_since_epoch();
let now = uptime().duration_since_epoch();
let ts = timestamp.micros();
// if the TS is already in the past make the timer fire immediately
let timeout = if ts > now { ts - now } else { 0.micros() };
Expand All @@ -130,7 +130,7 @@ impl EmbassyTimer {

impl Driver for EmbassyTimer {
fn now(&self) -> u64 {
current_time().ticks()
uptime().ticks()
}

unsafe fn allocate_alarm(&self) -> Option<AlarmHandle> {
Expand Down
6 changes: 3 additions & 3 deletions esp-hal/src/delay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
//! [DelayMs]: embedded_hal_02::blocking::delay::DelayMs
//! [DelayUs]: embedded_hal_02::blocking::delay::DelayUs
//! [embedded-hal]: https://docs.rs/embedded-hal/1.0.0/embedded_hal/delay/index.html
//! [current_time]: crate::time::current_time
//! [current_time]: crate::time::uptime
pub use fugit::MicrosDurationU64;

Expand Down Expand Up @@ -75,7 +75,7 @@ impl Delay {

/// Delay for the specified time
pub fn delay(&self, delay: MicrosDurationU64) {
let start = crate::time::current_time();
let start = crate::time::uptime();

while elapsed_since(start) < delay {}
}
Expand All @@ -101,7 +101,7 @@ impl Delay {
}

fn elapsed_since(start: fugit::Instant<u64, 1, 1_000_000>) -> MicrosDurationU64 {
let now = crate::time::current_time();
let now = crate::time::uptime();

if start.ticks() <= now.ticks() {
now - start
Expand Down
18 changes: 18 additions & 0 deletions esp-hal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -734,9 +734,27 @@ pub struct Config {
///
/// This function sets up the CPU clock and returns the peripherals and clocks.
pub fn init(config: Config) -> Peripherals {
use self::peripherals::*;
let peripherals = Peripherals::take();

Clocks::init(config.cpu_clock);

#[cfg(xtensa)]
crate::interrupt::setup_interrupts();
#[cfg(esp32)]
crate::time::time_init();

// RTC domain must be enabled before we try to disable
let mut rtc = crate::rtc_cntl::Rtc::new(unsafe { LPWR::steal() });
#[cfg(not(any(esp32, esp32s2)))]
rtc.swd.disable();
rtc.rwdt.disable();

unsafe {
crate::timer::timg::Wdt::<TIMG0, crate::Blocking>::set_wdt_enabled(false);
#[cfg(timg1)]
crate::timer::timg::Wdt::<TIMG1, crate::Blocking>::set_wdt_enabled(false);
}

peripherals
}
19 changes: 1 addition & 18 deletions esp-hal/src/soc/esp32/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@
use core::ptr::addr_of_mut;

use self::peripherals::{LPWR, TIMG0, TIMG1};
use crate::{
rtc_cntl::{Rtc, SocResetReason},
timer::timg::Wdt,
};
use crate::rtc_cntl::SocResetReason;

pub mod cpu_control;
pub mod efuse;
Expand Down Expand Up @@ -111,9 +107,6 @@ pub unsafe extern "C" fn ESP32Reset() -> ! {
stack_chk_guard.write_volatile(0xdeadbabe);
}

crate::interrupt::setup_interrupts();
crate::time::time_init();

// continue with default reset handler
xtensa_lx_rt::Reset();
}
Expand All @@ -126,13 +119,3 @@ pub unsafe extern "C" fn ESP32Reset() -> ! {
pub extern "Rust" fn __init_data() -> bool {
false
}

#[export_name = "__post_init"]
unsafe fn post_init() {
// RTC domain must be enabled before we try to disable
let mut rtc = Rtc::new(LPWR::steal());
rtc.rwdt.disable();

Wdt::<TIMG0, crate::Blocking>::set_wdt_enabled(false);
Wdt::<TIMG1, crate::Blocking>::set_wdt_enabled(false);
}
13 changes: 0 additions & 13 deletions esp-hal/src/soc/esp32c2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
//! The `SOC` module provides access, functions and structures that are useful
//! for interacting with various system-related peripherals on `ESP32-C2` chip.
use self::peripherals::{LPWR, TIMG0};
use crate::{rtc_cntl::Rtc, timer::timg::Wdt};

pub mod efuse;
pub mod gpio;
pub mod peripherals;
Expand Down Expand Up @@ -38,13 +35,3 @@ pub(crate) mod constants {
/// RC FAST Clock value (Hertz).
pub const RC_FAST_CLK: fugit::HertzU32 = fugit::HertzU32::kHz(17500);
}

#[export_name = "__post_init"]
unsafe fn post_init() {
// RTC domain must be enabled before we try to disable
let mut rtc = Rtc::new(LPWR::steal());
rtc.swd.disable();
rtc.rwdt.disable();

Wdt::<TIMG0, crate::Blocking>::set_wdt_enabled(false);
}
14 changes: 0 additions & 14 deletions esp-hal/src/soc/esp32c3/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
//! * I2S_SCLK: 160_000_000 - I2S clock frequency
//! * I2S_DEFAULT_CLK_SRC: 2 - I2S clock source
use self::peripherals::{LPWR, TIMG0, TIMG1};
use crate::{rtc_cntl::Rtc, timer::timg::Wdt};

pub mod efuse;
pub mod gpio;
pub mod peripherals;
Expand Down Expand Up @@ -56,14 +53,3 @@ pub(crate) mod constants {
/// RC FAST Clock value (Hertz).
pub const RC_FAST_CLK: fugit::HertzU32 = fugit::HertzU32::kHz(17500);
}

#[export_name = "__post_init"]
unsafe fn post_init() {
// RTC domain must be enabled before we try to disable
let mut rtc = Rtc::new(LPWR::steal());
rtc.swd.disable();
rtc.rwdt.disable();

Wdt::<TIMG0, crate::Blocking>::set_wdt_enabled(false);
Wdt::<TIMG1, crate::Blocking>::set_wdt_enabled(false);
}
14 changes: 0 additions & 14 deletions esp-hal/src/soc/esp32c6/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
//! * I2S_DEFAULT_CLK_SRC: 2 - I2S clock source
//! * I2S_SCLK: 160_000_000 - I2S clock frequency
use self::peripherals::{LPWR, TIMG0, TIMG1};
use crate::{rtc_cntl::Rtc, timer::timg::Wdt};

pub mod efuse;
pub mod gpio;
pub mod lp_core;
Expand Down Expand Up @@ -64,14 +61,3 @@ pub(crate) mod constants {
/// RC FAST Clock value (Hertz).
pub const RC_FAST_CLK: fugit::HertzU32 = fugit::HertzU32::kHz(17_500);
}

#[export_name = "__post_init"]
unsafe fn post_init() {
// RTC domain must be enabled before we try to disable
let mut rtc = Rtc::new(LPWR::steal());
rtc.swd.disable();
rtc.rwdt.disable();

Wdt::<TIMG0, crate::Blocking>::set_wdt_enabled(false);
Wdt::<TIMG1, crate::Blocking>::set_wdt_enabled(false);
}
14 changes: 0 additions & 14 deletions esp-hal/src/soc/esp32h2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
//! * I2S_DEFAULT_CLK_SRC: 1 - I2S clock source
//! * I2S_SCLK: 96_000_000 - I2S clock frequency
use self::peripherals::{LPWR, TIMG0, TIMG1};
use crate::{rtc_cntl::Rtc, timer::timg::Wdt};

pub mod efuse;
pub mod gpio;
pub mod peripherals;
Expand Down Expand Up @@ -64,14 +61,3 @@ pub(crate) mod constants {
/// RC FAST Clock value (Hertz).
pub const RC_FAST_CLK: fugit::HertzU32 = fugit::HertzU32::kHz(17500);
}

#[export_name = "__post_init"]
unsafe fn post_init() {
// RTC domain must be enabled before we try to disable
let mut rtc = Rtc::new(LPWR::steal());
rtc.swd.disable();
rtc.rwdt.disable();

Wdt::<TIMG0, crate::Blocking>::set_wdt_enabled(false);
Wdt::<TIMG1, crate::Blocking>::set_wdt_enabled(false);
}
18 changes: 1 addition & 17 deletions esp-hal/src/soc/esp32s2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@
use core::ptr::addr_of_mut;

use self::peripherals::{LPWR, TIMG0, TIMG1};
use crate::{
rtc_cntl::{Rtc, SocResetReason},
timer::timg::Wdt,
};
use crate::rtc_cntl::SocResetReason;

pub mod efuse;
pub mod gpio;
Expand Down Expand Up @@ -116,8 +112,6 @@ pub unsafe extern "C" fn ESP32Reset() -> ! {
stack_chk_guard.write_volatile(0xdeadbabe);
}

crate::interrupt::setup_interrupts();

// continue with default reset handler
xtensa_lx_rt::Reset();
}
Expand All @@ -130,13 +124,3 @@ pub unsafe extern "C" fn ESP32Reset() -> ! {
pub extern "Rust" fn __init_data() -> bool {
false
}

#[export_name = "__post_init"]
unsafe fn post_init() {
// RTC domain must be enabled before we try to disable
let mut rtc = Rtc::new(LPWR::steal());
rtc.rwdt.disable();

Wdt::<TIMG0, crate::Blocking>::set_wdt_enabled(false);
Wdt::<TIMG1, crate::Blocking>::set_wdt_enabled(false);
}
18 changes: 1 addition & 17 deletions esp-hal/src/soc/esp32s3/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@
use core::ptr::addr_of_mut;

use self::peripherals::{LPWR, TIMG0, TIMG1};
use crate::{
rtc_cntl::{Rtc, SocResetReason},
timer::timg::Wdt,
};
use crate::rtc_cntl::SocResetReason;

pub mod cpu_control;
pub mod efuse;
Expand Down Expand Up @@ -155,8 +151,6 @@ pub unsafe extern "C" fn ESP32Reset() -> ! {
stack_chk_guard.write_volatile(0xdeadbabe);
}

crate::interrupt::setup_interrupts();

// continue with default reset handler
xtensa_lx_rt::Reset();
}
Expand All @@ -170,16 +164,6 @@ pub extern "Rust" fn __init_data() -> bool {
false
}

#[export_name = "__post_init"]
unsafe fn post_init() {
// RTC domain must be enabled before we try to disable
let mut rtc = Rtc::new(LPWR::steal());
rtc.rwdt.disable();

Wdt::<TIMG0, crate::Blocking>::set_wdt_enabled(false);
Wdt::<TIMG1, crate::Blocking>::set_wdt_enabled(false);
}

/// Write back a specific range of data in the cache.
#[doc(hidden)]
#[link_section = ".rwtext"]
Expand Down
4 changes: 2 additions & 2 deletions esp-hal/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ pub fn uptime() -> fugit::Instant<u64, 1, 1_000_000> {

#[cfg(esp32)]
pub(crate) fn time_init() {
let apb = Clocks::get().apb_clock.to_Hz();
let apb = crate::Clocks::get().apb_clock.to_Hz();
// we assume 80MHz APB clock source - there is no way to configure it in a
// different way currently
assert!(apb, 80_000_000u32);
assert_eq!(apb, 80_000_000u32);

let tg0 = unsafe { crate::peripherals::TIMG0::steal() };

Expand Down

0 comments on commit befe0e0

Please sign in to comment.