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
349
350
351
use core::marker::PhantomData;

use cast::{u16, u32};
use crate::device::{TIM1, TIM2, TIM3, TIM4, tim2, tim1};
use crate::gpio::gpioa::{PA0, PA1, PA2, PA3, PA6, PA7, PA8, PA9, PA10, PA11, PA15};
use crate::gpio::gpiob::{PB0, PB1, PB3, PB4, PB5, PB6, PB7, PB8, PB9, PB10, PB11};
use crate::afio::MAPR;
use crate::bb;
use crate::rcc::{APB1, APB2, Clocks};
use crate::gpio::Alternate;
use crate::time::{U32Ext, Hertz};

pub trait PwmTrait<TIM, BUS, REGISTERBLOCK, MAPPING>: Sized
    where MAPPING: TimChannelsMapping<TIM>,
          REGISTERBLOCK: TimRegisterType {
    fn create_pwm(&self, clocks: Clocks, _pins: MAPPING, apb: &mut BUS, mapr: &mut MAPR)
                  -> PwmTimer<TIM, BUS, REGISTERBLOCK, MAPPING> {
        self.enable_and_reset_tim(apb);
        self.configure_pin_mapping(mapr);
        <Self as PwmTrait<TIM, BUS, REGISTERBLOCK, MAPPING>>::get_register_block().configure();
        PwmTimer {
            _tim: PhantomData,
            _bus: PhantomData,
            _mapping: PhantomData,
            _register: PhantomData,
            clock_freq: Self::get_tim_clock_freq(clocks),
        }
    }
    fn enable_and_reset_tim(&self, apb: &mut BUS);
    fn configure_pin_mapping(&self, mapr: &mut MAPR);
    fn get_tim_clock_freq(clocks: Clocks) -> Hertz {
        // by default we return the apb1 clocks because it's the bus with most timers
        clocks.pclk1_tim()
    }
    fn write_ccer(ccer_bit: u8, value: bool) {
        <Self as PwmTrait<TIM, BUS, REGISTERBLOCK, MAPPING>>::get_register_block().write_ccer(ccer_bit, value)
    }
    fn get_period(clock_freq: u32) -> u32 {
        clock_freq / <Self as PwmTrait<TIM, BUS, REGISTERBLOCK, MAPPING>>::get_register_block().get_clock_division()
    }
    fn get_register_block<'a>() -> &'a REGISTERBLOCK;
}

pub trait TimerMapping<TIM> {
    type Channels;
}

pub struct PwmTimer<TIM, BUS, REGISTERBLOCK, MAPPING> {
    _tim: PhantomData<TIM>,
    _bus: PhantomData<BUS>,
    _mapping: PhantomData<MAPPING>,
    _register: PhantomData<REGISTERBLOCK>,
    clock_freq: Hertz,
}

pub enum Channel { _1, _2, _3, _4 }

impl<TIM, BUS, REGISTERBLOCK, MAPPING> PwmTimer<TIM, BUS, REGISTERBLOCK, MAPPING>
    where TIM: PwmTrait<TIM, BUS, REGISTERBLOCK, MAPPING>,
          MAPPING: TimChannelsMapping<TIM>,
          REGISTERBLOCK: TimRegisterType {
    fn channel_to_ccer_bit(channel: Channel) -> u8 {
        match channel {
            Channel::_1 => 0u8,
            Channel::_2 => 4u8,
            Channel::_3 => 8u8,
            Channel::_4 => 12u8,
        }
    }
}

impl<TIM, BUS, REGISTERBLOCK, MAPPING> crate::hal::Pwm for PwmTimer<TIM, BUS, REGISTERBLOCK, MAPPING>
    where TIM: PwmTrait<TIM, BUS, REGISTERBLOCK, MAPPING>,
          MAPPING: TimChannelsMapping<TIM>,
          REGISTERBLOCK: TimRegisterType {
    type Channel = Channel;
    type Time = Hertz;
    type Duty = u16;

    fn disable(&mut self, channel: Self::Channel) {
        TIM::write_ccer(Self::channel_to_ccer_bit(channel), false);
    }

    fn enable(&mut self, channel: Self::Channel) {
        TIM::write_ccer(Self::channel_to_ccer_bit(channel), true);
    }

    fn get_period(&self) -> Self::Time {
        TIM::get_period(self.clock_freq.0).hz()
    }

    fn get_duty(&self, channel: Self::Channel) -> Self::Duty {
        TIM::get_register_block().get_channel_duty(channel)
    }

    fn get_max_duty(&self) -> Self::Duty {
        TIM::get_register_block().get_counter_autoreload()
    }

    fn set_duty(&mut self, channel: Self::Channel, duty: Self::Duty) {
        TIM::get_register_block().set_channel_duty(channel, duty)
    }

    fn set_period<P>(&mut self, period: P) where
        P: Into<Self::Time> {
        let ticks = self.clock_freq.0 / period.into().0;
        let psc = u16(ticks / (1 << 16)).unwrap();
        TIM::get_register_block().write_prescaler(psc);
        let arr = u16(ticks / u32(psc + 1)).unwrap();
        TIM::get_register_block().write_counter_autoreload(arr);
    }
}

