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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
//! API for the TIM peripherals
//!
//! ## Examples
//!
//! TODO.
use core::cell::Cell;

use cortex_m::interrupt::Mutex;

use crate::clock::Clocks;
use crate::gpio::{
    AltFunc1, AltFunc2, AltFunc3, DynPinId, Pin, PinId, PA0, PA1, PA10, PA11, PA12, PA13, PA14,
    PA15, PA2, PA3, PA4, PA5, PA6, PA7, PB0, PB1, PB10, PB11, PB12, PB13, PB14, PB15, PB2, PB3,
    PB4, PB5, PB6, PB7, PB8, PB9, PC0, PC1, PD0, PD1, PD10, PD11, PD12, PD13, PD14, PD15, PD2, PD3,
    PD4, PD5, PD6, PD7, PD8, PD9, PE0, PE1, PE10, PE11, PE12, PE13, PE14, PE15, PE2, PE3, PE4, PE5,
    PE6, PE7, PE8, PE9, PF0, PF1, PF10, PF11, PF12, PF13, PF14, PF15, PF2, PF3, PF4, PF5, PF6, PF7,
    PF8, PF9, PG0, PG1, PG2, PG3, PG6,
};
use crate::time::Hertz;
use crate::typelevel::Sealed;
use crate::{disable_interrupt, prelude::*};
use crate::{enable_interrupt, pac};

pub static MS_COUNTER: Mutex<Cell<u32>> = Mutex::new(Cell::new(0));

//==================================================================================================
// Defintions
//==================================================================================================

/// Interrupt events
//pub enum Event {
/// Timer timed out / count down ended
//TimeOut,
//}

#[derive(Default, Debug, PartialEq, Eq, Copy, Clone)]
pub struct CascadeCtrl {
    /// Enable Cascade 0 signal active as a requirement for counting
    pub enb_start_src_csd0: bool,
    /// Invert Cascade 0, making it active low
    pub inv_csd0: bool,
    /// Enable Cascade 1 signal active as a requirement for counting
    pub enb_start_src_csd1: bool,
    /// Invert Cascade 1, making it active low
    pub inv_csd1: bool,
    /// Specify required operation if both Cascade 0 and Cascade 1 are active.
    /// 0 is a logical AND of both cascade signals, 1 is a logical OR
    pub dual_csd_op: bool,
    /// Enable trigger mode for Cascade 0. In trigger mode, couting will start with the selected
    /// cascade signal active, but once the counter is active, cascade control will be ignored
    pub trg_csd0: bool,
    /// Trigger mode, identical to [`trg_csd0`](CascadeCtrl) but for Cascade 1
    pub trg_csd1: bool,
    /// Enable Cascade 2 signal active as a requirement to stop counting. This mode is similar
    /// to the REQ_STOP control bit, but signalled by a Cascade source
    pub enb_stop_src_csd2: bool,
    /// Invert Cascade 2, making it active low
    pub inv_csd2: bool,
    /// The counter is automatically disabled if the corresponding Cascade 2 level-sensitive input
    /// souce is active when the count reaches 0. If the counter is not 0, the cascade control is
    /// ignored
    pub trg_csd2: bool,
}

#[derive(Debug, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum CascadeSel {
    Sel0 = 0,
    Sel1 = 1,
    Sel2 = 2,
}

#[derive(Debug, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct InvalidCascadeSourceId;

#[derive(Debug, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum CascadeSource {
    PortA(u8),
    PortB(u8),
    PortC(u8),
    PortD(u8),
    PortE(u8),
    Tim(u8),
    TxEv,
    AdcIrq,
    RomSbe,
    RomMbe,
    Ram0Sbe,
    Ram0Mbe,
    Ram1Sbe,
    Ram2Mbe,
    WdogIrq,
}

