1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
use core::marker::PhantomData;
use core::ops::Deref;

use cortex_m::interrupt;

use crate::gpio::gpioa::{PA0, PA1, PA2, PA3};
use crate::gpio::{AltMode, PinMode};
use crate::hal;
use crate::pac::{tim2, TIM2, TIM3};
use crate::rcc::Rcc;
use crate::time::Hertz;
use cast::{u16, u32};

#[cfg(any(feature = "stm32l0x2", feature = "stm32l0x3"))]
use crate::gpio::{
    gpioa::{PA15, PA5},
    gpiob::{PB10, PB11, PB3},
};

#[cfg(any(feature = "stm32l072", feature = "stm32l082"))]
use crate::gpio::{
    gpioa::{PA6, PA7},
    gpiob::{PB0, PB1, PB4, PB5},
};

#[cfg(feature = "stm32l072")]
use crate::gpio::{
    gpioc::{PC6, PC7, PC8, PC9},
    gpioe::{PE10, PE11, PE12, PE3, PE4, PE5, PE6, PE9},
};

pub struct Timer<I> {
    instance: I,

    pub channel1: Pwm<I, C1, Unassigned>,
    pub channel2: Pwm<I, C2, Unassigned>,
    pub channel3: Pwm<I, C3, Unassigned>,
    pub channel4: Pwm<I, C4, Unassigned>,
}

impl<I> Timer<I>
where
    I: Instance,
{
    /// Create new timer instance that is automatically started with given frequency
    pub fn new(timer: I, frequency: Hertz, rcc: &mut Rcc) -> Self {
        timer.enable(rcc);

        let mut tim = Self {
            instance: timer,
            channel1: Pwm::new(),
            channel2: Pwm::new(),
            channel3: Pwm::new(),
            channel4: Pwm::new(),
        };
        tim.set_frequency(frequency, rcc);
        tim
    }

    /// Starts the PWM timer
    pub fn start(&mut self) {
        self.instance.cr1.write(|w| w.cen().set_bit());
    }

    /// Stops the PWM timer
    pub fn stop(&mut self) {
        self.instance.cr1.write(|w| w.cen().clear_bit());
    }

    /// Update frequency of the timer
    /// # Note
    /// In order to do this operation properly the function stop the timer and then starts it again.
    /// The duty cycle that was set before for given pin needs to adjusted according to the
    /// frequency
    pub fn set_frequency(&mut self, frequency: Hertz, rcc: &Rcc) {
        self.stop();
        let (psc, arr) = get_clock_config(frequency.0, self.instance.clock_frequency(rcc));
        self.instance.psc.write(|w| w.psc().bits(psc));
        self.instance.arr.write(|w| w.arr().bits(arr.into()));
        self.start();
    }

    /// Returns the timer, so it can be used by any else
    pub fn free(self) -> I {
        self.instance
    }
}

fn get_clock_config(freq: u32, clk: u32) -> (u16, u16) {
    let ticks = clk / freq;
    let psc = u16((ticks - 1) / (1 << 16)).unwrap();
    let arr = u16(ticks / u32(psc + 1)).unwrap();
    return (psc, arr);
}

pub trait Instance: Deref<Target = tim2::RegisterBlock> {
    fn ptr() -> *const tim2::RegisterBlock;
    fn enable(&self, _: &mut Rcc);
    fn clock_frequency(&self, _: &Rcc) -> u32;
}

macro_rules! impl_instance {
    (
        $(
            $name:ty,
            $apbXenr:ident,
            $apbXrstr:ident,
            $timXen:ident,
            $timXrst:ident,
            $apbX_clk:ident;
        )*
    ) => {
        $(
            impl Instance for $name {
                fn ptr() -> *const tim2::RegisterBlock {
                    Self::ptr()
                }

                fn enable(&self, rcc: &mut Rcc) {
                    rcc.rb.$apbXenr.modify(|_, w| w.$timXen().set_bit());
                    rcc.rb.$apbXrstr.modify(|_, w| w.$timXrst().set_bit());
                    rcc.rb.$apbXrstr.modify(|_, w| w.$timXrst().clear_bit());
                }

                fn clock_frequency(&self, rcc: &Rcc) -> u32 {
                    rcc.clocks.$apbX_clk().0
                }
            }
        )*
    }
}

impl_instance!(
    TIM2, apb1enr, apb1rstr, tim2en, tim2rst, apb1_clk;
    TIM3, apb1enr, apb1rstr, tim3en, tim3rst, apb1_clk;
);

pub trait Channel {
    fn disable(_: &tim2::RegisterBlock);
    fn enable(_: &tim2::RegisterBlock);
    fn get_duty(_: &tim2::RegisterBlock) -> u16;
    fn set_duty(_: &tim2::RegisterBlock, duty: u16);
}

