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

Remove the SystemExt trait and rename SystemParts to SystemControl #1495

Merged
merged 6 commits into from
Apr 23, 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
8 changes: 5 additions & 3 deletions esp-hal/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- i2c: i2c1_handler used I2C0 register block by mistake (#1487)
- Smart LEDs docs example (#1504)
- Removed ESP32 specific code for resolutions > 16 bit in ledc embedded_hal::pwm max_duty_cycle function. (#1441)
- Fixed division by zero in ledc embedded_hal::pwm set_duty_cycle function and converted to set_duty_hw instead of set_duty to eliminate loss of granularity. (#1441)

### Changed

Expand All @@ -23,9 +24,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `IO`, `ADC`, `DAC`, `RTC*`, `LEDC`, `PWM` and `PCNT` drivers have been converted to camel case format (#1473)
- RNG is no longer TRNG, the `CryptoRng` implementation has been removed. To track this being re-added see #1499 (#1498)
- Make software interrupts shareable (#1500)
- The `SystemParts` struct has been renamed to `SystemControl`, and now has a constructor which takes the `SYSTEM` peripheral (#1495)

### Removed

- Removed the `SystemExt` trait (#1495)

## [0.17.0] - 2024-04-18

### Added
Expand Down Expand Up @@ -62,8 +66,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed writes to SPI not flushing before attempting to write, causing corrupted writes (#1381)
- fix AdcConfig::adc_calibrate for xtensa targets (#1379)
- Fixed a divide by zero panic when setting the LEDC duty cycle to 0 with `SetDutyCycle::set_duty_cycle` (#1403)
- Fix for issue #1419. Removed ESP32 specific code for resolutions > 16 bit in ledc embedded_hal::pwm max_duty_cycle function.
- Fix for issue #1419. Fixed division by zero in ledc embedded_hal::pwm set_duty_cycle function and converted to set_duty_hw instead of set_duty to eliminate loss of granularity.
- Support 192 and 256-bit keys for AES (#1316)
- Fixed MCPWM DeadTimeCfg bit values (#1378)
- ESP32 LEDC `set_duty_cycle` used HighSpeedChannel for LowSpeedChannel (#1457)
Expand Down
2 changes: 0 additions & 2 deletions esp-hal/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ pub use crate::ledc::{
},
timer::{TimerHW as _esp_hal_ledc_timer_TimerHW, TimerIFace as _esp_hal_ledc_timer_TimerIFace},
};
#[cfg(any(dport, pcr, system))]
pub use crate::system::SystemExt as _esp_hal_system_SystemExt;
#[cfg(any(timg0, timg1))]
pub use crate::timer::{
Instance as _esp_hal_timer_Instance,
Expand Down
99 changes: 52 additions & 47 deletions esp-hal/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
//! ## Example
//! ```no_run
//! let peripherals = Peripherals::take();
//! let system = peripherals.SYSTEM.split();
//! let system = SystemControl::new(peripherals.SYSTEM);
//! let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
//! ```

Expand Down Expand Up @@ -99,9 +99,30 @@ pub enum Peripheral {
LcdCam,
}

/// The `DPORT`/`PCR`/`SYSTEM` peripheral split into its different logical
/// components.
pub struct SystemControl<'d> {
_inner: PeripheralRef<'d, SYSTEM>,
pub clock_control: SystemClockControl,
pub software_interrupt_control: SoftwareInterruptControl,
}

impl<'d> SystemControl<'d> {
/// Construct a new instance of [`SystemControl`].
pub fn new(system: impl crate::peripheral::Peripheral<P = SYSTEM> + 'd) -> Self {
crate::into_ref!(system);

Self {
_inner: system,
clock_control: SystemClockControl::new(),
software_interrupt_control: SoftwareInterruptControl::new(),
}
}
}

/// A software interrupt can be triggered by software.
#[non_exhaustive]
pub struct SoftwareInterrupt<const NUM: u8> {}
pub struct SoftwareInterrupt<const NUM: u8>;

impl<const NUM: u8> SoftwareInterrupt<NUM> {
/// Sets the interrupt handler for this software-interrupt
Expand Down Expand Up @@ -192,7 +213,7 @@ impl<const NUM: u8> SoftwareInterrupt<NUM> {
/// time.
#[inline]
pub unsafe fn steal() -> Self {
Self {}
Self
}
}

Expand Down Expand Up @@ -221,17 +242,17 @@ pub struct SoftwareInterruptControl {
}

impl SoftwareInterruptControl {
fn new_internal() -> Self {
fn new() -> Self {
// the thread-executor uses SW-INT0 when used on a multi-core system
// we cannot easily require `software_interrupt0` there since it's created
// before `main` via proc-macro

SoftwareInterruptControl {
#[cfg(not(all(feature = "embassy-executor-thread", multi_core)))]
software_interrupt0: SoftwareInterrupt {},
software_interrupt1: SoftwareInterrupt {},
software_interrupt2: SoftwareInterrupt {},
software_interrupt3: SoftwareInterrupt {},
software_interrupt0: SoftwareInterrupt,
software_interrupt1: SoftwareInterrupt,
software_interrupt2: SoftwareInterrupt,
software_interrupt3: SoftwareInterrupt,
}
}
}
Expand Down Expand Up @@ -1077,6 +1098,29 @@ pub struct SystemClockControl {
_private: (),
}

impl SystemClockControl {
pub fn new() -> Self {
Self { _private: () }
}
}

impl Default for SystemClockControl {
fn default() -> Self {
Self::new()
}
}

impl crate::peripheral::Peripheral for SystemClockControl {
type P = SystemClockControl;

#[inline]
unsafe fn clone_unchecked(&mut self) -> Self::P {
SystemClockControl { _private: () }
}
}

impl crate::private::Sealed for SystemClockControl {}

/// Enumeration of the available radio peripherals for this chip.
#[cfg(any(bt, ieee802154, wifi))]
pub enum RadioPeripherals {
Expand Down Expand Up @@ -1110,42 +1154,3 @@ pub trait RadioClockController {

fn reset_rpa(&mut self);
}

/// The SYSTEM/DPORT splitted into it's different logical parts.
pub struct SystemParts<'d> {
_private: PeripheralRef<'d, SYSTEM>,
pub clock_control: SystemClockControl,
pub software_interrupt_control: SoftwareInterruptControl,
}

/// Extension trait to split a SYSTEM/DPORT peripheral in independent logical
/// parts
pub trait SystemExt<'d> {
type Parts;

/// Splits the SYSTEM/DPORT peripheral into it's parts.
fn split(self) -> Self::Parts;
}

impl<'d, T: crate::peripheral::Peripheral<P = SYSTEM> + 'd> SystemExt<'d> for T {
type Parts = SystemParts<'d>;

fn split(self) -> Self::Parts {
Self::Parts {
_private: self.into_ref(),
clock_control: SystemClockControl { _private: () },
software_interrupt_control: SoftwareInterruptControl::new_internal(),
}
}
}

impl crate::peripheral::Peripheral for SystemClockControl {
type P = SystemClockControl;

#[inline]
unsafe fn clone_unchecked(&mut self) -> Self::P {
SystemClockControl { _private: () }
}
}

impl crate::private::Sealed for SystemClockControl {}
3 changes: 2 additions & 1 deletion examples/src/bin/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ use esp_hal::{
gpio::Io,
peripherals::Peripherals,
prelude::*,
system::SystemControl,
};
use esp_println::println;

#[entry]
fn main() -> ! {
let peripherals = Peripherals::take();
let system = peripherals.SYSTEM.split();
let system = SystemControl::new(peripherals.SYSTEM);
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();

let io = Io::new(peripherals.GPIO, peripherals.IO_MUX);
Expand Down
3 changes: 2 additions & 1 deletion examples/src/bin/adc_cal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ use esp_hal::{
gpio::Io,
peripherals::Peripherals,
prelude::*,
system::SystemControl,
};
use esp_println::println;

#[entry]
fn main() -> ! {
let peripherals = Peripherals::take();
let system = peripherals.SYSTEM.split();
let system = SystemControl::new(peripherals.SYSTEM);
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();

let io = Io::new(peripherals.GPIO, peripherals.IO_MUX);
Expand Down
3 changes: 2 additions & 1 deletion examples/src/bin/advanced_serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use esp_hal::{
gpio::Io,
peripherals::Peripherals,
prelude::*,
system::SystemControl,
uart::{config::Config, TxRxPins, Uart},
};
use esp_println::println;
Expand All @@ -27,7 +28,7 @@ use nb::block;
#[entry]
fn main() -> ! {
let peripherals = Peripherals::take();
let system = peripherals.SYSTEM.split();
let system = SystemControl::new(peripherals.SYSTEM);
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();

let io = Io::new(peripherals.GPIO, peripherals.IO_MUX);
Expand Down
11 changes: 9 additions & 2 deletions examples/src/bin/blinky.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,19 @@
#![no_main]

use esp_backtrace as _;
use esp_hal::{clock::ClockControl, delay::Delay, gpio::Io, peripherals::Peripherals, prelude::*};
use esp_hal::{
clock::ClockControl,
delay::Delay,
gpio::Io,
peripherals::Peripherals,
prelude::*,
system::SystemControl,
};

#[entry]
fn main() -> ! {
let peripherals = Peripherals::take();
let system = peripherals.SYSTEM.split();
let system = SystemControl::new(peripherals.SYSTEM);
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();

// Set GPIO0 as an output, and set its state high initially.
Expand Down
3 changes: 2 additions & 1 deletion examples/src/bin/blinky_erased_pins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ use esp_hal::{
gpio::{AnyPin, Input, Io, Output, PullDown, PushPull},
peripherals::Peripherals,
prelude::*,
system::SystemControl,
};

#[entry]
fn main() -> ! {
let peripherals = Peripherals::take();
let system = peripherals.SYSTEM.split();
let system = SystemControl::new(peripherals.SYSTEM);
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();

let io = Io::new(peripherals.GPIO, peripherals.IO_MUX);
Expand Down
10 changes: 8 additions & 2 deletions examples/src/bin/clock_monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,21 @@ use core::cell::RefCell;

use critical_section::Mutex;
use esp_backtrace as _;
use esp_hal::{clock::ClockControl, peripherals::Peripherals, prelude::*, rtc_cntl::Rtc};
use esp_hal::{
clock::ClockControl,
peripherals::Peripherals,
prelude::*,
rtc_cntl::Rtc,
system::SystemControl,
};
use esp_println::println;

static RTC: Mutex<RefCell<Option<Rtc>>> = Mutex::new(RefCell::new(None));

#[entry]
fn main() -> ! {
let peripherals = Peripherals::take();
let system = peripherals.SYSTEM.split();
let system = SystemControl::new(peripherals.SYSTEM);
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();

let mut rtc = Rtc::new(peripherals.LPWR, Some(interrupt_handler));
Expand Down
3 changes: 2 additions & 1 deletion examples/src/bin/crc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ use esp_hal::{
peripherals::Peripherals,
prelude::*,
rom::{crc, md5},
system::SystemControl,
uart::Uart,
};

#[entry]
fn main() -> ! {
let peripherals = Peripherals::take();
let system = peripherals.SYSTEM.split();
let system = SystemControl::new(peripherals.SYSTEM);
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();

let delay = Delay::new(&clocks);
Expand Down
3 changes: 2 additions & 1 deletion examples/src/bin/dac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ use esp_hal::{
gpio::Io,
peripherals::Peripherals,
prelude::*,
system::SystemControl,
};

#[entry]
fn main() -> ! {
let peripherals = Peripherals::take();
let system = peripherals.SYSTEM.split();
let system = SystemControl::new(peripherals.SYSTEM);
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();

let io = Io::new(peripherals.GPIO, peripherals.IO_MUX);
Expand Down
3 changes: 2 additions & 1 deletion examples/src/bin/debug_assist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use esp_hal::{
clock::ClockControl,
peripherals::Peripherals,
prelude::*,
system::SystemControl,
};
use esp_println::println;

Expand All @@ -24,7 +25,7 @@ static DA: Mutex<RefCell<Option<DebugAssist>>> = Mutex::new(RefCell::new(None));
#[entry]
fn main() -> ! {
let peripherals = Peripherals::take();
let system = peripherals.SYSTEM.split();
let system = SystemControl::new(peripherals.SYSTEM);
let _clocks = ClockControl::boot_defaults(system.clock_control).freeze();

let mut da = DebugAssist::new(peripherals.ASSIST_DEBUG, Some(interrupt_handler));
Expand Down
4 changes: 2 additions & 2 deletions examples/src/bin/direct_vectoring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use esp_hal::{
interrupt::{self, CpuInterrupt, Priority},
peripherals::{Interrupt, Peripherals},
prelude::*,
system::SoftwareInterrupt,
system::{SoftwareInterrupt, SystemControl},
};

static SWINT0: Mutex<RefCell<Option<SoftwareInterrupt<0>>>> = Mutex::new(RefCell::new(None));
Expand All @@ -28,7 +28,7 @@ fn main() -> ! {
}
let sw0_trigger_addr = cpu_intr.cpu_intr_from_cpu_0() as *const _ as u32;

let system = peripherals.SYSTEM.split();
let system = SystemControl::new(peripherals.SYSTEM);
let sw_int = system.software_interrupt_control;

critical_section::with(|cs| {
Expand Down
3 changes: 2 additions & 1 deletion examples/src/bin/embassy_hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use esp_hal::{
embassy::{self},
peripherals::Peripherals,
prelude::*,
system::SystemControl,
timer::TimerGroup,
};

Expand All @@ -33,7 +34,7 @@ async fn run() {
async fn main(spawner: Spawner) {
esp_println::println!("Init!");
let peripherals = Peripherals::take();
let system = peripherals.SYSTEM.split();
let system = SystemControl::new(peripherals.SYSTEM);
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();

let timg0 = TimerGroup::new_async(peripherals.TIMG0, &clocks);
Expand Down
3 changes: 2 additions & 1 deletion examples/src/bin/embassy_hello_world_systimer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use esp_hal::{
embassy,
peripherals::Peripherals,
prelude::*,
system::SystemControl,
systimer::SystemTimer,
};

Expand All @@ -35,7 +36,7 @@ async fn run() {
async fn main(spawner: Spawner) {
esp_println::println!("Init!");
let peripherals = Peripherals::take();
let system = peripherals.SYSTEM.split();
let system = SystemControl::new(peripherals.SYSTEM);
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();

let systimer = SystemTimer::new_async(peripherals.SYSTIMER);
Expand Down
Loading
Loading