Skip to content

Commit e6822c1

Browse files
dalpilRahix
authored andcommitted
Adds simple_pwm support for the attiny85
1 parent cd32fe7 commit e6822c1

File tree

4 files changed

+100
-2
lines changed

4 files changed

+100
-2
lines changed

arduino-hal/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ pub use delay::{delay_ms, delay_us, Delay};
113113
#[cfg(feature = "board-selected")]
114114
pub mod port;
115115

116-
#[cfg(feature = "mcu-atmega")]
116+
#[cfg(feature = "board-selected")]
117117
pub mod simple_pwm;
118118

119119
#[doc(no_inline)]

arduino-hal/src/simple_pwm.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,9 @@
77
88
pub use avr_hal_generic::simple_pwm::IntoPwmPin;
99
pub use avr_hal_generic::simple_pwm::Prescaler;
10-
pub use atmega_hal::simple_pwm::*;
10+
11+
#[cfg(feature = "mcu-atmega")]
12+
pub use atmega_hal::simple_pwm::*;
13+
14+
#[cfg(feature = "mcu-attiny")]
15+
pub use attiny_hal::simple_pwm::*;

mcu/attiny-hal/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ pub mod port;
5454
#[cfg(feature = "device-selected")]
5555
pub use port::Pins;
5656

57+
#[cfg(feature = "device-selected")]
58+
pub mod simple_pwm;
59+
5760
pub struct Attiny;
5861

5962
#[cfg(feature = "attiny84")]

mcu/attiny-hal/src/simple_pwm.rs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
use avr_hal_generic::simple_pwm::Prescaler;
2+
3+
use crate::port::*;
4+
5+
#[cfg(feature = "attiny85")]
6+
avr_hal_generic::impl_simple_pwm! {
7+
/// Use `TC0` for PWM (pins `PB0`, `PB1`)
8+
///
9+
/// # Example
10+
/// ```
11+
/// let mut timer0 = Timer0Pwm::new(dp.TC0, Prescaler::Prescale64);
12+
///
13+
/// let mut d0 = portd.d0.into_output().into_pwm(&mut timer0);
14+
/// let mut d1 = portd.d1.into_output().into_pwm(&mut timer0);
15+
///
16+
/// d0.set_duty(128);
17+
/// d0.enable();
18+
/// ```
19+
pub struct Timer0Pwm {
20+
timer: crate::pac::TC0,
21+
init: |tim, prescaler| {
22+
tim.tccr0a.modify(|_r, w| w.wgm0().pwm_fast());
23+
tim.tccr0b.modify(|_r, w| match prescaler {
24+
Prescaler::Direct => w.cs0().direct(),
25+
Prescaler::Prescale8 => w.cs0().prescale_8(),
26+
Prescaler::Prescale64 => w.cs0().prescale_64(),
27+
Prescaler::Prescale256 => w.cs0().prescale_256(),
28+
Prescaler::Prescale1024 => w.cs0().prescale_1024(),
29+
});
30+
},
31+
pins: {
32+
PB0: {
33+
ocr: ocr0a,
34+
into_pwm: |tim| if enable {
35+
tim.tccr0a.modify(|_r, w| w.com0a().match_clear());
36+
} else {
37+
tim.tccr0a.modify(|_r, w| w.com0a().disconnected());
38+
},
39+
},
40+
41+
PB1: {
42+
ocr: ocr0b,
43+
into_pwm: |tim| if enable {
44+
tim.tccr0a.modify(|_r, w| w.com0b().match_clear());
45+
} else {
46+
tim.tccr0a.modify(|_r, w| w.com0b().disconnected());
47+
},
48+
},
49+
},
50+
}
51+
}
52+
53+
#[cfg(feature = "attiny85")]
54+
avr_hal_generic::impl_simple_pwm! {
55+
/// Use `TC1` for PWM (pins `PB4`)
56+
///
57+
/// # Example
58+
/// ```
59+
/// let mut timer1 = Timer1Pwm::new(dp.TC1, Prescaler::Prescale64);
60+
///
61+
/// let mut d4 = portd.d4.into_output().into_pwm(&mut timer1);
62+
///
63+
/// d4.set_duty(128);
64+
/// d4.enable();
65+
/// ```
66+
pub struct Timer1Pwm {
67+
timer: crate::pac::TC1,
68+
init: |tim, prescaler| {
69+
tim.gtccr.modify(|_, w| w.pwm1b().bit(true));
70+
71+
tim.tccr1.modify(|_r, w| match prescaler {
72+
Prescaler::Direct => w.cs1().direct(),
73+
Prescaler::Prescale8 => w.cs1().prescale_8(),
74+
Prescaler::Prescale64 => w.cs1().prescale_64(),
75+
Prescaler::Prescale256 => w.cs1().prescale_256(),
76+
Prescaler::Prescale1024 => w.cs1().prescale_1024(),
77+
});
78+
},
79+
pins: {
80+
PB4: {
81+
ocr: ocr1b,
82+
into_pwm: |tim| if enable {
83+
tim.gtccr.modify(|_, w| w.com1b().bits(0b10));
84+
} else {
85+
tim.gtccr.modify(|_, w| w.com1b().disconnected());
86+
},
87+
},
88+
},
89+
}
90+
}

0 commit comments

Comments
 (0)