pio-rs icon indicating copy to clipboard operation
pio-rs copied to clipboard

The pio_asm macro returns ProgramWithDefines struct instead of Program struct

Open adoble opened this issue 3 years ago • 2 comments

The PIO examples in rp-rs/rp-hal/rp2040-hal tend to use the following "pattern" to install the PIO program:

// Define some simple PIO program.
let mut a = pio::Assembler::<32>::new();
// ...
let program = a.assemble_with_wrap(wrap_source, wrap_target);

// ...

let (mut pio, sm0, _, _, _) = pac.PIO0.split(&mut pac.RESETS);
let installed = pio.install(&program).unwrap(); 

Based on my understanding of the README file, I tried to repeat this pattern, but instead used the pio_asm! macro, i.e.:

let program = pio_proc::pio_asm!(
        "set pindirs, 1",
        ".wrap_target",
        "set pins, 0 [31]", //31
        "set pins, 1 [15]", //31
        ".wrap",
        options(max_program_size = 32) // Optional, defaults to 32
    );

// ...

let (mut pio, sm0, _, _, _) = pac.PIO0.split(&mut pac.RESETS);
let installed = pio.install(&program).unwrap();

However, I got the following error:

error[E0308]: mismatched types
  --> src\main.rs:77:33
   |
77 |     let installed = pio.install(&program).unwrap(); 
   |                                 ^^^^^^^^ expected struct `Program`, found struct `ProgramWithDefines`
   |
   = note: expected reference `&Program<32_usize>`
              found reference `&ProgramWithDefines<ExpandedDefines, 32_usize>`

For more information about this error, try `rustc --explain E0308`.

I managed to get it to compile (and load on the RP2040), by changing the installation line to:

let installed = pio.install(&program.program).unwrap(); 

I'm not sure if this just requires a change to the README or the code needs to be changed to return the Program struct instead of ProgramWithDefines (or maybe I've missed something).

adoble avatar May 05 '22 15:05 adoble

It is intended to return a ProgramWithDefines. The examples which use pio_asm! use program.program. I'm not sure there is really any issue here, you changed the API you were calling so you had to change your code to match it.

devsnek avatar May 05 '22 15:05 devsnek

Thanks for the quick answer.

I guess I was assuming from the README that the pio_asm! macro was a drop in substitute for the assemble* function. Maybe it just is really a case for making the docs clearer here. If you want me to do this, just let me know.

adoble avatar May 05 '22 15:05 adoble