diff options
| author | Dario Nieuwenhuis <[email protected]> | 2025-09-05 19:00:01 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-09-05 19:00:01 +0000 |
| commit | bbcf9af87eddbf9e1dba3281a3f1a9e5e419477f (patch) | |
| tree | 7dfaa5b0435e7210410f7f730d78a774c44707d6 /examples | |
| parent | 0407f7ebe8fabeb81b8a77811ec5dda0fee0b44b (diff) | |
| parent | 815ba8aa7507d0aa27dfb3f3823793e3567719f8 (diff) | |
Merge pull request #4639 from embassy-rs/rp-pio-spi
rp: add PIO SPI
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/rp/src/bin/pio_spi.rs | 48 | ||||
| -rw-r--r-- | examples/rp/src/bin/pio_spi_async.rs | 57 |
2 files changed, 105 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..4218327ec --- /dev/null +++ b/examples/rp/src/bin/pio_spi.rs | |||
| @@ -0,0 +1,48 @@ | |||
| 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 | |||
| 10 | use defmt::*; | ||
| 11 | use embassy_executor::Spawner; | ||
| 12 | use embassy_rp::peripherals::PIO0; | ||
| 13 | use embassy_rp::pio_programs::spi::Spi; | ||
| 14 | use embassy_rp::spi::Config; | ||
| 15 | use embassy_rp::{bind_interrupts, pio}; | ||
| 16 | use embassy_time::Timer; | ||
| 17 | use {defmt_rtt as _, panic_probe as _}; | ||
| 18 | |||
| 19 | bind_interrupts!(struct Irqs { | ||
| 20 | PIO0_IRQ_0 => pio::InterruptHandler<PIO0>; | ||
| 21 | }); | ||
| 22 | |||
| 23 | #[embassy_executor::main] | ||
| 24 | async fn main(_spawner: Spawner) { | ||
| 25 | let p = embassy_rp::init(Default::default()); | ||
| 26 | info!("Hello World!"); | ||
| 27 | |||
| 28 | // These pins are routed to different hardware SPI peripherals, but we can | ||
| 29 | // use them together regardless | ||
| 30 | let mosi = p.PIN_6; // SPI0 SCLK | ||
| 31 | let miso = p.PIN_7; // SPI0 MOSI | ||
| 32 | let clk = p.PIN_8; // SPI1 MISO | ||
| 33 | |||
| 34 | let pio::Pio { mut common, sm0, .. } = pio::Pio::new(p.PIO0, Irqs); | ||
| 35 | |||
| 36 | // Construct an SPI driver backed by a PIO state machine | ||
| 37 | let mut spi = Spi::new_blocking(&mut common, sm0, clk, mosi, miso, Config::default()); | ||
| 38 | |||
| 39 | loop { | ||
| 40 | let tx_buf = [1_u8, 2, 3, 4, 5, 6]; | ||
| 41 | let mut rx_buf = [0_u8; 6]; | ||
| 42 | |||
| 43 | spi.blocking_transfer(&mut rx_buf, &tx_buf).unwrap(); | ||
| 44 | info!("{:?}", rx_buf); | ||
| 45 | |||
| 46 | Timer::after_secs(1).await; | ||
| 47 | } | ||
| 48 | } | ||
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..18b57d26e --- /dev/null +++ b/examples/rp/src/bin/pio_spi_async.rs | |||
| @@ -0,0 +1,57 @@ | |||
| 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 | |||
| 10 | use defmt::*; | ||
| 11 | use embassy_executor::Spawner; | ||
| 12 | use embassy_rp::peripherals::PIO0; | ||
| 13 | use embassy_rp::pio_programs::spi::Spi; | ||
| 14 | use embassy_rp::spi::Config; | ||
| 15 | use embassy_rp::{bind_interrupts, pio}; | ||
| 16 | use embassy_time::Timer; | ||
| 17 | use {defmt_rtt as _, panic_probe as _}; | ||
| 18 | |||
| 19 | bind_interrupts!(struct Irqs { | ||
| 20 | PIO0_IRQ_0 => pio::InterruptHandler<PIO0>; | ||
| 21 | }); | ||
| 22 | |||
| 23 | #[embassy_executor::main] | ||
| 24 | async fn main(_spawner: Spawner) { | ||
| 25 | let p = embassy_rp::init(Default::default()); | ||
| 26 | info!("Hello World!"); | ||
| 27 | |||
| 28 | // These pins are routed to different hardware SPI peripherals, but we can | ||
| 29 | // use them together regardless | ||
| 30 | let mosi = p.PIN_6; // SPI0 SCLK | ||
| 31 | let miso = p.PIN_7; // SPI0 MOSI | ||
| 32 | let clk = p.PIN_8; // SPI1 MISO | ||
| 33 | |||
| 34 | let pio::Pio { mut common, sm0, .. } = pio::Pio::new(p.PIO0, Irqs); | ||
| 35 | |||
| 36 | // Construct an SPI driver backed by a PIO state machine | ||
| 37 | let mut spi = Spi::new( | ||
| 38 | &mut common, | ||
| 39 | sm0, | ||
| 40 | clk, | ||
| 41 | mosi, | ||
| 42 | miso, | ||
| 43 | p.DMA_CH0, | ||
| 44 | p.DMA_CH1, | ||
| 45 | Config::default(), | ||
| 46 | ); | ||
| 47 | |||
| 48 | loop { | ||
| 49 | let tx_buf = [1_u8, 2, 3, 4, 5, 6]; | ||
| 50 | let mut rx_buf = [0_u8; 6]; | ||
| 51 | |||
| 52 | spi.transfer(&mut rx_buf, &tx_buf).await.unwrap(); | ||
| 53 | info!("{:?}", rx_buf); | ||
| 54 | |||
| 55 | Timer::after_secs(1).await; | ||
| 56 | } | ||
| 57 | } | ||