macro_rules! impl_channel {
    (
        $(
            $name:ident,
            $ccxe:ident,
            $ccmr_output:ident,
            $ocxpe:ident,
            $ocxm:ident,
            $ccrx:ident;
        )*
    ) => {
        $(
            pub struct $name;

            impl Channel for $name {
                fn disable(tim: &tim2::RegisterBlock) {
                    tim.ccer.modify(|_, w| w.$ccxe().clear_bit());
                }

                fn enable(tim: &tim2::RegisterBlock) {
                    tim.$ccmr_output().modify(|_, w| {
                        w.$ocxpe().set_bit();
                        w.$ocxm().bits(0b110)
                    });
                    tim.ccer.modify(|_, w| w.$ccxe().set_bit());
                }

                fn get_duty(tim: &tim2::RegisterBlock) -> u16 {
                    // This cast to `u16` is fine. The type is already `u16`,
                    // but on STM32L0x2, the SVD file seems to be wrong about
                    // that (or the reference manual is wrong; but in any case,
                    // we only ever write `u16` into this field).
                    tim.$ccrx.read().ccr().bits() as u16
                }

                fn set_duty(tim: &tim2::RegisterBlock, duty: u16) {
                    tim.$ccrx.write(|w| w.ccr().bits(duty.into()));
                }
            }
        )*
    }
}

impl_channel!(
    C1, cc1e, ccmr1_output, oc1pe, oc1m, ccr1;
    C2, cc2e, ccmr1_output, oc2pe, oc2m, ccr2;
    C3, cc3e, ccmr2_output, oc3pe, oc3m, ccr3;
    C4, cc4e, ccmr2_output, oc4pe, oc4m, ccr4;
);

pub struct Pwm<I, C, State> {
    channel: PhantomData<C>,
    timer: PhantomData<I>,
    _state: State,
}

impl<I, C> Pwm<I, C, Unassigned> {
    fn new() -> Self {
        Self {
            channel: PhantomData,
            timer: PhantomData,
            _state: Unassigned,
        }
    }

    pub fn assign<P>(self, pin: P) -> Pwm<I, C, Assigned<P>>
    where
        P: Pin<I, C>,
    {
        pin.setup();
        Pwm {
            channel: self.channel,
            timer: self.timer,
            _state: Assigned(pin),
        }
    }
}

impl<I, C, P> hal::PwmPin for Pwm<I, C, Assigned<P>>
where
    I: Instance,
    C: Channel,
{
    type Duty = u16;

    fn disable(&mut self) {
        interrupt::free(|_|
            // Safe, as the read-modify-write within the critical section
            C::disable(unsafe { &*I::ptr() }))
    }

    fn enable(&mut self) {
        interrupt::free(|_|
            // Safe, as the read-modify-write within the critical section
            C::enable(unsafe { &*I::ptr() }))
    }

    fn get_duty(&self) -> u16 {
        // Safe, as we're only doing an atomic read.
        C::get_duty(unsafe { &*I::ptr() })
    }

    fn get_max_duty(&self) -> u16 {
        // Safe, as we're only doing an atomic read.
        let tim = unsafe { &*I::ptr() };

        // This cast to `u16` is fine. The type is already `u16`, but on
        // STM32L0x2, the SVD file seems to be wrong about that (or the
        // reference manual is wrong; but in any case, we only ever write `u16`
        // into this field).
        tim.arr.read().arr().bits() as u16
    }

    fn set_duty(&mut self, duty: u16) {
        // Safe, as we're only doing an atomic write.
        C::set_duty(unsafe { &*I::ptr() }, duty);
    }
}

pub trait Pin<I, C> {
    fn setup(&self);
}

macro_rules! impl_pin {
    (
        $(
            $instance:ty: (
                $(
                    $name:ident,
                    $channel:ty,
                    $alternate_function:ident;
                )*
            )
        )*
    ) => {
        $(
            $(
                impl<State: PinMode> Pin<$instance, $channel> for $name<State> {
                    fn setup(&self) {
                        self.set_alt_mode(AltMode::$alternate_function);
                    }
                }
            )*
        )*
    }
}

impl_pin!(
    TIM2: (
        PA0, C1, AF2;
        PA1, C2, AF2;
        PA2, C3, AF2;
        PA3, C4, AF2;
    )
);

#[cfg(any(feature = "stm32l0x2", feature = "stm32l0x3"))]
impl_pin!(
    TIM2: (
        PA5,  C1, AF5;
        PA15, C1, AF5;
        PB3,  C2, AF2;
        PB10, C3, AF2;
        PB11, C4, AF2;
    )
);

#[cfg(any(feature = "stm32l072", feature = "stm32l082"))]
impl_pin!(
    TIM3: (
        PA6, C1, AF2;
        PA7, C2, AF2;
        PB0, C3, AF2;
        PB1, C4, AF2;
        PB4, C1, AF2;
        PB5, C2, AF4;
    )
);

#[cfg(feature = "stm32l072")]
impl_pin!(
    TIM2: (
        PE9,  C1, AF0;
        PE10, C2, AF0;
        PE11, C3, AF0;
        PE12, C4, AF0;
    )
    TIM3: (
        PC6, C1, AF2;
        PC7, C2, AF2;
        PC8, C3, AF2;
        PC9, C4, AF2;
        PE3, C1, AF2;
        PE4, C2, AF2;
        PE5, C3, AF2;
        PE6, C4, AF2;
    )
);

/// Indicates that a PWM channel has not been assigned to a pin
pub struct Unassigned;

/// Indicates that a PWM channel has been assigned to the given pin
pub struct Assigned<P>(P);