Skip to content

Commit 72521e9

Browse files
authored
Merge pull request #918 from jannic/encode-at-compiletime
2 parents 0ccaf3d + 0d9bc4f commit 72521e9

File tree

2 files changed

+30
-34
lines changed

2 files changed

+30
-34
lines changed

rp2040-hal/src/pio.rs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -666,15 +666,16 @@ impl<SM: ValidStateMachine, State> StateMachine<SM, State> {
666666
// the 'PULL' has no effect, or the program will stall on the 'PULL' until data becomes
667667
// available in the FIFO.
668668

669-
// TODO: encode at compile time once pio 0.3.0 is out
670-
const OUT: InstructionOperands = InstructionOperands::OUT {
669+
const OUT: u16 = InstructionOperands::OUT {
671670
destination: pio::OutDestination::NULL,
672671
bit_count: 32,
673-
};
674-
const PULL: InstructionOperands = InstructionOperands::PULL {
672+
}
673+
.encode();
674+
const PULL: u16 = InstructionOperands::PULL {
675675
if_empty: false,
676676
block: false,
677-
};
677+
}
678+
.encode();
678679

679680
// Safety: all accesses to these registers are controlled by this instance
680681
unsafe {
@@ -687,8 +688,7 @@ impl<SM: ValidStateMachine, State> StateMachine<SM, State> {
687688
OUT
688689
} else {
689690
PULL
690-
}
691-
.encode();
691+
};
692692

693693
// Safety: sm0_instr may be accessed from SM::exec_instruction.
694694
let mut saved_sideset_count = 0;
@@ -757,13 +757,12 @@ impl<SM: ValidStateMachine> StateMachine<SM, Stopped> {
757757
///
758758
/// The iterator's item are pairs of `(pin_number, pin_state)`.
759759
pub fn set_pins(&mut self, pins: impl IntoIterator<Item = (u8, PinState)>) {
760-
// TODO: turn those three into const once pio 0.3.0 is released
761-
let set_high_instr = InstructionOperands::SET {
760+
const SET_HIGH_INSTR: u16 = InstructionOperands::SET {
762761
destination: pio::SetDestination::PINS,
763762
data: 1,
764763
}
765764
.encode();
766-
let set_low_instr = InstructionOperands::SET {
765+
const SET_LOW_INSTR: u16 = InstructionOperands::SET {
767766
destination: pio::SetDestination::PINS,
768767
data: 0,
769768
}
@@ -789,9 +788,9 @@ impl<SM: ValidStateMachine> StateMachine<SM, Stopped> {
789788
for (pin_num, pin_state) in pins {
790789
sm_pinctrl.write(|w| w.set_base().bits(pin_num).set_count().bits(1));
791790
let instruction = if pin_state == PinState::High {
792-
set_high_instr
791+
SET_HIGH_INSTR
793792
} else {
794-
set_low_instr
793+
SET_LOW_INSTR
795794
};
796795

797796
sm_instr.write(|w| w.sm0_instr().bits(instruction))
@@ -809,13 +808,12 @@ impl<SM: ValidStateMachine> StateMachine<SM, Stopped> {
809808
///
810809
/// The iterator's item are pairs of `(pin_number, pin_dir)`.
811810
pub fn set_pindirs(&mut self, pindirs: impl IntoIterator<Item = (u8, PinDir)>) {
812-
// TODO: turn those three into const once pio 0.3.0 is released
813-
let set_output_instr = InstructionOperands::SET {
811+
const SET_OUTPUT_INSTR: u16 = InstructionOperands::SET {
814812
destination: pio::SetDestination::PINDIRS,
815813
data: 1,
816814
}
817815
.encode();
818-
let set_input_instr = InstructionOperands::SET {
816+
const SET_INPUT_INSTR: u16 = InstructionOperands::SET {
819817
destination: pio::SetDestination::PINDIRS,
820818
data: 0,
821819
}
@@ -841,9 +839,9 @@ impl<SM: ValidStateMachine> StateMachine<SM, Stopped> {
841839
for (pin_num, pin_dir) in pindirs {
842840
sm_pinctrl.write(|w| w.set_base().bits(pin_num).set_count().bits(1));
843841
let instruction = if pin_dir == PinDir::Output {
844-
set_output_instr
842+
SET_OUTPUT_INSTR
845843
} else {
846-
set_input_instr
844+
SET_INPUT_INSTR
847845
};
848846

849847
sm_instr.write(|w| w.sm0_instr().bits(instruction))

rp235x-hal/src/pio.rs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -676,15 +676,16 @@ impl<SM: ValidStateMachine, State> StateMachine<SM, State> {
676676
// the 'PULL' has no effect, or the program will stall on the 'PULL' until data becomes
677677
// available in the FIFO.
678678

679-
// TODO: encode at compile time once pio 0.3.0 is out
680-
const OUT: InstructionOperands = InstructionOperands::OUT {
679+
const OUT: u16 = InstructionOperands::OUT {
681680
destination: pio::OutDestination::NULL,
682681
bit_count: 32,
683-
};
684-
const PULL: InstructionOperands = InstructionOperands::PULL {
682+
}
683+
.encode();
684+
const PULL: u16 = InstructionOperands::PULL {
685685
if_empty: false,
686686
block: false,
687-
};
687+
}
688+
.encode();
688689

689690
// Safety: all accesses to these registers are controlled by this instance
690691
unsafe {
@@ -697,8 +698,7 @@ impl<SM: ValidStateMachine, State> StateMachine<SM, State> {
697698
OUT
698699
} else {
699700
PULL
700-
}
701-
.encode();
701+
};
702702

703703
// Safety: sm0_instr may be accessed from SM::exec_instruction.
704704
let mut saved_sideset_count = 0;
@@ -767,13 +767,12 @@ impl<SM: ValidStateMachine> StateMachine<SM, Stopped> {
767767
///
768768
/// The iterator's item are pairs of `(pin_number, pin_state)`.
769769
pub fn set_pins(&mut self, pins: impl IntoIterator<Item = (u8, PinState)>) {
770-
// TODO: turn those three into const once pio 0.3.0 is released
771-
let set_high_instr = InstructionOperands::SET {
770+
const SET_HIGH_INSTR: u16 = InstructionOperands::SET {
772771
destination: pio::SetDestination::PINS,
773772
data: 1,
774773
}
775774
.encode();
776-
let set_low_instr = InstructionOperands::SET {
775+
const SET_LOW_INSTR: u16 = InstructionOperands::SET {
777776
destination: pio::SetDestination::PINS,
778777
data: 0,
779778
}
@@ -799,9 +798,9 @@ impl<SM: ValidStateMachine> StateMachine<SM, Stopped> {
799798
for (pin_num, pin_state) in pins {
800799
sm_pinctrl.write(|w| w.set_base().bits(pin_num).set_count().bits(1));
801800
let instruction = if pin_state == PinState::High {
802-
set_high_instr
801+
SET_HIGH_INSTR
803802
} else {
804-
set_low_instr
803+
SET_LOW_INSTR
805804
};
806805

807806
sm_instr.write(|w| w.sm0_instr().bits(instruction))
@@ -819,13 +818,12 @@ impl<SM: ValidStateMachine> StateMachine<SM, Stopped> {
819818
///
820819
/// The iterator's item are pairs of `(pin_number, pin_dir)`.
821820
pub fn set_pindirs(&mut self, pindirs: impl IntoIterator<Item = (u8, PinDir)>) {
822-
// TODO: turn those three into const once pio 0.3.0 is released
823-
let set_output_instr = InstructionOperands::SET {
821+
const SET_OUTPUT_INSTR: u16 = InstructionOperands::SET {
824822
destination: pio::SetDestination::PINDIRS,
825823
data: 1,
826824
}
827825
.encode();
828-
let set_input_instr = InstructionOperands::SET {
826+
const SET_INPUT_INSTR: u16 = InstructionOperands::SET {
829827
destination: pio::SetDestination::PINDIRS,
830828
data: 0,
831829
}
@@ -851,9 +849,9 @@ impl<SM: ValidStateMachine> StateMachine<SM, Stopped> {
851849
for (pin_num, pin_dir) in pindirs {
852850
sm_pinctrl.write(|w| w.set_base().bits(pin_num).set_count().bits(1));
853851
let instruction = if pin_dir == PinDir::Output {
854-
set_output_instr
852+
SET_OUTPUT_INSTR
855853
} else {
856-
set_input_instr
854+
SET_INPUT_INSTR
857855
};
858856

859857
sm_instr.write(|w| w.sm0_instr().bits(instruction))

0 commit comments

Comments
 (0)