Implement ERASE command

This commit is contained in:
Lexi / Zoe 2021-04-17 17:38:32 +02:00
parent 1f668b6032
commit d898f6e518
Signed by: binaryDiv
GPG Key ID: F8D4956E224DA232
3 changed files with 59 additions and 2 deletions

View File

@ -6,6 +6,7 @@
#include <string.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <util/delay.h>
@ -219,8 +220,30 @@ void commandWrite(char* arg) {
uartPutLine("OK END");
}
void commandErase() {
uartPutLine("ERROR not implemented");
void commandErase(char* arg) {
if (arg == NULL) {
uartPutLine("ERROR ERASE needs a start address");
return;
}
// Parse address(es)
AddressRange range = parseAddressRange(arg);
if (!range.isValid || range.to < range.from) {
uartPutLine("ERROR invalid address format");
return;
}
uint32_t bytesErased = eepromEraseBlock(range);
if (bytesErased == 0) {
uartPutLine("ERROR 0 bytes erased");
} else {
char outBuffer[16];
snprintf(outBuffer, 16, "%lu", bytesErased);
uartPutString("OK erased ");
uartPutString(outBuffer);
uartPutLine(" bytes");
}
}
// TESTREAD command: for testing purposes, reads a few bytes and returns them in a human readable format.

View File

@ -172,3 +172,34 @@ Address eepromWriteBlock(Address startAddress, DataBuffer buffer) {
return (Address) {true, currentAddress};
}
// Erase block of data on the EEPROM
uint32_t eepromEraseBlock(AddressRange addressRange) {
// Set write mode
eepromSetWriteMode();
if (!addressRange.isValid) {
return 0;
}
address_t currentAddress = addressRange.from;
address_t highestAddress = addressRange.to;
if (highestAddress > HIGHEST_VALID_ADDRESS) {
highestAddress = HIGHEST_VALID_ADDRESS;
}
uint32_t bytesErased = 0;
while (currentAddress <= highestAddress) {
eepromWriteByte(currentAddress++, 0x00);
bytesErased++;
// Handle integer overflow
if (currentAddress == 0) {
break;
}
}
return bytesErased;
}

View File

@ -46,4 +46,7 @@ Address eepromReadBlock(AddressRange addressRange, DataBuffer* buffer);
// Write multiple bytes from a buffer to EEPROM
Address eepromWriteBlock(Address startAddress, DataBuffer buffer);
// Erase block of data on the EEPROM
uint32_t eepromEraseBlock(AddressRange addressRange);
#endif /* EEPROM_H_ */