pub trait TimChannelsMapping<TIM>: Sized {
    const MAPPING: u8;
}

// this trait unifies Advanced Control timers and General-purpose timers
pub trait TimRegisterType {
    fn get_counter_autoreload(&self) -> u16;
    fn get_clock_division(&self) -> u32;
    fn write_ccer(&self, ccer_bits: u8, value: bool);
    fn write_prescaler(&self, value: u16);
    fn write_counter_autoreload(&self, value: u16);
    fn configure(&self);
    fn set_channel_duty(&self, channel: Channel, value: u16);
    fn get_channel_duty(&self, channel: Channel) -> u16;
}

impl TimRegisterType for tim2::RegisterBlock {
    fn get_counter_autoreload(&self) -> u16 {
        self.arr.read().arr().bits()
    }
    fn get_clock_division(&self) -> u32 {
        (u32(self.psc.read().psc().bits()) + 1)
            * (u32(self.get_counter_autoreload()) + 1)
    }
    fn write_ccer(&self, ccer_bits: u8, value: bool) {
        bb::write(&self.ccer, ccer_bits, value)
    }
    fn write_prescaler(&self, value: u16) {
        self.psc.write(|w| w.psc().bits(value));
    }
    fn write_counter_autoreload(&self, value: u16) {
        self.arr.write(|w| w.arr().bits(value));
    }
    fn configure(&self) {
        self.ccmr1_output.modify(|_, w| w.oc1pe().set_bit().oc1m().pwm1());
        self.ccmr1_output.modify(|_, w| w.oc2pe().set_bit().oc2m().pwm1());
        self.ccmr2_output.modify(|_, w| w.oc3pe().set_bit().oc3m().pwm1());
        self.ccmr2_output.modify(|_, w| w.oc4pe().set_bit().oc4m().pwm1());
        self.cr1.write(|w| unsafe {
            w.cms().bits(0b00).dir().up().opm().continuous().cen().enabled()
        });
    }
    fn set_channel_duty(&self, channel: Channel, value: u16) {
        match channel {
            Channel::_1 => { self.ccr1.write(|w| w.ccr1().bits(value)) }
            Channel::_2 => { self.ccr2.write(|w| w.ccr2().bits(value)) }
            Channel::_3 => { self.ccr3.write(|w| w.ccr3().bits(value)) }
            Channel::_4 => { self.ccr4.write(|w| w.ccr4().bits(value)) }
        }
    }
    fn get_channel_duty(&self, channel: Channel) -> u16 {
        match channel {
            Channel::_1 => self.ccr1.read().ccr1().bits(),
            Channel::_2 => self.ccr2.read().ccr2().bits(),
            Channel::_3 => self.ccr3.read().ccr3().bits(),
            Channel::_4 => self.ccr4.read().ccr4().bits()
        }
    }
}