impl CascadeSource {
    fn id(&self) -> Result<u8, InvalidCascadeSourceId> {
        let port_check = |base: u8, id: u8| {
            if id > 15 {
                return Err(InvalidCascadeSourceId);
            }
            Ok(base + id)
        };
        match self {
            CascadeSource::PortA(id) => port_check(0, *id),
            CascadeSource::PortB(id) => port_check(16, *id),
            CascadeSource::PortC(id) => port_check(32, *id),
            CascadeSource::PortD(id) => port_check(48, *id),
            CascadeSource::PortE(id) => port_check(65, *id),
            CascadeSource::Tim(id) => {
                if *id > 23 {
                    return Err(InvalidCascadeSourceId);
                }
                Ok(80 + id)
            }
            CascadeSource::TxEv => Ok(104),
            CascadeSource::AdcIrq => Ok(105),
            CascadeSource::RomSbe => Ok(106),
            CascadeSource::RomMbe => Ok(106),
            CascadeSource::Ram0Sbe => Ok(108),
            CascadeSource::Ram0Mbe => Ok(109),
            CascadeSource::Ram1Sbe => Ok(110),
            CascadeSource::Ram2Mbe => Ok(111),
            CascadeSource::WdogIrq => Ok(112),
        }
    }
}

//==================================================================================================
// Valid TIM and PIN combinations
//==================================================================================================

pub trait TimPin {
    const DYN: DynPinId;
}

pub trait ValidTim {
    // TIM ID ranging from 0 to 23 for 24 TIM peripherals
    const TIM_ID: u8;
    const IRQ: pac::Interrupt;

    fn clock(clocks: &Clocks) -> Hertz {
        if Self::TIM_ID <= 15 {
            clocks.apb1()
        } else {
            clocks.apb2()
        }
    }
}

macro_rules! tim_markers {
    (
        $(
            ($TimX:path, $id:expr, $Irq:path),
        )+
    ) => {
        $(
            impl ValidTim for $TimX {
                const TIM_ID: u8 = $id;
                const IRQ: pac::Interrupt = $Irq;
            }
        )+
    };
}

tim_markers!(
    (pac::Tim0, 0, pac::Interrupt::TIM0),
    (pac::Tim1, 1, pac::Interrupt::TIM1),
    (pac::Tim2, 2, pac::Interrupt::TIM2),
    (pac::Tim3, 3, pac::Interrupt::TIM3),
    (pac::Tim4, 4, pac::Interrupt::TIM4),
    (pac::Tim5, 5, pac::Interrupt::TIM5),
    (pac::Tim6, 6, pac::Interrupt::TIM6),
    (pac::Tim7, 7, pac::Interrupt::TIM7),
    (pac::Tim8, 8, pac::Interrupt::TIM8),
    (pac::Tim9, 9, pac::Interrupt::TIM9),
    (pac::Tim10, 10, pac::Interrupt::TIM10),
    (pac::Tim11, 11, pac::Interrupt::TIM11),
    (pac::Tim12, 12, pac::Interrupt::TIM12),
    (pac::Tim13, 13, pac::Interrupt::TIM13),
    (pac::Tim14, 14, pac::Interrupt::TIM14),
    (pac::Tim15, 15, pac::Interrupt::TIM15),
    (pac::Tim16, 16, pac::Interrupt::TIM16),
    (pac::Tim17, 17, pac::Interrupt::TIM17),
    (pac::Tim18, 18, pac::Interrupt::TIM18),
    (pac::Tim19, 19, pac::Interrupt::TIM19),
    (pac::Tim20, 20, pac::Interrupt::TIM20),
    (pac::Tim21, 21, pac::Interrupt::TIM21),
    (pac::Tim22, 22, pac::Interrupt::TIM22),
    (pac::Tim23, 23, pac::Interrupt::TIM23),
);

pub trait ValidTimAndPin<Pin: TimPin, Tim: ValidTim>: Sealed {}

macro_rules! valid_pin_and_tims {
    (
        $(
            ($PinX:ident, $AltFunc:ident, $TimX:path),
        )+
    ) => {
        $(
            impl TimPin for Pin<$PinX, $AltFunc>
            where
                $PinX: PinId,
            {
                const DYN: DynPinId = $PinX::DYN;
            }

            impl<
                PinInstance: TimPin,
                Tim: ValidTim
            > ValidTimAndPin<PinInstance, Tim> for (Pin<$PinX, $AltFunc>, $TimX)
            where
                Pin<$PinX, $AltFunc>: TimPin,
                $PinX: PinId,
            {
            }

            impl Sealed for (Pin<$PinX, $AltFunc>, $TimX) {}
        )+
    };
}

