diff --git a/app/commands.hh b/app/commands.hh index 7e8028e..75d77b3 100644 --- a/app/commands.hh +++ b/app/commands.hh @@ -218,7 +218,7 @@ struct LoadFile : public Commands { std::println("File is empty"); return 0; } - auto size = file_size + (file_size - file_size % flash::PageSize); + auto size = (file_size + flash::PageSize - 1) & ~(flash::PageSize - 1); std::vector buffer(size, 0xff); if (!file.read(reinterpret_cast(buffer.data()), file_size)) { std::println("Error reading the file."); diff --git a/lib/flash/flash.hh b/lib/flash/flash.hh index 51aee5e..8693599 100644 --- a/lib/flash/flash.hh +++ b/lib/flash/flash.hh @@ -264,16 +264,17 @@ class Generic { op = Opcode::PageProgram4b; } - std::array cmd = {op}; + auto total_size = 1 + ADDR_SIZE + data.size(); + std::array 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(address >> (8 * (ADDR_SIZE - 1 - i))); } - auto slice = std::span(cmd).last<256>(); + auto payload = std::span(cmd).last(); + std::ranges::copy(data, payload.begin()); - std::ranges::copy(data, slice.begin()); - TRY_OPT(spih.transfer(cmd, std::span())); + TRY_OPT(spih.transfer(std::span(cmd).subspan(0, total_size), std::span())); return true; }