aboutsummaryrefslogtreecommitdiff
path: root/examples/rp/src
diff options
context:
space:
mode:
authorxoviat <[email protected]>2023-11-04 13:51:11 -0500
committerxoviat <[email protected]>2023-11-04 13:51:11 -0500
commit3f2abd4fd5da51d7b8a7711d0eac02c7f7b9deed (patch)
tree3375dd57e07879f2d709f7547d95b2a7f757ae14 /examples/rp/src
parentdc467e89a0f093c1656eaf63955c28dd3b08be6c (diff)
parent655ed3aa887fe2d9e424f239cc07db055d8f8d61 (diff)
Merge branch 'main' of github.com:embassy-rs/embassy into low-power
Diffstat (limited to 'examples/rp/src')
-rw-r--r--examples/rp/src/bin/pio_rotary_encoder.rs81
-rw-r--r--examples/rp/src/bin/pwm_input.rs26
2 files changed, 107 insertions, 0 deletions
diff --git a/examples/rp/src/bin/pio_rotary_encoder.rs b/examples/rp/src/bin/pio_rotary_encoder.rs
new file mode 100644
index 000000000..6d9d59df6
--- /dev/null
+++ b/examples/rp/src/bin/pio_rotary_encoder.rs
@@ -0,0 +1,81 @@
1//! This example shows how to use the PIO module in the RP2040 to read a quadrature rotary encoder.
2
3#![no_std]
4#![no_main]
5#![feature(type_alias_impl_trait)]
6
7use defmt::info;
8use embassy_executor::Spawner;
9use embassy_rp::gpio::Pull;
10use embassy_rp::peripherals::PIO0;
11use embassy_rp::{bind_interrupts, pio};
12use fixed::traits::ToFixed;
13use pio::{Common, Config, FifoJoin, Instance, InterruptHandler, Pio, PioPin, ShiftDirection, StateMachine};
14use {defmt_rtt as _, panic_probe as _};
15
16bind_interrupts!(struct Irqs {
17 PIO0_IRQ_0 => InterruptHandler<PIO0>;
18});
19
20pub struct PioEncoder<'d, T: Instance, const SM: usize> {
21 sm: StateMachine<'d, T, SM>,
22}
23
24impl<'d, T: Instance, const SM: usize> PioEncoder<'d, T, SM> {
25 pub fn new(
26 pio: &mut Common<'d, T>,
27 mut sm: StateMachine<'d, T, SM>,
28 pin_a: impl PioPin,
29 pin_b: impl PioPin,
30 ) -> Self {
31 let mut pin_a = pio.make_pio_pin(pin_a);
32 let mut pin_b = pio.make_pio_pin(pin_b);
33 pin_a.set_pull(Pull::Up);
34 pin_b.set_pull(Pull::Up);
35 sm.set_pin_dirs(pio::Direction::In, &[&pin_a, &pin_b]);
36
37 let prg = pio_proc::pio_asm!("wait 1 pin 1", "wait 0 pin 1", "in pins, 2", "push",);
38
39 let mut cfg = Config::default();
40 cfg.set_in_pins(&[&pin_a, &pin_b]);
41 cfg.fifo_join = FifoJoin::RxOnly;
42 cfg.shift_in.direction = ShiftDirection::Left;
43 cfg.clock_divider = 10_000.to_fixed();
44 cfg.use_program(&pio.load_program(&prg.program), &[]);
45 sm.set_config(&cfg);
46 sm.set_enable(true);
47 Self { sm }
48 }
49
50 pub async fn read(&mut self) -> Direction {
51 loop {
52 match self.sm.rx().wait_pull().await {
53 0 => return Direction::CounterClockwise,
54 1 => return Direction::Clockwise,
55 _ => {}
56 }
57 }
58 }
59}
60
61pub enum Direction {
62 Clockwise,
63 CounterClockwise,
64}
65
66#[embassy_executor::main]
67async fn main(_spawner: Spawner) {
68 let p = embassy_rp::init(Default::default());
69 let Pio { mut common, sm0, .. } = Pio::new(p.PIO0, Irqs);
70
71 let mut encoder = PioEncoder::new(&mut common, sm0, p.PIN_4, p.PIN_5);
72
73 let mut count = 0;
74 loop {
75 info!("Count: {}", count);
76 count += match encoder.read().await {
77 Direction::Clockwise => 1,
78 Direction::CounterClockwise => -1,
79 };
80 }
81}
diff --git a/examples/rp/src/bin/pwm_input.rs b/examples/rp/src/bin/pwm_input.rs
new file mode 100644
index 000000000..0fc2e40c3
--- /dev/null
+++ b/examples/rp/src/bin/pwm_input.rs
@@ -0,0 +1,26 @@
1//! This example shows how to use the PWM module to measure the frequency of an input signal.
2
3#![no_std]
4#![no_main]
5#![feature(type_alias_impl_trait)]
6
7use defmt::*;
8use embassy_executor::Spawner;
9use embassy_rp::pwm::{Config, InputMode, Pwm};
10use embassy_time::{Duration, Ticker};
11use {defmt_rtt as _, panic_probe as _};
12
13#[embassy_executor::main]
14async fn main(_spawner: Spawner) {
15 let p = embassy_rp::init(Default::default());
16
17 let cfg: Config = Default::default();
18 let pwm = Pwm::new_input(p.PWM_CH2, p.PIN_5, InputMode::RisingEdge, cfg);
19
20 let mut ticker = Ticker::every(Duration::from_secs(1));
21 loop {
22 info!("Input frequency: {} Hz", pwm.counter());
23 pwm.set_counter(0);
24 ticker.next().await;
25 }
26}