Implement eeprom programming

This commit is contained in:
BlackMark 2020-04-11 19:19:23 +02:00
parent 9c24c32f3d
commit 03fd10860f

View File

@ -134,6 +134,16 @@ static inline bool isReadEepromIsp(const Message &msg)
return false;
}
static inline bool isProgramEepromIsp(const Message &msg)
{
if (msg.body[0] == CMD_PROGRAM_EEPROM_ISP) {
if (msg.size == (static_cast<uint16_t>(msg.body[1]) << 8 | msg.body[2]) + 10)
return true;
}
return false;
}
static inline bool isLeaveProgmodeIsp(const Message &msg)
{
if (msg.size == 3 && msg.body[0] == CMD_LEAVE_PROGMODE_ISP)
@ -292,6 +302,15 @@ uint8_t readEepromByte(const uint8_t *addr)
return EEDR;
}
void writeEepromByte(uint8_t *addr, uint8_t value)
{
EECR = 0;
EEAR = reinterpret_cast<uint16_t>(addr);
EEDR = value;
EECR |= (1 << EEMPE);
EECR |= (1 << EEPE);
}
} // namespace
static inline void formatReadEepromIspAnswer(Message &msg, uint32_t &addr)
@ -299,7 +318,6 @@ static inline void formatReadEepromIspAnswer(Message &msg, uint32_t &addr)
const uint16_t numBytes = static_cast<uint16_t>(msg.body[1]) << 8 | msg.body[2];
msg.size = 3 + numBytes;
msg.body[1] = STATUS_CMD_OK;
waitEepromReady();
for (uint16_t i = 0; i < numBytes; ++i) {
msg.body[i + 2] = readEepromByte(reinterpret_cast<const uint8_t *>(addr + i));
}
@ -309,6 +327,20 @@ static inline void formatReadEepromIspAnswer(Message &msg, uint32_t &addr)
msg.checksum = calcChecksum(msg);
}
static inline void formatProgramEepromIspAnswer(Message &msg, uint32_t &addr)
{
const uint16_t numBytes = static_cast<uint16_t>(msg.body[1]) << 8 | msg.body[2];
for (uint16_t i = 0; i < numBytes; ++i) {
writeEepromByte(reinterpret_cast<uint8_t *>(addr + i), msg.body[10 + i]);
waitEepromReady();
}
addr += numBytes;
msg.size = 2;
msg.body[1] = STATUS_CMD_OK;
msg.checksum = calcChecksum(msg);
}
static inline void formatLeaveProgmodeIspAnswer(Message &msg)
{
msg.size = 2;
@ -354,6 +386,8 @@ static inline void handleMessage(Message &msg)
formatReadFlashIspAnswer(msg, s_address);
else if (isReadEepromIsp(msg))
formatReadEepromIspAnswer(msg, s_address);
else if (isProgramEepromIsp(msg))
formatProgramEepromIspAnswer(msg, s_address);
else if (isLeaveProgmodeIsp(msg))
formatLeaveProgmodeIspAnswer(msg);
else