diff options
| author | Curly <[email protected]> | 2025-02-23 07:33:58 -0800 |
|---|---|---|
| committer | Curly <[email protected]> | 2025-02-23 07:33:58 -0800 |
| commit | 3932835998802fc3abf7cce4f736e072858ebfd1 (patch) | |
| tree | 5dd714b99bc74a03556c58809237c88691c293bb /examples/rp235x/src/bin/pio_stepper.rs | |
| parent | c3c67db93e627a4fafe5e1a1123e5cbb4abafe47 (diff) | |
rename `rp23` (?) folder to `rp235x`; fix `ci.sh` to use `rp235x` folder
Diffstat (limited to 'examples/rp235x/src/bin/pio_stepper.rs')
| -rw-r--r-- | examples/rp235x/src/bin/pio_stepper.rs | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/examples/rp235x/src/bin/pio_stepper.rs b/examples/rp235x/src/bin/pio_stepper.rs new file mode 100644 index 000000000..3862c248b --- /dev/null +++ b/examples/rp235x/src/bin/pio_stepper.rs | |||
| @@ -0,0 +1,49 @@ | |||
| 1 | //! This example shows how to use the PIO module in the RP2040 to implement a stepper motor driver | ||
| 2 | //! for a 5-wire stepper such as the 28BYJ-48. You can halt an ongoing rotation by dropping the future. | ||
| 3 | |||
| 4 | #![no_std] | ||
| 5 | #![no_main] | ||
| 6 | |||
| 7 | use defmt::info; | ||
| 8 | use embassy_executor::Spawner; | ||
| 9 | use embassy_rp::bind_interrupts; | ||
| 10 | use embassy_rp::peripherals::PIO0; | ||
| 11 | use embassy_rp::pio::{InterruptHandler, Pio}; | ||
| 12 | use embassy_rp::pio_programs::stepper::{PioStepper, PioStepperProgram}; | ||
| 13 | use embassy_time::{with_timeout, Duration, Timer}; | ||
| 14 | use {defmt_rtt as _, panic_probe as _}; | ||
| 15 | |||
| 16 | bind_interrupts!(struct Irqs { | ||
| 17 | PIO0_IRQ_0 => InterruptHandler<PIO0>; | ||
| 18 | }); | ||
| 19 | |||
| 20 | #[embassy_executor::main] | ||
| 21 | async fn main(_spawner: Spawner) { | ||
| 22 | let p = embassy_rp::init(Default::default()); | ||
| 23 | let Pio { | ||
| 24 | mut common, irq0, sm0, .. | ||
| 25 | } = Pio::new(p.PIO0, Irqs); | ||
| 26 | |||
| 27 | let prg = PioStepperProgram::new(&mut common); | ||
| 28 | let mut stepper = PioStepper::new(&mut common, sm0, irq0, p.PIN_4, p.PIN_5, p.PIN_6, p.PIN_7, &prg); | ||
| 29 | stepper.set_frequency(120); | ||
| 30 | loop { | ||
| 31 | info!("CW full steps"); | ||
| 32 | stepper.step(1000).await; | ||
| 33 | |||
| 34 | info!("CCW full steps, drop after 1 sec"); | ||
| 35 | if with_timeout(Duration::from_secs(1), stepper.step(-i32::MAX)) | ||
| 36 | .await | ||
| 37 | .is_err() | ||
| 38 | { | ||
| 39 | info!("Time's up!"); | ||
| 40 | Timer::after(Duration::from_secs(1)).await; | ||
| 41 | } | ||
| 42 | |||
| 43 | info!("CW half steps"); | ||
| 44 | stepper.step_half(1000).await; | ||
| 45 | |||
| 46 | info!("CCW half steps"); | ||
| 47 | stepper.step_half(-1000).await; | ||
| 48 | } | ||
| 49 | } | ||
