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
//! Pwm

use core::marker::PhantomData;
use gpio;
use hal;
use timer;

/// pwm
pub struct PwmBinding<P: gpio::GPIOPin,
 C: timer::TimerChannel,
 AF: gpio::AltFnNum>
{
    pin: P,
    channel: C,
    _af: PhantomData<AF>,
}

impl<P: gpio::GPIOPin, C: timer::TimerChannel, AF: gpio::AltFnNum>
    PwmBinding<P, C, AF>
{
    /// Consumes PwmBinding returning pin and channel
    pub fn release(self) -> (P, C) {
        (self.pin, self.channel)
    }
}

impl<P: gpio::GPIOPin, C: timer::TimerChannel, AF: gpio::AltFnNum> hal::PwmPin
    for PwmBinding<P, C, AF>
{
    type Duty = u32;
    fn disable(&mut self) {
        self.channel.disable()
    }

    fn enable(&mut self) {
        self.channel.enable()
    }

    fn get_duty(&self) -> u32 {
        self.channel.read_ccr()
    }

    fn get_max_duty(&self) -> u32 {
        self.channel.read_arr()
    }

    fn set_duty(&mut self, duty: u32) {
        self.channel.write_ccr(duty)
    }
}

/// PwmExtension trait
pub trait PwmExt {
    /// type of pin
    type P: gpio::GPIOPin;
    /// type of channel
    type C: timer::TimerChannel;
    /// type for AF
    type AF: gpio::AltFnNum;
    /// binding
    type Output;
    /// Configures pin and channel to create pwm binding
    fn new(pin: Self::P, channel: Self::C) -> Self::Output;
}

macro_rules! pwm {
    (
        $CRFN:ident,($PINMOD:ident, $PIN:ident),($TIM:ident, $CHN:ident, $CHPE:expr),($AF:ident, $PP:ident, $SPEED:ident)
    ) => {
        impl<PT: gpio::PullType, PM: gpio::PinMode, CM: timer::ChMode> PwmExt
            for PwmBinding<gpio::$PINMOD::$PIN<PT, PM>,
                           timer::$TIM::Channel<timer::$CHN, CM>,
                           gpio::$AF>
        {
            type P = gpio::$PINMOD::$PIN<PT, PM>;
            type C = timer::$TIM::Channel<timer::$CHN, CM>;
            type AF = gpio::$AF;
            type Output =
                PwmBinding<gpio::$PINMOD::$PIN<PT,
                                               gpio::AltFn<gpio::$AF,
                                                           gpio::$PP,
                                                           gpio::$SPEED>>,
                           timer::$TIM::Channel<timer::$CHN, timer::Pwm1>,
                           gpio::$AF>;
            fn new(pin: Self::P, channel: Self::C) -> Self::Output {
                let pin = pin.alternating(gpio::$AF)
                             .output_speed(gpio::$SPEED)
                             .output_type(gpio::$PP)
                             .alt_fn(gpio::$AF);
                let mut channel = channel.mode(timer::Pwm1);
                channel.preload($CHPE);
                PwmBinding { pin,
                             channel,
                             _af: PhantomData, }
            }
        }

        impl<PT: gpio::PullType, PM: gpio::PinMode, CM: timer::ChMode>
            PwmBinding<gpio::$PINMOD::$PIN<PT, PM>,
                       timer::$TIM::Channel<timer::$CHN, CM>,
                       gpio::$AF>
        {
            /// Binds PIN to Timer's channel and configures them to act as pwm;
            /// you can also use ::new with type annotations.
            pub fn $CRFN(
                pin: gpio::$PINMOD::$PIN<PT, PM>,
                channel: timer::$TIM::Channel<timer::$CHN, CM>)
                -> PwmBinding<gpio::$PINMOD::$PIN<PT,
                                                  gpio::AltFn<gpio::$AF,
                                                              gpio::$PP,
                                                              gpio::$SPEED>>,
                              timer::$TIM::Channel<timer::$CHN, timer::Pwm1>,
                              gpio::$AF> {
                PwmBinding::<gpio::$PINMOD::$PIN<_, _>,
                           timer::$TIM::Channel<timer::$CHN, _>,
                           gpio::$AF>::new(pin, channel)
            }

            /// Modify channel's preload
            pub fn channel_preload(&mut self, enabled: bool) {
                self.channel.preload(enabled)
            }
        }
    };
}

// XXX: don't force speed?
// XXX: don't force Pwm1? allow Pwm2 as well?

pwm!(bind_pa0_tim2_ch1,
     (gpioa, PA0),
     (tim2, CH1, true),
     (AF1, PushPull, MediumSpeed));

pwm!(bind_pa1_tim2_ch2,
     (gpioa, PA1),
     (tim2, CH2, true),
     (AF1, PushPull, MediumSpeed));

pwm!(bind_pa2_tim2_ch3,
     (gpioa, PA2),
     (tim2, CH3, true),
     (AF1, PushPull, MediumSpeed));

pwm!(bind_pa3_tim2_ch4,
     (gpioa, PA3),
     (tim2, CH4, true),
     (AF1, PushPull, MediumSpeed));

pwm!(bind_pa0_tim2_ch1_af2,
     (gpioa, PA0),
     (tim2, CH1, true),
     (AF2, PushPull, MediumSpeed));

pwm!(bind_pc6_tim3_ch1,
     (gpioc, PC6),
     (tim3, CH1, true),
     (AF2, PushPull, MediumSpeed));

pwm!(bind_pc7_tim3_ch1,
     (gpioc, PC7),
     (tim3, CH2, true),
     (AF2, PushPull, MediumSpeed));

pwm!(bind_pc8_tim3_ch1,
     (gpioc, PC8),
     (tim3, CH3, true),
     (AF2, PushPull, MediumSpeed));

pwm!(bind_pc9_tim3_ch1,
     (gpioc, PC9),
     (tim3, CH4, true),
     (AF2, PushPull, MediumSpeed));

pwm!(bind_pb0_tim3_ch3,
     (gpiob, PB0),
     (tim3, CH3, true),
     (AF2, PushPull, MediumSpeed));

pwm!(bind_pb1_tim3_ch4,
     (gpiob, PB1),
     (tim3, CH4, true),
     (AF2, PushPull, MediumSpeed));

pwm!(bind_pb6_tim4_ch1,
     (gpiob, PB6),
     (tim4, CH1, true),
     (AF2, PushPull, MediumSpeed));

pwm!(bind_pb7_tim4_ch2,
     (gpiob, PB7),
     (tim4, CH2, true),
     (AF2, PushPull, MediumSpeed));

pwm!(bind_pb8_tim4_ch3,
     (gpiob, PB8),
     (tim4, CH3, true),
     (AF2, PushPull, MediumSpeed));

pwm!(bind_pb9_tim4_ch4,
     (gpiob, PB9),
     (tim4, CH4, true),
     (AF2, PushPull, MediumSpeed));