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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
//! GPIO batch configuration.
//!
//! The `Batch` abstraction allows "collecting" changes to the configurations of different pins on
//! a GPIO port and writing the changes to the hardware all at once.
//! Changes to individual pins are performed **statically** using typestates, so the number of
//! register writes are minimized.
//!
//! For example, `P2.batch().config_pin3(|p| p.to_input_pullup()).config_pin1(|p| p.to_output()).split(&pmm)`
//! configures P2.3 as a pullup input pin and P2.1 as an output pin and then writes the
//! configuration to the hardware in a single set of writes.

use crate::gpio::*;
use crate::hw_traits::gpio::{GpioPeriph, IntrPeriph};
use crate::pmm::Pmm;
use crate::util::BitsExt;
use core::marker::PhantomData;

/// Proxy for a GPIO pin used for batch writes.
///
/// Configuring the proxy only changes the typestate of the proxy. Registers are only written once
/// all the proxies for the GPIO port are "committed".
pub struct PinProxy<PORT: PortNum, PIN: PinNum, DIR> {
    _port: PhantomData<PORT>,
    _pin: PhantomData<PIN>,
    _dir: PhantomData<DIR>,
}

macro_rules! make_proxy {
    () => {
        PinProxy {
            _port: PhantomData,
            _pin: PhantomData,
            _dir: PhantomData,
        }
    };
}

impl<PORT: PortNum, PIN: PinNum, PULL> PinProxy<PORT, PIN, Input<PULL>> {
    /// Configures pin as pulldown input
    #[inline(always)]
    pub fn pulldown(self) -> PinProxy<PORT, PIN, Input<Pulldown>> {
        make_proxy!()
    }

    /// Configures pin as pullup input
    #[inline(always)]
    pub fn pullup(self) -> PinProxy<PORT, PIN, Input<Pullup>> {
        make_proxy!()
    }

    /// Configures pin as floating input
    #[inline(always)]
    pub fn floating(self) -> PinProxy<PORT, PIN, Input<Floating>> {
        make_proxy!()
    }

    /// Configures pin as output
    #[inline(always)]
    pub fn to_output(self) -> PinProxy<PORT, PIN, Output> {
        make_proxy!()
    }
}

impl<PORT: PortNum, PIN: PinNum> PinProxy<PORT, PIN, Output> {
    /// Configures pin as floating input
    #[inline(always)]
    pub fn to_input_floating(self) -> PinProxy<PORT, PIN, Input<Floating>> {
        make_proxy!()
    }

    /// Configures pin as floating pullup
    #[inline(always)]
    pub fn to_input_pullup(self) -> PinProxy<PORT, PIN, Input<Pullup>> {
        make_proxy!()
    }

    /// Configures pin as floating pulldown
    #[inline(always)]
    pub fn to_input_pulldown(self) -> PinProxy<PORT, PIN, Input<Pulldown>> {
        make_proxy!()
    }
}

// Traits for deciding the value of a pin's registers
trait PxdirOn {}
trait PxoutOn {}
trait PxrenOn {}
trait Pxsel0On {}
trait Pxsel1On {}

trait WritePxdir {
    fn pxdir_on(&self) -> bool;
}
impl<T> WritePxdir for T {
    #[inline(always)]
    default fn pxdir_on(&self) -> bool {
        false
    }
}
impl<T: PxdirOn> WritePxdir for T {
    #[inline(always)]
    fn pxdir_on(&self) -> bool {
        true
    }
}

trait WritePxout {
    fn pxout_on(&self) -> bool;
}
impl<T> WritePxout for T {
    #[inline(always)]
    default fn pxout_on(&self) -> bool {
        false
    }
}
impl<T: PxoutOn> WritePxout for T {
    #[inline(always)]
    fn pxout_on(&self) -> bool {
        true
    }
}

trait WritePxren {
    fn pxren_on(&self) -> bool;
}
impl<T> WritePxren for T {
    #[inline(always)]
    default fn pxren_on(&self) -> bool {
        false
    }
}
impl<T: PxrenOn> WritePxren for T {
    #[inline(always)]
    fn pxren_on(&self) -> bool {
        true
    }
}