valid_pin_and_tims!(
    (PA0, AltFunc1, pac::Tim0),
    (PA1, AltFunc1, pac::Tim1),
    (PA2, AltFunc1, pac::Tim2),
    (PA3, AltFunc1, pac::Tim3),
    (PA4, AltFunc1, pac::Tim4),
    (PA5, AltFunc1, pac::Tim5),
    (PA6, AltFunc1, pac::Tim6),
    (PA7, AltFunc1, pac::Tim7),
    (PA10, AltFunc2, pac::Tim23),
    (PA11, AltFunc2, pac::Tim22),
    (PA12, AltFunc2, pac::Tim21),
    (PA13, AltFunc2, pac::Tim20),
    (PA14, AltFunc2, pac::Tim19),
    (PA15, AltFunc2, pac::Tim18),
    (PB0, AltFunc2, pac::Tim17),
    (PB1, AltFunc2, pac::Tim16),
    (PB2, AltFunc2, pac::Tim15),
    (PB3, AltFunc2, pac::Tim14),
    (PB4, AltFunc2, pac::Tim13),
    (PB5, AltFunc2, pac::Tim12),
    (PB6, AltFunc2, pac::Tim11),
    (PB7, AltFunc2, pac::Tim10),
    (PB8, AltFunc2, pac::Tim9),
    (PB9, AltFunc2, pac::Tim8),
    (PB10, AltFunc2, pac::Tim7),
    (PB11, AltFunc2, pac::Tim6),
    (PB12, AltFunc2, pac::Tim5),
    (PB13, AltFunc2, pac::Tim4),
    (PB14, AltFunc2, pac::Tim3),
    (PB15, AltFunc2, pac::Tim2),
    (PC0, AltFunc2, pac::Tim1),
    (PC1, AltFunc2, pac::Tim0),
    (PD0, AltFunc2, pac::Tim0),
    (PD1, AltFunc2, pac::Tim1),
    (PD2, AltFunc2, pac::Tim2),
    (PD3, AltFunc2, pac::Tim3),
    (PD4, AltFunc2, pac::Tim4),
    (PD5, AltFunc2, pac::Tim5),
    (PD6, AltFunc2, pac::Tim6),
    (PD7, AltFunc2, pac::Tim7),
    (PD8, AltFunc2, pac::Tim8),
    (PD9, AltFunc2, pac::Tim9),
    (PD10, AltFunc2, pac::Tim10),
    (PD11, AltFunc2, pac::Tim11),
    (PD12, AltFunc2, pac::Tim12),
    (PD13, AltFunc2, pac::Tim13),
    (PD14, AltFunc2, pac::Tim14),
    (PD15, AltFunc2, pac::Tim15),
    (PE0, AltFunc2, pac::Tim16),
    (PE1, AltFunc2, pac::Tim17),
    (PE2, AltFunc2, pac::Tim18),
    (PE3, AltFunc2, pac::Tim19),
    (PE4, AltFunc2, pac::Tim20),
    (PE5, AltFunc2, pac::Tim21),
    (PE6, AltFunc2, pac::Tim22),
    (PE7, AltFunc2, pac::Tim23),
    (PE8, AltFunc3, pac::Tim16),
    (PE9, AltFunc3, pac::Tim17),
    (PE10, AltFunc3, pac::Tim18),
    (PE11, AltFunc3, pac::Tim19),
    (PE12, AltFunc3, pac::Tim20),
    (PE13, AltFunc3, pac::Tim21),
    (PE14, AltFunc3, pac::Tim22),
    (PE15, AltFunc3, pac::Tim23),
    (PF0, AltFunc3, pac::Tim0),
    (PF1, AltFunc3, pac::Tim1),
    (PF2, AltFunc3, pac::Tim2),
    (PF3, AltFunc3, pac::Tim3),
    (PF4, AltFunc3, pac::Tim4),
    (PF5, AltFunc3, pac::Tim5),
    (PF6, AltFunc3, pac::Tim6),
    (PF7, AltFunc3, pac::Tim7),
    (PF8, AltFunc3, pac::Tim8),
    (PF9, AltFunc3, pac::Tim9),
    (PF10, AltFunc3, pac::Tim10),
    (PF11, AltFunc3, pac::Tim11),
    (PF12, AltFunc3, pac::Tim12),
    (PF13, AltFunc2, pac::Tim19),
    (PF14, AltFunc2, pac::Tim20),
    (PF15, AltFunc2, pac::Tim21),
    (PG0, AltFunc2, pac::Tim22),
    (PG1, AltFunc2, pac::Tim23),
    (PG2, AltFunc1, pac::Tim9),
    (PG3, AltFunc1, pac::Tim10),
    (PG6, AltFunc1, pac::Tim12),
);

