Skip to content

Commit

Permalink
Don't depend on PACs directly (esp-rs#260)
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani authored and bjoernQ committed May 24, 2024
1 parent ca88f04 commit 69d5fda
Show file tree
Hide file tree
Showing 13 changed files with 119 additions and 148 deletions.
16 changes: 6 additions & 10 deletions esp-wifi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ esp32c6-hal = { workspace = true, optional = true }
esp32-hal = { workspace = true, optional = true }
esp32s3-hal = { workspace = true, optional = true }
esp32s2-hal = { workspace = true, optional = true }
esp32c3 = { workspace = true, optional = true }
esp32c2 = { workspace = true, optional = true }
esp32c6 = { workspace = true, optional = true }
smoltcp = { workspace = true, optional = true }
esp-hal-common = { workspace = true, optional = true }
critical-section.workspace = true
atomic-polyfill.workspace = true
log = { workspace = true, optional = true }
Expand All @@ -38,12 +34,12 @@ toml-cfg.workspace = true
default = [ "utils", "log" ]

# chip features
esp32c3 = [ "esp32c3-hal", "dep:esp32c3", "esp-wifi-sys/esp32c3", "esp-hal-common/esp32c3" ]
esp32c2 = [ "esp32c2-hal", "dep:esp32c2", "esp-wifi-sys/esp32c2", "esp-hal-common/esp32c2" ]
esp32c6 = [ "esp32c6-hal", "dep:esp32c6", "esp-wifi-sys/esp32c6", "esp-hal-common/esp32c6" ]
esp32 = [ "esp32-hal", "esp-wifi-sys/esp32", "esp-hal-common/esp32" ]
esp32s3 = [ "esp32s3-hal", "esp-wifi-sys/esp32s3", "esp-hal-common/esp32s3" ]
esp32s2 = [ "esp32s2-hal", "esp-wifi-sys/esp32s2", "esp-hal-common/esp32s2" ]
esp32c2 = [ "esp32c2-hal", "esp-wifi-sys/esp32c2" ]
esp32c3 = [ "esp32c3-hal", "esp-wifi-sys/esp32c3" ]
esp32c6 = [ "esp32c6-hal", "esp-wifi-sys/esp32c6" ]
esp32 = [ "esp32-hal", "esp-wifi-sys/esp32" ]
esp32s2 = [ "esp32s2-hal", "esp-wifi-sys/esp32s2" ]
esp32s3 = [ "esp32s3-hal", "esp-wifi-sys/esp32s3" ]

# async features
async = [
Expand Down
15 changes: 9 additions & 6 deletions esp-wifi/src/ble/controller/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@ use embedded_io::{
blocking::{Read, Write},
Error, Io,
};
use esp_hal_common::peripheral::{Peripheral, PeripheralRef};

use crate::hal::{
peripheral::{Peripheral, PeripheralRef},
radio,
};
use crate::panic;
use crate::EspWifiInitialization;

use super::{read_hci, read_next, send_hci};

pub struct BleConnector<'d> {
_device: PeripheralRef<'d, esp_hal_common::radio::Bluetooth>,
_device: PeripheralRef<'d, radio::Bluetooth>,
}

