Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create virtual peripherals for CPU control and radio clocks #1428

Merged
merged 4 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions esp-hal/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- Created virtual peripherals for CPU control and radio clocks, rather than splitting them from `SYSTEM` (#1428)

### Removed

## [0.17.0] - 2024-04-18
Expand Down
18 changes: 12 additions & 6 deletions esp-hal/src/soc/esp32/cpu_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
//!
//! let counter = Mutex::new(RefCell::new(0));
//!
//! let mut cpu_control = CpuControl::new(system.cpu_control);
//! let mut cpu_control = CpuControl::new(peripherals.CPU_CTRL);
//! let cpu1_fnctn = || {
//! cpu1_task(&mut timer1, &counter);
//! };
Expand Down Expand Up @@ -53,7 +53,11 @@ use core::{
mem::{ManuallyDrop, MaybeUninit},
};

use crate::Cpu;
use crate::{
peripheral::{Peripheral, PeripheralRef},
peripherals::CPU_CTRL,
Cpu,
};

/// Data type for a properly aligned stack of N bytes
// Xtensa ISA 10.5: [B]y default, the
Expand Down Expand Up @@ -125,8 +129,8 @@ pub enum Error {
}

/// Control CPU Cores
pub struct CpuControl {
_cpu_control: crate::system::CpuControl,
pub struct CpuControl<'d> {
_cpu_control: PeripheralRef<'d, CPU_CTRL>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we don't really need to store it if we don't have any use for it

}

unsafe fn internal_park_core(core: Cpu) {
Expand All @@ -153,8 +157,10 @@ unsafe fn internal_park_core(core: Cpu) {
}
}

impl CpuControl {
pub fn new(cpu_control: crate::system::CpuControl) -> CpuControl {
impl<'d> CpuControl<'d> {
pub fn new(cpu_control: impl Peripheral<P = CPU_CTRL> + 'd) -> CpuControl<'d> {
crate::into_ref!(cpu_control);

CpuControl {
_cpu_control: cpu_control,
}
Expand Down
2 changes: 2 additions & 0 deletions esp-hal/src/soc/esp32/peripherals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ crate::peripherals! {
APB_CTRL <= APB_CTRL,
BB <= BB,
BT <= virtual,
CPU_CTRL <= virtual,
DAC1 <= virtual,
DAC2 <= virtual,
DMA <= virtual,
Expand All @@ -50,6 +51,7 @@ crate::peripherals! {
RNG <= RNG,
RSA <= RSA,
LPWR <= RTC_CNTL,
RADIO_CLK <= virtual,
jessebraham marked this conversation as resolved.
Show resolved Hide resolved
RTC_IO <= RTC_IO,
RTC_I2C <= RTC_I2C,
SDHOST <= SDHOST,
Expand Down
4 changes: 2 additions & 2 deletions esp-hal/src/soc/esp32/radio_clocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
//! `RadioClockControl` struct. This trait provides methods to enable, disable,
//! reset the MAC, initialize clocks and perform other related operations.

use crate::system::{RadioClockControl, RadioClockController, RadioPeripherals};
use crate::system::{RadioClockController, RadioPeripherals};

const DPORT_WIFI_CLK_WIFI_BT_COMMON_M: u32 = 0x000003c9;
const DPORT_WIFI_CLK_WIFI_EN_M: u32 = 0x00000406;
const DPORT_WIFI_CLK_BT_EN_M: u32 = 0x00030800;

impl RadioClockController for RadioClockControl {
impl RadioClockController for crate::peripherals::RADIO_CLK {
JurajSadel marked this conversation as resolved.
Show resolved Hide resolved
fn enable(&mut self, peripheral: RadioPeripherals) {
match peripheral {
RadioPeripherals::Phy => enable_phy(),
Expand Down
1 change: 1 addition & 0 deletions esp-hal/src/soc/esp32c2/peripherals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ crate::peripherals! {
IO_MUX <= IO_MUX,
LEDC <= LEDC,
LPWR <= RTC_CNTL,
RADIO_CLK <= virtual,
RNG <= RNG,
SENSITIVE <= SENSITIVE,
SHA <= SHA,
Expand Down
4 changes: 2 additions & 2 deletions esp-hal/src/soc/esp32c2/radio_clocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
//! `RadioClockControl` struct. This trait provides methods to enable, disable,
//! reset the MAC, initialize clocks and perform other related operations.

use crate::system::{RadioClockControl, RadioClockController, RadioPeripherals};
use crate::system::{RadioClockController, RadioPeripherals};

// Mask for clock bits used by both WIFI and Bluetooth, 0, 1, 2, 3, 7, 8, 9, 10,
// 19, 20, 21, 22, 23
const SYSTEM_WIFI_CLK_WIFI_BT_COMMON_M: u32 = 0x78078F;
// SYSTEM_WIFI_CLK_EN : R/W ;bitpos:[31:0] ;default: 32'hfffce030
const SYSTEM_WIFI_CLK_EN: u32 = 0x00FB9FCF;

impl RadioClockController for RadioClockControl {
impl RadioClockController for crate::peripherals::RADIO_CLK {
fn enable(&mut self, peripheral: RadioPeripherals) {
match peripheral {
RadioPeripherals::Phy => enable_phy(),
Expand Down
1 change: 1 addition & 0 deletions esp-hal/src/soc/esp32c3/peripherals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ crate::peripherals! {
IO_MUX <= IO_MUX,
LEDC <= LEDC,
LPWR <= RTC_CNTL,
RADIO_CLK <= virtual,
RMT <= RMT,
RNG <= RNG,
RSA <= RSA,
Expand Down
4 changes: 2 additions & 2 deletions esp-hal/src/soc/esp32c3/radio_clocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
//! `RadioClockControl` struct. This trait provides methods to enable, disable,
//! reset the MAC, initialize clocks and perform other related operations.

use crate::system::{RadioClockControl, RadioClockController, RadioPeripherals};
use crate::system::{RadioClockController, RadioPeripherals};

// Mask for clock bits used by both WIFI and Bluetooth, 0, 1, 2, 3, 7, 8, 9, 10,
// 19, 20, 21, 22, 23
const SYSTEM_WIFI_CLK_WIFI_BT_COMMON_M: u32 = 0x78078F;
// SYSTEM_WIFI_CLK_EN : R/W ;bitpos:[31:0] ;default: 32'hfffce030
const SYSTEM_WIFI_CLK_EN: u32 = 0x00FB9FCF;

impl RadioClockController for RadioClockControl {
impl RadioClockController for crate::peripherals::RADIO_CLK {
fn enable(&mut self, peripheral: RadioPeripherals) {
match peripheral {
RadioPeripherals::Phy => enable_phy(),
Expand Down
1 change: 1 addition & 0 deletions esp-hal/src/soc/esp32c6/peripherals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ crate::peripherals! {
PAU <= PAU,
PCNT <= PCNT,
PMU <= PMU,
RADIO_CLK <= virtual,
RMT <= RMT,
RNG <= RNG,
RSA <= RSA,
Expand Down
4 changes: 2 additions & 2 deletions esp-hal/src/soc/esp32c6/radio_clocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
//! `RadioClockControl` struct. This trait provides methods to enable, disable,
//! reset the MAC, initialize clocks and perform other related operations.

use crate::system::{RadioClockControl, RadioClockController, RadioPeripherals};
use crate::system::{RadioClockController, RadioPeripherals};

impl RadioClockController for RadioClockControl {
impl RadioClockController for crate::peripherals::RADIO_CLK {
fn enable(&mut self, peripheral: RadioPeripherals) {
match peripheral {
RadioPeripherals::Phy => enable_phy(),
Expand Down
1 change: 1 addition & 0 deletions esp-hal/src/soc/esp32h2/peripherals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ crate::peripherals! {
PAU <= PAU,
PCNT <= PCNT,
PMU <= PMU,
RADIO_CLK <= virtual,
RMT <= RMT,
RNG <= RNG,
RSA <= RSA,
Expand Down
4 changes: 2 additions & 2 deletions esp-hal/src/soc/esp32h2/radio_clocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
//! `RadioClockControl` struct. This trait provides methods to enable, disable,
//! reset the MAC, initialize clocks and perform other related operations.

use crate::system::{RadioClockControl, RadioClockController, RadioPeripherals};
use crate::system::{RadioClockController, RadioPeripherals};

impl RadioClockController for RadioClockControl {
impl RadioClockController for crate::peripherals::RADIO_CLK {
fn enable(&mut self, peripheral: RadioPeripherals) {
match peripheral {
RadioPeripherals::Phy => enable_phy(),
Expand Down
1 change: 1 addition & 0 deletions esp-hal/src/soc/esp32s2/peripherals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ crate::peripherals! {
PCNT <= PCNT,
PMS <= PMS,
PSRAM <= virtual,
RADIO_CLK <= virtual,
RMT <= RMT,
RNG <= RNG,
RSA <= RSA,
Expand Down
4 changes: 2 additions & 2 deletions esp-hal/src/soc/esp32s2/radio_clocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
//! `RadioClockControl` struct. This trait provides methods to enable, disable,
//! reset the MAC, initialize clocks and perform other related operations.

use crate::system::{RadioClockControl, RadioClockController, RadioPeripherals};
use crate::system::{RadioClockController, RadioPeripherals};

// Mask for clock bits used by both WIFI and Bluetooth, bit 0, 3, 6, 7, 8, 9
const DPORT_WIFI_CLK_WIFI_BT_COMMON_M: u32 = 0x000003c9;
const DPORT_WIFI_CLK_WIFI_EN_M: u32 = 0x000007cf;

impl RadioClockController for RadioClockControl {
impl RadioClockController for crate::peripherals::RADIO_CLK {
fn enable(&mut self, peripheral: RadioPeripherals) {
match peripheral {
RadioPeripherals::Phy => enable_phy(),
Expand Down
18 changes: 12 additions & 6 deletions esp-hal/src/soc/esp32s3/cpu_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
//!
//! let counter = Mutex::new(RefCell::new(0));
//!
//! let mut cpu_control = CpuControl::new(system.cpu_control);
//! let mut cpu_control = CpuControl::new(peripherals.CPU_CTRL);
//! let cpu1_fnctn = || {
//! cpu1_task(&mut timer1, &counter);
//! };
Expand Down Expand Up @@ -53,7 +53,11 @@ use core::{
mem::{ManuallyDrop, MaybeUninit},
};

use crate::Cpu;
use crate::{
peripheral::{Peripheral, PeripheralRef},
peripherals::CPU_CTRL,
Cpu,
};

/// Data type for a properly aligned stack of N bytes
// Xtensa ISA 10.5: [B]y default, the
Expand Down Expand Up @@ -124,8 +128,8 @@ pub enum Error {
}

/// Control CPU Cores
pub struct CpuControl {
_cpu_control: crate::system::CpuControl,
pub struct CpuControl<'d> {
_cpu_control: PeripheralRef<'d, CPU_CTRL>,
}

unsafe fn internal_park_core(core: Cpu) {
Expand All @@ -152,8 +156,10 @@ unsafe fn internal_park_core(core: Cpu) {
}
}

impl CpuControl {
pub fn new(cpu_control: crate::system::CpuControl) -> CpuControl {
impl<'d> CpuControl<'d> {
pub fn new(cpu_control: impl Peripheral<P = CPU_CTRL> + 'd) -> CpuControl<'d> {
crate::into_ref!(cpu_control);

CpuControl {
_cpu_control: cpu_control,
}
Expand Down
2 changes: 2 additions & 0 deletions esp-hal/src/soc/esp32s3/peripherals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ crate::peripherals! {
APB_CTRL <= APB_CTRL,
ASSIST_DEBUG <= ASSIST_DEBUG,
BT <= virtual,
CPU_CTRL <= virtual,
DMA <= DMA (DMA_IN_CH0,DMA_IN_CH1,DMA_IN_CH2,DMA_IN_CH3,DMA_IN_CH4,DMA_OUT_CH0,DMA_OUT_CH1,DMA_OUT_CH2,DMA_OUT_CH3,DMA_OUT_CH4),
DS <= DS,
EFUSE <= EFUSE,
Expand All @@ -48,6 +49,7 @@ crate::peripherals! {
PSRAM <= virtual,
MCPWM0 <= MCPWM0,
MCPWM1 <= MCPWM1,
RADIO_CLK <= virtual,
RMT <= RMT,
RNG <= RNG,
RSA <= RSA,
Expand Down
5 changes: 3 additions & 2 deletions esp-hal/src/soc/esp32s3/radio_clocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
//! The module defines a `RadioClockController` trait implemented by the
//! `RadioClockControl` struct. This trait provides methods to enable, disable,
//! reset the MAC, initialize clocks and perform other related operations.
use crate::system::{RadioClockControl, RadioClockController, RadioPeripherals};

use crate::system::{RadioClockController, RadioPeripherals};

// Note: this comment has been copied from esp-idf, including the mistake.
// Mask for clock bits used by both WIFI and Bluetooth, 0, 1, 2, 3, 7,
Expand All @@ -20,7 +21,7 @@ const SYSTEM_WIFI_CLK_WIFI_BT_COMMON_M: u32 = 0x78078F;
// SYSTEM_WIFI_CLK_EN : R/W ;bitpos:[31:0] ;default: 32'hfffce030
const SYSTEM_WIFI_CLK_EN: u32 = 0x00FB9FCF;

impl RadioClockController for RadioClockControl {
impl RadioClockController for crate::peripherals::RADIO_CLK {
fn enable(&mut self, peripheral: RadioPeripherals) {
match peripheral {
RadioPeripherals::Phy => enable_phy(),
Expand Down
16 changes: 2 additions & 14 deletions esp-hal/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1077,12 +1077,8 @@ pub struct SystemClockControl {
_private: (),
}

/// Controls the configuration of the chip's clocks.
pub struct CpuControl {
_private: (),
}

/// Enumeration of the available radio peripherals for this chip.
#[cfg(any(bt, ieee802154, wifi))]
pub enum RadioPeripherals {
#[cfg(phy)]
Phy,
Expand All @@ -1094,12 +1090,8 @@ pub enum RadioPeripherals {
Ieee802154,
}

/// Functionality of clocks controlling the radio peripherals.
pub struct RadioClockControl {
_private: (),
}

/// Control the radio peripheral clocks
#[cfg(any(bt, ieee802154, wifi))]
pub trait RadioClockController {
/// Enable the peripheral
fn enable(&mut self, peripheral: RadioPeripherals);
Expand All @@ -1123,8 +1115,6 @@ pub trait RadioClockController {
pub struct SystemParts<'d> {
_private: PeripheralRef<'d, SYSTEM>,
pub clock_control: SystemClockControl,
pub cpu_control: CpuControl,
pub radio_clock_control: RadioClockControl,
pub software_interrupt_control: SoftwareInterruptControl,
}

Expand All @@ -1144,8 +1134,6 @@ impl<'d, T: crate::peripheral::Peripheral<P = SYSTEM> + 'd> SystemExt<'d> for T
Self::Parts {
_private: self.into_ref(),
clock_control: SystemClockControl { _private: () },
cpu_control: CpuControl { _private: () },
radio_clock_control: RadioClockControl { _private: () },
software_interrupt_control: SoftwareInterruptControl::new_internal(),
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/src/bin/embassy_multicore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ async fn main(_spawner: Spawner) {
let timg0 = TimerGroup::new_async(peripherals.TIMG0, &clocks);
embassy::init(&clocks, timg0);

let mut cpu_control = CpuControl::new(system.cpu_control);
let mut cpu_control = CpuControl::new(peripherals.CPU_CTRL);

let led_ctrl_signal = &*make_static!(Signal::new());

Expand Down
2 changes: 1 addition & 1 deletion examples/src/bin/embassy_multicore_interrupt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ fn main() -> ! {
let timg0 = TimerGroup::new_async(peripherals.TIMG0, &clocks);
embassy::init(&clocks, timg0);

let mut cpu_control = CpuControl::new(system.cpu_control);
let mut cpu_control = CpuControl::new(peripherals.CPU_CTRL);

let led_ctrl_signal = &*make_static!(Signal::new());

Expand Down
2 changes: 1 addition & 1 deletion examples/src/bin/multicore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fn main() -> ! {

let counter = Mutex::new(RefCell::new(0u32));

let mut cpu_control = CpuControl::new(system.cpu_control);
let mut cpu_control = CpuControl::new(peripherals.CPU_CTRL);
let _guard = cpu_control
.start_app_core(unsafe { &mut *addr_of_mut!(APP_CORE_STACK) }, || {
println!("Hello World - Core 1!");
Expand Down
Loading