esp-hal/esp32-hal/examples/ledc.rs
Scott Mabin be184a552d
critical_section implementations & esp_backtrace (#151)
* CS impl

* use CS Mutex in C3 examples

* use CS Mutex in S2 examples

* Update esp32 example

* run fmt

* Update S3 examples

* Remove uses of unsafe where no longer required

* use esp_backtrace in examples

* fix import & fmt once more

* Bump MSRV to 1.60.0

Co-authored-by: Jesse Braham <jesse@beta7.io>
2022-08-22 20:02:28 +01:00

71 lines
1.8 KiB
Rust

//! Turns on LED with the option to change LED intensity depending on `duty`
//! value.
//!
//! This assumes that a LED is connected to the pin assigned to `led`. (GPIO4)
#![no_std]
#![no_main]
use core::fmt::Write;
use esp32_hal::{
clock::ClockControl,
gpio::IO,
ledc::{
channel::{self, ChannelIFace},
timer::{self, TimerIFace},
HighSpeed,
LEDC,
},
pac::Peripherals,
prelude::*,
timer::TimerGroup,
Rtc,
Serial,
};
use esp_backtrace as _;
use xtensa_lx_rt::entry;
#[entry]
fn main() -> ! {
let peripherals = Peripherals::take().unwrap();
let mut system = peripherals.DPORT.split();
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks);
let mut wdt = timer_group0.wdt;
let mut serial0 = Serial::new(peripherals.UART0);
let mut rtc = Rtc::new(peripherals.RTC_CNTL);
// Disable watchdog timer
wdt.disable();
rtc.rwdt.disable();
let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
let led = io.pins.gpio4.into_push_pull_output();
writeln!(serial0, "\nESP32 Started\n\n").unwrap();
let ledc = LEDC::new(&clocks, &mut system.peripheral_clock_control);
let mut hstimer0 = ledc.get_timer::<HighSpeed>(timer::Number::Timer0);
hstimer0
.configure(timer::config::Config {
duty: timer::config::Duty::Duty5Bit,
clock_source: timer::HSClockSource::APBClk,
frequency: 24u32.kHz(),
})
.unwrap();
let mut channel0 = ledc.get_channel(channel::Number::Channel0, led);
channel0
.configure(channel::config::Config {
timer: &hstimer0,
duty_pct: 0.1,
})
.unwrap();
loop {}
}