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
|
#![no_std]
#![no_main]
#[cfg(feature = "rp2040")]
teleprobe_meta::target!(b"rpi-pico");
#[cfg(feature = "rp235xb")]
teleprobe_meta::target!(b"pimoroni-pico-plus-2");
use defmt::{assert, *};
use embassy_executor::Spawner;
#[cfg(feature = "rp2040")]
use embassy_rp::gpio::OutputOpenDrain;
use embassy_rp::gpio::{Flex, Input, Level, Output, Pull};
use {defmt_rtt as _, panic_probe as _};
#[embassy_executor::main]
async fn main(_spawner: Spawner) {
let p = embassy_rp::init(Default::default());
info!("Hello World!");
let (mut a, mut b) = (p.PIN_0, p.PIN_1);
// Test initial output
{
let b = Input::new(b.reborrow(), Pull::None);
{
let a = Output::new(a.reborrow(), Level::Low);
delay();
assert!(b.is_low());
assert!(!b.is_high());
assert!(a.is_set_low());
assert!(!a.is_set_high());
}
{
let mut a = Output::new(a.reborrow(), Level::High);
delay();
assert!(!b.is_low());
assert!(b.is_high());
assert!(!a.is_set_low());
assert!(a.is_set_high());
// Test is_set_low / is_set_high
a.set_low();
delay();
assert!(b.is_low());
assert!(a.is_set_low());
assert!(!a.is_set_high());
a.set_high();
delay();
assert!(b.is_high());
assert!(!a.is_set_low());
assert!(a.is_set_high());
// Test toggle
a.toggle();
delay();
assert!(b.is_low());
assert!(a.is_set_low());
assert!(!a.is_set_high());
a.toggle();
delay();
assert!(b.is_high());
assert!(!a.is_set_low());
assert!(a.is_set_high());
}
}
// Test input no pull
{
let b = Input::new(b.reborrow(), Pull::None);
// no pull, the status is undefined
let mut a = Output::new(a.reborrow(), Level::Low);
delay();
assert!(b.is_low());
a.set_high();
delay();
assert!(b.is_high());
}
// Test input pulldown
#[cfg(feature = "rp2040")]
{
let b = Input::new(b.reborrow(), Pull::Down);
delay();
assert!(b.is_low());
let mut a = Output::new(a.reborrow(), Level::Low);
delay();
assert!(b.is_low());
a.set_high();
delay();
assert!(b.is_high());
}
// Test input pullup
{
let b = Input::new(b.reborrow(), Pull::Up);
delay();
assert!(b.is_high());
let mut a = Output::new(a.reborrow(), Level::Low);
delay();
assert!(b.is_low());
a.set_high();
delay();
assert!(b.is_high());
}
// OUTPUT OPEN DRAIN
#[cfg(feature = "rp2040")]
{
let mut b = OutputOpenDrain::new(b.reborrow(), Level::High);
let mut a = Flex::new(a.reborrow());
a.set_as_input();
// When an OutputOpenDrain is high, it doesn't drive the pin.
b.set_high();
a.set_pull(Pull::Up);
delay();
assert!(a.is_high());
a.set_pull(Pull::Down);
delay();
assert!(a.is_low());
// When an OutputOpenDrain is low, it drives the pin low.
b.set_low();
a.set_pull(Pull::Up);
delay();
assert!(a.is_low());
a.set_pull(Pull::Down);
delay();
assert!(a.is_low());
// Check high again
b.set_high();
a.set_pull(Pull::Up);
delay();
assert!(a.is_high());
a.set_pull(Pull::Down);
delay();
assert!(a.is_low());
// When an OutputOpenDrain is high, it reads the input value in the pin.
b.set_high();
a.set_as_input();
a.set_pull(Pull::Up);
delay();
assert!(b.is_high());
a.set_as_output();
a.set_low();
delay();
assert!(b.is_low());
// When an OutputOpenDrain is low, it always reads low.
b.set_low();
a.set_as_input();
a.set_pull(Pull::Up);
delay();
assert!(b.is_low());
a.set_as_output();
a.set_low();
delay();
assert!(b.is_low());
}
// FLEX
// Test initial output
{
//Flex pin configured as input
let mut b = Flex::new(b.reborrow());
b.set_as_input();
{
//Flex pin configured as output
let mut a = Flex::new(a.reborrow()); //Flex pin configured as output
a.set_low(); // Pin state must be set before configuring the pin, thus we avoid unknown state
a.set_as_output();
delay();
assert!(b.is_low());
}
{
//Flex pin configured as output
let mut a = Flex::new(a.reborrow());
a.set_high();
a.set_as_output();
delay();
assert!(b.is_high());
}
}
// Test input no pull
{
let mut b = Flex::new(b.reborrow());
b.set_as_input(); // no pull by default.
let mut a = Flex::new(a.reborrow());
a.set_low();
a.set_as_output();
delay();
assert!(b.is_low());
a.set_high();
delay();
assert!(b.is_high());
}
// Test input pulldown
#[cfg(feature = "rp2040")]
{
let mut b = Flex::new(b.reborrow());
b.set_as_input();
b.set_pull(Pull::Down);
delay();
assert!(b.is_low());
let mut a = Flex::new(a.reborrow());
a.set_low();
a.set_as_output();
delay();
assert!(b.is_low());
a.set_high();
delay();
assert!(b.is_high());
}
// Test input pullup
{
let mut b = Flex::new(b.reborrow());
b.set_as_input();
b.set_pull(Pull::Up);
delay();
assert!(b.is_high());
let mut a = Flex::new(a.reborrow());
a.set_high();
a.set_as_output();
delay();
assert!(b.is_high());
a.set_low();
delay();
assert!(b.is_low());
}
info!("Test OK");
cortex_m::asm::bkpt();
}
fn delay() {
cortex_m::asm::delay(10000);
}
|