trait WritePxsel0 {
    fn pxsel0_on(&self) -> bool;
}
impl<T> WritePxsel0 for T {
    #[inline(always)]
    default fn pxsel0_on(&self) -> bool {
        false
    }
}
impl<T: Pxsel0On> WritePxsel0 for T {
    #[inline(always)]
    fn pxsel0_on(&self) -> bool {
        true
    }
}

trait WritePxsel1 {
    fn pxsel1_on(&self) -> bool;
}
impl<T> WritePxsel1 for T {
    #[inline(always)]
    default fn pxsel1_on(&self) -> bool {
        false
    }
}
impl<T: Pxsel1On> WritePxsel1 for T {
    #[inline(always)]
    fn pxsel1_on(&self) -> bool {
        true
    }
}

// Register marker trait implementations
impl<PORT: PortNum, PIN: PinNum> PxdirOn for PinProxy<PORT, PIN, Output> {}
impl<PORT: PortNum, PIN: PinNum> PxdirOn for PinProxy<PORT, PIN, Alternate1<Output>> {}
impl<PORT: PortNum, PIN: PinNum> PxdirOn for PinProxy<PORT, PIN, Alternate2<Output>> {}
impl<PORT: PortNum, PIN: PinNum> PxdirOn for PinProxy<PORT, PIN, Alternate3<Output>> {}

impl<PORT: PortNum, PIN: PinNum> PxrenOn for PinProxy<PORT, PIN, Input<Pullup>> {}
impl<PORT: PortNum, PIN: PinNum> PxrenOn for PinProxy<PORT, PIN, Input<Pulldown>> {}
impl<PORT: PortNum, PIN: PinNum> PxrenOn for PinProxy<PORT, PIN, Alternate1<Input<Pullup>>> {}
impl<PORT: PortNum, PIN: PinNum> PxrenOn for PinProxy<PORT, PIN, Alternate1<Input<Pulldown>>> {}
impl<PORT: PortNum, PIN: PinNum> PxrenOn for PinProxy<PORT, PIN, Alternate2<Input<Pullup>>> {}
impl<PORT: PortNum, PIN: PinNum> PxrenOn for PinProxy<PORT, PIN, Alternate2<Input<Pulldown>>> {}
impl<PORT: PortNum, PIN: PinNum> PxrenOn for PinProxy<PORT, PIN, Alternate3<Input<Pullup>>> {}
impl<PORT: PortNum, PIN: PinNum> PxrenOn for PinProxy<PORT, PIN, Alternate3<Input<Pulldown>>> {}

impl<PORT: PortNum, PIN: PinNum> PxoutOn for PinProxy<PORT, PIN, Input<Pullup>> {}
impl<PORT: PortNum, PIN: PinNum> PxoutOn for PinProxy<PORT, PIN, Alternate1<Input<Pullup>>> {}
impl<PORT: PortNum, PIN: PinNum> PxoutOn for PinProxy<PORT, PIN, Alternate2<Input<Pullup>>> {}
impl<PORT: PortNum, PIN: PinNum> PxoutOn for PinProxy<PORT, PIN, Alternate3<Input<Pullup>>> {}

impl<PORT: PortNum, PIN: PinNum, DIR> Pxsel0On for PinProxy<PORT, PIN, Alternate1<DIR>> {}
impl<PORT: PortNum, PIN: PinNum, DIR> Pxsel0On for PinProxy<PORT, PIN, Alternate3<DIR>> {}

impl<PORT: PortNum, PIN: PinNum, DIR> Pxsel1On for PinProxy<PORT, PIN, Alternate2<DIR>> {}
impl<PORT: PortNum, PIN: PinNum, DIR> Pxsel1On for PinProxy<PORT, PIN, Alternate3<DIR>> {}

// Derive bitmasks for different GPIO registers from pin numbers and register trait implementations
trait MaskRegisters {
    fn pxout_mask(&self) -> u8;
    fn pxdir_mask(&self) -> u8;
    fn pxren_mask(&self) -> u8;
    fn pxsel0_mask(&self) -> u8;
    fn pxsel1_mask(&self) -> u8;
}

