Skip to content

Add support for programming AVR EEPROM memory if .eeprom section foun… #403

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions ravedude/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ravedude/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ serialport = "4.0.0"
anyhow = "1.0.38"
git-version = "0.3.4"
ctrlc = "3.2.1"
elf = "0.7.2"

[dependencies.structopt]
version = "0.3.21"
Expand Down
21 changes: 21 additions & 0 deletions ravedude/src/avrdude/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ impl Avrdude {

pub fn run(
options: &AvrdudeOptions,
do_eeprom: bool,
port: Option<impl AsRef<path::Path>>,
bin: &path::Path,
) -> anyhow::Result<Self> {
Expand Down Expand Up @@ -95,6 +96,26 @@ impl Avrdude {

command = command.arg("-D").arg("-U").arg(flash_instruction);

// Check whether we should program the EEPROM. The command can be
// appended to the command-line, but avrdude will return with a
// nonzero error code if the ELF file didn't have an `.eeprom` section.
// So we conditionally add it.
if do_eeprom {
let fp = std::fs::File::open(bin).context("could not read ELF file")?;
let mut elf = elf::ElfStream::<elf::endian::LittleEndian, _>::open_stream(fp).context("parsing ELF file failed")?;

if elf.section_header_by_name(".eeprom")
.context("error parsing ELF section headers")?
.is_some()
{
let mut eeprom_instruction: std::ffi::OsString = "eeprom:w:".into();
eeprom_instruction.push(bin);
eeprom_instruction.push(":e");

command = command.arg("-U").arg(eeprom_instruction);
}
}

let process = command.spawn().context("failed starting avrdude")?;

Ok(Self { config, process })
Expand Down
7 changes: 6 additions & 1 deletion ravedude/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ struct Args {
#[structopt(short = "c", long = "open-console")]
open_console: bool,

/// Skip flashing .eeprom section to AVR EEPROM. Default is to auto-detect
/// an .eeprom section.
#[structopt(short = "s", long = "skip-eeprom")]
skip_eeprom: bool,

/// Baudrate which should be used for the serial console.
#[structopt(short = "b", long = "baudrate")]
baudrate: Option<u32>,
Expand Down Expand Up @@ -135,7 +140,7 @@ fn ravedude() -> anyhow::Result<()> {
task_message!("Programming", "{}", bin.display(),);
}

let mut avrdude = avrdude::Avrdude::run(&board.avrdude_options(), port.as_ref(), bin)?;
let mut avrdude = avrdude::Avrdude::run(&board.avrdude_options(), !args.skip_eeprom, port.as_ref(), bin)?;
avrdude.wait()?;

task_message!("Programmed", "{}", bin.display());
Expand Down