impl TimRegisterType for tim1::RegisterBlock {
    fn get_counter_autoreload(&self) -> u16 {
        self.arr.read().arr().bits()
    }
    fn get_clock_division(&self) -> u32 {
        (u32(self.psc.read().psc().bits()) + 1)
            * (u32(self.get_counter_autoreload()) + 1)
    }
    fn write_ccer(&self, ccer_bits: u8, value: bool) {
        bb::write(&self.ccer, ccer_bits, value)
    }
    fn write_prescaler(&self, value: u16) {
        self.psc.write(|w| w.psc().bits(value));
    }
    fn write_counter_autoreload(&self, value: u16) {
        self.arr.write(|w| w.arr().bits(value));
    }
    fn configure(&self) {
        self.ccmr1_output.modify(|_, w| w.oc1pe().set_bit().oc1m().pwm1());
        self.ccmr1_output.modify(|_, w| w.oc2pe().set_bit().oc2m().pwm1());
        self.ccmr2_output.modify(|_, w| w.oc3pe().set_bit().oc3m().pwm1());
        self.ccmr2_output.modify(|_, w| w.oc4pe().set_bit().oc4m().pwm1());
        self.cr1.write(|w| unsafe {
            w.cms().bits(0b00).dir().up().opm().continuous().cen().enabled()
        });
    }
    fn set_channel_duty(&self, channel: Channel, value: u16) {
        match channel {
            Channel::_1 => { self.ccr1.write(|w| w.ccr1().bits(value)) }
            Channel::_2 => { self.ccr2.write(|w| w.ccr2().bits(value)) }
            Channel::_3 => { self.ccr3.write(|w| w.ccr3().bits(value)) }
            Channel::_4 => { self.ccr4.write(|w| w.ccr4().bits(value)) }
        }
    }
    fn get_channel_duty(&self, channel: Channel) -> u16 {
        match channel {
            Channel::_1 => self.ccr1.read().ccr1().bits(),
            Channel::_2 => self.ccr2.read().ccr2().bits(),
            Channel::_3 => self.ccr3.read().ccr3().bits(),
            Channel::_4 => self.ccr4.read().ccr4().bits()
        }
    }
}

// TIM1 ////////////////////////
pub type Pwm1Mapping0<M1, M2, M3, M4> = (Option<PA8<Alternate<M1>>>,
                                         Option<PA9<Alternate<M2>>>,
                                         Option<PA10<Alternate<M3>>>,
                                         Option<PA11<Alternate<M4>>>);
pub type PWM1<Mapping> = PwmTimer<TIM1, APB2, tim1::RegisterBlock, Mapping>;

impl<M1, M2, M3, M4> TimChannelsMapping<TIM1> for Pwm1Mapping0<M1, M2, M3, M4>
{ const MAPPING: u8 = 0; }

impl<T> PwmTrait<TIM1, APB2, tim1::RegisterBlock, T> for TIM1
    where T: TimChannelsMapping<TIM1> {
    fn enable_and_reset_tim(&self, apb: &mut APB2) {
        apb.enr().modify(|_, w| w.tim1en().enabled());
        apb.rstr().modify(|_, w| w.tim1rst().set_bit());
        apb.rstr().modify(|_, w| w.tim1rst().clear_bit());
    }

    fn configure_pin_mapping(&self, mapr: &mut MAPR) {
        mapr.mapr().modify(|_, w| unsafe { w.tim1_remap().bits(T::MAPPING) });
    }
    fn get_tim_clock_freq(clocks: Clocks) -> Hertz {
        clocks.pclk2_tim()
    }
    fn get_register_block<'a>() -> &'a tim1::RegisterBlock {
        unsafe { &(*TIM1::ptr()) }
    }
}

// TIM2 ////////////////////////
pub type Pwm2Mapping0<M1, M2, M3, M4> = (Option<PA0<Alternate<M1>>>,
                                         Option<PA1<Alternate<M2>>>,
                                         Option<PA2<Alternate<M3>>>,
                                         Option<PA3<Alternate<M4>>>);
pub type Pwm2Mapping01<M1, M2, M3, M4> = (Option<PA15<Alternate<M1>>>,
                                          Option<PB3<Alternate<M2>>>,
                                          Option<PA2<Alternate<M3>>>,
                                          Option<PA3<Alternate<M4>>>);
pub type Pwm2Mapping10<M1, M2, M3, M4> = (Option<PA0<Alternate<M1>>>,
                                          Option<PA1<Alternate<M2>>>,
                                          Option<PB10<Alternate<M3>>>,
                                          Option<PB11<Alternate<M4>>>);
pub type Pwm2Mapping11<M1, M2, M3, M4> = (Option<PA15<Alternate<M1>>>,
                                          Option<PB3<Alternate<M2>>>,
                                          Option<PB10<Alternate<M3>>>,
                                          Option<PB11<Alternate<M4>>>);

pub type PWM2<Mapping> = PwmTimer<TIM2, APB1, tim2::RegisterBlock, Mapping>;

impl<M1, M2, M3, M4> TimChannelsMapping<TIM2> for Pwm2Mapping0<M1, M2, M3, M4>
{ const MAPPING: u8 = 0; }

impl<M1, M2, M3, M4> TimChannelsMapping<TIM2> for Pwm2Mapping01<M1, M2, M3, M4>
{ const MAPPING: u8 = 0b01; }