impl<PORT: PortNum, PIN: PinNum, DIR> MaskRegisters for PinProxy<PORT, PIN, DIR> {
    #[inline(always)]
    fn pxout_mask(&self) -> u8 {
        (self.pxout_on() as u8) << PIN::NUM
    }

    #[inline(always)]
    fn pxdir_mask(&self) -> u8 {
        (self.pxdir_on() as u8) << PIN::NUM
    }

    #[inline(always)]
    fn pxren_mask(&self) -> u8 {
        (self.pxren_on() as u8) << PIN::NUM
    }

    #[inline(always)]
    fn pxsel0_mask(&self) -> u8 {
        (self.pxsel0_on() as u8) << PIN::NUM
    }

    #[inline(always)]
    fn pxsel1_mask(&self) -> u8 {
        (self.pxsel1_on() as u8) << PIN::NUM
    }
}

trait InterruptOperations {
    fn maybe_set_pxie(&self, b: u8);
}

impl<P: GpioPeriph> InterruptOperations for P {
    #[inline(always)]
    default fn maybe_set_pxie(&self, _b: u8) {}
}

impl<P: IntrPeriph> InterruptOperations for P {
    #[inline(always)]
    fn maybe_set_pxie(&self, b: u8) {
        self.pxie_set(b);
    }
}

impl<P: PortNum>
    Batch<
        P,
        Input<Floating>,
        Input<Floating>,
        Input<Floating>,
        Input<Floating>,
        Input<Floating>,
        Input<Floating>,
        Input<Floating>,
        Input<Floating>,
    >
{
    /// Split into a batch of individual GPIO pin proxies
    pub fn new(_port: P) -> Self {
        Self::create()
    }
}

/// Collection of proxies for pins 0 to 7 of a specific port, used to commit configurations for
/// all pins in a single step.
pub struct Batch<PORT: PortNum, DIR0, DIR1, DIR2, DIR3, DIR4, DIR5, DIR6, DIR7> {
    pin0: PinProxy<PORT, Pin0, DIR0>,
    pin1: PinProxy<PORT, Pin1, DIR1>,
    pin2: PinProxy<PORT, Pin2, DIR2>,
    pin3: PinProxy<PORT, Pin3, DIR3>,
    pin4: PinProxy<PORT, Pin4, DIR4>,
    pin5: PinProxy<PORT, Pin5, DIR5>,
    pin6: PinProxy<PORT, Pin6, DIR6>,
    pin7: PinProxy<PORT, Pin7, DIR7>,
}

