aboutsummaryrefslogtreecommitdiff
path: root/examples/rp/src
diff options
context:
space:
mode:
authorAdrian Wowk <[email protected]>2025-07-02 20:53:26 -0500
committerDario Nieuwenhuis <[email protected]>2025-09-05 20:35:48 +0200
commit236662c748eab43a701bf4f43570b191ec2c1158 (patch)
tree3aead4a9d6807a98a166342a335044df0b786944 /examples/rp/src
parent4cac3ac1d24d6b651d79bfca8401824c28f5102c (diff)
rp: add pio spi examples
Diffstat (limited to 'examples/rp/src')
-rw-r--r--examples/rp/src/bin/pio_spi.rs63
-rw-r--r--examples/rp/src/bin/pio_spi_async.rs65
2 files changed, 128 insertions, 0 deletions
diff --git a/examples/rp/src/bin/pio_spi.rs b/examples/rp/src/bin/pio_spi.rs
new file mode 100644
index 000000000..1fbe235e5
--- /dev/null
+++ b/examples/rp/src/bin/pio_spi.rs
@@ -0,0 +1,63 @@
1//! This example shows how to use a PIO state machine as an additional SPI
2//! (Serial Peripheral Interface) on the RP2040 chip. No specific hardware is
3//! specified in this example.
4//!
5//! If you connect pin 6 and 7 you should get the same data back.
6
7#![no_std]
8#![no_main]
9
10use defmt::*;
11use embassy_executor::Spawner;
12use embassy_rp::{
13 bind_interrupts,
14 peripherals::PIO0,
15 pio,
16 pio_programs::spi::{Config, PioSpiProgram, Spi},
17 spi::Phase,
18};
19use embassy_time::Timer;
20use {defmt_rtt as _, panic_probe as _};
21
22bind_interrupts!(struct Irqs {
23 PIO0_IRQ_0 => pio::InterruptHandler<PIO0>;
24});
25
26#[embassy_executor::main]
27async fn main(_spawner: Spawner) {
28 let p = embassy_rp::init(Default::default());
29 info!("Hello World!");
30
31 // These pins are routed to differnet hardware SPI peripherals, but we can
32 // use them together regardless
33 let mosi = p.PIN_6; // SPI0 SCLK
34 let miso = p.PIN_7; // SPI0 MOSI
35 let clk = p.PIN_8; // SPI1 MISO
36
37 let pio::Pio { mut common, sm0, .. } = pio::Pio::new(p.PIO0, Irqs);
38
39 // The PIO program must be configured with the clock phase
40 let program = PioSpiProgram::new(&mut common, Phase::CaptureOnFirstTransition);
41
42 // Construct an SPI driver backed by a PIO state machine
43 let mut spi = Spi::new_blocking(
44 &mut common,
45 sm0,
46 clk,
47 mosi,
48 miso,
49 &program,
50 // Only the frequency and polarity are set here
51 Config::default(),
52 );
53
54 loop {
55 let tx_buf = [1_u8, 2, 3, 4, 5, 6];
56 let mut rx_buf = [0_u8; 6];
57
58 spi.blocking_transfer(&mut rx_buf, &tx_buf).unwrap();
59 info!("{:?}", rx_buf);
60
61 Timer::after_secs(1).await;
62 }
63}
diff --git a/examples/rp/src/bin/pio_spi_async.rs b/examples/rp/src/bin/pio_spi_async.rs
new file mode 100644
index 000000000..5abf6fc71
--- /dev/null
+++ b/examples/rp/src/bin/pio_spi_async.rs
@@ -0,0 +1,65 @@
1//! This example shows how to use a PIO state machine as an additional SPI
2//! (Serial Peripheral Interface) on the RP2040 chip. No specific hardware is
3//! specified in this example.
4//!
5//! If you connect pin 6 and 7 you should get the same data back.
6
7#![no_std]
8#![no_main]
9
10use defmt::*;
11use embassy_executor::Spawner;
12use embassy_rp::{
13 bind_interrupts,
14 peripherals::PIO0,
15 pio,
16 pio_programs::spi::{Config, PioSpiProgram, Spi},
17 spi::Phase,
18};
19use embassy_time::Timer;
20use {defmt_rtt as _, panic_probe as _};
21
22bind_interrupts!(struct Irqs {
23 PIO0_IRQ_0 => pio::InterruptHandler<PIO0>;
24});
25
26#[embassy_executor::main]
27async fn main(_spawner: Spawner) {
28 let p = embassy_rp::init(Default::default());
29 info!("Hello World!");
30
31 // These pins are routed to differnet hardware SPI peripherals, but we can
32 // use them together regardless
33 let mosi = p.PIN_6; // SPI0 SCLK
34 let miso = p.PIN_7; // SPI0 MOSI
35 let clk = p.PIN_8; // SPI1 MISO
36
37 let pio::Pio { mut common, sm0, .. } = pio::Pio::new(p.PIO0, Irqs);
38
39 // The PIO program must be configured with the clock phase
40 let program = PioSpiProgram::new(&mut common, Phase::CaptureOnFirstTransition);
41
42 // Construct an SPI driver backed by a PIO state machine
43 let mut spi = Spi::new(
44 &mut common,
45 sm0,
46 clk,
47 mosi,
48 miso,
49 p.DMA_CH0,
50 p.DMA_CH1,
51 &program,
52 // Only the frequency and polarity are set here
53 Config::default(),
54 );
55
56 loop {
57 let tx_buf = [1_u8, 2, 3, 4, 5, 6];
58 let mut rx_buf = [0_u8; 6];
59
60 spi.transfer(&mut rx_buf, &tx_buf).await.unwrap();
61 info!("{:?}", rx_buf);
62
63 Timer::after_secs(1).await;
64 }
65}