2023-07-13 11:16:11 +02:00
|
|
|
//! This example shows powerful PIO module in the RP2040 chip.
|
|
|
|
|
2022-07-27 22:45:46 +02:00
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
|
|
|
#![feature(type_alias_impl_trait)]
|
|
|
|
use defmt::info;
|
|
|
|
use embassy_executor::Spawner;
|
2023-07-07 04:30:46 +02:00
|
|
|
use embassy_rp::bind_interrupts;
|
2023-04-26 00:23:18 +02:00
|
|
|
use embassy_rp::peripherals::PIO0;
|
2023-07-07 04:30:46 +02:00
|
|
|
use embassy_rp::pio::{Common, Config, InterruptHandler, Irq, Pio, PioPin, ShiftDirection, StateMachine};
|
2023-05-06 11:36:07 +02:00
|
|
|
use fixed::traits::ToFixed;
|
|
|
|
use fixed_macro::types::U56F8;
|
2022-07-27 22:45:46 +02:00
|
|
|
use {defmt_rtt as _, panic_probe as _};
|
|
|
|
|
2023-07-07 04:30:46 +02:00
|
|
|
bind_interrupts!(struct Irqs {
|
|
|
|
PIO0_IRQ_0 => InterruptHandler<PIO0>;
|
|
|
|
});
|
|
|
|
|
2023-05-05 19:49:34 +02:00
|
|
|
fn setup_pio_task_sm0<'a>(pio: &mut Common<'a, PIO0>, sm: &mut StateMachine<'a, PIO0, 0>, pin: impl PioPin) {
|
2022-07-27 22:45:46 +02:00
|
|
|
// Setup sm0
|
|
|
|
|
|
|
|
// Send data serially to pin
|
|
|
|
let prg = pio_proc::pio_asm!(
|
|
|
|
".origin 16",
|
|
|
|
"set pindirs, 1",
|
|
|
|
".wrap_target",
|
|
|
|
"out pins,1 [19]",
|
|
|
|
".wrap",
|
|
|
|
);
|
|
|
|
|
2023-05-06 11:36:07 +02:00
|
|
|
let mut cfg = Config::default();
|
2023-07-28 18:45:57 +02:00
|
|
|
cfg.use_program(&pio.load_program(&prg.program), &[]);
|
2023-04-25 20:16:27 +02:00
|
|
|
let out_pin = pio.make_pio_pin(pin);
|
2023-05-06 11:36:07 +02:00
|
|
|
cfg.set_out_pins(&[&out_pin]);
|
|
|
|
cfg.set_set_pins(&[&out_pin]);
|
|
|
|
cfg.clock_divider = (U56F8!(125_000_000) / 20 / 200).to_fixed();
|
|
|
|
cfg.shift_out.auto_fill = true;
|
|
|
|
sm.set_config(&cfg);
|
2023-04-25 20:16:27 +02:00
|
|
|
}
|
2022-07-27 22:45:46 +02:00
|
|
|
|
2023-04-25 20:16:27 +02:00
|
|
|
#[embassy_executor::task]
|
2023-05-03 17:16:35 +02:00
|
|
|
async fn pio_task_sm0(mut sm: StateMachine<'static, PIO0, 0>) {
|
2022-07-27 22:45:46 +02:00
|
|
|
sm.set_enable(true);
|
|
|
|
|
|
|
|
let mut v = 0x0f0caffa;
|
|
|
|
loop {
|
2023-05-03 12:49:55 +02:00
|
|
|
sm.tx().wait_push(v).await;
|
2022-07-27 22:45:46 +02:00
|
|
|
v ^= 0xffff;
|
|
|
|
info!("Pushed {:032b} to FIFO", v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-05 19:49:34 +02:00
|
|
|
fn setup_pio_task_sm1<'a>(pio: &mut Common<'a, PIO0>, sm: &mut StateMachine<'a, PIO0, 1>) {
|
2022-07-27 22:45:46 +02:00
|
|
|
// Setupm sm1
|
|
|
|
|
|
|
|
// Read 0b10101 repeatedly until ISR is full
|
2023-07-13 11:16:11 +02:00
|
|
|
let prg = pio_proc::pio_asm!(
|
|
|
|
//
|
|
|
|
".origin 8",
|
|
|
|
"set x, 0x15",
|
|
|
|
".wrap_target",
|
|
|
|
"in x, 5 [31]",
|
|
|
|
".wrap",
|
|
|
|
);
|
2022-07-27 22:45:46 +02:00
|
|
|
|
2023-05-06 11:36:07 +02:00
|
|
|
let mut cfg = Config::default();
|
2023-07-28 18:45:57 +02:00
|
|
|
cfg.use_program(&pio.load_program(&prg.program), &[]);
|
2023-05-06 11:36:07 +02:00
|
|
|
cfg.clock_divider = (U56F8!(125_000_000) / 2000).to_fixed();
|
|
|
|
cfg.shift_in.auto_fill = true;
|
|
|
|
cfg.shift_in.direction = ShiftDirection::Right;
|
|
|
|
sm.set_config(&cfg);
|
2023-04-25 20:40:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[embassy_executor::task]
|
2023-05-03 17:16:35 +02:00
|
|
|
async fn pio_task_sm1(mut sm: StateMachine<'static, PIO0, 1>) {
|
2022-07-27 22:45:46 +02:00
|
|
|
sm.set_enable(true);
|
|
|
|
loop {
|
2023-05-03 12:49:55 +02:00
|
|
|
let rx = sm.rx().wait_pull().await;
|
2022-07-27 22:45:46 +02:00
|
|
|
info!("Pulled {:032b} from FIFO", rx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-05 19:49:34 +02:00
|
|
|
fn setup_pio_task_sm2<'a>(pio: &mut Common<'a, PIO0>, sm: &mut StateMachine<'a, PIO0, 2>) {
|
2022-07-27 22:45:46 +02:00
|
|
|
// Setup sm2
|
|
|
|
|
|
|
|
// Repeatedly trigger IRQ 3
|
|
|
|
let prg = pio_proc::pio_asm!(
|
|
|
|
".origin 0",
|
|
|
|
".wrap_target",
|
|
|
|
"set x,10",
|
|
|
|
"delay:",
|
|
|
|
"jmp x-- delay [15]",
|
|
|
|
"irq 3 [15]",
|
|
|
|
".wrap",
|
|
|
|
);
|
2023-05-06 11:36:07 +02:00
|
|
|
let mut cfg = Config::default();
|
2023-07-28 18:45:57 +02:00
|
|
|
cfg.use_program(&pio.load_program(&prg.program), &[]);
|
2023-05-06 11:36:07 +02:00
|
|
|
cfg.clock_divider = (U56F8!(125_000_000) / 2000).to_fixed();
|
|
|
|
sm.set_config(&cfg);
|
2023-04-25 20:40:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[embassy_executor::task]
|
2023-05-03 17:16:35 +02:00
|
|
|
async fn pio_task_sm2(mut irq: Irq<'static, PIO0, 3>, mut sm: StateMachine<'static, PIO0, 2>) {
|
2022-07-27 22:45:46 +02:00
|
|
|
sm.set_enable(true);
|
|
|
|
loop {
|
2023-04-27 02:12:49 +02:00
|
|
|
irq.wait().await;
|
2022-07-27 22:45:46 +02:00
|
|
|
info!("IRQ trigged");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[embassy_executor::main]
|
|
|
|
async fn main(spawner: Spawner) {
|
|
|
|
let p = embassy_rp::init(Default::default());
|
|
|
|
let pio = p.PIO0;
|
|
|
|
|
2023-04-26 19:43:57 +02:00
|
|
|
let Pio {
|
|
|
|
mut common,
|
2023-04-27 02:12:49 +02:00
|
|
|
irq3,
|
2023-04-26 19:43:57 +02:00
|
|
|
mut sm0,
|
|
|
|
mut sm1,
|
|
|
|
mut sm2,
|
|
|
|
..
|
2023-07-07 04:30:46 +02:00
|
|
|
} = Pio::new(pio, Irqs);
|
2023-04-26 19:43:57 +02:00
|
|
|
|
2023-05-03 08:15:46 +02:00
|
|
|
setup_pio_task_sm0(&mut common, &mut sm0, p.PIN_0);
|
2023-04-26 19:43:57 +02:00
|
|
|
setup_pio_task_sm1(&mut common, &mut sm1);
|
|
|
|
setup_pio_task_sm2(&mut common, &mut sm2);
|
2023-04-25 20:16:27 +02:00
|
|
|
spawner.spawn(pio_task_sm0(sm0)).unwrap();
|
2022-07-27 22:45:46 +02:00
|
|
|
spawner.spawn(pio_task_sm1(sm1)).unwrap();
|
2023-04-27 02:12:49 +02:00
|
|
|
spawner.spawn(pio_task_sm2(irq3, sm2)).unwrap();
|
2022-07-27 22:45:46 +02:00
|
|
|
}
|