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
20 changes: 16 additions & 4 deletions app/commands.hh
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,15 @@ struct LoadFile : public Commands<T> {
return 0;
}

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;
}
if (quad && !this->flash.enable_quad(true)) {
std::println("enable quad failed");
return 0;
Expand All @@ -230,13 +236,19 @@ struct LoadFile : public Commands<T> {
auto progress_bar = ProgressBar(buffer.size(), 50, "Loading").with_throughput();
size_t addr = start_addr;
while (data.size() > 0) {
if ((addr % flash::SectorSize) == 0 && !this->flash.erase(addr)) {
std::println("Failed to erase block {:#x}", addr);
return 0;
if ((addr % flash::SectorSize) == 0) {
auto erased = addr4b ? this->flash.template erase<4, flash::Opcode::SectorErase4b>(addr)
: this->flash.erase(addr);
if (!erased) {
std::println("Failed to erase block {:#x}", addr);
return 0;
}
}

std::optional<bool> res;
if (quad) {
if (addr4b) {
res = this->flash.template single_page_program<4>(addr, data.first<flash::PageSize>());
} else if (quad) {
res = this->flash.quad_page_program(addr, data.first<flash::PageSize>());
} else {
res = this->flash.single_page_program(addr, data.first<flash::PageSize>());
Expand Down
3 changes: 2 additions & 1 deletion app/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ int main(int argc, char* argv[]) {
return 0;
};

auto test_page_cmd = new_flash_command("test-page", "Write a pattern to a page and read it back.");
auto test_page_cmd =
new_flash_command("test-page", "Write a pattern to a page and read it back.");
test_page_cmd->add_argument("--addr")
.help("The address to be loaded")
.default_value(std::size_t{0})
Expand Down
51 changes: 38 additions & 13 deletions lib/flash/flash.hh
Original file line number Diff line number Diff line change
Expand Up @@ -199,21 +199,37 @@ class Generic {
return true;
}

Option<bool> erase(uint32_t address, Opcode op = Opcode::SectorErase) {
template <size_t ADDR_SIZE = 3, Opcode OP = Opcode::SectorErase>
Option<bool> erase(uint32_t address) {
static_assert(ADDR_SIZE == 3 || ADDR_SIZE == 4, "Only 3 or 4 byte addresses supported");
static_assert(
(ADDR_SIZE == 3 && (OP == Opcode::SectorErase || OP == Opcode::BlockErase32k ||
OP == Opcode::BlockErase64k)) ||
(ADDR_SIZE == 4 && (OP == Opcode::SectorErase4b || OP == Opcode::BlockErase32k4b ||
OP == Opcode::BlockErase64k4b)),
"Opcode should match address size");

write_enable();
std::array<uint8_t, 4> cmd = {
op,
static_cast<uint8_t>(address >> 16),
static_cast<uint8_t>(address >> 8),
static_cast<uint8_t>(address >> 0),
};

std::array<uint8_t, 1 + ADDR_SIZE> 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)
cmd[1 + i] = static_cast<uint8_t>(address >> (8 * (ADDR_SIZE - 1 - i)));
}

auto ret = spih.transfer(cmd, std::span<uint8_t>());
if (embeddedpp::is_error(ret)) {
return std::nullopt;
}
return wait_not_busy();
}

Option<bool> enter_4b_addr() {
std::array<uint8_t, 1> cmd = {Opcode::Enter4bAddr};
TRY_OPT(spih.transfer(cmd, std::span<uint8_t>()));
return true;
}

Option<bool> reset() {
std::array<uint8_t, 1> cmd = {Opcode::Reset};
TRY_OPT(spih.transfer(cmd, std::span<uint8_t>()));
Expand All @@ -233,14 +249,23 @@ class Generic {
return ret[0];
}

template <size_t ADDR_SIZE = 3>
Option<bool> single_page_program(uint32_t address, std::span<uint8_t, 256> data) {
static_assert(ADDR_SIZE == 3 || ADDR_SIZE == 4, "Only 3 or 4 byte addresses supported");

Opcode op;
if constexpr (ADDR_SIZE == 3) {
op = Opcode::PageProgram;
} else {
op = Opcode::PageProgram4b;
}
write_enable();
std::array<uint8_t, 256 + 4> cmd = {
Opcode::PageProgram,
static_cast<uint8_t>(address >> 16),
static_cast<uint8_t>(address >> 8),
static_cast<uint8_t>(address >> 0),
};

std::array<uint8_t, 256 + 1 + ADDR_SIZE> 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)
cmd[1 + i] = static_cast<uint8_t>(address >> (8 * (ADDR_SIZE - 1 - i)));
}

auto slice = std::span<uint8_t>(cmd).last<256>();

Expand Down
3 changes: 1 addition & 2 deletions lib/ftdi/spi_host.hh
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ class SpiHost {
public:
explicit SpiHost(FT_HANDLE handle, bool mpsse = false) noexcept
: handle(handle), mpsse(mpsse), traces(false) {}
~SpiHost() {
}
~SpiHost() {}

void close() {
if (mpsse) {
Expand Down