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
//! Code for GPIO pins

use core::marker::PhantomData;

/// All unlocked pin modes implement this
pub trait IsUnlocked {}

/// All input modes implement this
pub trait InputMode {}

/// All output modes implement this
pub trait OutputMode {}

/// OpenDrain modes implement this
pub trait OpenDrainMode {
    /// Is pull-up enabled
    fn pup() -> bool;
}

/// All the different Alternate Functions you can choose implement this
pub trait AlternateFunctionChoice {
    /// Which Alternate Function (numbered 1 through 15) is this?
    fn number() -> u32;
}

/// Input mode (type state)
pub struct Input<MODE>
where
    MODE: InputMode,
{
    _mode: PhantomData<MODE>,
}
impl<MODE> IsUnlocked for Input<MODE> where MODE: InputMode {}

/// Sub-mode of Input: Floating input (type state)
pub struct Floating;
impl InputMode for Floating {}
impl OpenDrainMode for Floating {
    /// Pull-up is not enabled
    fn pup() -> bool {
        false
    }
}

/// Sub-mode of Input: Pulled down input (type state)
pub struct PullDown;
impl InputMode for PullDown {}

/// Sub-mode of Input: Pulled up input (type state)
pub struct PullUp;
impl InputMode for PullUp {}
impl OpenDrainMode for PullUp {
    /// Pull-up is enabled
    fn pup() -> bool {
        true
    }
}

/// Tri-state
pub struct Tristate;
impl IsUnlocked for Tristate {}

/// Output mode (type state)
pub struct Output<MODE>
where
    MODE: OutputMode,
{
    _mode: PhantomData<MODE>,
}
impl<MODE> IsUnlocked for Output<MODE> where MODE: OutputMode {}

/// AlternateFunction mode (type state for a GPIO pin)
pub struct AlternateFunction<AF, MODE>
where
    AF: AlternateFunctionChoice,
    MODE: OutputMode,
{
    _func: PhantomData<AF>,
    _mode: PhantomData<MODE>,
}
impl<AF, MODE> IsUnlocked for AlternateFunction<AF, MODE>
where
    AF: AlternateFunctionChoice,
    MODE: OutputMode,
{
}

/// Sub-mode of Output/AlternateFunction: Push pull output (type state for
/// Output)
pub struct PushPull;
impl OutputMode for PushPull {}
impl OutputMode for PullDown {}
impl OutputMode for PullUp {}

/// Sub-mode of Output/AlternateFunction: Open drain output (type state for
/// Output)
pub struct OpenDrain<ODM>
where
    ODM: OpenDrainMode,
{
    _pull: PhantomData<ODM>,
}
impl<ODM> OutputMode for OpenDrain<ODM> where ODM: OpenDrainMode {}

/// Alternate function 1 (type state)
pub struct AF1;
impl AlternateFunctionChoice for AF1 {
    fn number() -> u32 {
        1
    }
}

/// Alternate function 2 (type state)
pub struct AF2;
impl AlternateFunctionChoice for AF2 {
    fn number() -> u32 {
        2
    }
}

/// Alternate function 3 (type state)
pub struct AF3;
impl AlternateFunctionChoice for AF3 {
    fn number() -> u32 {
        3
    }
}

/// Alternate function 4 (type state)
pub struct AF4;
impl AlternateFunctionChoice for AF4 {
    fn number() -> u32 {
        4
    }
}

/// Alternate function 5 (type state)
pub struct AF5;
impl AlternateFunctionChoice for AF5 {
    fn number() -> u32 {
        5
    }
}

/// Alternate function 6 (type state)
pub struct AF6;
impl AlternateFunctionChoice for AF6 {
    fn number() -> u32 {
        6
    }
}

/// Alternate function 7 (type state)
pub struct AF7;
impl AlternateFunctionChoice for AF7 {
    fn number() -> u32 {
        7
    }
}

/// Alternate function 8 (type state)
pub struct AF8;
impl AlternateFunctionChoice for AF8 {
    fn number() -> u32 {
        8
    }
}

/// Alternate function 9 (type state)
pub struct AF9;
impl AlternateFunctionChoice for AF9 {
    fn number() -> u32 {
        9
    }
}

// 10 through 13 are not available on this chip.

/// Alternate function 14 (type state)
pub struct AF14;
impl AlternateFunctionChoice for AF14 {
    fn number() -> u32 {
        14
    }
}

/// Pin is locked through the GPIOCR register
pub struct Locked;

/// Sets when a GPIO pin triggers an interrupt.
pub enum InterruptMode {
    /// Interrupt when level is low
    LevelLow,
    /// Interrupt when level is high
    LevelHigh,
    /// Interrupt on rising edge
    EdgeRising,
    /// Interrupt on falling edge
    EdgeFalling,
    /// Interrupt on both rising and falling edges
    EdgeBoth,
    /// Disable interrupts on this pin
    Disabled,
}

// End of file