Skip to content

Commit 793b01b

Browse files
authored
Move timer instance config into driver metadata (#3626)
* Remove timg_timer1 symbol * Ensure instances exist * Rename timers to timergroup * Remove unnecessary cfg
1 parent 1b5a85e commit 793b01b

File tree

17 files changed

+145
-71
lines changed

17 files changed

+145
-71
lines changed

esp-hal/build.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ fn main() -> Result<(), Box<dyn Error>> {
168168

169169
for (key, value) in &cfg {
170170
if let Value::Bool(true) = value {
171-
config_symbols.push(key);
171+
config_symbols.push(key.to_string());
172172
}
173173
}
174174

@@ -230,7 +230,7 @@ fn main() -> Result<(), Box<dyn Error>> {
230230
// Helper Functions
231231

232232
fn copy_dir_all(
233-
config_symbols: &[&str],
233+
config_symbols: &[String],
234234
cfg: &HashMap<String, Value>,
235235
src: impl AsRef<Path>,
236236
dst: impl AsRef<Path>,
@@ -260,7 +260,7 @@ fn copy_dir_all(
260260

261261
/// A naive pre-processor for linker scripts
262262
fn preprocess_file(
263-
config: &[&str],
263+
config: &[String],
264264
cfg: &HashMap<String, Value>,
265265
src: impl AsRef<Path>,
266266
dst: impl AsRef<Path>,
@@ -279,7 +279,7 @@ fn preprocess_file(
279279

280280
if let Some(condition) = trimmed.strip_prefix("#IF ") {
281281
let should_take = take.iter().all(|v| *v);
282-
let should_take = should_take && config.contains(&condition);
282+
let should_take = should_take && config.iter().any(|c| c.as_str() == condition);
283283
take.push(should_take);
284284
continue;
285285
} else if trimmed == "#ELSE" {

esp-hal/src/config.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,9 @@ pub struct WatchdogConfig {
5858
/// Configures the reset watchdog timer.
5959
rwdt: WatchdogStatus,
6060
/// Configures the `timg0` watchdog timer.
61+
#[cfg(timergroup_timg0)]
6162
timg0: WatchdogStatus,
62-
#[cfg(timg1)]
63+
#[cfg(timergroup_timg1)]
6364
/// Configures the `timg1` watchdog timer.
6465
///
6566
/// By default, the bootloader does not enable this watchdog timer.

esp-hal/src/lib.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ unstable_module! {
308308
// Drivers needed for initialization or they are tightly coupled to something else.
309309
#[cfg(any(adc1, adc2, dac))]
310310
pub mod analog;
311-
#[cfg(any(systimer, timg0, timg1))]
311+
#[cfg(any(systimer, timergroup))]
312312
pub mod timer;
313313
#[cfg(any(lp_clkrst, rtc_cntl))]
314314
pub mod rtc_cntl;
@@ -325,7 +325,6 @@ unstable_driver! {
325325
pub mod aes;
326326
#[cfg(assist_debug)]
327327
pub mod assist_debug;
328-
#[cfg(any(xtensa, all(riscv, systimer)))]
329328
pub mod delay;
330329
#[cfg(ecc)]
331330
pub mod ecc;
@@ -634,6 +633,7 @@ pub fn init(config: Config) -> Peripherals {
634633
}
635634
}
636635

636+
#[cfg(timergroup_timg0)]
637637
match config.watchdog.timg0() {
638638
WatchdogStatus::Enabled(duration) => {
639639
let mut timg0_wd = crate::timer::timg::Wdt::<crate::peripherals::TIMG0<'static>>::new();
@@ -645,7 +645,7 @@ pub fn init(config: Config) -> Peripherals {
645645
}
646646
}
647647

648-
#[cfg(timg1)]
648+
#[cfg(timergroup_timg1)]
649649
match config.watchdog.timg1() {
650650
WatchdogStatus::Enabled(duration) => {
651651
let mut timg1_wd = crate::timer::timg::Wdt::<crate::peripherals::TIMG1<'static>>::new();
@@ -664,9 +664,10 @@ pub fn init(config: Config) -> Peripherals {
664664

665665
rtc.rwdt.disable();
666666

667+
#[cfg(timergroup_timg0)]
667668
crate::timer::timg::Wdt::<crate::peripherals::TIMG0<'static>>::new().disable();
668669

669-
#[cfg(timg1)]
670+
#[cfg(timergroup_timg1)]
670671
crate::timer::timg::Wdt::<crate::peripherals::TIMG1<'static>>::new().disable();
671672
}
672673
}

esp-hal/src/system.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ impl Peripheral {
129129
Peripheral::UsbDevice,
130130
#[cfg(systimer)]
131131
Peripheral::Systimer,
132+
#[cfg(timg0)]
132133
Peripheral::Timg0,
133134
#[cfg(esp32c6)] // used by some wifi calibration steps.
134135
// TODO: We should probably automatically enable this when needed.

esp-hal/src/timer/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ use crate::{
6060

6161
#[cfg(systimer)]
6262
pub mod systimer;
63-
#[cfg(any(timg0, timg1))]
63+
#[cfg(timergroup)]
6464
pub mod timg;
6565

6666
/// Timer errors.
@@ -416,6 +416,7 @@ where
416416
crate::any_peripheral! {
417417
/// Any Timer peripheral.
418418
pub peripheral AnyTimer<'d> {
419+
#[cfg(timergroup)]
419420
TimgTimer(timg::Timer<'d>),
420421
#[cfg(systimer)]
421422
SystimerAlarm(systimer::Alarm<'d>),

esp-hal/src/timer/timg.rs

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
use core::marker::PhantomData;
7070

7171
use super::Error;
72-
#[cfg(timg1)]
72+
#[cfg(timergroup_timg1)]
7373
use crate::peripherals::TIMG1;
7474
#[cfg(any(esp32c6, esp32h2))]
7575
use crate::soc::constants::TIMG_DEFAULT_CLK_SRC;
@@ -84,21 +84,21 @@ use crate::{
8484
time::{Duration, Instant, Rate},
8585
};
8686

87-
const NUM_TIMG: usize = 1 + cfg!(timg1) as usize;
87+
const NUM_TIMG: usize = 1 + cfg!(timergroup_timg1) as usize;
8888

8989
cfg_if::cfg_if! {
9090
// We need no locks when a TIMG has a single timer, and we don't need locks for ESP32
9191
// and S2 where the effective interrupt enable register (config) is not shared between
9292
// the timers.
93-
if #[cfg(all(timg_timer1, not(any(esp32, esp32s2))))] {
93+
if #[cfg(all(timergroup_timg_has_timer1, not(any(esp32, esp32s2))))] {
9494
use crate::sync::{lock, RawMutex};
9595
static INT_ENA_LOCK: [RawMutex; NUM_TIMG] = [const { RawMutex::new() }; NUM_TIMG];
9696
}
9797
}
9898

9999
/// A timer group consisting of
100-
#[cfg_attr(not(timg_timer1), doc = "a general purpose timer")]
101-
#[cfg_attr(timg_timer1, doc = "2 timers")]
100+
#[cfg_attr(not(timergroup_timg_has_timer1), doc = "a general purpose timer")]
101+
#[cfg_attr(timergroup_timg_has_timer1, doc = "2 timers")]
102102
/// and a watchdog timer.
103103
pub struct TimerGroup<'d, T>
104104
where
@@ -108,7 +108,7 @@ where
108108
/// Timer 0
109109
pub timer0: Timer<'d>,
110110
/// Timer 1
111-
#[cfg(timg_timer1)]
111+
#[cfg(timergroup_timg_has_timer1)]
112112
pub timer1: Timer<'d>,
113113
/// Watchdog timer
114114
pub wdt: Wdt<T>,
@@ -125,6 +125,7 @@ pub trait TimerGroupInstance {
125125
fn wdt_interrupt() -> Interrupt;
126126
}
127127

128+
#[cfg(timergroup_timg0)]
128129
impl TimerGroupInstance for TIMG0<'_> {
129130
fn id() -> u8 {
130131
0
@@ -186,7 +187,7 @@ impl TimerGroupInstance for TIMG0<'_> {
186187
}
187188
}
188189

189-
#[cfg(timg1)]
190+
#[cfg(timergroup_timg1)]
190191
impl TimerGroupInstance for crate::peripherals::TIMG1<'_> {
191192
fn id() -> u8 {
192193
1
@@ -262,7 +263,7 @@ where
262263
register_block: T::register_block(),
263264
_lifetime: PhantomData,
264265
},
265-
#[cfg(timg_timer1)]
266+
#[cfg(timergroup_timg_has_timer1)]
266267
timer1: Timer {
267268
timer: 1,
268269
tg: T::id(),
@@ -325,11 +326,11 @@ impl super::Timer for Timer<'_> {
325326
fn async_interrupt_handler(&self) -> InterruptHandler {
326327
match (self.timer_group(), self.timer_number()) {
327328
(0, 0) => asynch::timg0_timer0_handler,
328-
#[cfg(timg_timer1)]
329+
#[cfg(timergroup_timg_has_timer1)]
329330
(0, 1) => asynch::timg0_timer1_handler,
330-
#[cfg(timg1)]
331+
#[cfg(timergroup_timg1)]
331332
(1, 0) => asynch::timg1_timer0_handler,
332-
#[cfg(all(timg_timer1, timg1))]
333+
#[cfg(all(timergroup_timg_has_timer1, timergroup_timg1))]
333334
(1, 1) => asynch::timg1_timer1_handler,
334335
_ => unreachable!(),
335336
}
@@ -338,11 +339,11 @@ impl super::Timer for Timer<'_> {
338339
fn peripheral_interrupt(&self) -> Interrupt {
339340
match (self.timer_group(), self.timer_number()) {
340341
(0, 0) => Interrupt::TG0_T0_LEVEL,
341-
#[cfg(timg_timer1)]
342+
#[cfg(timergroup_timg_has_timer1)]
342343
(0, 1) => Interrupt::TG0_T1_LEVEL,
343-
#[cfg(timg1)]
344+
#[cfg(timergroup_timg1)]
344345
(1, 0) => Interrupt::TG1_T0_LEVEL,
345-
#[cfg(all(timg_timer1, timg1))]
346+
#[cfg(all(timergroup_timg_has_timer1, timergroup_timg1))]
346347
(1, 1) => Interrupt::TG1_T1_LEVEL,
347348
_ => unreachable!(),
348349
}
@@ -398,11 +399,11 @@ impl Timer<'_> {
398399
pub(crate) fn set_interrupt_handler(&self, handler: InterruptHandler) {
399400
let interrupt = match (self.timer_group(), self.timer_number()) {
400401
(0, 0) => Interrupt::TG0_T0_LEVEL,
401-
#[cfg(timg_timer1)]
402+
#[cfg(timergroup_timg_has_timer1)]
402403
(0, 1) => Interrupt::TG0_T1_LEVEL,
403-
#[cfg(timg1)]
404+
#[cfg(timergroup_timg1)]
404405
(1, 0) => Interrupt::TG1_T0_LEVEL,
405-
#[cfg(all(timg_timer1, timg1))]
406+
#[cfg(all(timergroup_timg_has_timer1, timergroup_timg1))]
406407
(1, 1) => Interrupt::TG1_T1_LEVEL,
407408
_ => unreachable!(),
408409
};
@@ -560,7 +561,7 @@ impl Timer<'_> {
560561
.t(self.timer as usize)
561562
.config()
562563
.modify(|_, w| w.level_int_en().bit(state));
563-
} else if #[cfg(timg_timer1)] {
564+
} else if #[cfg(timergroup_timg_has_timer1)] {
564565
lock(&INT_ENA_LOCK[self.timer_group() as usize], || {
565566
self.register_block()
566567
.int_ena()
@@ -828,7 +829,7 @@ mod asynch {
828829
use crate::asynch::AtomicWaker;
829830

830831
const NUM_WAKERS: usize = {
831-
let timer_per_group = 1 + cfg!(timg_timer1) as usize;
832+
let timer_per_group = 1 + cfg!(timergroup_timg_has_timer1) as usize;
832833
NUM_TIMG * timer_per_group
833834
};
834835

@@ -859,7 +860,7 @@ mod asynch {
859860
});
860861
}
861862

862-
#[cfg(timg1)]
863+
#[cfg(timergroup_timg1)]
863864
#[handler]
864865
pub(crate) fn timg1_timer0_handler() {
865866
handle_irq(Timer {
@@ -870,7 +871,7 @@ mod asynch {
870871
});
871872
}
872873

873-
#[cfg(timg_timer1)]
874+
#[cfg(timergroup_timg_has_timer1)]
874875
#[handler]
875876
pub(crate) fn timg0_timer1_handler() {
876877
handle_irq(Timer {
@@ -881,7 +882,7 @@ mod asynch {
881882
});
882883
}
883884

884-
#[cfg(all(timg1, timg_timer1))]
885+
#[cfg(all(timergroup_timg1, timergroup_timg_has_timer1))]
885886
#[handler]
886887
pub(crate) fn timg1_timer1_handler() {
887888
handle_irq(Timer {

esp-metadata/devices/esp32.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ symbols = [
6767
"pdma",
6868
"phy",
6969
"psram",
70-
"timg_timer1",
7170
"touch",
7271
"large_intr_status",
7372
"gpio_bank_1",
@@ -108,6 +107,10 @@ channel_ram_size = 64
108107
[device.spi_master]
109108
status = "supported"
110109

110+
[device.timergroup]
111+
timg_has_timer1 = true
112+
instances = [{ name = "timg0" }, { name = "timg1" }]
113+
111114
[device.uart]
112115
status = "supported"
113116

@@ -146,7 +149,7 @@ status = "not_supported"
146149
[device.psram]
147150
[device.temp_sensor]
148151
[device.sleep]
149-
[device.timers]
152+
150153
[device.ulp_fsm]
151154

152155
## Radio

esp-metadata/devices/esp32c2.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ bus_timeout_is_exponential = true
8080
[device.spi_master]
8181
status = "supported"
8282

83+
[device.timergroup]
84+
instances = [{ name = "timg0" }]
85+
8386
[device.uart]
8487
status = "supported"
8588

@@ -103,7 +106,6 @@ status = "supported"
103106
[device.io_mux]
104107
[device.temp_sensor]
105108
[device.sleep]
106-
[device.timers]
107109
[device.systimer]
108110

109111
## Radio

esp-metadata/devices/esp32c3.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ channel_ram_size = 48
100100
[device.spi_master]
101101
status = "supported"
102102

103+
[device.timergroup]
104+
instances = [{ name = "timg0" }, { name = "timg1" }]
105+
103106
[device.uart]
104107
status = "supported"
105108

@@ -129,7 +132,6 @@ status = "not_supported"
129132
[device.interrupts]
130133
[device.io_mux]
131134
[device.temp_sensor]
132-
[device.timers]
133135
[device.sleep]
134136
[device.systimer]
135137

esp-metadata/devices/esp32c6.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ channel_ram_size = 48
131131
[device.spi_master]
132132
status = "supported"
133133

134+
[device.timergroup]
135+
instances = [{ name = "timg0" }, { name = "timg1" }]
136+
134137
[device.uart]
135138
status = "supported"
136139

@@ -172,7 +175,6 @@ has_wifi6 = true
172175
[device.sleep]
173176
[device.temp_sensor]
174177
[device.systimer]
175-
[device.timers]
176178
[device.ulp_riscv]
177179

178180
## Radio

0 commit comments

Comments
 (0)