mirror of
https://github.com/embassy-rs/embassy.git
synced 2025-09-28 12:50:37 +00:00
rp: add pio spi examples
This commit is contained in:
parent
4cac3ac1d2
commit
236662c748
63
examples/rp/src/bin/pio_spi.rs
Normal file
63
examples/rp/src/bin/pio_spi.rs
Normal file
@ -0,0 +1,63 @@
|
||||
//! This example shows how to use a PIO state machine as an additional SPI
|
||||
//! (Serial Peripheral Interface) on the RP2040 chip. No specific hardware is
|
||||
//! specified in this example.
|
||||
//!
|
||||
//! If you connect pin 6 and 7 you should get the same data back.
|
||||
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
use defmt::*;
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_rp::{
|
||||
bind_interrupts,
|
||||
peripherals::PIO0,
|
||||
pio,
|
||||
pio_programs::spi::{Config, PioSpiProgram, Spi},
|
||||
spi::Phase,
|
||||
};
|
||||
use embassy_time::Timer;
|
||||
use {defmt_rtt as _, panic_probe as _};
|
||||
|
||||
bind_interrupts!(struct Irqs {
|
||||
PIO0_IRQ_0 => pio::InterruptHandler<PIO0>;
|
||||
});
|
||||
|
||||
#[embassy_executor::main]
|
||||
async fn main(_spawner: Spawner) {
|
||||
let p = embassy_rp::init(Default::default());
|
||||
info!("Hello World!");
|
||||
|
||||
// These pins are routed to differnet hardware SPI peripherals, but we can
|
||||
// use them together regardless
|
||||
let mosi = p.PIN_6; // SPI0 SCLK
|
||||
let miso = p.PIN_7; // SPI0 MOSI
|
||||
let clk = p.PIN_8; // SPI1 MISO
|
||||
|
||||
let pio::Pio { mut common, sm0, .. } = pio::Pio::new(p.PIO0, Irqs);
|
||||
|
||||
// The PIO program must be configured with the clock phase
|
||||
let program = PioSpiProgram::new(&mut common, Phase::CaptureOnFirstTransition);
|
||||
|
||||
// Construct an SPI driver backed by a PIO state machine
|
||||
let mut spi = Spi::new_blocking(
|
||||
&mut common,
|
||||
sm0,
|
||||
clk,
|
||||
mosi,
|
||||
miso,
|
||||
&program,
|
||||
// Only the frequency and polarity are set here
|
||||
Config::default(),
|
||||
);
|
||||
|
||||
loop {
|
||||
let tx_buf = [1_u8, 2, 3, 4, 5, 6];
|
||||
let mut rx_buf = [0_u8; 6];
|
||||
|
||||
spi.blocking_transfer(&mut rx_buf, &tx_buf).unwrap();
|
||||
info!("{:?}", rx_buf);
|
||||
|
||||
Timer::after_secs(1).await;
|
||||
}
|
||||
}
|
65
examples/rp/src/bin/pio_spi_async.rs
Normal file
65
examples/rp/src/bin/pio_spi_async.rs
Normal file
@ -0,0 +1,65 @@
|
||||
//! This example shows how to use a PIO state machine as an additional SPI
|
||||
//! (Serial Peripheral Interface) on the RP2040 chip. No specific hardware is
|
||||
//! specified in this example.
|
||||
//!
|
||||
//! If you connect pin 6 and 7 you should get the same data back.
|
||||
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
use defmt::*;
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_rp::{
|
||||
bind_interrupts,
|
||||
peripherals::PIO0,
|
||||
pio,
|
||||
pio_programs::spi::{Config, PioSpiProgram, Spi},
|
||||
spi::Phase,
|
||||
};
|
||||
use embassy_time::Timer;
|
||||
use {defmt_rtt as _, panic_probe as _};
|
||||
|
||||
bind_interrupts!(struct Irqs {
|
||||
PIO0_IRQ_0 => pio::InterruptHandler<PIO0>;
|
||||
});
|
||||
|
||||
#[embassy_executor::main]
|
||||
async fn main(_spawner: Spawner) {
|
||||
let p = embassy_rp::init(Default::default());
|
||||
info!("Hello World!");
|
||||
|
||||
// These pins are routed to differnet hardware SPI peripherals, but we can
|
||||
// use them together regardless
|
||||
let mosi = p.PIN_6; // SPI0 SCLK
|
||||
let miso = p.PIN_7; // SPI0 MOSI
|
||||
let clk = p.PIN_8; // SPI1 MISO
|
||||
|
||||
let pio::Pio { mut common, sm0, .. } = pio::Pio::new(p.PIO0, Irqs);
|
||||
|
||||
// The PIO program must be configured with the clock phase
|
||||
let program = PioSpiProgram::new(&mut common, Phase::CaptureOnFirstTransition);
|
||||
|
||||
// Construct an SPI driver backed by a PIO state machine
|
||||
let mut spi = Spi::new(
|
||||
&mut common,
|
||||
sm0,
|
||||
clk,
|
||||
mosi,
|
||||
miso,
|
||||
p.DMA_CH0,
|
||||
p.DMA_CH1,
|
||||
&program,
|
||||
// Only the frequency and polarity are set here
|
||||
Config::default(),
|
||||
);
|
||||
|
||||
loop {
|
||||
let tx_buf = [1_u8, 2, 3, 4, 5, 6];
|
||||
let mut rx_buf = [0_u8; 6];
|
||||
|
||||
spi.transfer(&mut rx_buf, &tx_buf).await.unwrap();
|
||||
info!("{:?}", rx_buf);
|
||||
|
||||
Timer::after_secs(1).await;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user