//==================================================================================================
// Register Interface for TIM registers and TIM pins
//==================================================================================================

/// Clear the reset bit of the TIM, holding it in reset
///
/// # Safety
///
/// Only the bit related to the corresponding TIM peripheral is modified
#[inline]
fn assert_tim_reset(syscfg: &mut pac::Sysconfig, tim_id: u8) {
    syscfg
        .tim_reset()
        .modify(|r, w| unsafe { w.bits(r.bits() & !(1 << tim_id as u32)) })
}

#[inline]
fn deassert_tim_reset(syscfg: &mut pac::Sysconfig, tim_id: u8) {
    syscfg
        .tim_reset()
        .modify(|r, w| unsafe { w.bits(r.bits() | (1 << tim_id as u32)) })
}

pub type TimRegBlock = pac::tim0::RegisterBlock;

/// Register interface.
///
/// This interface provides valid TIM pins a way to access their corresponding TIM
/// registers
///
/// # Safety
///
/// Users should only implement the [`tim_id`] function. No default function
/// implementations should be overridden. The implementing type must also have
/// "control" over the corresponding pin ID, i.e. it must guarantee that a each
/// pin ID is a singleton.
pub(super) unsafe trait TimRegInterface {
    fn tim_id(&self) -> u8;

    const PORT_BASE: *const pac::tim0::RegisterBlock = pac::Tim0::ptr() as *const _;

    /// All 24 TIM blocks are identical. This helper functions returns the correct
    /// memory mapped peripheral depending on the TIM ID.
    #[inline(always)]
    fn reg(&self) -> &TimRegBlock {
        unsafe { &*Self::PORT_BASE.offset(self.tim_id() as isize) }
    }

    #[inline(always)]
    fn mask_32(&self) -> u32 {
        1 << self.tim_id()
    }

    /// Clear the reset bit of the TIM, holding it in reset
    ///
    /// # Safety
    ///
    /// Only the bit related to the corresponding TIM peripheral is modified
    #[inline]
    #[allow(dead_code)]
    fn assert_tim_reset(&self, syscfg: &mut pac::Sysconfig) {
        assert_tim_reset(syscfg, self.tim_id());
    }

    #[inline]
    #[allow(dead_code)]
    fn deassert_time_reset(&self, syscfg: &mut pac::Sysconfig) {
        deassert_tim_reset(syscfg, self.tim_id());
    }
}

/// Provide a safe register interface for [`ValidTimAndPin`]s
///
/// This `struct` takes ownership of a [`ValidTimAndPin`] and provides an API to
/// access the corresponding registers.
pub(super) struct TimAndPinRegister<Pin: TimPin, Tim: ValidTim> {
    pin: Pin,
    tim: Tim,
}

pub(super) struct TimRegister<TIM: ValidTim> {
    tim: TIM,
}

impl<TIM: ValidTim> TimRegister<TIM> {
    #[inline]
    pub(super) unsafe fn new(tim: TIM) -> Self {
        TimRegister { tim }
    }

    pub(super) fn release(self) -> TIM {
        self.tim
    }
}

unsafe impl<Tim: ValidTim> TimRegInterface for TimRegister<Tim> {
    #[inline(always)]
    fn tim_id(&self) -> u8 {
        Tim::TIM_ID
    }
}

