Skip to content
Merged
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
42 changes: 14 additions & 28 deletions app/commands.hh
Original file line number Diff line number Diff line change
Expand Up @@ -188,16 +188,10 @@ struct LoadFile : public Commands<T> {
std::size_t start_addr;
std::string& filename;
bool quad;
bool bootstrap;
bool skip_erase;
LoadFile(flash::Generic<T> f, std::string& filename, std::size_t addr = 0, bool bootstrap = false,
LoadFile(flash::Generic<T> f, std::string& filename, std::size_t addr = 0,
bool skip_erase = false, bool quad = false)
: Commands<T>(f),
filename(filename),
start_addr(addr),
quad(quad),
bootstrap(bootstrap),
skip_erase(skip_erase) {}
: Commands<T>(f), filename(filename), start_addr(addr), quad(quad), skip_erase(skip_erase) {}

int run() override {
if ((this->start_addr % flash::SectorSize) != 0) {
Expand Down Expand Up @@ -227,9 +221,6 @@ struct LoadFile : public Commands<T> {

bool addr4b = this->start_addr > 0xFFFFFF;

if (!bootstrap) {
this->flash.reset();
}
if (addr4b && !this->flash.enter_4b_addr()) {
std::println("Enter 4-byte address mode failed");
return 0;
Expand Down Expand Up @@ -276,9 +267,6 @@ struct LoadFile : public Commands<T> {
this->flash.wait_not_busy();
this->flash.write_enable(false);

if (bootstrap) {
this->flash.reset();
}
return 1;
}
};
Expand All @@ -287,15 +275,10 @@ template <typename T>
struct LoadFileElf : public Commands<T> {
std::string& filename;
bool quad;
bool bootstrap;
bool skip_erase;
LoadFileElf(flash::Generic<T> f, std::string& filename, bool bootstrap = false,
bool skip_erase = false, bool quad = false)
: Commands<T>(f),
filename(filename),
quad(quad),
bootstrap(bootstrap),
skip_erase(skip_erase) {}
LoadFileElf(flash::Generic<T> f, std::string& filename, bool skip_erase = false,
bool quad = false)
: Commands<T>(f), filename(filename), quad(quad), skip_erase(skip_erase) {}

int run() override {
ELFIO::elfio reader;
Expand Down Expand Up @@ -333,9 +316,6 @@ struct LoadFileElf : public Commands<T> {
}
}

if (!bootstrap) {
this->flash.reset();
}
if (addr4b && !this->flash.enter_4b_addr()) {
std::println("Enter 4-byte address mode failed");
return 0;
Expand Down Expand Up @@ -445,10 +425,16 @@ struct LoadFileElf : public Commands<T> {
this->flash.wait_not_busy();
this->flash.write_enable(false);

if (bootstrap) {
this->flash.reset();
}
return 1;
}
};

template <typename T>
struct ResetFlash : public Commands<T> {
ResetFlash(flash::Generic<T> f) : Commands<T>(f) {}

int run() override {
this->flash.reset();
return 1;
}
};
Expand Down
88 changes: 35 additions & 53 deletions app/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -170,27 +170,6 @@ int main(int argc, char* argv[]) {
return 0;
};

auto load_file_cmd =
new_flash_command("load-file", "Write the content of a file to the address and verify it.");
load_file_cmd->add_argument("filename").help("The file path.");
load_file_cmd->add_argument("--addr")
.help("The address to be loaded")
.default_value(std::size_t{0})
.scan<'x', std::size_t>();
load_file_cmd->add_argument("--quad").help("Use qSPI").default_value(false).implicit_value(true);
program.add_subparser(*load_file_cmd);
commands["load-file"] = [&]() -> int {
auto pid = program.get<uint16_t>("--pid");
auto filename = load_file_cmd->get<std::string>("filename");
auto addr = load_file_cmd->get<std::size_t>("--addr");
auto quad = load_file_cmd->get<bool>("--quad");
auto spih = handle_flash_command(load_file_cmd, pid);
commands::LoadFile(flash::Generic(*spih), filename, addr, false, quad).run();

spih->close();
return 0;
};

auto verify_file_cmd = new_flash_command(
"verify-file", "Compare the hash of a file to the hash of the flash content at the address.");
verify_file_cmd->add_argument("filename").help("The file path.");
Expand All @@ -215,51 +194,44 @@ int main(int argc, char* argv[]) {
return 0;
};

auto bootstrap_cmd = new_flash_command(
"bootstrap", "Write the content of a binary file to an address and reset the target.");
bootstrap_cmd->add_argument("filename").help("The file path.");
bootstrap_cmd->add_argument("--addr")
.help("The address to be loaded")
.default_value(std::size_t{0})
.scan<'x', std::size_t>();
bootstrap_cmd->add_argument("--quad").help("Use qSPI").default_value(false).implicit_value(true);
bootstrap_cmd->add_argument("--skip-erase")
auto load_file_cmd = new_flash_command(
"load-file", "Write the content of a binary file to a destination address.");
load_file_cmd->add_argument("filename").help("The file path.");
load_file_cmd->add_argument("--addr").help("The address to be loaded").scan<'x', std::size_t>();
load_file_cmd->add_argument("--quad").help("Use qSPI").default_value(false).implicit_value(true);
load_file_cmd->add_argument("--skip-erase")
.help("Don't issue erase commands")
.default_value(false)
.implicit_value(true);
program.add_subparser(*bootstrap_cmd);
commands["bootstrap"] = [&]() -> int {
program.add_subparser(*load_file_cmd);
commands["load-file"] = [&]() -> int {
auto pid = program.get<uint16_t>("--pid");
auto filename = bootstrap_cmd->get<std::string>("filename");
auto addr = bootstrap_cmd->get<std::size_t>("--addr");
auto quad = bootstrap_cmd->get<bool>("--quad");
auto skip_erase = bootstrap_cmd->get<bool>("--skip-erase");
auto spih = handle_flash_command(bootstrap_cmd, pid);
commands::LoadFile(flash::Generic(*spih), filename, addr, true, skip_erase, quad).run();
auto filename = load_file_cmd->get<std::string>("filename");
auto addr = load_file_cmd->get<std::size_t>("--addr");
auto quad = load_file_cmd->get<bool>("--quad");
auto skip_erase = load_file_cmd->get<bool>("--skip-erase");
auto spih = handle_flash_command(load_file_cmd, pid);
commands::LoadFile(flash::Generic(*spih), filename, addr, skip_erase, quad).run();

spih->close();
return 0;
};

auto bootstrap_elf_cmd = new_flash_command(
"bootstrap-elf", "Write the loadable contents of an ELF file and reset the target.");
bootstrap_elf_cmd->add_argument("filename").help("The file path.");
bootstrap_elf_cmd->add_argument("--quad")
.help("Use qSPI")
.default_value(false)
.implicit_value(true);
bootstrap_elf_cmd->add_argument("--skip-erase")
auto load_elf_cmd = new_flash_command("load-elf", "Write the loadable contents of an ELF file.");
load_elf_cmd->add_argument("filename").help("The file path.");
load_elf_cmd->add_argument("--quad").help("Use qSPI").default_value(false).implicit_value(true);
load_elf_cmd->add_argument("--skip-erase")
.help("Don't issue erase commands")
.default_value(false)
.implicit_value(true);
program.add_subparser(*bootstrap_elf_cmd);
commands["bootstrap-elf"] = [&]() -> int {
program.add_subparser(*load_elf_cmd);
commands["load-elf"] = [&]() -> int {
auto pid = program.get<uint16_t>("--pid");
auto filename = bootstrap_elf_cmd->get<std::string>("filename");
auto quad = bootstrap_elf_cmd->get<bool>("--quad");
auto skip_erase = bootstrap_elf_cmd->get<bool>("--skip-erase");
auto spih = handle_flash_command(bootstrap_elf_cmd, pid);
commands::LoadFileElf(flash::Generic(*spih), filename, true, skip_erase, quad).run();
auto filename = load_elf_cmd->get<std::string>("filename");
auto quad = load_elf_cmd->get<bool>("--quad");
auto skip_erase = load_elf_cmd->get<bool>("--skip-erase");
auto spih = handle_flash_command(load_elf_cmd, pid);
commands::LoadFileElf(flash::Generic(*spih), filename, skip_erase, quad).run();

spih->close();
return 0;
Expand All @@ -279,6 +251,16 @@ int main(int argc, char* argv[]) {
return 0;
};

auto flash_reset_cmd = new_flash_command("flash-reset", "Reset flash.");
program.add_subparser(*flash_reset_cmd);
commands["flash-reset"] = [&]() -> int {
auto pid = program.get<uint16_t>("--pid");
auto spih = handle_flash_command(flash_reset_cmd, pid);
commands::ResetFlash(flash::Generic(*spih)).run();
spih->close();
return 0;
};

if (argc == 1) {
std::cout << program; // This prints the auto-generated help
return 0;
Expand Down
2 changes: 1 addition & 1 deletion lib/flash/flash.hh
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ class Generic {
op = Opcode::PageProgram4b;
}

auto total_size = 1 + ADDR_SIZE + data.size();
auto total_size = 1 + ADDR_SIZE + data.size();
std::array<uint8_t, 1 + ADDR_SIZE + flash::PageSize> cmd = {op};
for (size_t i = 0; i < ADDR_SIZE; ++i) {
// This calculates the correct shift (24, 16, 8, 0 for 4-byte; 16, 8, 0 for 3-byte)
Expand Down
2 changes: 1 addition & 1 deletion nix/ftditool.nix
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
}:
stdenv.mkDerivation rec {
pname = "ftditool";
version = "0.4.0";
version = "0.5.0";
src = ../.;

nativeBuildInputs = [
Expand Down