Skip to content

Commit e3edd67

Browse files
committed
rp/pio: move instructions to methods of the SM.
1 parent 1780f84 commit e3edd67

File tree

3 files changed

+91
-89
lines changed

3 files changed

+91
-89
lines changed

cyw43-pio/src/lib.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use core::slice;
88
use cyw43::SpiBusCyw43;
99
use embassy_rp::dma::Channel;
1010
use embassy_rp::gpio::{Drive, Level, Output, Pull, SlewRate};
11-
use embassy_rp::pio::{instr, Common, Config, Direction, Instance, Irq, PioPin, ShiftDirection, StateMachine};
11+
use embassy_rp::pio::{Common, Config, Direction, Instance, Irq, PioPin, ShiftDirection, StateMachine};
1212
use embassy_rp::{Peripheral, PeripheralRef};
1313
use fixed::types::extra::U8;
1414
use fixed::FixedU32;
@@ -161,10 +161,10 @@ where
161161
defmt::trace!("write={} read={}", write_bits, read_bits);
162162

163163
unsafe {
164-
instr::set_x(&mut self.sm, write_bits as u32);
165-
instr::set_y(&mut self.sm, read_bits as u32);
166-
instr::set_pindir(&mut self.sm, 0b1);
167-
instr::exec_jmp(&mut self.sm, self.wrap_target);
164+
self.sm.set_x(write_bits as u32);
165+
self.sm.set_y(read_bits as u32);
166+
self.sm.set_pindir(0b1);
167+
self.sm.exec_jmp(self.wrap_target);
168168
}
169169

170170
self.sm.set_enable(true);
@@ -192,10 +192,10 @@ where
192192
defmt::trace!("cmd_read cmd = {:02x} len = {}", cmd, read.len());
193193

194194
unsafe {
195-
instr::set_y(&mut self.sm, read_bits as u32);
196-
instr::set_x(&mut self.sm, write_bits as u32);
197-
instr::set_pindir(&mut self.sm, 0b1);
198-
instr::exec_jmp(&mut self.sm, self.wrap_target);
195+
self.sm.set_y(read_bits as u32);
196+
self.sm.set_x(write_bits as u32);
197+
self.sm.set_pindir(0b1);
198+
self.sm.exec_jmp(self.wrap_target);
199199
}
200200

201201
// self.cs.set_low();

embassy-rp/src/pio/instr.rs

