1 | //! PWM driver. |
2 | |
3 | use core::marker::PhantomData; |
4 | |
5 | use embassy_hal_internal::{into_ref, PeripheralRef}; |
6 | |
7 | use super::timer::Timer; |
8 | #[cfg (not(any(lptim_v2a, lptim_v2b)))] |
9 | use super::OutputPin; |
10 | #[cfg (any(lptim_v2a, lptim_v2b))] |
11 | use super::{channel::Channel, timer::ChannelDirection, Channel1Pin, Channel2Pin}; |
12 | use super::{BasicInstance, Instance}; |
13 | use crate::gpio::{AfType, AnyPin, OutputType, Speed}; |
14 | use crate::time::Hertz; |
15 | use crate::Peripheral; |
16 | |
17 | /// Output marker type. |
18 | pub enum Output {} |
19 | /// Channel 1 marker type. |
20 | pub enum Ch1 {} |
21 | /// Channel 2 marker type. |
22 | pub enum Ch2 {} |
23 | |
24 | /// PWM pin wrapper. |
25 | /// |
26 | /// This wraps a pin to make it usable with PWM. |
27 | pub struct PwmPin<'d, T, C> { |
28 | _pin: PeripheralRef<'d, AnyPin>, |
29 | phantom: PhantomData<(T, C)>, |
30 | } |
31 | |
32 | macro_rules! channel_impl { |
33 | ($new_chx:ident, $channel:ident, $pin_trait:ident) => { |
34 | impl<'d, T: BasicInstance> PwmPin<'d, T, $channel> { |
35 | #[doc = concat!("Create a new " , stringify!($channel), " PWM pin instance." )] |
36 | pub fn $new_chx(pin: impl Peripheral<P = impl $pin_trait<T>> + 'd) -> Self { |
37 | into_ref!(pin); |
38 | critical_section::with(|_| { |
39 | pin.set_low(); |
40 | pin.set_as_af( |
41 | pin.af_num(), |
42 | AfType::output(OutputType::PushPull, Speed::VeryHigh), |
43 | ); |
44 | }); |
45 | PwmPin { |
46 | _pin: pin.map_into(), |
47 | phantom: PhantomData, |
48 | } |
49 | } |
50 | } |
51 | }; |
52 | } |
53 | |
54 | #[cfg (not(any(lptim_v2a, lptim_v2b)))] |
55 | channel_impl!(new, Output, OutputPin); |
56 | #[cfg (any(lptim_v2a, lptim_v2b))] |
57 | channel_impl!(new_ch1, Ch1, Channel1Pin); |
58 | #[cfg (any(lptim_v2a, lptim_v2b))] |
59 | channel_impl!(new_ch2, Ch2, Channel2Pin); |
60 | |
61 | /// PWM driver. |
62 | pub struct Pwm<'d, T: Instance> { |
63 | inner: Timer<'d, T>, |
64 | } |
65 | |
66 | #[cfg (not(any(lptim_v2a, lptim_v2b)))] |
67 | impl<'d, T: Instance> Pwm<'d, T> { |
68 | /// Create a new PWM driver. |
69 | pub fn new(tim: impl Peripheral<P = T> + 'd, _output_pin: PwmPin<'d, T, Output>, freq: Hertz) -> Self { |
70 | Self::new_inner(tim, freq) |
71 | } |
72 | |
73 | /// Set the duty. |
74 | /// |
75 | /// The value ranges from 0 for 0% duty, to [`get_max_duty`](Self::get_max_duty) for 100% duty, both included. |
76 | pub fn set_duty(&mut self, duty: u16) { |
77 | assert!(duty <= self.get_max_duty()); |
78 | self.inner.set_compare_value(duty) |
79 | } |
80 | |
81 | /// Get the duty. |
82 | /// |
83 | /// The value ranges from 0 for 0% duty, to [`get_max_duty`](Self::get_max_duty) for 100% duty, both included. |
84 | pub fn get_duty(&self) -> u16 { |
85 | self.inner.get_compare_value() |
86 | } |
87 | |
88 | fn post_init(&mut self) {} |
89 | } |
90 | |
91 | #[cfg (any(lptim_v2a, lptim_v2b))] |
92 | impl<'d, T: Instance> Pwm<'d, T> { |
93 | /// Create a new PWM driver. |
94 | pub fn new( |
95 | tim: impl Peripheral<P = T> + 'd, |
96 | _ch1_pin: Option<PwmPin<'d, T, Ch1>>, |
97 | _ch2_pin: Option<PwmPin<'d, T, Ch2>>, |
98 | freq: Hertz, |
99 | ) -> Self { |
100 | Self::new_inner(tim, freq) |
101 | } |
102 | |
103 | /// Enable the given channel. |
104 | pub fn enable(&mut self, channel: Channel) { |
105 | self.inner.enable_channel(channel, true); |
106 | } |
107 | |
108 | /// Disable the given channel. |
109 | pub fn disable(&mut self, channel: Channel) { |
110 | self.inner.enable_channel(channel, false); |
111 | } |
112 | |
113 | /// Check whether given channel is enabled |
114 | pub fn is_enabled(&self, channel: Channel) -> bool { |
115 | self.inner.get_channel_enable_state(channel) |
116 | } |
117 | |
118 | /// Set the duty for a given channel. |
119 | /// |
120 | /// The value ranges from 0 for 0% duty, to [`get_max_duty`](Self::get_max_duty) for 100% duty, both included. |
121 | pub fn set_duty(&mut self, channel: Channel, duty: u16) { |
122 | assert!(duty <= self.get_max_duty()); |
123 | self.inner.set_compare_value(channel, duty) |
124 | } |
125 | |
126 | /// Get the duty for a given channel. |
127 | /// |
128 | /// The value ranges from 0 for 0% duty, to [`get_max_duty`](Self::get_max_duty) for 100% duty, both included. |
129 | pub fn get_duty(&self, channel: Channel) -> u16 { |
130 | self.inner.get_compare_value(channel) |
131 | } |
132 | |
133 | fn post_init(&mut self) { |
134 | [Channel::Ch1, Channel::Ch2].iter().for_each(|&channel| { |
135 | self.inner.set_channel_direction(channel, ChannelDirection::OutputPwm); |
136 | }); |
137 | } |
138 | } |
139 | |
140 | impl<'d, T: Instance> Pwm<'d, T> { |
141 | fn new_inner(tim: impl Peripheral<P = T> + 'd, freq: Hertz) -> Self { |
142 | let mut this = Self { inner: Timer::new(tim) }; |
143 | |
144 | this.inner.enable(); |
145 | this.set_frequency(freq); |
146 | |
147 | this.post_init(); |
148 | |
149 | this.inner.continuous_mode_start(); |
150 | |
151 | this |
152 | } |
153 | |
154 | /// Set PWM frequency. |
155 | /// |
156 | /// Note: when you call this, the max duty value changes, so you will have to |
157 | /// call `set_duty` on all channels with the duty calculated based on the new max duty. |
158 | pub fn set_frequency(&mut self, frequency: Hertz) { |
159 | self.inner.set_frequency(frequency); |
160 | } |
161 | |
162 | /// Get max duty value. |
163 | /// |
164 | /// This value depends on the configured frequency and the timer's clock rate from RCC. |
165 | pub fn get_max_duty(&self) -> u16 { |
166 | self.inner.get_max_compare_value() + 1 |
167 | } |
168 | } |
169 | |