Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add initial support for PCNT in ESP32-H2 (#551)
- Add initial support for RMT in ESP32-H2 (#556)
- Add a fn to poll DMA transfers
- Add initial support for LEDC in ESP32-H2 (#560)

### Changed

Expand Down
2 changes: 1 addition & 1 deletion esp-hal-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ esp32 = { version = "0.23.0", features = ["critical-section"], optional = true
esp32c2 = { version = "0.11.0", features = ["critical-section"], optional = true }
esp32c3 = { version = "0.14.0", features = ["critical-section"], optional = true }
esp32c6 = { version = "0.4.0", features = ["critical-section"], optional = true }
esp32h2 = { git = "https://github.com/esp-rs/esp-pacs", rev = "d22c06a", package = "esp32h2", features = ["critical-section"], optional = true }
esp32h2 = { git = "https://github.com/esp-rs/esp-pacs", rev = "687a383", package = "esp32h2", features = ["critical-section"], optional = true }
esp32s2 = { version = "0.14.0", features = ["critical-section"], optional = true }
esp32s3 = { version = "0.18.0", features = ["critical-section"], optional = true }

Expand Down
2 changes: 1 addition & 1 deletion esp-hal-common/devices/esp32h2.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ peripherals = [
"interrupt_core0",
"intpri",
"io_mux",
# "ledc",
"ledc",
# "lp_ana",
# "lp_aon",
# "lp_apm",
Expand Down
28 changes: 14 additions & 14 deletions esp-hal-common/src/ledc/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ pub enum Number {
Channel3,
Channel4,
Channel5,
#[cfg(not(any(esp32c2, esp32c3, esp32c6)))]
#[cfg(not(any(esp32c2, esp32c3, esp32c6, esp32h2)))]
Channel6,
#[cfg(not(any(esp32c2, esp32c3, esp32c6)))]
#[cfg(not(any(esp32c2, esp32c3, esp32c6, esp32h2)))]
Channel7,
}

Expand Down Expand Up @@ -327,7 +327,7 @@ macro_rules! start_duty_without_fading {
};
}

#[cfg(esp32c6)]
#[cfg(any(esp32c6, esp32h2))]
/// Macro to start duty cycle, without fading
macro_rules! start_duty_without_fading {
($self: ident, $num: literal) => {
Expand All @@ -350,7 +350,7 @@ macro_rules! start_duty_without_fading {
};
}

#[cfg(not(any(esp32, esp32c6)))]
#[cfg(not(any(esp32, esp32c6, esp32h2)))]
/// Macro to start duty cycle, without fading
macro_rules! start_duty_without_fading {
($self: ident, $num: literal) => {
Expand Down Expand Up @@ -392,7 +392,7 @@ macro_rules! start_duty_fade {
};
}

#[cfg(esp32c6)]
#[cfg(any(esp32c6, esp32h2))]
/// Macro to start a duty cycle fade
macro_rules! start_duty_fade {
($self: ident, $num: literal, $duty_inc: ident, $duty_steps: ident, $cycles_per_step: ident, $duty_per_cycle: ident) => {
Expand Down Expand Up @@ -423,7 +423,7 @@ macro_rules! start_duty_fade {
};
}

#[cfg(not(any(esp32, esp32c6)))]
#[cfg(not(any(esp32, esp32c6, esp32h2)))]
/// Macro to start a duty cycle fade
macro_rules! start_duty_fade {
($self: ident, $num: literal, $duty_inc: ident, $duty_steps: ident, $cycles_per_step: ident, $duty_per_cycle: ident) => {
Expand Down Expand Up @@ -852,14 +852,14 @@ where
self.output_pin
.connect_peripheral_to_output(OutputSignal::LEDC_LS_SIG5);
}
#[cfg(not(any(esp32c2, esp32c3, esp32c6)))]
#[cfg(not(any(esp32c2, esp32c3, esp32c6, esp32h2)))]
Number::Channel6 => {
set_channel!(self, l, 6, timer_number);
update_channel!(self, l, 6);
self.output_pin
.connect_peripheral_to_output(OutputSignal::LEDC_LS_SIG6);
}
#[cfg(not(any(esp32c2, esp32c3, esp32c6)))]
#[cfg(not(any(esp32c2, esp32c3, esp32c6, esp32h2)))]
Number::Channel7 => {
set_channel!(self, l, 7, timer_number);
update_channel!(self, l, 7);
Expand All @@ -883,9 +883,9 @@ where
Number::Channel3 => set_duty!(self, l, 3, duty),
Number::Channel4 => set_duty!(self, l, 4, duty),
Number::Channel5 => set_duty!(self, l, 5, duty),
#[cfg(not(any(esp32c2, esp32c3, esp32c6)))]
#[cfg(not(any(esp32c2, esp32c3, esp32c6, esp32h2)))]
Number::Channel6 => set_duty!(self, l, 6, duty),
#[cfg(not(any(esp32c2, esp32c3, esp32c6)))]
#[cfg(not(any(esp32c2, esp32c3, esp32c6, esp32h2)))]
Number::Channel7 => set_duty!(self, l, 7, duty),
};
}
Expand Down Expand Up @@ -960,7 +960,7 @@ where
cycles_per_step,
duty_per_cycle
),
#[cfg(not(any(esp32c2, esp32c3, esp32c6)))]
#[cfg(not(any(esp32c2, esp32c3, esp32c6, esp32h2)))]
Number::Channel6 => set_duty_fade!(
self,
l,
Expand All @@ -971,7 +971,7 @@ where
cycles_per_step,
duty_per_cycle
),
#[cfg(not(any(esp32c2, esp32c3, esp32c6)))]
#[cfg(not(any(esp32c2, esp32c3, esp32c6, esp32h2)))]
Number::Channel7 => set_duty_fade!(
self,
l,
Expand All @@ -993,9 +993,9 @@ where
Number::Channel3 => is_duty_fade_running!(self, l, 3),
Number::Channel4 => is_duty_fade_running!(self, l, 4),
Number::Channel5 => is_duty_fade_running!(self, l, 5),
#[cfg(not(any(esp32c2, esp32c3, esp32c6)))]
#[cfg(not(any(esp32c2, esp32c3, esp32c6, esp32h2)))]
Number::Channel6 => is_duty_fade_running!(self, l, 6),
#[cfg(not(any(esp32c2, esp32c3, esp32c6)))]
#[cfg(not(any(esp32c2, esp32c3, esp32c6, esp32h2)))]
Number::Channel7 => is_duty_fade_running!(self, l, 7),
}
}
Expand Down
9 changes: 6 additions & 3 deletions esp-hal-common/src/ledc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,22 @@ impl<'d> LEDC<'d> {
#[cfg(not(esp32))]
/// Set global slow clock source
pub fn set_global_slow_clock(&mut self, clock_source: LSGlobalClkSource) {
#[cfg(esp32c6)]
#[cfg(any(esp32c6, esp32h2))]
let pcr = unsafe { &*crate::peripherals::PCR::ptr() };

#[cfg(esp32c6)]
#[cfg(any(esp32c6, esp32h2))]
pcr.ledc_sclk_conf.write(|w| w.ledc_sclk_en().set_bit());

match clock_source {
LSGlobalClkSource::APBClk => {
#[cfg(not(esp32c6))]
#[cfg(not(any(esp32c6, esp32h2)))]
self.ledc.conf.write(|w| unsafe { w.apb_clk_sel().bits(1) });
#[cfg(esp32c6)]
pcr.ledc_sclk_conf
.write(|w| unsafe { w.ledc_sclk_sel().bits(1) });
#[cfg(esp32h2)]
pcr.ledc_sclk_conf
.write(|w| unsafe { w.ledc_sclk_sel().bits(0) });
}
}
self.ledc.timer0_conf.modify(|_, w| w.para_up().set_bit());
Expand Down
12 changes: 6 additions & 6 deletions esp-hal-common/src/soc/esp32h2/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,12 @@ pub enum InputSignal {
#[allow(non_camel_case_types)]
#[derive(PartialEq, Copy, Clone)]
pub enum OutputSignal {
LEDC_LS_SIG_OUT0 = 0,
LEDC_LS_SIG_OUT1 = 1,
LEDC_LS_SIG_OUT2 = 2,
LEDC_LS_SIG_OUT3 = 3,
LEDC_LS_SIG_OUT4 = 4,
LEDC_LS_SIG_OUT5 = 5,
LEDC_LS_SIG0 = 0,
LEDC_LS_SIG1 = 1,
LEDC_LS_SIG2 = 2,
LEDC_LS_SIG3 = 3,
LEDC_LS_SIG4 = 4,
LEDC_LS_SIG5 = 5,
U0TXD = 6,
U0RTS = 7,
U0DTR = 8,
Expand Down
2 changes: 1 addition & 1 deletion esp-hal-common/src/soc/esp32h2/peripherals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ crate::peripherals! {
INTERRUPT_CORE0 => true,
INTPRI => true,
IO_MUX => true,
// LEDC => true,
LEDC => true,
// LP_ANA => true,
// LP_AON => true,
// LP_APM => true,
Expand Down
94 changes: 94 additions & 0 deletions esp32h2-hal/examples/ledc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
//! Turns on LED with the option to change LED intensity depending on `duty`
//! value. Possible values (`u32`) are in range 0..100.
//!
//! This assumes that a LED is connected to the pin assigned to `led`. (GPIO4)

#![no_std]
#![no_main]

use esp32h2_hal::{
clock::ClockControl,
gpio::IO,
ledc::{
channel::{self, ChannelIFace},
timer::{self, TimerIFace},
LSGlobalClkSource,
LowSpeed,
LEDC,
},
peripherals::Peripherals,
prelude::*,
timer::TimerGroup,
Rtc,
};
use esp_backtrace as _;

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

// Disable the watchdog timers. For the ESP32-H2, this includes the Super WDT,
// and the TIMG WDTs.
let mut rtc = Rtc::new(peripherals.LP_CLKRST);
let timer_group0 = TimerGroup::new(
peripherals.TIMG0,
&clocks,
&mut system.peripheral_clock_control,
);
let mut wdt0 = timer_group0.wdt;
let timer_group1 = TimerGroup::new(
peripherals.TIMG1,
&clocks,
&mut system.peripheral_clock_control,
);
let mut wdt1 = timer_group1.wdt;

// Disable watchdog timers
rtc.swd.disable();
rtc.rwdt.disable();
wdt0.disable();
wdt1.disable();

let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
let led = io.pins.gpio4.into_push_pull_output();

let mut ledc = LEDC::new(
peripherals.LEDC,
&clocks,
&mut system.peripheral_clock_control,
);
ledc.set_global_slow_clock(LSGlobalClkSource::APBClk);
let mut lstimer0 = ledc.get_timer::<LowSpeed>(timer::Number::Timer2);

lstimer0
.configure(timer::config::Config {
duty: timer::config::Duty::Duty5Bit,
clock_source: timer::LSClockSource::APBClk,
frequency: 24u32.kHz(),
})
.unwrap();

let mut channel0 = ledc.get_channel(channel::Number::Channel0, led);
channel0
.configure(channel::config::Config {
timer: &lstimer0,
duty_pct: 10,
pin_config: channel::config::PinConfig::PushPull,
})
.unwrap();

channel0.start_duty_fade(0, 100, 2000).expect_err(
"Fading from 0% to 100%, at 24kHz and 5-bit resolution, over 2 seconds, should fail",
);

loop {
// Set up a breathing LED: fade from off to on over a second, then
// from on back off over the next second. Then loop.
channel0.start_duty_fade(0, 100, 1000).unwrap();
while channel0.is_duty_fade_running() {}
channel0.start_duty_fade(100, 0, 1000).unwrap();
while channel0.is_duty_fade_running() {}
}
}