impl<Pin: TimPin, Tim: ValidTim> TimAndPinRegister<Pin, Tim>
where
    (Pin, Tim): ValidTimAndPin<Pin, Tim>,
{
    #[inline]
    pub(super) unsafe fn new(pin: Pin, tim: Tim) -> Self {
        TimAndPinRegister { pin, tim }
    }

    pub(super) fn release(self) -> (Pin, Tim) {
        (self.pin, self.tim)
    }
}

unsafe impl<Pin: TimPin, Tim: ValidTim> TimRegInterface for TimAndPinRegister<Pin, Tim> {
    #[inline(always)]
    fn tim_id(&self) -> u8 {
        Tim::TIM_ID
    }
}

pub(super) struct TimDynRegister {
    tim_id: u8,
    #[allow(dead_code)]
    pin_id: DynPinId,
}

impl<Pin: TimPin, Tim: ValidTim> From<TimAndPinRegister<Pin, Tim>> for TimDynRegister {
    fn from(_reg: TimAndPinRegister<Pin, Tim>) -> Self {
        Self {
            tim_id: Tim::TIM_ID,
            pin_id: Pin::DYN,
        }
    }
}

unsafe impl TimRegInterface for TimDynRegister {
    #[inline(always)]
    fn tim_id(&self) -> u8 {
        self.tim_id
    }
}

//==================================================================================================
// Timers
//==================================================================================================

/// Hardware timers
pub struct CountdownTimer<TIM: ValidTim> {
    tim: TimRegister<TIM>,
    curr_freq: Hertz,
    clock: Hertz,
    rst_val: u32,
    last_cnt: u32,
    listening: bool,
}

#[inline]
fn enable_tim_clk(syscfg: &mut pac::Sysconfig, idx: u8) {
    syscfg
        .tim_clk_enable()
        .modify(|r, w| unsafe { w.bits(r.bits() | (1 << idx)) });
}

unsafe impl<TIM: ValidTim> TimRegInterface for CountdownTimer<TIM> {
    #[inline]
    fn tim_id(&self) -> u8 {
        TIM::TIM_ID
    }
}

impl<Tim: ValidTim> CountdownTimer<Tim> {
    /// Create a new countdown timer, but does not start it.
    ///
    /// You can use [Self::start] to start the countdown timer, and you may optionally call
    /// [Self::listen] to enable interrupts for the TIM peripheral as well.
    pub fn new(syscfg: &mut pac::Sysconfig, tim: Tim, clocks: &Clocks) -> Self {
        enable_tim_clk(syscfg, Tim::TIM_ID);
        assert_tim_reset(syscfg, Tim::TIM_ID);
        cortex_m::asm::nop();
        cortex_m::asm::nop();
        deassert_tim_reset(syscfg, Tim::TIM_ID);

        CountdownTimer {
            tim: unsafe { TimRegister::new(tim) },
            clock: Tim::clock(clocks),
            rst_val: 0,
            curr_freq: 0_u32.Hz(),
            listening: false,
            last_cnt: 0,
        }
    }

    #[inline]
    pub fn start(&mut self, timeout: impl Into<Hertz>) {
        self.load(timeout);
        self.enable();
    }

    /// Listen for events. Depending on the IRQ configuration, this also activates the IRQ in the
    /// IRQSEL peripheral for the provided interrupt and unmasks the interrupt
    #[inline]
    pub fn listen(&mut self) {
        self.listening = true;
        self.enable_interrupt();
        unsafe { enable_interrupt(Tim::IRQ) }
    }

    /// Return `Ok` if the timer has wrapped. Peripheral will automatically clear the
    /// flag and restart the time if configured correctly
    pub fn wait(&mut self) -> nb::Result<(), void::Void> {
        let cnt = self.tim.reg().cnt_value().read().bits();
        if (cnt > self.last_cnt) || cnt == 0 {
            self.last_cnt = self.rst_val;
            Ok(())
        } else {
            self.last_cnt = cnt;
            Err(nb::Error::WouldBlock)
        }
    }

    #[inline]
    pub fn stop(&mut self) {
        self.tim.reg().ctrl().write(|w| w.enable().clear_bit());
    }

    #[inline]
    pub fn unlisten(&mut self) {
        self.listening = true;
        self.disable_interrupt();
        disable_interrupt(Tim::IRQ);
    }

