1use core::marker::PhantomData;
4
5use embassy_hal_internal::Peri;
6
7use super::timer::Timer;
8#[cfg(not(any(lptim_v2a, lptim_v2b)))]
9use super::OutputPin;
10#[cfg(any(lptim_v2a, lptim_v2b))]
11use super::{channel::Channel, timer::ChannelDirection, Channel1Pin, Channel2Pin};
12use super::{BasicInstance, Instance};
13#[cfg(gpio_v2)]
14use crate::gpio::Pull;
15use crate::gpio::{AfType, AnyPin, OutputType, Speed};
16use crate::time::Hertz;
17
18pub enum Output {}
20pub enum Ch1 {}
22pub enum Ch2 {}
24
25pub struct PwmPin<'d, T, C> {
29 _pin: Peri<'d, AnyPin>,
30 phantom: PhantomData<(T, C)>,
31}
32
33pub struct PwmPinConfig {
37 pub output_type: OutputType,
39 pub speed: Speed,
41 #[cfg(gpio_v2)]
43 pub pull: Pull,
44}
45
46macro_rules! channel_impl {
47 ($new_chx:ident, $new_chx_with_config:ident, $channel:ident, $pin_trait:ident) => {
48 impl<'d, T: BasicInstance> PwmPin<'d, T, $channel> {
49 #[doc = concat!("Create a new ", stringify!($channel), " PWM pin instance.")]
50 pub fn $new_chx(pin: Peri<'d, impl $pin_trait<T>>) -> Self {
51 critical_section::with(|_| {
52 pin.set_low();
53 pin.set_as_af(
54 pin.af_num(),
55 AfType::output(OutputType::PushPull, Speed::VeryHigh),
56 );
57 });
58 PwmPin {
59 _pin: pin.into(),
60 phantom: PhantomData,
61 }
62 }
63 #[doc = concat!("Create a new ", stringify!($channel), " PWM pin instance with config.")]
64 pub fn $new_chx_with_config(pin: Peri<'d, impl $pin_trait<T>>, pin_config: PwmPinConfig) -> Self {
65 critical_section::with(|_| {
66 pin.set_low();
67 pin.set_as_af(
68 pin.af_num(),
69 #[cfg(gpio_v1)]
70 AfType::output(pin_config.output_type, pin_config.speed),
71 #[cfg(gpio_v2)]
72 AfType::output_pull(pin_config.output_type, pin_config.speed, pin_config.pull),
73 );
74 });
75 PwmPin {
76 _pin: pin.into(),
77 phantom: PhantomData,
78 }
79 }
80 }
81 };
82}
83
84#[cfg(not(any(lptim_v2a, lptim_v2b)))]
85channel_impl!(new, new_with_config, Output, OutputPin);
86#[cfg(any(lptim_v2a, lptim_v2b))]
87channel_impl!(new_ch1, new_ch1_with_config, Ch1, Channel1Pin);
88#[cfg(any(lptim_v2a, lptim_v2b))]
89channel_impl!(new_ch2, new_ch2_with_config, Ch2, Channel2Pin);
90
91pub struct Pwm<'d, T: Instance> {
93 inner: Timer<'d, T>,
94}
95
96#[cfg(not(any(lptim_v2a, lptim_v2b)))]
97impl<'d, T: Instance> Pwm<'d, T> {
98 pub fn new(tim: Peri<'d, T>, _output_pin: PwmPin<'d, T, Output>, freq: Hertz) -> Self {
100 Self::new_inner(tim, freq)
101 }
102
103 pub fn set_duty(&mut self, duty: u16) {
107 assert!(duty <= self.get_max_duty());
108 self.inner.set_compare_value(duty)
109 }
110
111 pub fn get_duty(&self) -> u16 {
115 self.inner.get_compare_value()
116 }
117
118 fn post_init(&mut self) {}
119}
120
121#[cfg(any(lptim_v2a, lptim_v2b))]
122impl<'d, T: Instance> Pwm<'d, T> {
123 pub fn new(
125 tim: Peri<'d, T>,
126 _ch1_pin: Option<PwmPin<'d, T, Ch1>>,
127 _ch2_pin: Option<PwmPin<'d, T, Ch2>>,
128 freq: Hertz,
129 ) -> Self {
130 Self::new_inner(tim, freq)
131 }
132
133 pub fn enable(&mut self, channel: Channel) {
135 self.inner.enable_channel(channel, true);
136 }
137
138 pub fn disable(&mut self, channel: Channel) {
140 self.inner.enable_channel(channel, false);
141 }
142
143 pub fn is_enabled(&self, channel: Channel) -> bool {
145 self.inner.get_channel_enable_state(channel)
146 }
147
148 pub fn set_duty(&mut self, channel: Channel, duty: u16) {
152 assert!(duty <= self.get_max_duty());
153 self.inner.set_compare_value(channel, duty)
154 }
155
156 pub fn get_duty(&self, channel: Channel) -> u16 {
160 self.inner.get_compare_value(channel)
161 }
162
163 fn post_init(&mut self) {
164 [Channel::Ch1, Channel::Ch2].iter().for_each(|&channel| {
165 self.inner.set_channel_direction(channel, ChannelDirection::OutputPwm);
166 });
167 }
168}
169
170impl<'d, T: Instance> Pwm<'d, T> {
171 fn new_inner(tim: Peri<'d, T>, freq: Hertz) -> Self {
172 let mut this = Self { inner: Timer::new(tim) };
173
174 this.inner.enable();
175 this.set_frequency(freq);
176
177 this.post_init();
178
179 this.inner.continuous_mode_start();
180
181 this
182 }
183
184 pub fn set_frequency(&mut self, frequency: Hertz) {
189 self.inner.set_frequency(frequency);
190 }
191
192 pub fn get_max_duty(&self) -> u16 {
196 self.inner.get_max_compare_value() + 1
197 }
198}