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
|
#![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, assert_eq, assert_ne, *};
use embassy_executor::Spawner;
use embassy_rp::gpio::{Input, Pull};
#[cfg(feature = "rp2040")]
use embassy_rp::gpio::{Level, Output};
use embassy_rp::pwm::{Config, InputMode, Pwm};
use embassy_time::Timer;
use {defmt_rtt as _, panic_probe as _};
#[embassy_executor::main]
async fn main(_spawner: Spawner) {
let mut p = embassy_rp::init(Default::default());
info!("Hello World!");
// Connections on CI device: 6 -> 9, 7 -> 11
let (mut p6, mut p7, mut p9, mut p11) = (p.PIN_6, p.PIN_7, p.PIN_9, p.PIN_11);
let cfg = {
let mut c = Config::default();
c.divider = 125.into();
c.top = 10000;
c.compare_a = 5000;
c.compare_b = 5000;
c
};
// Test free-running clock
{
let pwm = Pwm::new_free(p.PWM_SLICE3.reborrow(), cfg.clone());
cortex_m::asm::delay(125);
let ctr = pwm.counter();
assert!(ctr > 0);
assert!(ctr < 100);
cortex_m::asm::delay(125);
assert!(ctr < pwm.counter());
}
for invert_a in [false, true] {
info!("free-running, invert A: {}", invert_a);
let mut cfg = cfg.clone();
cfg.invert_a = invert_a;
cfg.invert_b = !invert_a;
// Test output from A
{
let pin1 = Input::new(p9.reborrow(), Pull::None);
let _pwm = Pwm::new_output_a(p.PWM_SLICE3.reborrow(), p6.reborrow(), cfg.clone());
Timer::after_millis(1).await;
assert_eq!(pin1.is_low(), invert_a);
Timer::after_millis(5).await;
assert_eq!(pin1.is_high(), invert_a);
Timer::after_millis(5).await;
assert_eq!(pin1.is_low(), invert_a);
Timer::after_millis(5).await;
assert_eq!(pin1.is_high(), invert_a);
}
// Test output from B
{
let pin2 = Input::new(p11.reborrow(), Pull::None);
let _pwm = Pwm::new_output_b(p.PWM_SLICE3.reborrow(), p7.reborrow(), cfg.clone());
Timer::after_millis(1).await;
assert_ne!(pin2.is_low(), invert_a);
Timer::after_millis(5).await;
assert_ne!(pin2.is_high(), invert_a);
Timer::after_millis(5).await;
assert_ne!(pin2.is_low(), invert_a);
Timer::after_millis(5).await;
assert_ne!(pin2.is_high(), invert_a);
}
// Test output from A+B
{
let pin1 = Input::new(p9.reborrow(), Pull::None);
let pin2 = Input::new(p11.reborrow(), Pull::None);
let _pwm = Pwm::new_output_ab(p.PWM_SLICE3.reborrow(), p6.reborrow(), p7.reborrow(), cfg.clone());
Timer::after_millis(1).await;
assert_eq!(pin1.is_low(), invert_a);
assert_ne!(pin2.is_low(), invert_a);
Timer::after_millis(5).await;
assert_eq!(pin1.is_high(), invert_a);
assert_ne!(pin2.is_high(), invert_a);
Timer::after_millis(5).await;
assert_eq!(pin1.is_low(), invert_a);
assert_ne!(pin2.is_low(), invert_a);
Timer::after_millis(5).await;
assert_eq!(pin1.is_high(), invert_a);
assert_ne!(pin2.is_high(), invert_a);
}
}
// Test level-gated
#[cfg(feature = "rp2040")]
{
let mut pin2 = Output::new(p11.reborrow(), Level::Low);
let pwm = Pwm::new_input(
p.PWM_SLICE3.reborrow(),
p7.reborrow(),
Pull::None,
InputMode::Level,
cfg.clone(),
);
assert_eq!(pwm.counter(), 0);
Timer::after_millis(5).await;
assert_eq!(pwm.counter(), 0);
pin2.set_high();
Timer::after_millis(1).await;
pin2.set_low();
let ctr = pwm.counter();
info!("ctr: {}", ctr);
assert!(ctr >= 1000);
Timer::after_millis(1).await;
assert_eq!(pwm.counter(), ctr);
}
// Test rising-gated
#[cfg(feature = "rp2040")]
{
let mut pin2 = Output::new(p11.reborrow(), Level::Low);
let pwm = Pwm::new_input(
p.PWM_SLICE3.reborrow(),
p7.reborrow(),
Pull::None,
InputMode::RisingEdge,
cfg.clone(),
);
assert_eq!(pwm.counter(), 0);
Timer::after_millis(5).await;
assert_eq!(pwm.counter(), 0);
pin2.set_high();
Timer::after_millis(1).await;
pin2.set_low();
assert_eq!(pwm.counter(), 1);
Timer::after_millis(1).await;
assert_eq!(pwm.counter(), 1);
}
// Test falling-gated
#[cfg(feature = "rp2040")]
{
let mut pin2 = Output::new(p11.reborrow(), Level::High);
let pwm = Pwm::new_input(
p.PWM_SLICE3.reborrow(),
p7.reborrow(),
Pull::None,
InputMode::FallingEdge,
cfg.clone(),
);
assert_eq!(pwm.counter(), 0);
Timer::after_millis(5).await;
assert_eq!(pwm.counter(), 0);
pin2.set_low();
Timer::after_millis(1).await;
pin2.set_high();
assert_eq!(pwm.counter(), 1);
Timer::after_millis(1).await;
assert_eq!(pwm.counter(), 1);
}
// pull-down
{
let pin2 = Input::new(p11.reborrow(), Pull::None);
Pwm::new_input(
p.PWM_SLICE3.reborrow(),
p7.reborrow(),
Pull::Down,
InputMode::FallingEdge,
cfg.clone(),
);
Timer::after_millis(1).await;
assert!(pin2.is_low());
}
// pull-up
{
let pin2 = Input::new(p11.reborrow(), Pull::None);
Pwm::new_input(
p.PWM_SLICE3.reborrow(),
p7.reborrow(),
Pull::Up,
InputMode::FallingEdge,
cfg.clone(),
);
Timer::after_millis(1).await;
assert!(pin2.is_high());
}
info!("Test OK");
cortex_m::asm::bkpt();
}
|