Skip to content

Commit a919f1f

Browse files
omunztronje
authored andcommitted
atmega164pa: Add simple_pwm implementation
1 parent 480f3cd commit a919f1f

File tree

1 file changed

+92
-1
lines changed

1 file changed

+92
-1
lines changed

mcu/atmega-hal/src/simple_pwm.rs

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pub use avr_hal_generic::simple_pwm::{PwmPinOps, Prescaler};
1+
pub use avr_hal_generic::simple_pwm::{IntoPwmPin, PwmPinOps, Prescaler};
22

33
#[allow(unused_imports)]
44
use crate::port::*;
@@ -1088,3 +1088,94 @@ avr_hal_generic::impl_simple_pwm! {
10881088
},
10891089
}
10901090
}
1091+
1092+
#[cfg(any(feature = "atmega164pa"))]
1093+
avr_hal_generic::impl_simple_pwm! {
1094+
/// Use `TC0` for PWM (pins `PB3`)
1095+
///
1096+
/// # Example
1097+
/// ```
1098+
/// let mut timer0 = Timer0Pwm::new(dp.TC0, Prescaler::Prescale64);
1099+
///
1100+
/// let mut b3 = pins.pb3.into_output().into_pwm(&mut timer0);
1101+
///
1102+
/// b3.set_duty(128);
1103+
/// b3.enable();
1104+
/// ```
1105+
pub struct Timer0Pwm {
1106+
timer: crate::pac::TC0,
1107+
init: |tim, prescaler| {
1108+
tim.tccr0a.modify(|_r, w| w.wgm0().bits(0b11));
1109+
tim.tccr0a.modify(|_r, w| w.com0a().bits(0b00));
1110+
1111+
tim.tccr0b.modify(|_r, w| match prescaler {
1112+
Prescaler::Direct => w.cs0().running_no_prescaling(),
1113+
Prescaler::Prescale8 => w.cs0().running_clk_8(),
1114+
Prescaler::Prescale64 => w.cs0().running_clk_64(),
1115+
Prescaler::Prescale256 => w.cs0().running_clk_256(),
1116+
Prescaler::Prescale1024 => w.cs0().running_clk_1024(),
1117+
});
1118+
},
1119+
pins: {
1120+
PB3: {
1121+
ocr: ocr0a,
1122+
into_pwm: |tim| if enable {
1123+
tim.tccr0a.modify(|_r, w| w.com0a().bits(0b11));
1124+
} else {
1125+
tim.tccr0a.modify(|_r, w| w.com0a().bits(0b00));
1126+
},
1127+
},
1128+
},
1129+
}
1130+
}
1131+
1132+
#[cfg(any(feature = "atmega164pa"))]
1133+
avr_hal_generic::impl_simple_pwm! {
1134+
/// Use `TC1` for PWM (pins `PD4`, `PD5`)
1135+
///
1136+
/// # Example
1137+
/// ```
1138+
/// let mut timer1 = Timer1Pwm::new(dp.TC1, Prescaler::Prescale64);
1139+
///
1140+
/// let mut d4 = pins.pd4.into_output().into_pwm(&mut timer1);
1141+
/// let mut d5 = pins.pd5.into_output().into_pwm(&mut timer1);
1142+
///
1143+
/// d4.set_duty(128);
1144+
/// d4.enable();
1145+
/// d5.set_duty(64);
1146+
/// d5.enable();
1147+
/// ```
1148+
pub struct Timer1Pwm {
1149+
timer: crate::pac::TC1,
1150+
init: |tim, prescaler| {
1151+
tim.tccr1a.modify(|_r, w| w.wgm1().bits(0b01));
1152+
tim.tccr1a.modify(|_r, w| w.com1a().bits(0b00));
1153+
tim.tccr1a.modify(|_r, w| w.com1b().bits(0b00));
1154+
tim.tccr1b.modify(|_r, w| match prescaler {
1155+
Prescaler::Direct => w.cs1().running_no_prescaling(),
1156+
Prescaler::Prescale8 => w.cs1().running_clk_8(),
1157+
Prescaler::Prescale64 => w.cs1().running_clk_64(),
1158+
Prescaler::Prescale256 => w.cs1().running_clk_256(),
1159+
Prescaler::Prescale1024 => w.cs1().running_clk_1024(),
1160+
});
1161+
},
1162+
pins: {
1163+
PD4: {
1164+
ocr: ocr1a,
1165+
into_pwm: |tim| if enable {
1166+
tim.tccr1a.modify(|_r, w| w.com1a().bits(0b11));
1167+
} else {
1168+
tim.tccr1a.modify(|_r, w| w.com1a().bits(0b00));
1169+
},
1170+
},
1171+
PD5: {
1172+
ocr: ocr1b,
1173+
into_pwm: |tim| if enable {
1174+
tim.tccr1a.modify(|_r, w| w.com1b().bits(0b11));
1175+
} else {
1176+
tim.tccr1a.modify(|_r, w| w.com1b().bits(0b00));
1177+
},
1178+
},
1179+
},
1180+
}
1181+
}

0 commit comments

Comments
 (0)