    #[inline(always)]
    pub fn enable_interrupt(&mut self) {
        self.tim.reg().ctrl().modify(|_, w| w.irq_enb().set_bit());
    }

    #[inline(always)]
    pub fn disable_interrupt(&mut self) {
        self.tim.reg().ctrl().modify(|_, w| w.irq_enb().clear_bit());
    }

    #[inline]
    pub fn release(self, syscfg: &mut pac::Sysconfig) -> Tim {
        self.tim.reg().ctrl().write(|w| w.enable().clear_bit());
        syscfg
            .tim_clk_enable()
            .modify(|r, w| unsafe { w.bits(r.bits() & !(1 << Tim::TIM_ID)) });
        self.tim.release()
    }

    /// Load the count down timer with a timeout but do not start it.
    pub fn load(&mut self, timeout: impl Into<Hertz>) {
        self.tim.reg().ctrl().modify(|_, w| w.enable().clear_bit());
        self.curr_freq = timeout.into();
        self.rst_val = self.clock.raw() / self.curr_freq.raw();
        self.set_reload(self.rst_val);
        self.set_count(0);
    }

    #[inline(always)]
    pub fn set_reload(&mut self, val: u32) {
        self.tim.reg().rst_value().write(|w| unsafe { w.bits(val) });
    }

    #[inline(always)]
    pub fn set_count(&mut self, val: u32) {
        self.tim.reg().cnt_value().write(|w| unsafe { w.bits(val) });
    }

    #[inline(always)]
    pub fn count(&self) -> u32 {
        self.tim.reg().cnt_value().read().bits()
    }

    #[inline(always)]
    pub fn enable(&mut self) {
        self.tim.reg().ctrl().modify(|_, w| w.enable().set_bit());
    }

    #[inline(always)]
    pub fn disable(&mut self) {
        self.tim.reg().ctrl().modify(|_, w| w.enable().clear_bit());
    }

    /// Disable the counter, setting both enable and active bit to 0
    #[inline]
    pub fn auto_disable(self, enable: bool) -> Self {
        if enable {
            self.tim
                .reg()
                .ctrl()
                .modify(|_, w| w.auto_disable().set_bit());
        } else {
            self.tim
                .reg()
                .ctrl()
                .modify(|_, w| w.auto_disable().clear_bit());
        }
        self
    }

    /// This option only applies when the Auto-Disable functionality is 0.
    ///
    /// The active bit is changed to 0 when count reaches 0, but the counter stays
    /// enabled. When Auto-Disable is 1, Auto-Deactivate is implied
    #[inline]
    pub fn auto_deactivate(self, enable: bool) -> Self {
        if enable {
            self.tim
                .reg()
                .ctrl()
                .modify(|_, w| w.auto_deactivate().set_bit());
        } else {
            self.tim
                .reg()
                .ctrl()
                .modify(|_, w| w.auto_deactivate().clear_bit());
        }
        self
    }

    /// Configure the cascade parameters
    #[inline]
    pub fn cascade_control(&mut self, ctrl: CascadeCtrl) {
        self.tim.reg().csd_ctrl().write(|w| {
            w.csden0().bit(ctrl.enb_start_src_csd0);
            w.csdinv0().bit(ctrl.inv_csd0);
            w.csden1().bit(ctrl.enb_start_src_csd1);
            w.csdinv1().bit(ctrl.inv_csd1);
            w.dcasop().bit(ctrl.dual_csd_op);
            w.csdtrg0().bit(ctrl.trg_csd0);
            w.csdtrg1().bit(ctrl.trg_csd1);
            w.csden2().bit(ctrl.enb_stop_src_csd2);
            w.csdinv2().bit(ctrl.inv_csd2);
            w.csdtrg2().bit(ctrl.trg_csd2)
        });
    }

    #[inline]
    pub fn cascade_0_source(&mut self, src: CascadeSource) -> Result<(), InvalidCascadeSourceId> {
        let id = src.id()?;
        self.tim
            .reg()
            .cascade0()
            .write(|w| unsafe { w.cassel().bits(id) });
        Ok(())
    }

