Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call include_file! on loaded pio file #54

Merged
merged 3 commits into from
Feb 9, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 39 additions & 11 deletions pio-proc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,13 @@ struct PioFileMacroArgs {
max_program_size: Expr,
program: String,
program_name: Option<(String, LitStr)>,
file_path: PathBuf,
}

impl syn::parse::Parse for PioFileMacroArgs {
fn parse(stream: syn::parse::ParseStream) -> syn::parse::Result<Self> {
let mut program = String::new();
let mut file_path = PathBuf::new();

// Parse the list of instructions
if let Ok(s) = stream.parse::<LitStr>() {
Expand All @@ -152,6 +154,8 @@ impl syn::parse::Parse for PioFileMacroArgs {
abort!(s, "the file '{}' does not exist", pathbuf.display());
}

file_path = pathbuf.to_owned();

match fs::read(pathbuf) {
Ok(content) => match std::str::from_utf8(&content) {
Ok(prog) => program = prog.to_string(),
Expand Down Expand Up @@ -203,6 +207,7 @@ impl syn::parse::Parse for PioFileMacroArgs {
program_name: select_program.map(|v| (v.name, v.ident)),
max_program_size,
program,
file_path,
})
}
}
Expand Down Expand Up @@ -279,7 +284,17 @@ pub fn pio_file(item: TokenStream) -> TokenStream {
Err(e) => return parse_error(e, &args.program).into(),
};

to_codegen(program, args.max_program_size).into()
to_codegen(
program,
args.max_program_size,
Some(
args.file_path
.into_os_string()
.into_string()
.expect("file path must be valid UTF-8"),
),
)
.into()
}

/// A macro which invokes the PIO assembler at compile time.
Expand All @@ -295,12 +310,13 @@ pub fn pio_asm(item: TokenStream) -> TokenStream {
Err(e) => return parse_error(e, &args.program).into(),
};

to_codegen(program, args.max_program_size).into()
to_codegen(program, args.max_program_size, None).into()
}

fn to_codegen(
program: &pio::ProgramWithDefines<HashMap<String, i32>, { MAX_PROGRAM_SIZE }>,
max_program_size: Expr,
file: Option<String>,
) -> proc_macro2::TokenStream {
let pio::ProgramWithDefines {
program,
Expand Down Expand Up @@ -381,18 +397,30 @@ fn to_codegen(
.parse()
.unwrap();
let program_size = max_program_size;

// This makes sure the file is added to the list
// of tracked files, so a change of that file triggers
// a recompile. Should be replaced by
// `proc_macro::tracked_path::path` when it is stable.
let dummy_include = match file {
Some(file_path) => quote! {let _ = include_bytes!( #file_path );},
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could also be:

Suggested change
Some(file_path) => quote! {let _ = include_bytes!( #file_path );},
Some(file_path) => quote! {const _:&[u8] = include_bytes!( #file_path );},

Not sure which one is better.

None => quote!(),
};
quote! {
{
#defines_struct
::pio::ProgramWithDefines {
program: ::pio::Program::<{ #program_size }> {
code: #code,
origin: #origin,
wrap: #wrap,
side_set: #side_set,
version: #version,
},
public_defines: #defines_init,
{
#dummy_include;
::pio::ProgramWithDefines {
program: ::pio::Program::<{ #program_size }> {
code: #code,
origin: #origin,
wrap: #wrap,
side_set: #side_set,
version: #version,
},
public_defines: #defines_init,
}
}
}
}
Expand Down
Loading