aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorPedro Ferreira <[email protected]>2024-09-11 21:21:41 +0200
committerDario Nieuwenhuis <[email protected]>2025-01-15 01:56:37 +0100
commit933e888ed059d03f8c306537f14369521166e3a3 (patch)
treeba1f2482459031e7d3e3f7bb40415fbc81a96a9e /examples
parent028de6d640b51610f65415ea2a9c7941938ea6b2 (diff)
RP235x: support new FIFO options, set IE, OD on PIO pins.
Diffstat (limited to 'examples')
-rw-r--r--examples/rp/Cargo.toml4
-rw-r--r--examples/rp23/Cargo.toml4
-rw-r--r--examples/rp23/src/bin/pio_rotary_encoder_rxf.rs116
3 files changed, 120 insertions, 4 deletions
diff --git a/examples/rp/Cargo.toml b/examples/rp/Cargo.toml
index d9decc2b0..5294ec477 100644
--- a/examples/rp/Cargo.toml
+++ b/examples/rp/Cargo.toml
@@ -55,8 +55,8 @@ embedded-storage = { version = "0.3" }
55static_cell = "2.1" 55static_cell = "2.1"
56portable-atomic = { version = "1.5", features = ["critical-section"] } 56portable-atomic = { version = "1.5", features = ["critical-section"] }
57log = "0.4" 57log = "0.4"
58pio-proc = "0.2" 58pio-proc = { git = "https://github.com/rp-rs/pio-rs", rev = "fa586448b0b223217eec8c92c19fe6823dd04cc4" }
59pio = "0.2.1" 59pio = { git = "https://github.com/rp-rs/pio-rs", rev = "fa586448b0b223217eec8c92c19fe6823dd04cc4" }
60rand = { version = "0.8.5", default-features = false } 60rand = { version = "0.8.5", default-features = false }
61embedded-sdmmc = "0.7.0" 61embedded-sdmmc = "0.7.0"
62 62
diff --git a/examples/rp23/Cargo.toml b/examples/rp23/Cargo.toml
index 4d99ecc72..8f9c14c5c 100644
--- a/examples/rp23/Cargo.toml
+++ b/examples/rp23/Cargo.toml
@@ -55,8 +55,8 @@ embedded-storage = { version = "0.3" }
55static_cell = "2.1" 55static_cell = "2.1"
56portable-atomic = { version = "1.5", features = ["critical-section"] } 56portable-atomic = { version = "1.5", features = ["critical-section"] }
57log = "0.4" 57log = "0.4"
58pio-proc = "0.2" 58pio-proc = { git = "https://github.com/rp-rs/pio-rs", rev = "fa586448b0b223217eec8c92c19fe6823dd04cc4" }
59pio = "0.2.1" 59pio = { git = "https://github.com/rp-rs/pio-rs", rev = "fa586448b0b223217eec8c92c19fe6823dd04cc4" }
60rand = { version = "0.8.5", default-features = false } 60rand = { version = "0.8.5", default-features = false }
61embedded-sdmmc = "0.7.0" 61embedded-sdmmc = "0.7.0"
62 62
diff --git a/examples/rp23/src/bin/pio_rotary_encoder_rxf.rs b/examples/rp23/src/bin/pio_rotary_encoder_rxf.rs
new file mode 100644
index 000000000..7a1046610
--- /dev/null
+++ b/examples/rp23/src/bin/pio_rotary_encoder_rxf.rs
@@ -0,0 +1,116 @@
1//! This example shows how to use the PIO module in the RP235x to read a quadrature rotary encoder.
2//! It differs from the other example in that it uses the RX FIFO as a status register
3
4#![no_std]
5#![no_main]
6
7use defmt::info;
8use embassy_executor::Spawner;
9use embassy_rp::block::ImageDef;
10use embassy_rp::gpio::Pull;
11use embassy_rp::peripherals::PIO0;
12use embassy_rp::{bind_interrupts, pio};
13use embassy_time::Timer;
14use fixed::traits::ToFixed;
15use pio::{Common, Config, FifoJoin, Instance, InterruptHandler, Pio, PioPin, ShiftDirection, StateMachine};
16use {defmt_rtt as _, panic_probe as _};
17
18#[link_section = ".start_block"]
19#[used]
20pub static IMAGE_DEF: ImageDef = ImageDef::secure_exe();
21
22// Program metadata for `picotool info`
23#[link_section = ".bi_entries"]
24#[used]
25pub static PICOTOOL_ENTRIES: [embassy_rp::binary_info::EntryAddr; 4] = [
26 embassy_rp::binary_info::rp_program_name!(c"example_pio_rotary_encoder_rxf"),
27 embassy_rp::binary_info::rp_cargo_version!(),
28 embassy_rp::binary_info::rp_program_description!(c"Rotary encoder (RXF)"),
29 embassy_rp::binary_info::rp_program_build_attribute!(),
30];
31
32bind_interrupts!(struct Irqs {
33 PIO0_IRQ_0 => InterruptHandler<PIO0>;
34});
35
36pub struct PioEncoder<'d, T: Instance, const SM: usize> {
37 sm: StateMachine<'d, T, SM>,
38}
39
40impl<'d, T: Instance, const SM: usize> PioEncoder<'d, T, SM> {
41 pub fn new(
42 pio: &mut Common<'d, T>,
43 mut sm: StateMachine<'d, T, SM>,
44 pin_a: impl PioPin,
45 pin_b: impl PioPin,
46 ) -> Self {
47 let mut pin_a = pio.make_pio_pin(pin_a);
48 let mut pin_b = pio.make_pio_pin(pin_b);
49 pin_a.set_pull(Pull::Up);
50 pin_b.set_pull(Pull::Up);
51
52 sm.set_pin_dirs(pio::Direction::In, &[&pin_a, &pin_b]);
53
54 let prg = pio_proc::pio_asm!(
55 "start:"
56 // encoder count is stored in X
57 "mov isr, x"
58 // and then moved to the RX FIFO register
59 "mov rxfifo[0], isr"
60
61 // wait for encoder transition
62 "wait 1 pin 1"
63 "wait 0 pin 1"
64
65 "set y, 0"
66 "mov y, pins[1]"
67
68 // update X depending on pin 1
69 "jmp !y decr"
70
71 // this is just a clever way of doing x++
72 "mov x, ~x"
73 "jmp x--, incr"
74 "incr:"
75 "mov x, ~x"
76 "jmp start"
77
78 // and this is x--
79 "decr:"
80 "jmp x--, start"
81 );
82
83 let mut cfg = Config::default();
84 cfg.set_in_pins(&[&pin_a, &pin_b]);
85 cfg.fifo_join = FifoJoin::RxAsStatus;
86 cfg.shift_in.direction = ShiftDirection::Left;
87 cfg.clock_divider = 10_000.to_fixed();
88 cfg.use_program(&pio.load_program(&prg.program), &[]);
89 sm.set_config(&cfg);
90
91 sm.set_enable(true);
92 Self { sm }
93 }
94
95 pub async fn read(&mut self) -> u32 {
96 self.sm.get_rxf_entry(0)
97 }
98}
99
100pub enum Direction {
101 Clockwise,
102 CounterClockwise,
103}
104
105#[embassy_executor::main]
106async fn main(_spawner: Spawner) {
107 let p = embassy_rp::init(Default::default());
108 let Pio { mut common, sm0, .. } = Pio::new(p.PIO0, Irqs);
109
110 let mut encoder = PioEncoder::new(&mut common, sm0, p.PIN_4, p.PIN_5);
111
112 loop {
113 info!("Count: {}", encoder.read().await);
114 Timer::after_millis(1000).await;
115 }
116}