    #[inline]
    pub fn cascade_1_source(&mut self, src: CascadeSource) -> Result<(), InvalidCascadeSourceId> {
        let id = src.id()?;
        self.tim
            .reg()
            .cascade1()
            .write(|w| unsafe { w.cassel().bits(id) });
        Ok(())
    }

    #[inline]
    pub fn cascade_2_source(&mut self, src: CascadeSource) -> Result<(), InvalidCascadeSourceId> {
        let id = src.id()?;
        self.tim
            .reg()
            .cascade2()
            .write(|w| unsafe { w.cassel().bits(id) });
        Ok(())
    }

    #[inline]
    pub fn curr_freq(&self) -> Hertz {
        self.curr_freq
    }

    #[inline]
    pub fn listening(&self) -> bool {
        self.listening
    }
}

impl<Tim: ValidTim> embedded_hal::delay::DelayNs for CountdownTimer<Tim> {
    fn delay_ns(&mut self, ns: u32) {
        let ticks = (u64::from(ns)) * (u64::from(self.clock.raw())) / 1_000_000_000;

        let full_cycles = ticks >> 32;
        let mut last_count;
        let mut new_count;
        if full_cycles > 0 {
            self.set_reload(u32::MAX);
            self.set_count(u32::MAX);
            self.enable();

            for _ in 0..full_cycles {
                // Always ensure that both values are the same at the start.
                new_count = self.count();
                last_count = new_count;
                loop {
                    new_count = self.count();
                    if new_count == 0 {
                        // Wait till timer has wrapped.
                        while self.count() == 0 {
                            cortex_m::asm::nop()
                        }
                        break;
                    }
                    // Timer has definitely wrapped.
                    if new_count > last_count {
                        break;
                    }
                    last_count = new_count;
                }
            }
        }
        let ticks = (ticks & u32::MAX as u64) as u32;
        self.disable();
        if ticks > 1 {
            self.set_reload(ticks);
            self.set_count(ticks);
            self.enable();
            last_count = ticks;

            loop {
                new_count = self.count();
                if new_count == 0 || (new_count > last_count) {
                    break;
                }
                last_count = new_count;
            }
        }

        self.disable();
    }
}

//==================================================================================================
// MS tick implementations
//==================================================================================================

// Set up a millisecond timer on TIM0. Please note that the user still has to provide an IRQ handler
// which should call [default_ms_irq_handler].
pub fn set_up_ms_tick<Tim: ValidTim>(
    sys_cfg: &mut pac::Sysconfig,
    tim: Tim,
    clocks: &Clocks,
) -> CountdownTimer<Tim> {
    let mut ms_timer = CountdownTimer::new(sys_cfg, tim, clocks);
    ms_timer.listen();
    ms_timer.start(1000.Hz());
    ms_timer
}

/// This function can be called in a specified interrupt handler to increment
/// the MS counter
pub fn default_ms_irq_handler() {
    cortex_m::interrupt::free(|cs| {
        let mut ms = MS_COUNTER.borrow(cs).get();
        ms += 1;
        MS_COUNTER.borrow(cs).set(ms);
    });
}

/// Get the current MS tick count
pub fn get_ms_ticks() -> u32 {
    cortex_m::interrupt::free(|cs| MS_COUNTER.borrow(cs).get())
}

pub struct DelayMs<Tim: ValidTim = pac::Tim0>(CountdownTimer<Tim>);

impl<Tim: ValidTim> DelayMs<Tim> {
    pub fn new(timer: CountdownTimer<Tim>) -> Option<Self> {
        if timer.curr_freq() != Hertz::from_raw(1000) || !timer.listening() {
            return None;
        }
        Some(Self(timer))
    }
}

/// This assumes that the user has already set up a MS tick timer with [set_up_ms_tick]
impl embedded_hal::delay::DelayNs for DelayMs {
    fn delay_ns(&mut self, ns: u32) {
        let ns_as_ms = ns / 1_000_000;
        if self.0.curr_freq() != Hertz::from_raw(1000) || !self.0.listening() {
            return;
        }
        let start_time = get_ms_ticks();
        while get_ms_ticks() - start_time < ns_as_ms {
            cortex_m::asm::nop();
        }
    }
}