aboutsummaryrefslogtreecommitdiff
path: root/tests/rp/src/bin
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2022-07-09 02:13:43 +0200
committerDario Nieuwenhuis <[email protected]>2022-07-09 02:14:30 +0200
commitccf57cfab63d9562d617a667ef6dfa2c9edd572f (patch)
treee325a0d1151022e14563311c708366d709253375 /tests/rp/src/bin
parent5cc5961c94568db768c241e8e8bf91a692c1803e (diff)
rp: add GPIO HIL test.
Diffstat (limited to 'tests/rp/src/bin')
-rw-r--r--tests/rp/src/bin/gpio.rs192
1 files changed, 192 insertions, 0 deletions
diff --git a/tests/rp/src/bin/gpio.rs b/tests/rp/src/bin/gpio.rs
new file mode 100644
index 000000000..0be9d9f24
--- /dev/null
+++ b/tests/rp/src/bin/gpio.rs
@@ -0,0 +1,192 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use defmt::{assert, *};
6use embassy::executor::Spawner;
7use embassy_rp::gpio::{Flex, Input, Level, Output, OutputOpenDrain, Pull};
8use embassy_rp::Peripherals;
9use {defmt_rtt as _, panic_probe as _};
10
11#[embassy::main]
12async fn main(_spawner: Spawner, p: Peripherals) {
13 info!("Hello World!");
14
15 let (mut a, mut b) = (p.PIN_0, p.PIN_1);
16
17 // Test initial output
18 {
19 let b = Input::new(&mut b, Pull::None);
20
21 {
22 let _a = Output::new(&mut a, Level::Low);
23 delay();
24 assert!(b.is_low());
25 }
26 {
27 let _a = Output::new(&mut a, Level::High);
28 delay();
29 assert!(b.is_high());
30 }
31 }
32
33 // Test input no pull
34 {
35 let b = Input::new(&mut b, Pull::None);
36 // no pull, the status is undefined
37
38 let mut a = Output::new(&mut a, Level::Low);
39 delay();
40 assert!(b.is_low());
41 a.set_high();
42 delay();
43 assert!(b.is_high());
44 }
45
46 // Test input pulldown
47 {
48 let b = Input::new(&mut b, Pull::Down);
49 delay();
50 assert!(b.is_low());
51
52 let mut a = Output::new(&mut a, Level::Low);
53 delay();
54 assert!(b.is_low());
55 a.set_high();
56 delay();
57 assert!(b.is_high());
58 }
59
60 // Test input pullup
61 {
62 let b = Input::new(&mut b, Pull::Up);
63 delay();
64 assert!(b.is_high());
65
66 let mut a = Output::new(&mut a, Level::Low);
67 delay();
68 assert!(b.is_low());
69 a.set_high();
70 delay();
71 assert!(b.is_high());
72 }
73
74 // OUTPUT OPEN DRAIN
75 {
76 let mut b = OutputOpenDrain::new(&mut b, Level::High);
77 let mut a = Flex::new(&mut a);
78 a.set_as_input();
79
80 // When an OutputOpenDrain is high, it doesn't drive the pin.
81 a.set_pull(Pull::Up);
82 delay();
83 assert!(a.is_high());
84 a.set_pull(Pull::Down);
85 delay();
86 assert!(a.is_low());
87
88 b.set_low();
89
90 // When an OutputOpenDrain is low, it drives the pin low.
91 a.set_pull(Pull::Up);
92 delay();
93 assert!(a.is_low());
94 a.set_pull(Pull::Down);
95 delay();
96 assert!(a.is_low());
97
98 b.set_high();
99
100 a.set_pull(Pull::Up);
101 delay();
102 assert!(a.is_high());
103 a.set_pull(Pull::Down);
104 delay();
105 assert!(a.is_low());
106 }
107
108 // FLEX
109 // Test initial output
110 {
111 //Flex pin configured as input
112 let mut b = Flex::new(&mut b);
113 b.set_as_input();
114
115 {
116 //Flex pin configured as output
117 let mut a = Flex::new(&mut a); //Flex pin configured as output
118 a.set_low(); // Pin state must be set before configuring the pin, thus we avoid unknown state
119 a.set_as_output();
120 delay();
121 assert!(b.is_low());
122 }
123 {
124 //Flex pin configured as output
125 let mut a = Flex::new(&mut a);
126 a.set_high();
127 a.set_as_output();
128
129 delay();
130 assert!(b.is_high());
131 }
132 }
133
134 // Test input no pull
135 {
136 let mut b = Flex::new(&mut b);
137 b.set_as_input(); // no pull by default.
138
139 let mut a = Flex::new(&mut a);
140 a.set_low();
141 a.set_as_output();
142
143 delay();
144 assert!(b.is_low());
145 a.set_high();
146 delay();
147 assert!(b.is_high());
148 }
149
150 // Test input pulldown
151 {
152 let mut b = Flex::new(&mut b);
153 b.set_as_input();
154 b.set_pull(Pull::Down);
155 delay();
156 assert!(b.is_low());
157
158 let mut a = Flex::new(&mut a);
159 a.set_low();
160 a.set_as_output();
161 delay();
162 assert!(b.is_low());
163 a.set_high();
164 delay();
165 assert!(b.is_high());
166 }
167
168 // Test input pullup
169 {
170 let mut b = Flex::new(&mut b);
171 b.set_as_input();
172 b.set_pull(Pull::Up);
173 delay();
174 assert!(b.is_high());
175
176 let mut a = Flex::new(&mut a);
177 a.set_high();
178 a.set_as_output();
179 delay();
180 assert!(b.is_high());
181 a.set_low();
182 delay();
183 assert!(b.is_low());
184 }
185
186 info!("Test OK");
187 cortex_m::asm::bkpt();
188}
189
190fn delay() {
191 cortex_m::asm::delay(10000);
192}