aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2023-05-01 11:00:48 +0000
committerGitHub <[email protected]>2023-05-01 11:00:48 +0000
commitac0ea406f91a5b630811accd0c97a622a020c700 (patch)
tree08ae76de328feabf6406e6ad7f79b7d23bbe035e /examples
parentce04b732d131f488967846f6f593e10d7721ca86 (diff)
parentf4ade6af8bb2571ce2de0531d9c9715a7b8b941c (diff)
Merge #1395
1395: rp/pio: bit of a rework r=Dirbaio a=pennae the pio module is currently in a Bit of a State. this is far from all that's needed to make it more useful, but it's a start. Co-authored-by: pennae <[email protected]>
Diffstat (limited to 'examples')
-rw-r--r--examples/rp/src/bin/pio_async.rs46
-rw-r--r--examples/rp/src/bin/pio_dma.rs6
-rw-r--r--examples/rp/src/bin/ws2812-pio.rs13
3 files changed, 40 insertions, 25 deletions
diff --git a/examples/rp/src/bin/pio_async.rs b/examples/rp/src/bin/pio_async.rs
index e616d8c5a..1b075b8fd 100644
--- a/examples/rp/src/bin/pio_async.rs
+++ b/examples/rp/src/bin/pio_async.rs
@@ -4,13 +4,15 @@
4use defmt::info; 4use defmt::info;
5use embassy_executor::Spawner; 5use embassy_executor::Spawner;
6use embassy_rp::gpio::{AnyPin, Pin}; 6use embassy_rp::gpio::{AnyPin, Pin};
7use embassy_rp::pio::{Pio0, PioPeripheral, PioStateMachine, PioStateMachineInstance, ShiftDirection, Sm0, Sm1, Sm2}; 7use embassy_rp::pio::{
8 Pio0, PioCommon, PioCommonInstance, PioPeripheral, PioStateMachine, PioStateMachineInstance, ShiftDirection, Sm0,
9 Sm1, Sm2,
10};
8use embassy_rp::pio_instr_util; 11use embassy_rp::pio_instr_util;
9use embassy_rp::relocate::RelocatedProgram; 12use embassy_rp::relocate::RelocatedProgram;
10use {defmt_rtt as _, panic_probe as _}; 13use {defmt_rtt as _, panic_probe as _};
11 14
12#[embassy_executor::task] 15fn setup_pio_task_sm0(pio: &mut PioCommonInstance<Pio0>, sm: &mut PioStateMachineInstance<Pio0, Sm0>, pin: AnyPin) {
13async fn pio_task_sm0(mut sm: PioStateMachineInstance<Pio0, Sm0>, pin: AnyPin) {
14 // Setup sm0 16 // Setup sm0
15 17
16 // Send data serially to pin 18 // Send data serially to pin
@@ -23,11 +25,11 @@ async fn pio_task_sm0(mut sm: PioStateMachineInstance<Pio0, Sm0>, pin: AnyPin) {
23 ); 25 );
24 26
25 let relocated = RelocatedProgram::new(&prg.program); 27 let relocated = RelocatedProgram::new(&prg.program);
26 let out_pin = sm.make_pio_pin(pin); 28 let out_pin = pio.make_pio_pin(pin);
27 let pio_pins = [&out_pin]; 29 let pio_pins = [&out_pin];
28 sm.set_out_pins(&pio_pins); 30 sm.set_out_pins(&pio_pins);
29 sm.write_instr(relocated.origin() as usize, relocated.code()); 31 pio.write_instr(relocated.origin() as usize, relocated.code());
30 pio_instr_util::exec_jmp(&mut sm, relocated.origin()); 32 pio_instr_util::exec_jmp(sm, relocated.origin());
31 sm.set_clkdiv((125e6 / 20.0 / 2e2 * 256.0) as u32); 33 sm.set_clkdiv((125e6 / 20.0 / 2e2 * 256.0) as u32);
32 sm.set_set_range(0, 1); 34 sm.set_set_range(0, 1);
33 let pio::Wrap { source, target } = relocated.wrap(); 35 let pio::Wrap { source, target } = relocated.wrap();
@@ -35,7 +37,10 @@ async fn pio_task_sm0(mut sm: PioStateMachineInstance<Pio0, Sm0>, pin: AnyPin) {
35 37
36 sm.set_autopull(true); 38 sm.set_autopull(true);
37 sm.set_out_shift_dir(ShiftDirection::Left); 39 sm.set_out_shift_dir(ShiftDirection::Left);
40}
38 41
42#[embassy_executor::task]
43async fn pio_task_sm0(mut sm: PioStateMachineInstance<Pio0, Sm0>) {
39 sm.set_enable(true); 44 sm.set_enable(true);
40 45
41 let mut v = 0x0f0caffa; 46 let mut v = 0x0f0caffa;
@@ -46,16 +51,15 @@ async fn pio_task_sm0(mut sm: PioStateMachineInstance<Pio0, Sm0>, pin: AnyPin) {
46 } 51 }
47} 52}
48 53
49#[embassy_executor::task] 54fn setup_pio_task_sm1(pio: &mut PioCommonInstance<Pio0>, sm: &mut PioStateMachineInstance<Pio0, Sm1>) {
50async fn pio_task_sm1(mut sm: PioStateMachineInstance<Pio0, Sm1>) {
51 // Setupm sm1 55 // Setupm sm1
52 56
53 // Read 0b10101 repeatedly until ISR is full 57 // Read 0b10101 repeatedly until ISR is full
54 let prg = pio_proc::pio_asm!(".origin 8", "set x, 0x15", ".wrap_target", "in x, 5 [31]", ".wrap",); 58 let prg = pio_proc::pio_asm!(".origin 8", "set x, 0x15", ".wrap_target", "in x, 5 [31]", ".wrap",);
55 59
56 let relocated = RelocatedProgram::new(&prg.program); 60 let relocated = RelocatedProgram::new(&prg.program);
57 sm.write_instr(relocated.origin() as usize, relocated.code()); 61 pio.write_instr(relocated.origin() as usize, relocated.code());
58 pio_instr_util::exec_jmp(&mut sm, relocated.origin()); 62 pio_instr_util::exec_jmp(sm, relocated.origin());
59 sm.set_clkdiv((125e6 / 2e3 * 256.0) as u32); 63 sm.set_clkdiv((125e6 / 2e3 * 256.0) as u32);
60 sm.set_set_range(0, 0); 64 sm.set_set_range(0, 0);
61 let pio::Wrap { source, target } = relocated.wrap(); 65 let pio::Wrap { source, target } = relocated.wrap();
@@ -63,6 +67,10 @@ async fn pio_task_sm1(mut sm: PioStateMachineInstance<Pio0, Sm1>) {
63 67
64 sm.set_autopush(true); 68 sm.set_autopush(true);
65 sm.set_in_shift_dir(ShiftDirection::Right); 69 sm.set_in_shift_dir(ShiftDirection::Right);
70}
71
72#[embassy_executor::task]
73async fn pio_task_sm1(mut sm: PioStateMachineInstance<Pio0, Sm1>) {
66 sm.set_enable(true); 74 sm.set_enable(true);
67 loop { 75 loop {
68 let rx = sm.wait_pull().await; 76 let rx = sm.wait_pull().await;
@@ -70,8 +78,7 @@ async fn pio_task_sm1(mut sm: PioStateMachineInstance<Pio0, Sm1>) {
70 } 78 }
71} 79}
72 80
73#[embassy_executor::task] 81fn setup_pio_task_sm2(pio: &mut PioCommonInstance<Pio0>, sm: &mut PioStateMachineInstance<Pio0, Sm2>) {
74async fn pio_task_sm2(mut sm: PioStateMachineInstance<Pio0, Sm2>) {
75 // Setup sm2 82 // Setup sm2
76 83
77 // Repeatedly trigger IRQ 3 84 // Repeatedly trigger IRQ 3
@@ -85,13 +92,17 @@ async fn pio_task_sm2(mut sm: PioStateMachineInstance<Pio0, Sm2>) {
85 ".wrap", 92 ".wrap",
86 ); 93 );
87 let relocated = RelocatedProgram::new(&prg.program); 94 let relocated = RelocatedProgram::new(&prg.program);
88 sm.write_instr(relocated.origin() as usize, relocated.code()); 95 pio.write_instr(relocated.origin() as usize, relocated.code());
89 96
90 let pio::Wrap { source, target } = relocated.wrap(); 97 let pio::Wrap { source, target } = relocated.wrap();
91 sm.set_wrap(source, target); 98 sm.set_wrap(source, target);
92 99
93 pio_instr_util::exec_jmp(&mut sm, relocated.origin()); 100 pio_instr_util::exec_jmp(sm, relocated.origin());
94 sm.set_clkdiv((125e6 / 2e3 * 256.0) as u32); 101 sm.set_clkdiv((125e6 / 2e3 * 256.0) as u32);
102}
103
104#[embassy_executor::task]
105async fn pio_task_sm2(mut sm: PioStateMachineInstance<Pio0, Sm2>) {
95 sm.set_enable(true); 106 sm.set_enable(true);
96 loop { 107 loop {
97 sm.wait_irq(3).await; 108 sm.wait_irq(3).await;
@@ -104,9 +115,12 @@ async fn main(spawner: Spawner) {
104 let p = embassy_rp::init(Default::default()); 115 let p = embassy_rp::init(Default::default());
105 let pio = p.PIO0; 116 let pio = p.PIO0;
106 117
107 let (_, sm0, sm1, sm2, ..) = pio.split(); 118 let (mut pio0, mut sm0, mut sm1, mut sm2, ..) = pio.split();
108 119
109 spawner.spawn(pio_task_sm0(sm0, p.PIN_0.degrade())).unwrap(); 120 setup_pio_task_sm0(&mut pio0, &mut sm0, p.PIN_0.degrade());
121 setup_pio_task_sm1(&mut pio0, &mut sm1);
122 setup_pio_task_sm2(&mut pio0, &mut sm2);
123 spawner.spawn(pio_task_sm0(sm0)).unwrap();
110 spawner.spawn(pio_task_sm1(sm1)).unwrap(); 124 spawner.spawn(pio_task_sm1(sm1)).unwrap();
111 spawner.spawn(pio_task_sm2(sm2)).unwrap(); 125 spawner.spawn(pio_task_sm2(sm2)).unwrap();
112} 126}
diff --git a/examples/rp/src/bin/pio_dma.rs b/examples/rp/src/bin/pio_dma.rs
index 145e4a656..7d4919f75 100644
--- a/examples/rp/src/bin/pio_dma.rs
+++ b/examples/rp/src/bin/pio_dma.rs
@@ -4,7 +4,7 @@
4use defmt::info; 4use defmt::info;
5use embassy_executor::Spawner; 5use embassy_executor::Spawner;
6use embassy_futures::join::join; 6use embassy_futures::join::join;
7use embassy_rp::pio::{PioPeripheral, PioStateMachine, ShiftDirection}; 7use embassy_rp::pio::{PioCommon, PioPeripheral, PioStateMachine, ShiftDirection};
8use embassy_rp::relocate::RelocatedProgram; 8use embassy_rp::relocate::RelocatedProgram;
9use embassy_rp::{pio_instr_util, Peripheral}; 9use embassy_rp::{pio_instr_util, Peripheral};
10use {defmt_rtt as _, panic_probe as _}; 10use {defmt_rtt as _, panic_probe as _};
@@ -19,7 +19,7 @@ fn swap_nibbles(v: u32) -> u32 {
19async fn main(_spawner: Spawner) { 19async fn main(_spawner: Spawner) {
20 let p = embassy_rp::init(Default::default()); 20 let p = embassy_rp::init(Default::default());
21 let pio = p.PIO0; 21 let pio = p.PIO0;
22 let (_, mut sm, ..) = pio.split(); 22 let (mut pio0, mut sm, ..) = pio.split();
23 23
24 let prg = pio_proc::pio_asm!( 24 let prg = pio_proc::pio_asm!(
25 ".origin 0", 25 ".origin 0",
@@ -34,7 +34,7 @@ async fn main(_spawner: Spawner) {
34 ); 34 );
35 35
36 let relocated = RelocatedProgram::new(&prg.program); 36 let relocated = RelocatedProgram::new(&prg.program);
37 sm.write_instr(relocated.origin() as usize, relocated.code()); 37 pio0.write_instr(relocated.origin() as usize, relocated.code());
38 pio_instr_util::exec_jmp(&mut sm, relocated.origin()); 38 pio_instr_util::exec_jmp(&mut sm, relocated.origin());
39 sm.set_clkdiv((125e6 / 10e3 * 256.0) as u32); 39 sm.set_clkdiv((125e6 / 10e3 * 256.0) as u32);
40 let pio::Wrap { source, target } = relocated.wrap(); 40 let pio::Wrap { source, target } = relocated.wrap();
diff --git a/examples/rp/src/bin/ws2812-pio.rs b/examples/rp/src/bin/ws2812-pio.rs
index 5f8a3baee..041e8ae11 100644
--- a/examples/rp/src/bin/ws2812-pio.rs
+++ b/examples/rp/src/bin/ws2812-pio.rs
@@ -6,7 +6,8 @@ use defmt::*;
6use embassy_executor::Spawner; 6use embassy_executor::Spawner;
7use embassy_rp::gpio::{self, Pin}; 7use embassy_rp::gpio::{self, Pin};
8use embassy_rp::pio::{ 8use embassy_rp::pio::{
9 FifoJoin, PioInstance, PioPeripheral, PioStateMachine, PioStateMachineInstance, ShiftDirection, SmInstance, 9 FifoJoin, PioCommon, PioCommonInstance, PioInstance, PioPeripheral, PioStateMachine, PioStateMachineInstance,
10 ShiftDirection, SmInstance,
10}; 11};
11use embassy_rp::pio_instr_util; 12use embassy_rp::pio_instr_util;
12use embassy_rp::relocate::RelocatedProgram; 13use embassy_rp::relocate::RelocatedProgram;
@@ -18,7 +19,7 @@ pub struct Ws2812<P: PioInstance, S: SmInstance> {
18} 19}
19 20
20impl<P: PioInstance, S: SmInstance> Ws2812<P, S> { 21impl<P: PioInstance, S: SmInstance> Ws2812<P, S> {
21 pub fn new(mut sm: PioStateMachineInstance<P, S>, pin: gpio::AnyPin) -> Self { 22 pub fn new(mut pio: PioCommonInstance<P>, mut sm: PioStateMachineInstance<P, S>, pin: gpio::AnyPin) -> Self {
22 // Setup sm0 23 // Setup sm0
23 24
24 // prepare the PIO program 25 // prepare the PIO program
@@ -49,11 +50,11 @@ impl<P: PioInstance, S: SmInstance> Ws2812<P, S> {
49 let prg = a.assemble_with_wrap(wrap_source, wrap_target); 50 let prg = a.assemble_with_wrap(wrap_source, wrap_target);
50 51
51 let relocated = RelocatedProgram::new(&prg); 52 let relocated = RelocatedProgram::new(&prg);
52 sm.write_instr(relocated.origin() as usize, relocated.code()); 53 pio.write_instr(relocated.origin() as usize, relocated.code());
53 pio_instr_util::exec_jmp(&mut sm, relocated.origin()); 54 pio_instr_util::exec_jmp(&mut sm, relocated.origin());
54 55
55 // Pin config 56 // Pin config
56 let out_pin = sm.make_pio_pin(pin); 57 let out_pin = pio.make_pio_pin(pin);
57 sm.set_set_pins(&[&out_pin]); 58 sm.set_set_pins(&[&out_pin]);
58 sm.set_sideset_base_pin(&out_pin); 59 sm.set_sideset_base_pin(&out_pin);
59 sm.set_sideset_count(1); 60 sm.set_sideset_count(1);
@@ -115,7 +116,7 @@ async fn main(_spawner: Spawner) {
115 info!("Start"); 116 info!("Start");
116 let p = embassy_rp::init(Default::default()); 117 let p = embassy_rp::init(Default::default());
117 118
118 let (_pio0, sm0, _sm1, _sm2, _sm3) = p.PIO0.split(); 119 let (pio0, sm0, _sm1, _sm2, _sm3) = p.PIO0.split();
119 120
120 // This is the number of leds in the string. Helpfully, the sparkfun thing plus and adafruit 121 // This is the number of leds in the string. Helpfully, the sparkfun thing plus and adafruit
121 // feather boards for the 2040 both have one built in. 122 // feather boards for the 2040 both have one built in.
@@ -124,7 +125,7 @@ async fn main(_spawner: Spawner) {
124 125
125 // For the thing plus, use pin 8 126 // For the thing plus, use pin 8
126 // For the feather, use pin 16 127 // For the feather, use pin 16
127 let mut ws2812 = Ws2812::new(sm0, p.PIN_8.degrade()); 128 let mut ws2812 = Ws2812::new(pio0, sm0, p.PIN_8.degrade());
128 129
129 // Loop forever making RGB values and pushing them out to the WS2812. 130 // Loop forever making RGB values and pushing them out to the WS2812.
130 loop { 131 loop {