From 1ba07b09e780a2c01ec3e3c052eaa28bcd250d2a Mon Sep 17 00:00:00 2001 From: David Cole Date: Wed, 26 Oct 2022 21:03:58 +1300 Subject: [PATCH 1/2] Add ability to modify installed program wrap bounds The wrap source and target cannot change after installing the program into the PIO at present, even though this is possible with the C/C++ HAL. This is useful when using the same program across multiple state machines that have different wrap sources and targets. --- rp2040-hal/src/pio.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/rp2040-hal/src/pio.rs b/rp2040-hal/src/pio.rs index e9565cc..5b25b7e 100644 --- a/rp2040-hal/src/pio.rs +++ b/rp2040-hal/src/pio.rs @@ -292,7 +292,25 @@ pub struct InstalledProgram

{ } impl InstalledProgram

{ - /// Get the warp target (entry point) of the instaled program. + /// Change the source and/or target for automatic program wrapping. + /// + /// This replaces the current wrap bounds with a new set. This can be useful if you are running + /// multiple state machines with the same program but using different wrap bounds. + /// + /// # Returns + /// + /// * [`Ok`] containing a new program with the provided wrap bounds + /// * [`Err`] containing the old program if the provided wrap was invalid (outside the bounds of + /// the program length) + pub fn set_wrap(self, wrap: Wrap) -> Result { + if (self.offset + wrap.source) < self.length && (self.offset + wrap.target) < self.length { + Ok(InstalledProgram { wrap, ..self }) + } else { + Err(self) + } + } + + /// Get the wrap target (entry point) of the installed program. pub fn wrap_target(&self) -> u8 { self.offset + self.wrap.target } From 3c06a3359e2c9ab6208ed180cb1eaa01c21f1f94 Mon Sep 17 00:00:00 2001 From: David Cole Date: Thu, 27 Oct 2022 12:57:35 +1300 Subject: [PATCH 2/2] do not include offset when checking wrap bounds --- rp2040-hal/src/pio.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rp2040-hal/src/pio.rs b/rp2040-hal/src/pio.rs index 5b25b7e..486af5d 100644 --- a/rp2040-hal/src/pio.rs +++ b/rp2040-hal/src/pio.rs @@ -303,7 +303,7 @@ impl InstalledProgram

{ /// * [`Err`] containing the old program if the provided wrap was invalid (outside the bounds of /// the program length) pub fn set_wrap(self, wrap: Wrap) -> Result { - if (self.offset + wrap.source) < self.length && (self.offset + wrap.target) < self.length { + if wrap.source < self.length && wrap.target < self.length { Ok(InstalledProgram { wrap, ..self }) } else { Err(self)