Lines changed: 80 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -3,99 +3,101 @@ use pio::{InSource, InstructionOperands, JmpCondition, OutDestination, SetDestin
33

44
use crate::pio::{Instance, StateMachine};
55

6-
/// Set value of scratch register X.
7-
pub unsafe fn set_x<PIO: Instance, const SM: usize>(sm: &mut StateMachine<PIO, SM>, value: u32) {
8-
const OUT: u16 = InstructionOperands::OUT {
9-
destination: OutDestination::X,
10-
bit_count: 32,
6+
impl<'d, PIO: Instance, const SM: usize> StateMachine<'d, PIO, SM> {
7+
/// Set value of scratch register X.
8+
pub unsafe fn set_x(&mut self, value: u32) {
9+
const OUT: u16 = InstructionOperands::OUT {
10+
destination: OutDestination::X,
11+
bit_count: 32,
12+
}
13+
.encode();
14+
self.tx().push(value);
15+
self.exec_instr(OUT);
1116
}
12-
.encode();
13-
sm.tx().push(value);
14-
sm.exec_instr(OUT);
15-
}
1617

17-
/// Get value of scratch register X.
18-
pub unsafe fn get_x<PIO: Instance, const SM: usize>(sm: &mut StateMachine<PIO, SM>) -> u32 {
19-
const IN: u16 = InstructionOperands::IN {
20-
source: InSource::X,
21-
bit_count: 32,
18+
/// Get value of scratch register X.
19+
pub unsafe fn get_x(&mut self) -> u32 {
20+
const IN: u16 = InstructionOperands::IN {
21+
source: InSource::X,
22+
bit_count: 32,
23+
}
24+
.encode();
25+
self.exec_instr(IN);
26+
self.rx().pull()
2227
}
23-
.encode();
24-
sm.exec_instr(IN);
25-
sm.rx().pull()
26-
}
2728

28-
/// Set value of scratch register Y.
29-
pub unsafe fn set_y<PIO: Instance, const SM: usize>(sm: &mut StateMachine<PIO, SM>, value: u32) {
30-
const OUT: u16 = InstructionOperands::OUT {
31-
destination: OutDestination::Y,
32-
bit_count: 32,
29+
/// Set value of scratch register Y.
30+
pub unsafe fn set_y(&mut self, value: u32) {
31+
const OUT: u16 = InstructionOperands::OUT {
32+
destination: OutDestination::Y,
33+
bit_count: 32,
34+
}
35+
.encode();
36+
self.tx().push(value);
37+
self.exec_instr(OUT);
3338
}
34-
.encode();
35-
sm.tx().push(value);
36-
sm.exec_instr(OUT);
37-
}
3839

39-
/// Get value of scratch register Y.
40-
pub unsafe fn get_y<PIO: Instance, const SM: usize>(sm: &mut StateMachine<PIO, SM>) -> u32 {
41-
const IN: u16 = InstructionOperands::IN {
42-
source: InSource::Y,
43-
bit_count: 32,
44-
}
45-
.encode();
46-
sm.exec_instr(IN);
40+
/// Get value of scratch register Y.
41+
pub unsafe fn get_y(&mut self) -> u32 {
42+
const IN: u16 = InstructionOperands::IN {
43+
source: InSource::Y,
44+
bit_count: 32,
45+
}
46+
.encode();
47+
self.exec_instr(IN);
4748

48-
sm.rx().pull()
49-
}
49+
self.rx().pull()
50+
}
5051

51-
/// Set instruction for pindir destination.
52-
pub unsafe fn set_pindir<PIO: Instance, const SM: usize>(sm: &mut StateMachine<PIO, SM>, data: u8) {
53-
let set: u16 = InstructionOperands::SET {
54-
destination: SetDestination::PINDIRS,
55-
data,
52+
/// Set instruction for pindir destination.
53+
pub unsafe fn set_pindir(&mut self, data: u8) {
54+
let set: u16 = InstructionOperands::SET {
55+
destination: SetDestination::PINDIRS,
56+
data,
57+
}
58+
.encode();
59+
self.exec_instr(set);
5660
}
57-
.encode();
58-
sm.exec_instr(set);
59-
}
6061

61-
/// Set instruction for pin destination.
62-
pub unsafe fn set_pin<PIO: Instance, const SM: usize>(sm: &mut StateMachine<PIO, SM>, data: u8) {
63-
let set: u16 = InstructionOperands::SET {
64-
destination: SetDestination::PINS,
65-
data,
62+
/// Set instruction for pin destination.
63+
pub unsafe fn set_pin(&mut self, data: u8) {
64+
let set: u16 = InstructionOperands::SET {
65+
destination: SetDestination::PINS,
66+
data,
67+
}
68+
.encode();
69+
self.exec_instr(set);
6670
}
67-
.encode();
68-
sm.exec_instr(set);
69-
}
7071

71-
/// Out instruction for pin destination.
72-
pub unsafe fn set_out_pin<PIO: Instance, const SM: usize>(sm: &mut StateMachine<PIO, SM>, data: u32) {
73-
const OUT: u16 = InstructionOperands::OUT {
74-
destination: OutDestination::PINS,
75-
bit_count: 32,
72+
/// Out instruction for pin destination.
73+
pub unsafe fn set_out_pin(&mut self, data: u32) {
74+
const OUT: u16 = InstructionOperands::OUT {
75+
destination: OutDestination::PINS,
76+
bit_count: 32,
77+
}
78+
.encode();
79+
self.tx().push(data);
80+
self.exec_instr(OUT);
7681
}
77-
.encode();
78-
sm.tx().push(data);
79-
sm.exec_instr(OUT);
80-
}
8182

82-
/// Out instruction for pindir destination.
83-
pub unsafe fn set_out_pindir<PIO: Instance, const SM: usize>(sm: &mut StateMachine<PIO, SM>, data: u32) {
84-
const OUT: u16 = InstructionOperands::OUT {
85-
destination: OutDestination::PINDIRS,
86-
bit_count: 32,
83+
/// Out instruction for pindir destination.
84+
pub unsafe fn set_out_pindir(&mut self, data: u32) {
85+
const OUT: u16 = InstructionOperands::OUT {
86+
destination: OutDestination::PINDIRS,
87+
bit_count: 32,
88+
}
89+
.encode();
90+
self.tx().push(data);
91+
self.exec_instr(OUT);
8792
}
88-
.encode();
89-
sm.tx().push(data);
90-
sm.exec_instr(OUT);
91-
}
9293

93-
/// Jump instruction to address.
94-
pub unsafe fn exec_jmp<PIO: Instance, const SM: usize>(sm: &mut StateMachine<PIO, SM>, to_addr: u8) {
95-
let jmp: u16 = InstructionOperands::JMP {
96-
address: to_addr,
97-
condition: JmpCondition::Always,
94+
/// Jump instruction to address.
95+
pub unsafe fn exec_jmp(&mut self, to_addr: u8) {
96+
let jmp: u16 = InstructionOperands::JMP {
97+
address: to_addr,
98+
condition: JmpCondition::Always,
99+
}
100+
.encode();
101+
self.exec_instr(jmp);
98102
}
99-
.encode();
100-
sm.exec_instr(jmp);
101103
}

embassy-rp/src/pio/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::interrupt::typelevel::{Binding, Handler, Interrupt};
1818
use crate::relocate::RelocatedProgram;
1919
use crate::{pac, peripherals, RegExt};
2020

21-
pub mod instr;
21+
mod instr;
2222

2323
/// Wakers for interrupts and FIFOs.
2424
pub struct Wakers([AtomicWaker; 12]);
@@ -812,7 +812,7 @@ impl<'d, PIO: Instance + 'd, const SM: usize> StateMachine<'d, PIO, SM> {
812812
}
813813

814814
if let Some(origin) = config.origin {
815-
unsafe { instr::exec_jmp(self, origin) }
815+
unsafe { self.exec_jmp(origin) }
816816
}
817817
}
818818

0 commit comments

Comments
 (0)