impl<M1, M2, M3, M4> TimChannelsMapping<TIM2> for Pwm2Mapping10<M1, M2, M3, M4>
{ const MAPPING: u8 = 0b10; }

impl<M1, M2, M3, M4> TimChannelsMapping<TIM2> for Pwm2Mapping11<M1, M2, M3, M4>
{ const MAPPING: u8 = 0b11; }

impl<T> PwmTrait<TIM2, APB1, tim2::RegisterBlock, T> for TIM2
    where T: TimChannelsMapping<TIM2> {
    fn enable_and_reset_tim(&self, apb: &mut APB1) {
        apb.enr().modify(|_, w| w.tim2en().enabled());
        apb.rstr().modify(|_, w| w.tim2rst().set_bit());
        apb.rstr().modify(|_, w| w.tim2rst().clear_bit());
    }
    fn configure_pin_mapping(&self, mapr: &mut MAPR) {
        mapr.mapr().modify(|_, w| unsafe { w.tim2_remap().bits(T::MAPPING) });
    }
    fn get_register_block<'a>() -> &'a tim2::RegisterBlock {
        unsafe { &(*TIM2::ptr()) }
    }
}

// TIM3 ////////////////////////
pub type Pwm3Mapping0<M1, M2, M3, M4> = (Option<PA6<Alternate<M1>>>,
                                         Option<PA7<Alternate<M2>>>,
                                         Option<PB0<Alternate<M3>>>,
                                         Option<PB1<Alternate<M4>>>);

pub type Pwm3Mapping10<M1, M2, M3, M4> = (Option<PB4<Alternate<M1>>>,
                                          Option<PB5<Alternate<M2>>>,
                                          Option<PB0<Alternate<M3>>>,
                                          Option<PB1<Alternate<M4>>>);
pub type PWM3<Mapping> = PwmTimer<TIM3, APB1, tim2::RegisterBlock, Mapping>;

impl<M1, M2, M3, M4> TimChannelsMapping<TIM3> for Pwm3Mapping0<M1, M2, M3, M4>
{ const MAPPING: u8 = 0; }

impl<M1, M2, M3, M4> TimChannelsMapping<TIM3> for Pwm3Mapping10<M1, M2, M3, M4>
{ const MAPPING: u8 = 0b10; }

impl<T> PwmTrait<TIM3, APB1, tim2::RegisterBlock, T> for TIM3
    where T: TimChannelsMapping<TIM3> {
    fn enable_and_reset_tim(&self, apb: &mut APB1) {
        apb.enr().modify(|_, w| w.tim3en().enabled());
        apb.rstr().modify(|_, w| w.tim3rst().set_bit());
        apb.rstr().modify(|_, w| w.tim3rst().clear_bit());
    }
    fn configure_pin_mapping(&self, mapr: &mut MAPR) {
        mapr.mapr().modify(|_, w| unsafe { w.tim3_remap().bits(T::MAPPING) });
    }
    fn get_register_block<'a>() -> &'a tim2::RegisterBlock {
        unsafe { &(*TIM3::ptr()) }
    }
}

// TIM4 ////////////////////////
pub type Pwm4Mapping0<M1, M2, M3, M4> = (Option<PB6<Alternate<M1>>>,
                                         Option<PB7<Alternate<M2>>>,
                                         Option<PB8<Alternate<M3>>>,
                                         Option<PB9<Alternate<M4>>>);
pub type PWM4<Mapping> = PwmTimer<TIM4, APB1, tim2::RegisterBlock, Mapping>;

impl<M1, M2, M3, M4> TimChannelsMapping<TIM4> for Pwm4Mapping0<M1, M2, M3, M4>
{ const MAPPING: u8 = 0; }

impl<T> PwmTrait<TIM4, APB1, tim2::RegisterBlock, T> for TIM4
    where T: TimChannelsMapping<TIM4> {
    fn enable_and_reset_tim(&self, apb: &mut APB1) {
        apb.enr().modify(|_, w| w.tim4en().enabled());
        apb.rstr().modify(|_, w| w.tim4rst().set_bit());
        apb.rstr().modify(|_, w| w.tim4rst().clear_bit());
    }
    fn configure_pin_mapping(&self, mapr: &mut MAPR) {
        mapr.mapr().modify(|_, w| w.tim4_remap().bit(T::MAPPING != 0));
    }

    fn get_register_block<'a>() -> &'a tim2::RegisterBlock {
        unsafe { &(*TIM4::ptr()) }
    }
}