aboutsummaryrefslogtreecommitdiff
path: root/homogeneous/src/lib.rs
blob: a4f0ec570e76dadbd6735148f1dd78020e8e35b6 (plain) (blame)
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
//! Fake multi-core PAC

#![no_std]

use core::{
    cmp::Ordering,
    ops::{Add, Sub},
};

use bare_metal::Nr;
use rtfm::Monotonic;

// both cores have the exact same interrupts
pub use Interrupt_0 as Interrupt_1;

// Fake priority bits
pub const NVIC_PRIO_BITS: u8 = 3;

pub fn xpend(_core: u8, _interrupt: impl Nr) {}

/// Fake monotonic timer
pub struct MT;

unsafe impl Monotonic for MT {
    type Instant = Instant;

    fn ratio() -> u32 {
        1
    }

    unsafe fn reset() {
        (0xE0001004 as *mut u32).write_volatile(0)
    }

    fn now() -> Instant {
        unsafe { Instant((0xE0001004 as *const u32).read_volatile() as i32) }
    }

    fn zero() -> Instant {
        Instant(0)
    }
}

#[derive(Clone, Copy, Eq, PartialEq)]
pub struct Instant(i32);

impl Add<u32> for Instant {
    type Output = Instant;

    fn add(self, rhs: u32) -> Self {
        Instant(self.0.wrapping_add(rhs as i32))
    }
}

impl Sub for Instant {
    type Output = u32;

    fn sub(self, rhs: Self) -> u32 {
        self.0.checked_sub(rhs.0).unwrap() as u32
    }
}

impl Ord for Instant {
    fn cmp(&self, rhs: &Self) -> Ordering {
        self.0.wrapping_sub(rhs.0).cmp(&0)
    }
}

impl PartialOrd for Instant {
    fn partial_cmp(&self, rhs: &Self) -> Option<Ordering> {
        Some(self.cmp(rhs))
    }
}

// Fake interrupts
#[allow(non_camel_case_types)]
#[derive(Clone, Copy)]
#[repr(u8)]
pub enum Interrupt_0 {
    I0 = 0,
    I1 = 1,
    I2 = 2,
    I3 = 3,
    I4 = 4,
    I5 = 5,
    I6 = 6,
    I7 = 7,
}

unsafe impl Nr for Interrupt_0 {
    fn nr(&self) -> u8 {
        *self as u8
    }
}