1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
//! # LEDC timer
//!
//! ## Overview
//! The `LEDC Timer` module is a part of the `LED Controller (LEDC)` driver
//! designed for ESP microcontrollers. It provides a high-level interface to
//! configure and control individual timers of the `LEDC` peripheral.
//!
//! The module allows precise and flexible control over timer configurations,
//! duty cycles and frequencies, making it ideal for Pulse-Width Modulation
//! (PWM) applications and LED lighting control.
use fugit::HertzU32;
#[cfg(esp32)]
use super::HighSpeed;
use super::{LowSpeed, Speed};
use crate::{clock::Clocks, peripherals::ledc};
const LEDC_TIMER_DIV_NUM_MAX: u64 = 0x3FFFF;
/// Timer errors
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Error {
/// Invalid Divisor
Divisor,
}
#[cfg(esp32)]
/// Clock source for HS Timers
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum HSClockSource {
APBClk,
// TODO RefTick,
}
/// Clock source for LS Timers
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum LSClockSource {
APBClk,
// TODO SLOWClk
}
/// Timer number
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Number {
Timer0,
Timer1,
Timer2,
Timer3,
}
/// Timer configuration
pub mod config {
use fugit::HertzU32;
/// Number of bits reserved for duty cycle adjustment
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Duty {
Duty1Bit = 1,
Duty2Bit,
Duty3Bit,
Duty4Bit,
Duty5Bit,
Duty6Bit,
Duty7Bit,
Duty8Bit,
Duty9Bit,
Duty10Bit,
Duty11Bit,
Duty12Bit,
Duty13Bit,
Duty14Bit,
#[cfg(esp32)]
Duty15Bit,
#[cfg(esp32)]
Duty16Bit,
#[cfg(esp32)]
Duty17Bit,
#[cfg(esp32)]
Duty18Bit,
#[cfg(esp32)]
Duty19Bit,
#[cfg(esp32)]
Duty20Bit,
}
/// Timer configuration
#[derive(Copy, Clone)]
pub struct Config<CS> {
pub duty: Duty,
pub clock_source: CS,
pub frequency: HertzU32,
}
}
/// Trait defining the type of timer source
pub trait TimerSpeed: Speed {
type ClockSourceType;
}
/// Timer source type for LowSpeed timers
impl TimerSpeed for LowSpeed {
type ClockSourceType = LSClockSource;
}
#[cfg(esp32)]
/// Timer source type for HighSpeed timers
impl TimerSpeed for HighSpeed {
type ClockSourceType = HSClockSource;
}
/// Interface for Timers
pub trait TimerIFace<S: TimerSpeed> {
/// Return the frequency of the timer
fn get_freq(&self) -> Option<HertzU32>;
/// Configure the timer
fn configure(&mut self, config: config::Config<S::ClockSourceType>) -> Result<(), Error>;
/// Check if the timer has been configured
fn is_configured(&self) -> bool;
/// Return the duty resolution of the timer
fn get_duty(&self) -> Option<config::Duty>;
/// Return the timer number
fn get_number(&self) -> Number;
/// Return the timer frequency, or 0 if not configured
fn get_frequency(&self) -> u32;
}
/// Interface for HW configuration of timer
pub trait TimerHW<S: TimerSpeed> {
/// Get the current source timer frequency from the HW
fn get_freq_hw(&self) -> Option<HertzU32>;
/// Configure the HW for the timer
fn configure_hw(&self, divisor: u32);
/// Update the timer in HW
fn update_hw(&self);
}
/// Timer struct
pub struct Timer<'a, S: TimerSpeed> {
ledc: &'a crate::peripherals::ledc::RegisterBlock,
clock_control_config: &'a Clocks<'a>,
number: Number,
duty: Option<config::Duty>,
frequency: u32,
configured: bool,
use_ref_tick: bool,
clock_source: Option<S::ClockSourceType>,
}
impl<'a, S: TimerSpeed> TimerIFace<S> for Timer<'a, S>
where
Timer<'a, S>: TimerHW<S>,
{
/// Return the frequency of the timer
fn get_freq(&self) -> Option<HertzU32> {
self.get_freq_hw()
}
/// Configure the timer
fn configure(&mut self, config: config::Config<S::ClockSourceType>) -> Result<(), Error> {
self.duty = Some(config.duty);
self.clock_source = Some(config.clock_source);
// TODO: we should return some error here if `unwrap()` fails
let src_freq: u32 = self.get_freq().unwrap().to_Hz();
let precision = 1 << config.duty as u32;
let frequency: u32 = config.frequency.raw();
self.frequency = frequency;
let mut divisor = ((src_freq as u64) << 8) / frequency as u64 / precision as u64;
if divisor > LEDC_TIMER_DIV_NUM_MAX {
// APB_CLK results in divisor which too high. Try using REF_TICK as clock
// source.
self.use_ref_tick = true;
divisor = (1_000_000u64 << 8) / frequency as u64 / precision as u64;
}
if !(256..LEDC_TIMER_DIV_NUM_MAX).contains(&divisor) {
return Err(Error::Divisor);
}
self.configure_hw(divisor as u32);
self.update_hw();
self.configured = true;
Ok(())
}
/// Check if the timer has been configured
fn is_configured(&self) -> bool {
self.configured
}
/// Return the duty resolution of the timer
fn get_duty(&self) -> Option<config::Duty> {
self.duty
}
/// Return the timer number
fn get_number(&self) -> Number {
self.number
}
/// Return the timer frequency
fn get_frequency(&self) -> u32 {
self.frequency
}
}
impl<'a, S: TimerSpeed> Timer<'a, S> {
/// Create a new intance of a timer
pub fn new(
ledc: &'a ledc::RegisterBlock,
clock_control_config: &'a Clocks,
number: Number,
) -> Self {
Timer {
ledc,
clock_control_config,
number,
duty: None,
frequency: 0u32,
configured: false,
use_ref_tick: false,
clock_source: None,
}
}
}
/// Timer HW implementation for LowSpeed timers
impl<'a> TimerHW<LowSpeed> for Timer<'a, LowSpeed> {
/// Get the current source timer frequency from the HW
fn get_freq_hw(&self) -> Option<fugit::HertzU32> {
self.clock_source.map(|cs| match cs {
LSClockSource::APBClk => self.clock_control_config.apb_clock,
})
}
#[cfg(esp32)]
/// Configure the HW for the timer
fn configure_hw(&self, divisor: u32) {
let duty = unwrap!(self.duty) as u8;
let use_apb = !self.use_ref_tick;
match self.number {
Number::Timer0 => self.ledc.lstimer0_conf().modify(|_, w| unsafe {
w.tick_sel()
.bit(use_apb)
.rst()
.clear_bit()
.pause()
.clear_bit()
.div_num()
.bits(divisor)
.duty_res()
.bits(duty)
}),
Number::Timer1 => self.ledc.lstimer1_conf().modify(|_, w| unsafe {
w.tick_sel()
.bit(use_apb)
.rst()
.clear_bit()
.pause()
.clear_bit()
.div_num()
.bits(divisor)
.duty_res()
.bits(duty)
}),
Number::Timer2 => self.ledc.lstimer2_conf().modify(|_, w| unsafe {
w.tick_sel()
.bit(use_apb)
.rst()
.clear_bit()
.pause()
.clear_bit()
.div_num()
.bits(divisor)
.duty_res()
.bits(duty)
}),
Number::Timer3 => self.ledc.lstimer3_conf().modify(|_, w| unsafe {
w.tick_sel()
.bit(use_apb)
.rst()
.clear_bit()
.pause()
.clear_bit()
.div_num()
.bits(divisor)
.duty_res()
.bits(duty)
}),
};
}
#[cfg(not(esp32))]
/// Configure the HW for the timer
fn configure_hw(&self, divisor: u32) {
let duty = unwrap!(self.duty) as u8;
let use_ref_tick = self.use_ref_tick;
match self.number {
Number::Timer0 => self.ledc.timer0_conf().modify(|_, w| unsafe {
w.tick_sel()
.bit(use_ref_tick)
.rst()
.clear_bit()
.pause()
.clear_bit()
.clk_div()
.bits(divisor)
.duty_res()
.bits(duty)
}),
Number::Timer1 => self.ledc.timer1_conf().modify(|_, w| unsafe {
w.tick_sel()
.bit(use_ref_tick)
.rst()
.clear_bit()
.pause()
.clear_bit()
.clk_div()
.bits(divisor)
.duty_res()
.bits(duty)
}),
Number::Timer2 => self.ledc.timer2_conf().modify(|_, w| unsafe {
w.tick_sel()
.bit(use_ref_tick)
.rst()
.clear_bit()
.pause()
.clear_bit()
.clk_div()
.bits(divisor)
.duty_res()
.bits(duty)
}),
Number::Timer3 => self.ledc.timer3_conf().modify(|_, w| unsafe {
w.tick_sel()
.bit(use_ref_tick)
.rst()
.clear_bit()
.pause()
.clear_bit()
.clk_div()
.bits(divisor)
.duty_res()
.bits(duty)
}),
};
}
#[cfg(esp32)]
/// Update the timer in HW
fn update_hw(&self) {
match self.number {
Number::Timer0 => self
.ledc
.lstimer0_conf()
.modify(|_, w| w.para_up().set_bit()),
Number::Timer1 => self
.ledc
.lstimer1_conf()
.modify(|_, w| w.para_up().set_bit()),
Number::Timer2 => self
.ledc
.lstimer2_conf()
.modify(|_, w| w.para_up().set_bit()),
Number::Timer3 => self
.ledc
.lstimer3_conf()
.modify(|_, w| w.para_up().set_bit()),
};
}
#[cfg(not(esp32))]
/// Update the timer in HW
fn update_hw(&self) {
match self.number {
Number::Timer0 => self.ledc.timer0_conf().modify(|_, w| w.para_up().set_bit()),
Number::Timer1 => self.ledc.timer1_conf().modify(|_, w| w.para_up().set_bit()),
Number::Timer2 => self.ledc.timer2_conf().modify(|_, w| w.para_up().set_bit()),
Number::Timer3 => self.ledc.timer3_conf().modify(|_, w| w.para_up().set_bit()),
};
}
}
#[cfg(esp32)]
/// Timer HW implementation for HighSpeed timers
impl<'a> TimerHW<HighSpeed> for Timer<'a, HighSpeed> {
/// Get the current source timer frequency from the HW
fn get_freq_hw(&self) -> Option<HertzU32> {
self.clock_source.map(|cs| match cs {
// TODO RefTick HSClockSource::RefTick => self.clock_control_config.apb_clock,
HSClockSource::APBClk => self.clock_control_config.apb_clock,
})
}
/// Configure the HW for the timer
fn configure_hw(&self, divisor: u32) {
let duty = unwrap!(self.duty) as u8;
let sel_hstimer = self.clock_source == Some(HSClockSource::APBClk);
match self.number {
Number::Timer0 => self.ledc.hstimer0_conf().modify(|_, w| unsafe {
w.tick_sel()
.bit(sel_hstimer)
.rst()
.clear_bit()
.pause()
.clear_bit()
.div_num()
.bits(divisor)
.duty_res()
.bits(duty)
}),
Number::Timer1 => self.ledc.hstimer1_conf().modify(|_, w| unsafe {
w.tick_sel()
.bit(sel_hstimer)
.rst()
.clear_bit()
.pause()
.clear_bit()
.div_num()
.bits(divisor)
.duty_res()
.bits(duty)
}),
Number::Timer2 => self.ledc.hstimer2_conf().modify(|_, w| unsafe {
w.tick_sel()
.bit(sel_hstimer)
.rst()
.clear_bit()
.pause()
.clear_bit()
.div_num()
.bits(divisor)
.duty_res()
.bits(duty)
}),
Number::Timer3 => self.ledc.hstimer3_conf().modify(|_, w| unsafe {
w.tick_sel()
.bit(sel_hstimer)
.rst()
.clear_bit()
.pause()
.clear_bit()
.div_num()
.bits(divisor)
.duty_res()
.bits(duty)
}),
};
}
/// Update the timer in HW
fn update_hw(&self) {
// Nothing to do for HS timers
}
}