aboutsummaryrefslogtreecommitdiff
path: root/examples/rp235x/src/bin/pio_async.rs
diff options
context:
space:
mode:
authorCurly <[email protected]>2025-02-23 07:33:58 -0800
committerCurly <[email protected]>2025-02-23 07:33:58 -0800
commit3932835998802fc3abf7cce4f736e072858ebfd1 (patch)
tree5dd714b99bc74a03556c58809237c88691c293bb /examples/rp235x/src/bin/pio_async.rs
parentc3c67db93e627a4fafe5e1a1123e5cbb4abafe47 (diff)
rename `rp23` (?) folder to `rp235x`; fix `ci.sh` to use `rp235x` folder
Diffstat (limited to 'examples/rp235x/src/bin/pio_async.rs')
-rw-r--r--examples/rp235x/src/bin/pio_async.rs131
1 files changed, 131 insertions, 0 deletions
diff --git a/examples/rp235x/src/bin/pio_async.rs b/examples/rp235x/src/bin/pio_async.rs
new file mode 100644
index 000000000..08c702347
--- /dev/null
+++ b/examples/rp235x/src/bin/pio_async.rs
@@ -0,0 +1,131 @@
1//! This example shows powerful PIO module in the RP2040 chip.
2
3#![no_std]
4#![no_main]
5use defmt::info;
6use embassy_executor::Spawner;
7use embassy_rp::bind_interrupts;
8use embassy_rp::peripherals::PIO0;
9use embassy_rp::pio::program::pio_asm;
10use embassy_rp::pio::{Common, Config, InterruptHandler, Irq, Pio, PioPin, ShiftDirection, StateMachine};
11use fixed::traits::ToFixed;
12use fixed_macro::types::U56F8;
13use {defmt_rtt as _, panic_probe as _};
14
15bind_interrupts!(struct Irqs {
16 PIO0_IRQ_0 => InterruptHandler<PIO0>;
17});
18
19fn setup_pio_task_sm0<'a>(pio: &mut Common<'a, PIO0>, sm: &mut StateMachine<'a, PIO0, 0>, pin: impl PioPin) {
20 // Setup sm0
21
22 // Send data serially to pin
23 let prg = pio_asm!(
24 ".origin 16",
25 "set pindirs, 1",
26 ".wrap_target",
27 "out pins,1 [19]",
28 ".wrap",
29 );
30
31 let mut cfg = Config::default();
32 cfg.use_program(&pio.load_program(&prg.program), &[]);
33 let out_pin = pio.make_pio_pin(pin);
34 cfg.set_out_pins(&[&out_pin]);
35 cfg.set_set_pins(&[&out_pin]);
36 cfg.clock_divider = (U56F8!(125_000_000) / 20 / 200).to_fixed();
37 cfg.shift_out.auto_fill = true;
38 sm.set_config(&cfg);
39}
40
41#[embassy_executor::task]
42async fn pio_task_sm0(mut sm: StateMachine<'static, PIO0, 0>) {
43 sm.set_enable(true);
44
45 let mut v = 0x0f0caffa;
46 loop {
47 sm.tx().wait_push(v).await;
48 v ^= 0xffff;
49 info!("Pushed {:032b} to FIFO", v);
50 }
51}
52
53fn setup_pio_task_sm1<'a>(pio: &mut Common<'a, PIO0>, sm: &mut StateMachine<'a, PIO0, 1>) {
54 // Setupm sm1
55
56 // Read 0b10101 repeatedly until ISR is full
57 let prg = pio_asm!(
58 //
59 ".origin 8",
60 "set x, 0x15",
61 ".wrap_target",
62 "in x, 5 [31]",
63 ".wrap",
64 );
65
66 let mut cfg = Config::default();
67 cfg.use_program(&pio.load_program(&prg.program), &[]);
68 cfg.clock_divider = (U56F8!(125_000_000) / 2000).to_fixed();
69 cfg.shift_in.auto_fill = true;
70 cfg.shift_in.direction = ShiftDirection::Right;
71 sm.set_config(&cfg);
72}
73
74#[embassy_executor::task]
75async fn pio_task_sm1(mut sm: StateMachine<'static, PIO0, 1>) {
76 sm.set_enable(true);
77 loop {
78 let rx = sm.rx().wait_pull().await;
79 info!("Pulled {:032b} from FIFO", rx);
80 }
81}
82
83fn setup_pio_task_sm2<'a>(pio: &mut Common<'a, PIO0>, sm: &mut StateMachine<'a, PIO0, 2>) {
84 // Setup sm2
85
86 // Repeatedly trigger IRQ 3
87 let prg = pio_asm!(
88 ".origin 0",
89 ".wrap_target",
90 "set x,10",
91 "delay:",
92 "jmp x-- delay [15]",
93 "irq 3 [15]",
94 ".wrap",
95 );
96 let mut cfg = Config::default();
97 cfg.use_program(&pio.load_program(&prg.program), &[]);
98 cfg.clock_divider = (U56F8!(125_000_000) / 2000).to_fixed();
99 sm.set_config(&cfg);
100}
101
102#[embassy_executor::task]
103async fn pio_task_sm2(mut irq: Irq<'static, PIO0, 3>, mut sm: StateMachine<'static, PIO0, 2>) {
104 sm.set_enable(true);
105 loop {
106 irq.wait().await;
107 info!("IRQ trigged");
108 }
109}
110
111#[embassy_executor::main]
112async fn main(spawner: Spawner) {
113 let p = embassy_rp::init(Default::default());
114 let pio = p.PIO0;
115
116 let Pio {
117 mut common,
118 irq3,
119 mut sm0,
120 mut sm1,
121 mut sm2,
122 ..
123 } = Pio::new(pio, Irqs);
124
125 setup_pio_task_sm0(&mut common, &mut sm0, p.PIN_0);
126 setup_pio_task_sm1(&mut common, &mut sm1);
127 setup_pio_task_sm2(&mut common, &mut sm2);
128 spawner.spawn(pio_task_sm0(sm0)).unwrap();
129 spawner.spawn(pio_task_sm1(sm1)).unwrap();
130 spawner.spawn(pio_task_sm2(irq3, sm2)).unwrap();
131}