aboutsummaryrefslogtreecommitdiffstats
path: root/src/pipe/ctrl_pipe.rs
blob: 360a0e7e86a323cce57c24f6008bb19dce72131a (plain)
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
/// Host Control Pipe.
///
/// Offset: 0x0c
/// Reset: 0xXXXX
/// Property: PAC Write-Protection, Write-Synchronized, Read-Synchronized
#[derive(Clone, Copy, Debug)]
#[repr(C, packed)]
pub struct CtrlPipe(u16);

pub struct R {
    bits: u16,
}

pub struct W {
    bits: u16,
}

impl CtrlPipe {
    pub fn read(self) -> R {
        R { bits: self.0 }
    }

    pub fn write<F>(&mut self, f: F)
    where
        F: FnOnce(&mut W) -> &mut W,
    {
        let mut w = W { bits: self.0 };
        f(&mut w);
        self.0 = w.bits;
    }

    pub fn modify<F>(&mut self, f: F)
    where
        for<'w> F: FnOnce(&R, &'w mut W) -> &'w mut W,
    {
        let r = R { bits: self.0 };
        let mut w = W { bits: self.0 };
        f(&r, &mut w);
        self.0 = w.bits;
    }
}

impl From<u16> for CtrlPipe {
    fn from(v: u16) -> Self {
        Self(v)
    }
}

impl R {
    /// Value in raw bits.
    pub fn bits(&self) -> u16 {
        self.bits
    }

    pub fn permax(&self) -> PErMaxR {
        let bits = {
            const POS: u8 = 12;
            const MASK: u16 = 0xf;
            ((self.bits >> POS) & MASK) as u8
        };

        PErMaxR(bits)
    }

    pub fn pepnum(&self) -> PEpNumR {
        let bits = {
            const POS: u8 = 8;
            const MASK: u16 = 0xf;
            ((self.bits >> POS) & MASK) as u8
        };

        PEpNumR(bits)
    }

    pub fn pdaddr(&self) -> PDAddrR {
        let bits = {
            const POS: u8 = 0;
            const MASK: u16 = 0x3f;
            ((self.bits >> POS) & MASK) as u8
        };

        PDAddrR(bits)
    }
}

/// Pipe Error Max Number
///
/// These bits define the maximum number of error for this Pipe before
/// freezing the pipe automatically.
pub struct PErMaxR(u8);
impl PErMaxR {
    pub fn max(&self) -> u8 {
        self.0
    }
}

/// Pipe EndPoint Number
///
/// These bits define the number of endpoint for this Pipe.
pub struct PEpNumR(u8);
impl PEpNumR {
    pub fn epnum(&self) -> u8 {
        self.0
    }
}

/// Pipe Device Address
///
/// These bits define the Device Address for this pipe.
pub struct PDAddrR(u8);
impl PDAddrR {
    pub fn addr(&self) -> u8 {
        self.0
    }
}

impl W {
    /// Write raw bits.

    pub unsafe fn bits(&mut self, v: u16) -> &mut Self {
        self.bits = v;
        self
    }

    pub fn permax(&mut self) -> PErMaxW {
        PErMaxW { w: self }
    }

    pub fn pepnum(&mut self) -> PEpNumW {
        PEpNumW { w: self }
    }

    pub fn pdaddr(&mut self) -> PDAddrW {
        PDAddrW { w: self }
    }
}

pub struct PErMaxW<'a> {
    w: &'a mut W,
}
impl<'a> PErMaxW<'a> {
    pub unsafe fn bits(self, v: u8) -> &'a mut W {
        const POS: u8 = 12;
        const MASK: u8 = 0xf;
        self.w.bits &= !(u16::from(MASK) << POS);
        self.w.bits |= u16::from(v & MASK) << POS;
        self.w
    }

    pub fn set_max(self, v: u8) -> &'a mut W {
        unsafe { self.bits(v) }
    }
}

pub struct PEpNumW<'a> {
    w: &'a mut W,
}
impl<'a> PEpNumW<'a> {
    pub unsafe fn bits(self, v: u8) -> &'a mut W {
        const POS: u8 = 8;
        const MASK: u8 = 0xf;
        self.w.bits &= !(u16::from(MASK) << POS);
        self.w.bits |= u16::from(v & MASK) << POS;
        self.w
    }

    pub fn set_epnum(self, v: u8) -> &'a mut W {
        unsafe { self.bits(v) }
    }
}

pub struct PDAddrW<'a> {
    w: &'a mut W,
}
impl<'a> PDAddrW<'a> {
    pub unsafe fn bits(self, v: u8) -> &'a mut W {
        const POS: u8 = 0;
        const MASK: u8 = 0x3f;
        self.w.bits &= !(u16::from(MASK) << POS);
        self.w.bits |= u16::from(v & MASK) << POS;
        self.w
    }

    pub fn set_addr(self, v: u8) -> &'a mut W {
        unsafe { self.bits(v) }
    }
}