impl<'d> BleConnector<'d> {
pub fn new(
init: &EspWifiInitialization,
device: impl Peripheral<P = esp_hal_common::radio::Bluetooth> + 'd,
device: impl Peripheral<P = radio::Bluetooth> + 'd,
) -> BleConnector<'d> {
if !init.is_ble() {
panic!("Not initialized for BLE use");
Expand Down Expand Up @@ -89,10 +92,10 @@ pub mod asynch {

use super::BleConnectorError;
use super::{read_hci, send_hci};
use crate::hal::peripheral::{Peripheral, PeripheralRef};
use embassy_sync::waitqueue::AtomicWaker;
use embedded_io::asynch;
use embedded_io::Io;
use esp_hal_common::peripheral::{Peripheral, PeripheralRef};

static HCI_WAKER: AtomicWaker = AtomicWaker::new();

Expand All @@ -101,13 +104,13 @@ pub mod asynch {
}

pub struct BleConnector<'d> {
_device: PeripheralRef<'d, esp_hal_common::radio::Bluetooth>,
_device: PeripheralRef<'d, crate::hal::radio::Bluetooth>,
}

impl<'d> BleConnector<'d> {
pub fn new(
init: &EspWifiInitialization,
device: impl Peripheral<P = esp_hal_common::radio::Bluetooth> + 'd,
device: impl Peripheral<P = crate::hal::radio::Bluetooth> + 'd,
) -> BleConnector<'d> {
if !init.is_ble() {
panic!("Not initialized for BLE use");
Expand Down
13 changes: 1 addition & 12 deletions esp-wifi/src/common_adapter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,7 @@ use crate::trace;

use crate::compat::common::*;

#[cfg(esp32)]
use esp32_hal as hal;
#[cfg(esp32c2)]
use esp32c2_hal as hal;
#[cfg(esp32c3)]
use esp32c3_hal as hal;
#[cfg(esp32c6)]
use esp32c6_hal as hal;
#[cfg(esp32s2)]
use esp32s2_hal as hal;
#[cfg(esp32s3)]
use esp32s3_hal as hal;
use crate::hal;

use hal::system::RadioClockControl;
use hal::Rng;
Expand Down
17 changes: 7 additions & 10 deletions esp-wifi/src/esp_now/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
use core::{cell::RefCell, fmt::Debug};

use crate::hal::peripheral::{Peripheral, PeripheralRef};
use atomic_polyfill::{AtomicBool, Ordering};
use critical_section::Mutex;
use esp_hal_common::peripheral::{Peripheral, PeripheralRef};

use crate::compat::queue::SimpleQueue;
use crate::EspWifiInitialization;
Expand Down Expand Up @@ -258,8 +258,8 @@ pub struct EspNowWithWifiCreateToken {
}

pub fn enable_esp_now_with_wifi(
device: esp_hal_common::radio::Wifi,
) -> (esp_hal_common::radio::Wifi, EspNowWithWifiCreateToken) {
device: crate::hal::radio::Wifi,
) -> (crate::hal::radio::Wifi, EspNowWithWifiCreateToken) {
(device, EspNowWithWifiCreateToken { _private: () })
}

Expand All @@ -271,13 +271,13 @@ pub fn enable_esp_now_with_wifi(
/// Currently this implementation (when used together with traditional Wi-Fi) ONLY support STA mode.
///
pub struct EspNow<'d> {
_device: Option<PeripheralRef<'d, esp_hal_common::radio::Wifi>>,
_device: Option<PeripheralRef<'d, crate::hal::radio::Wifi>>,
}

impl<'d> EspNow<'d> {
pub fn new(
inited: &EspWifiInitialization,
device: impl Peripheral<P = esp_hal_common::radio::Wifi> + 'd,
device: impl Peripheral<P = crate::hal::radio::Wifi> + 'd,
) -> Result<EspNow<'d>, EspNowError> {
EspNow::new_internal(inited, Some(device.into_ref()))
}
Expand All @@ -286,15 +286,12 @@ impl<'d> EspNow<'d> {
inited: &EspWifiInitialization,
_token: EspNowWithWifiCreateToken,
) -> Result<EspNow<'d>, EspNowError> {
EspNow::new_internal(
inited,
None::<PeripheralRef<'d, esp_hal_common::radio::Wifi>>,
)
EspNow::new_internal(inited, None::<PeripheralRef<'d, crate::hal::radio::Wifi>>)
}

fn new_internal(
inited: &EspWifiInitialization,
device: Option<PeripheralRef<'d, esp_hal_common::radio::Wifi>>,
device: Option<PeripheralRef<'d, crate::hal::radio::Wifi>>,
) -> Result<EspNow<'d>, EspNowError> {
if !inited.is_wifi() {
return Err(EspNowError::Error(Error::NotInitialized));
Expand Down
2 changes: 1 addition & 1 deletion esp-wifi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ use esp32s2_hal as hal;
#[cfg(esp32s3)]
use esp32s3_hal as hal;

use crate::hal::system::RadioClockController;
use common_adapter::init_radio_clock_control;
use hal::system::RadioClockController;

use fugit::MegahertzU32;
use hal::clock::Clocks;
Expand Down
43 changes: 21 additions & 22 deletions esp-wifi/src/timer_esp32c2.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use core::cell::RefCell;

use crate::hal::interrupt::{self, TrapFrame};
use crate::hal::peripherals::{self, Interrupt};
use crate::hal::prelude::*;
use crate::hal::riscv;
use crate::hal::systimer::{Alarm, Periodic, Target};
use critical_section::Mutex;
use esp32c2 as pac;
use esp32c2_hal as hal;
use esp32c2_hal::interrupt::TrapFrame;
use esp32c2_hal::prelude::*;
use hal::peripherals::Interrupt;
use hal::systimer::{Alarm, Periodic, Target};

use crate::{binary, preempt::preempt::task_switch};
use crate::{trace, unwrap};
Expand All @@ -27,42 +26,42 @@ pub fn setup_timer_isr(systimer: Alarm<Target, 0>) {

critical_section::with(|cs| ALARM0.borrow_ref_mut(cs).replace(alarm0));

unwrap!(esp32c2_hal::interrupt::enable(
unwrap!(interrupt::enable(
Interrupt::SYSTIMER_TARGET0,
hal::interrupt::Priority::Priority1,
interrupt::Priority::Priority1,
));

#[cfg(feature = "wifi")]
unwrap!(esp32c2_hal::interrupt::enable(
unwrap!(interrupt::enable(
Interrupt::WIFI_MAC,
hal::interrupt::Priority::Priority1
interrupt::Priority::Priority1
));

#[cfg(feature = "wifi")]
unwrap!(esp32c2_hal::interrupt::enable(
unwrap!(interrupt::enable(
Interrupt::WIFI_PWR,
hal::interrupt::Priority::Priority1
interrupt::Priority::Priority1
));

#[cfg(feature = "ble")]
{
unwrap!(esp32c2_hal::interrupt::enable(
unwrap!(interrupt::enable(
Interrupt::LP_TIMER,
hal::interrupt::Priority::Priority1
interrupt::Priority::Priority1
));
unwrap!(esp32c2_hal::interrupt::enable(
unwrap!(interrupt::enable(
Interrupt::BT_MAC,
hal::interrupt::Priority::Priority1
interrupt::Priority::Priority1
));
}

unwrap!(esp32c2_hal::interrupt::enable(
unwrap!(interrupt::enable(
Interrupt::FROM_CPU_INTR3,
hal::interrupt::Priority::Priority1,
interrupt::Priority::Priority1,
));

unsafe {
esp32c2_hal::riscv::interrupt::enable();
riscv::interrupt::enable();
}

while unsafe { crate::preempt::FIRST_SWITCH.load(core::sync::atomic::Ordering::Relaxed) } {}
Expand Down Expand Up @@ -160,7 +159,7 @@ fn SYSTIMER_TARGET0(trap_frame: &mut TrapFrame) {
fn FROM_CPU_INTR3(trap_frame: &mut TrapFrame) {
unsafe {
// clear ETS_FROM_CPU_INTR3
(&*pac::SYSTEM::PTR)
(&*peripherals::SYSTEM::PTR)
.cpu_intr_from_cpu_3
.modify(|_, w| w.cpu_intr_from_cpu_3().clear_bit());
}
Expand All @@ -178,7 +177,7 @@ fn FROM_CPU_INTR3(trap_frame: &mut TrapFrame) {

pub fn yield_task() {
unsafe {
(&*pac::SYSTEM::PTR)
(&*peripherals::SYSTEM::PTR)
.cpu_intr_from_cpu_3
.modify(|_, w| w.cpu_intr_from_cpu_3().set_bit());
}
Expand All @@ -188,7 +187,7 @@ pub fn yield_task() {
/// A tick is 1 / 16_000_000 seconds
pub fn get_systimer_count() -> u64 {
critical_section::with(|_| unsafe {
let systimer = &(*pac::SYSTIMER::ptr());
let systimer = &(*peripherals::SYSTIMER::ptr());

systimer.unit0_op.write(|w| w.bits(1 << 30));

Expand Down
48 changes: 24 additions & 24 deletions esp-wifi/src/timer_esp32c3.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use core::cell::RefCell;

use critical_section::Mutex;
use esp32c3 as pac;
use esp32c3_hal as hal;
use esp32c3_hal::prelude::*;
use esp32c3_hal::trapframe::TrapFrame;
use hal::peripherals::Interrupt;
use hal::systimer::{Alarm, Periodic, Target};

use crate::hal::interrupt::{self, TrapFrame};
use crate::hal::peripherals::{self, Interrupt};
use crate::hal::prelude::*;
use crate::hal::riscv;
use crate::hal::systimer::{Alarm, Periodic, Target};

use crate::{binary, preempt::preempt::task_switch};
use crate::{trace, unwrap};
Expand All @@ -27,46 +27,46 @@ pub fn setup_timer_isr(systimer: Alarm<Target, 0>) {

critical_section::with(|cs| ALARM0.borrow_ref_mut(cs).replace(alarm0));

unwrap!(esp32c3_hal::interrupt::enable(
unwrap!(interrupt::enable(
Interrupt::SYSTIMER_TARGET0,
hal::interrupt::Priority::Priority1,
interrupt::Priority::Priority1,
));

#[cfg(feature = "wifi")]
unwrap!(esp32c3_hal::interrupt::enable(
unwrap!(interrupt::enable(
Interrupt::WIFI_MAC,
hal::interrupt::Priority::Priority1
interrupt::Priority::Priority1
));

#[cfg(feature = "wifi")]
unwrap!(esp32c3_hal::interrupt::enable(
unwrap!(interrupt::enable(
Interrupt::WIFI_PWR,
hal::interrupt::Priority::Priority1
interrupt::Priority::Priority1
));

#[cfg(feature = "ble")]
{
unwrap!(esp32c3_hal::interrupt::enable(
unwrap!(interrupt::enable(
Interrupt::RWBT,
hal::interrupt::Priority::Priority1
interrupt::Priority::Priority1
));
unwrap!(esp32c3_hal::interrupt::enable(
unwrap!(interrupt::enable(
Interrupt::RWBLE,
hal::interrupt::Priority::Priority1
interrupt::Priority::Priority1
));
unwrap!(esp32c3_hal::interrupt::enable(
unwrap!(interrupt::enable(
Interrupt::BT_BB,
hal::interrupt::Priority::Priority1
interrupt::Priority::Priority1
));
}

unwrap!(esp32c3_hal::interrupt::enable(
unwrap!(interrupt::enable(
Interrupt::FROM_CPU_INTR3,
hal::interrupt::Priority::Priority1,
interrupt::Priority::Priority1,
));

unsafe {
esp32c3_hal::riscv::interrupt::enable();
riscv::interrupt::enable();
}

while unsafe { crate::preempt::FIRST_SWITCH.load(core::sync::atomic::Ordering::Relaxed) } {}
Expand Down Expand Up @@ -171,7 +171,7 @@ fn SYSTIMER_TARGET0(trap_frame: &mut TrapFrame) {
fn FROM_CPU_INTR3(trap_frame: &mut TrapFrame) {
unsafe {
// clear FROM_CPU_INTR3
(&*pac::SYSTEM::PTR)
(&*peripherals::SYSTEM::PTR)
.cpu_intr_from_cpu_3
.modify(|_, w| w.cpu_intr_from_cpu_3().clear_bit());
}
Expand All @@ -189,7 +189,7 @@ fn FROM_CPU_INTR3(trap_frame: &mut TrapFrame) {

pub fn yield_task() {
unsafe {
(&*pac::SYSTEM::PTR)
(&*peripherals::SYSTEM::PTR)
.cpu_intr_from_cpu_3
.modify(|_, w| w.cpu_intr_from_cpu_3().set_bit());
}
Expand All @@ -199,7 +199,7 @@ pub fn yield_task() {
/// A tick is 1 / 16_000_000 seconds
pub fn get_systimer_count() -> u64 {
critical_section::with(|_| unsafe {
let systimer = &(*pac::SYSTIMER::ptr());
let systimer = &(*peripherals::SYSTIMER::ptr());

systimer.unit0_op.write(|w| w.bits(1 << 30));

Expand Down
Loading

0 comments on commit 69d5fda

Please sign in to comment.