Skip to content

Commit e490872

Browse files
lopsided98Rahix
authored andcommitted
attiny-hal: add watchdog support
This has been tested on the ATtiny85, but compiles for all other parts as well and should work.
1 parent 3c02df9 commit e490872

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

mcu/attiny-hal/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ pub use port::Pins;
7676
#[cfg(feature = "device-selected")]
7777
pub mod simple_pwm;
7878

79+
#[cfg(feature = "device-selected")]
80+
pub mod wdt;
81+
#[cfg(feature = "device-selected")]
82+
pub use wdt::Wdt;
83+
7984
#[cfg(feature = "device-selected")]
8085
pub mod eeprom;
8186
#[cfg(feature = "device-selected")]

mcu/attiny-hal/src/wdt.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#[allow(unused_imports)]
2+
pub use avr_hal_generic::wdt::{Timeout, WdtOps};
3+
4+
pub type Wdt = avr_hal_generic::wdt::Wdt<crate::Attiny, crate::pac::WDT>;
5+
6+
#[cfg(any(feature = "attiny85", feature = "attiny167", feature = "attiny2313"))]
7+
avr_hal_generic::impl_wdt! {
8+
hal: crate::Attiny,
9+
peripheral: crate::pac::WDT,
10+
mcusr: crate::pac::cpu::MCUSR,
11+
wdtcsr_name: wdtcr,
12+
timeout: |to, w| match to {
13+
Timeout::Ms16 => w.wdpl().cycles_2k_512k(),
14+
Timeout::Ms32 => w.wdpl().cycles_4k_1024k(),
15+
Timeout::Ms64 => w.wdpl().cycles_8k(),
16+
Timeout::Ms125 => w.wdpl().cycles_16k(),
17+
Timeout::Ms250 => w.wdpl().cycles_32k(),
18+
Timeout::Ms500 => w.wdpl().cycles_64k(),
19+
Timeout::Ms1000 => w.wdpl().cycles_128k(),
20+
Timeout::Ms2000 => w.wdpl().cycles_256k(),
21+
Timeout::Ms4000 => w.wdph().set_bit().wdpl().cycles_2k_512k(),
22+
Timeout::Ms8000 => w.wdph().set_bit().wdpl().cycles_4k_1024k(),
23+
},
24+
}
25+
26+
#[cfg(any(feature = "attiny84", feature = "attiny88"))]
27+
avr_hal_generic::impl_wdt! {
28+
hal: crate::Attiny,
29+
peripheral: crate::pac::WDT,
30+
mcusr: crate::pac::cpu::MCUSR,
31+
wdtcsr_name: wdtcsr,
32+
timeout: |to, w| match to {
33+
Timeout::Ms16 => w.wdpl().cycles_2k_512k(),
34+
Timeout::Ms32 => w.wdpl().cycles_4k_1024k(),
35+
Timeout::Ms64 => w.wdpl().cycles_8k(),
36+
Timeout::Ms125 => w.wdpl().cycles_16k(),
37+
Timeout::Ms250 => w.wdpl().cycles_32k(),
38+
Timeout::Ms500 => w.wdpl().cycles_64k(),
39+
Timeout::Ms1000 => w.wdpl().cycles_128k(),
40+
Timeout::Ms2000 => w.wdpl().cycles_256k(),
41+
Timeout::Ms4000 => w.wdph().set_bit().wdpl().cycles_2k_512k(),
42+
Timeout::Ms8000 => w.wdph().set_bit().wdpl().cycles_4k_1024k(),
43+
},
44+
}

0 commit comments

Comments
 (0)