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-04-26 00:23:18 +02:00
|
|
|
use embassy_rp::peripherals::PIO0;
|
2023-04-27 02:12:49 +02:00
|
|
|
use embassy_rp::pio::{Pio, PioCommon, PioIrq, PioPin, PioStateMachine, ShiftDirection};
|
2022-07-27 22:45:46 +02:00
|
|
|
use embassy_rp::pio_instr_util;
|
2022-12-06 21:42:30 +01:00
|
|
|
use embassy_rp::relocate::RelocatedProgram;
|
2022-07-27 22:45:46 +02:00
|
|
|
use {defmt_rtt as _, panic_probe as _};
|
|
|
|
|
2023-05-03 09:42:42 +02:00
|
|
|
fn setup_pio_task_sm0(pio: &mut PioCommon<PIO0>, sm: &mut PioStateMachine<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",
|
|
|
|
);
|
|
|
|
|
2022-12-06 21:42:30 +01:00
|
|
|
let relocated = RelocatedProgram::new(&prg.program);
|
2023-04-25 20:16:27 +02:00
|
|
|
let out_pin = pio.make_pio_pin(pin);
|
2022-07-27 22:45:46 +02:00
|
|
|
let pio_pins = [&out_pin];
|
|
|
|
sm.set_out_pins(&pio_pins);
|
2023-04-25 20:40:18 +02:00
|
|
|
pio.write_instr(relocated.origin() as usize, relocated.code());
|
2023-04-25 20:16:27 +02:00
|
|
|
pio_instr_util::exec_jmp(sm, relocated.origin());
|
2022-07-27 22:45:46 +02:00
|
|
|
sm.set_clkdiv((125e6 / 20.0 / 2e2 * 256.0) as u32);
|
|
|
|
sm.set_set_range(0, 1);
|
2022-12-06 21:42:30 +01:00
|
|
|
let pio::Wrap { source, target } = relocated.wrap();
|
|
|
|
sm.set_wrap(source, target);
|
|
|
|
|
2022-07-27 22:45:46 +02:00
|
|
|
sm.set_autopull(true);
|
|
|
|
sm.set_out_shift_dir(ShiftDirection::Left);
|
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 09:42:42 +02:00
|
|
|
async fn pio_task_sm0(mut sm: PioStateMachine<'static, PIO0, 0>) {
|
2022-07-27 22:45:46 +02:00
|
|
|
sm.set_enable(true);
|
|
|
|
|
|
|
|
let mut v = 0x0f0caffa;
|
|
|
|
loop {
|
|
|
|
sm.wait_push(v).await;
|
|
|
|
v ^= 0xffff;
|
|
|
|
info!("Pushed {:032b} to FIFO", v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-03 09:42:42 +02:00
|
|
|
fn setup_pio_task_sm1(pio: &mut PioCommon<PIO0>, sm: &mut PioStateMachine<PIO0, 1>) {
|
2022-07-27 22:45:46 +02:00
|
|
|
// Setupm sm1
|
|
|
|
|
|
|
|
// Read 0b10101 repeatedly until ISR is full
|
|
|
|
let prg = pio_proc::pio_asm!(".origin 8", "set x, 0x15", ".wrap_target", "in x, 5 [31]", ".wrap",);
|
|
|
|
|
2022-12-06 21:42:30 +01:00
|
|
|
let relocated = RelocatedProgram::new(&prg.program);
|
2023-04-25 20:40:18 +02:00
|
|
|
pio.write_instr(relocated.origin() as usize, relocated.code());
|
|
|
|
pio_instr_util::exec_jmp(sm, relocated.origin());
|
2022-07-27 22:45:46 +02:00
|
|
|
sm.set_clkdiv((125e6 / 2e3 * 256.0) as u32);
|
|
|
|
sm.set_set_range(0, 0);
|
2022-12-06 21:42:30 +01:00
|
|
|
let pio::Wrap { source, target } = relocated.wrap();
|
|
|
|
sm.set_wrap(source, target);
|
|
|
|
|
2022-07-27 22:45:46 +02:00
|
|
|
sm.set_autopush(true);
|
|
|
|
sm.set_in_shift_dir(ShiftDirection::Right);
|
2023-04-25 20:40:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[embassy_executor::task]
|
2023-05-03 09:42:42 +02:00
|
|
|
async fn pio_task_sm1(mut sm: PioStateMachine<'static, PIO0, 1>) {
|
2022-07-27 22:45:46 +02:00
|
|
|
sm.set_enable(true);
|
|
|
|
loop {
|
|
|
|
let rx = sm.wait_pull().await;
|
|
|
|
info!("Pulled {:032b} from FIFO", rx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-03 09:42:42 +02:00
|
|
|
fn setup_pio_task_sm2(pio: &mut PioCommon<PIO0>, sm: &mut PioStateMachine<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",
|
|
|
|
);
|
2022-12-06 21:42:30 +01:00
|
|
|
let relocated = RelocatedProgram::new(&prg.program);
|
2023-04-25 20:40:18 +02:00
|
|
|
pio.write_instr(relocated.origin() as usize, relocated.code());
|
2022-12-06 21:42:30 +01:00
|
|
|
|
|
|
|
let pio::Wrap { source, target } = relocated.wrap();
|
|
|
|
sm.set_wrap(source, target);
|
2022-07-27 22:45:46 +02:00
|
|
|
|
2023-04-25 20:40:18 +02:00
|
|
|
pio_instr_util::exec_jmp(sm, relocated.origin());
|
2022-07-27 22:45:46 +02:00
|
|
|
sm.set_clkdiv((125e6 / 2e3 * 256.0) as u32);
|
2023-04-25 20:40:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[embassy_executor::task]
|
2023-04-27 02:12:49 +02:00
|
|
|
async fn pio_task_sm2(mut irq: PioIrq<'static, PIO0, 3>, mut sm: PioStateMachine<'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,
|
|
|
|
..
|
|
|
|
} = Pio::new(pio);
|
|
|
|
|
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
|
|
|
}
|