From 7cca9a71d712b50f40e89fd1a47fa18b9354b3b9 Mon Sep 17 00:00:00 2001 From: Jan Niehusmann Date: Thu, 15 Sep 2022 20:41:25 +0000 Subject: [PATCH] simplify jump offset calculation when loading PIO programs --- rp2040-hal/src/pio.rs | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/rp2040-hal/src/pio.rs b/rp2040-hal/src/pio.rs index 698d4be..61d5521 100644 --- a/rp2040-hal/src/pio.rs +++ b/rp2040-hal/src/pio.rs @@ -195,26 +195,17 @@ impl PIO

{ .iter() .cloned() .map(|instr| { - if let Some(Instruction { - operands: InstructionOperands::JMP { condition, address }, - delay, - side_set, - }) = Instruction::decode(instr, p.side_set) - { - // JMP instruction. We need to apply offset here - let address = address + offset as u8; + if instr & 0b1110_0000_0000_0000 == 0 { + // this is a JMP instruction -> add offset to address + let address = (instr & 0b11111) as usize; + let address = address + offset; assert!( - address < pio::RP2040_MAX_PROGRAM_SIZE as u8, + address < pio::RP2040_MAX_PROGRAM_SIZE, "Invalid JMP out of the program after offset addition" ); - - Instruction { - operands: InstructionOperands::JMP { condition, address }, - delay, - side_set, - } - .encode(p.side_set) + instr & (!0b11111) | address as u16 } else { + // this is not a JMP instruction -> keep it unchanged instr } })