impl<PORT: PortNum, DIR0, DIR1, DIR2, DIR3, DIR4, DIR5, DIR6, DIR7>
    Batch<PORT, DIR0, DIR1, DIR2, DIR3, DIR4, DIR5, DIR6, DIR7>
{
    #[inline]
    fn write_regs(&self) {
        let pxdir = 0u8
            .set_mask(self.pin0.pxdir_mask())
            .set_mask(self.pin1.pxdir_mask())
            .set_mask(self.pin2.pxdir_mask())
            .set_mask(self.pin3.pxdir_mask())
            .set_mask(self.pin4.pxdir_mask())
            .set_mask(self.pin5.pxdir_mask())
            .set_mask(self.pin6.pxdir_mask())
            .set_mask(self.pin7.pxdir_mask());

        let pxout = 0u8
            .set_mask(self.pin0.pxout_mask())
            .set_mask(self.pin1.pxout_mask())
            .set_mask(self.pin2.pxout_mask())
            .set_mask(self.pin3.pxout_mask())
            .set_mask(self.pin4.pxout_mask())
            .set_mask(self.pin5.pxout_mask())
            .set_mask(self.pin6.pxout_mask())
            .set_mask(self.pin7.pxout_mask());

        let pxren = 0u8
            .set_mask(self.pin0.pxren_mask())
            .set_mask(self.pin1.pxren_mask())
            .set_mask(self.pin2.pxren_mask())
            .set_mask(self.pin3.pxren_mask())
            .set_mask(self.pin4.pxren_mask())
            .set_mask(self.pin5.pxren_mask())
            .set_mask(self.pin6.pxren_mask())
            .set_mask(self.pin7.pxren_mask());

        let pxsel0 = 0u8
            .set_mask(self.pin0.pxsel0_mask())
            .set_mask(self.pin1.pxsel0_mask())
            .set_mask(self.pin2.pxsel0_mask())
            .set_mask(self.pin3.pxsel0_mask())
            .set_mask(self.pin4.pxsel0_mask())
            .set_mask(self.pin5.pxsel0_mask())
            .set_mask(self.pin6.pxsel0_mask())
            .set_mask(self.pin7.pxsel0_mask());

        let pxsel1 = 0u8
            .set_mask(self.pin0.pxsel1_mask())
            .set_mask(self.pin1.pxsel1_mask())
            .set_mask(self.pin2.pxsel1_mask())
            .set_mask(self.pin3.pxsel1_mask())
            .set_mask(self.pin4.pxsel1_mask())
            .set_mask(self.pin5.pxsel1_mask())
            .set_mask(self.pin6.pxsel1_mask())
            .set_mask(self.pin7.pxsel1_mask());

        let p = unsafe { PORT::steal() };
        // Turn off interrupts first so nothing fires during subsequent register writes
        p.maybe_set_pxie(0);
        p.pxsel0_wr(pxsel0);
        p.pxsel1_wr(pxsel1);
        p.pxout_wr(pxout);
        p.pxdir_wr(pxdir);
        p.pxren_wr(pxren);
    }

    #[inline(always)]
    pub(super) fn create() -> Self {
        Self {
            pin0: make_proxy!(),
            pin1: make_proxy!(),
            pin2: make_proxy!(),
            pin3: make_proxy!(),
            pin4: make_proxy!(),
            pin5: make_proxy!(),
            pin6: make_proxy!(),
            pin7: make_proxy!(),
        }
    }

    /// Commits all pin configurations to GPIO registers and returns GPIO parts and turns off all
    /// interrupt enable bits.
    ///
    /// Note that the pin's interrupt flags may become set as a result of
    /// this operation.
    ///
    /// GPIO input/output operations only work after the LOCKLPM5 bit has been set, which is
    /// ensured when passing `&Pmm` into the method, since a `Pmm` is created only by setting
    /// LOCKLPM5.
    #[inline]
    pub fn split(self, _pmm: &Pmm) -> Parts<PORT, DIR0, DIR1, DIR2, DIR3, DIR4, DIR5, DIR6, DIR7> {
        self.write_regs();
        Parts::new()
    }

    /// Edit configuration of pin 0
    #[inline(always)]
    pub fn config_pin0<NEW, F: FnOnce(PinProxy<PORT, Pin0, DIR0>) -> PinProxy<PORT, Pin0, NEW>>(
        self,
        f: F,
    ) -> Batch<PORT, NEW, DIR1, DIR2, DIR3, DIR4, DIR5, DIR6, DIR7> {
        Batch {
            pin0: f(self.pin0),
            pin1: make_proxy!(),
            pin2: make_proxy!(),
            pin3: make_proxy!(),
            pin4: make_proxy!(),
            pin5: make_proxy!(),
            pin6: make_proxy!(),
            pin7: make_proxy!(),
        }
    }

    /// Edit configuration of pin 1
    #[inline(always)]
    pub fn config_pin1<NEW, F: FnOnce(PinProxy<PORT, Pin1, DIR1>) -> PinProxy<PORT, Pin1, NEW>>(
        self,
        f: F,
    ) -> Batch<PORT, DIR0, NEW, DIR2, DIR3, DIR4, DIR5, DIR6, DIR7> {
        Batch {
            pin0: make_proxy!(),
            pin1: f(self.pin1),
            pin2: make_proxy!(),
            pin3: make_proxy!(),
            pin4: make_proxy!(),
            pin5: make_proxy!(),
            pin6: make_proxy!(),
            pin7: make_proxy!(),
        }
    }

    /// Edit configuration of pin 2
    #[inline(always)]
    pub fn config_pin2<NEW, F: FnOnce(PinProxy<PORT, Pin2, DIR2>) -> PinProxy<PORT, Pin2, NEW>>(
        self,
        f: F,
    ) -> Batch<PORT, DIR0, DIR1, NEW, DIR3, DIR4, DIR5, DIR6, DIR7> {
        Batch {
            pin0: make_proxy!(),
            pin1: make_proxy!(),
            pin2: f(self.pin2),
            pin3: make_proxy!(),
            pin4: make_proxy!(),
            pin5: make_proxy!(),
            pin6: make_proxy!(),
            pin7: make_proxy!(),
        }
    }

    /// Edit configuration of pin 3
    #[inline(always)]
    pub fn config_pin3<NEW, F: FnOnce(PinProxy<PORT, Pin3, DIR3>) -> PinProxy<PORT, Pin3, NEW>>(
        self,
        f: F,
    ) -> Batch<PORT, DIR0, DIR1, DIR2, NEW, DIR4, DIR5, DIR6, DIR7> {
        Batch {
            pin0: make_proxy!(),
            pin1: make_proxy!(),
            pin2: make_proxy!(),
            pin3: f(self.pin3),
            pin4: make_proxy!(),
            pin5: make_proxy!(),
            pin6: make_proxy!(),
            pin7: make_proxy!(),
        }
    }

    /// Edit configuration of pin 4
    #[inline(always)]
    pub fn config_pin4<NEW, F: FnOnce(PinProxy<PORT, Pin4, DIR4>) -> PinProxy<PORT, Pin4, NEW>>(
        self,
        f: F,
    ) -> Batch<PORT, DIR0, DIR1, DIR2, DIR3, NEW, DIR5, DIR6, DIR7> {
        Batch {
            pin0: make_proxy!(),
            pin1: make_proxy!(),
            pin2: make_proxy!(),
            pin3: make_proxy!(),
            pin4: f(self.pin4),
            pin5: make_proxy!(),
            pin6: make_proxy!(),
            pin7: make_proxy!(),
        }
    }

    /// Edit configuration of pin 5
    #[inline(always)]
    pub fn config_pin5<NEW, F: FnOnce(PinProxy<PORT, Pin5, DIR5>) -> PinProxy<PORT, Pin5, NEW>>(
        self,
        f: F,
    ) -> Batch<PORT, DIR0, DIR1, DIR2, DIR3, DIR4, NEW, DIR6, DIR7> {
        Batch {
            pin0: make_proxy!(),
            pin1: make_proxy!(),
            pin2: make_proxy!(),
            pin3: make_proxy!(),
            pin4: make_proxy!(),
            pin5: f(self.pin5),
            pin6: make_proxy!(),
            pin7: make_proxy!(),
        }
    }

    /// Edit configuration of pin 6
    #[inline(always)]
    pub fn config_pin6<NEW, F: FnOnce(PinProxy<PORT, Pin6, DIR6>) -> PinProxy<PORT, Pin6, NEW>>(
        self,
        f: F,
    ) -> Batch<PORT, DIR0, DIR1, DIR2, DIR3, DIR4, DIR5, NEW, DIR7> {
        Batch {
            pin0: make_proxy!(),
            pin1: make_proxy!(),
            pin2: make_proxy!(),
            pin3: make_proxy!(),
            pin4: make_proxy!(),
            pin5: make_proxy!(),
            pin6: f(self.pin6),
            pin7: make_proxy!(),
        }
    }

    /// Edit configuration of pin 7
    #[inline(always)]
    pub fn config_pin7<NEW, F: FnOnce(PinProxy<PORT, Pin7, DIR7>) -> PinProxy<PORT, Pin7, NEW>>(
        self,
        f: F,
    ) -> Batch<PORT, DIR0, DIR1, DIR2, DIR3, DIR4, DIR5, DIR6, NEW> {
        Batch {
            pin0: make_proxy!(),
            pin1: make_proxy!(),
            pin2: make_proxy!(),
            pin3: make_proxy!(),
            pin4: make_proxy!(),
            pin5: make_proxy!(),
            pin6: make_proxy!(),
            pin7: f(self.pin7),
        }
    }
}