Fix flash memory address being a word-address

This commit is contained in:
BlackMark 2020-04-12 13:10:00 +02:00
parent 3a04b1f488
commit a72bf67b07

View File

@ -321,13 +321,15 @@ static inline void formatLoadAddressAnswer(Message &msg)
static inline void formatReadFlashIspAnswer(Message &msg, uint32_t &addr) static inline void formatReadFlashIspAnswer(Message &msg, uint32_t &addr)
{ {
const uint16_t byteAddress = 2 * addr;
const uint16_t numBytes = static_cast<uint16_t>(msg.body[1]) << 8 | msg.body[2]; const uint16_t numBytes = static_cast<uint16_t>(msg.body[1]) << 8 | msg.body[2];
msg.size = 3 + numBytes; msg.size = 3 + numBytes;
msg.body[1] = STATUS_CMD_OK; msg.body[1] = STATUS_CMD_OK;
for (uint16_t i = 0; i < numBytes; ++i) { for (uint16_t i = 0; i < numBytes; ++i) {
msg.body[i + 2] = pgm_read_byte(static_cast<uint16_t>(addr + i)); msg.body[i + 2] = pgm_read_byte(static_cast<uint16_t>(byteAddress + i));
} }
addr += numBytes; const auto numWords = numBytes / 2;
addr += numWords;
msg.body[numBytes + 2] = STATUS_CMD_OK; msg.body[numBytes + 2] = STATUS_CMD_OK;
msg.checksum = calcChecksum(msg); msg.checksum = calcChecksum(msg);
@ -459,10 +461,12 @@ static inline void formatReadEepromIspAnswer(Message &msg, uint32_t &addr)
static inline void formatProgramFlashIspAnswer(Message &msg, uint32_t &addr) static inline void formatProgramFlashIspAnswer(Message &msg, uint32_t &addr)
{ {
if (addr < getFlashSize()) const auto byteAddress = 2 * addr;
writeFlashPage(addr, msg.body + 10); if (byteAddress < getFlashSize())
writeFlashPage(byteAddress, msg.body + 10);
const uint16_t numBytes = static_cast<uint16_t>(msg.body[1]) << 8 | msg.body[2]; const uint16_t numBytes = static_cast<uint16_t>(msg.body[1]) << 8 | msg.body[2];
addr += numBytes; const auto numWords = numBytes / 2;
addr += numWords;
msg.size = 2; msg.size = 2;
msg.body[1] = STATUS_CMD_OK; msg.body[1] = STATUS_CMD_OK;