Skip to content

Commit 754619f

Browse files
dalpilRahix
authored andcommitted
Adds simple_pwm for the attiny84
1 parent d32864a commit 754619f

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

mcu/attiny-hal/src/simple_pwm.rs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,82 @@ use avr_hal_generic::simple_pwm::Prescaler;
22

33
use crate::port::*;
44

5+
#[cfg(feature = "attiny84")]
6+
avr_hal_generic::impl_simple_pwm! {
7+
/// Use `TC0` for PWM (pins `PB2`, `PA7`)
8+
pub struct Timer0Pwm {
9+
timer: crate::pac::TC0,
10+
init: |tim, prescaler| {
11+
tim.tccr0a.modify(|_r, w| w.wgm0().pwm_fast());
12+
tim.tccr0b.modify(|_r, w| match prescaler {
13+
Prescaler::Direct => w.cs0().direct(),
14+
Prescaler::Prescale8 => w.cs0().prescale_8(),
15+
Prescaler::Prescale64 => w.cs0().prescale_64(),
16+
Prescaler::Prescale256 => w.cs0().prescale_256(),
17+
Prescaler::Prescale1024 => w.cs0().prescale_1024(),
18+
});
19+
},
20+
pins: {
21+
PB2: {
22+
ocr: ocr0a,
23+
into_pwm: |tim| if enable {
24+
tim.tccr0a.modify(|_r, w| w.com0a().match_clear());
25+
} else {
26+
tim.tccr0a.modify(|_r, w| w.com0a().disconnected());
27+
},
28+
},
29+
30+
PA7: {
31+
ocr: ocr0b,
32+
into_pwm: |tim| if enable {
33+
tim.tccr0a.modify(|_r, w| w.com0b().match_clear());
34+
} else {
35+
tim.tccr0a.modify(|_r, w| w.com0b().disconnected());
36+
},
37+
},
38+
},
39+
}
40+
}
41+
42+
#[cfg(feature = "attiny84")]
43+
avr_hal_generic::impl_simple_pwm! {
44+
/// Use `TC1` for PWM (pins `PA6`, 'PA5')
45+
pub struct Timer1Pwm {
46+
timer: crate::pac::TC1,
47+
init: |tim, prescaler| {
48+
tim.tccr1a.modify(|_, w| w.wgm1().bits(0b01));
49+
tim.tccr1b.modify(|_, w| w.wgm1().bits(0b01));
50+
51+
tim.tccr1b.modify(|_r, w| match prescaler {
52+
Prescaler::Direct => w.cs1().direct(),
53+
Prescaler::Prescale8 => w.cs1().prescale_8(),
54+
Prescaler::Prescale64 => w.cs1().prescale_64(),
55+
Prescaler::Prescale256 => w.cs1().prescale_256(),
56+
Prescaler::Prescale1024 => w.cs1().prescale_1024(),
57+
});
58+
},
59+
pins: {
60+
PA6: {
61+
ocr: ocr1a,
62+
into_pwm: |tim| if enable {
63+
tim.tccr1a.modify(|_, w| w.com1a().bits(0b10));
64+
} else {
65+
tim.tccr1a.modify(|_, w| w.com1a().disconnected());
66+
},
67+
},
68+
69+
PA5: {
70+
ocr: ocr1b,
71+
into_pwm: |tim| if enable {
72+
tim.tccr1a.modify(|_, w| w.com1b().bits(0b10));
73+
} else {
74+
tim.tccr1a.modify(|_, w| w.com1b().disconnected());
75+
},
76+
},
77+
},
78+
}
79+
}
80+
581
#[cfg(feature = "attiny85")]
682
avr_hal_generic::impl_simple_pwm! {
783
/// Use `TC0` for PWM (pins `PB0`, `PB1`)

0 commit comments

Comments
 (0)