Implement WRITE; refactor address parsing

This commit is contained in:
Lexi / Zoe 2021-04-17 02:35:44 +02:00
parent 07e5b74e83
commit 4dfb0d7edf
Signed by: binaryDiv
GPG key ID: F8D4956E224DA232
6 changed files with 154 additions and 51 deletions

View file

@ -30,11 +30,11 @@ void executeCommand(CommandLine cmdLine) {
}
else if (strcmp(cmdLine.command, "WRITE") == 0) {
// WRITE command: Takes a hex address as argument, reads data from UART and writes it to the EEPROM.
commandRead(cmdLine.arg);
commandWrite(cmdLine.arg);
}
else if (strcmp(cmdLine.command, "ERASE") == 0) {
// ERASE command: Takes a hex address range as argument and writes 0x00 bytes to the specified range.
commandRead(cmdLine.arg);
commandErase(cmdLine.arg);
}
else if (strcmp(cmdLine.command, "TESTREAD") == 0) {
// TESTREAD command: for testing purposes, reads a few bytes and returns them in a human readable format.
@ -111,7 +111,9 @@ void commandRead(char* arg) {
do {
// Read a single block with up to DATA_BLOCK_SIZE bytes
range.from = eepromReadBlock(range, &buffer);
Address nextAddress = eepromReadBlock(range, &buffer);
range.isValid = nextAddress.isValid;
range.from = nextAddress.value;
if (binary_mode) {
// Send block as binary "package":
@ -144,6 +146,8 @@ void commandWrite(char* arg) {
}
// Parse address
Address startAddress = parseSingleAddress(arg);
if (!startAddress.isValid) {
uartPutLine("ERROR invalid address format");
return;
}
@ -154,8 +158,65 @@ void commandWrite(char* arg) {
return;
}
// TODO read data from input and write to EEPROM
uartPutLine("ERROR not implemented");
// Create data buffer
uint8_t byteBuffer[DATA_BLOCK_SIZE];
DataBuffer buffer = {
.data = byteBuffer,
.maxSize = DATA_BLOCK_SIZE,
.bytes = 0
};
// "OK" indicates that data can be sent now.
uartPutLine("OK START");
Address currentAddress = startAddress;
uint8_t block_length = 0;
do {
// Reset buffer
buffer.bytes = 0;
// Read and check block length
block_length = uartGetChar();
if (block_length > buffer.maxSize) {
uartPutString("ERROR maximal block size is: ");
uartPutInteger(buffer.maxSize);
uartPutLine(NULL);
return;
}
// Read data bytes
while (buffer.bytes < block_length) {
buffer.data[buffer.bytes++] = uartGetChar();
}
// Write blocks to EEPROM until an "end block" (0 bytes) is received
if (buffer.bytes > 0) {
currentAddress = eepromWriteBlock(currentAddress, buffer);
// TODO wait necessary?
//_delay_ms(100);
if (!currentAddress.isValid) {
uartPutLine("ERROR reached end of EEPROM while writing block");
return;
}
else if (currentAddress.value == 0 || currentAddress.value >= HIGHEST_VALID_ADDRESS) {
// Block was written completely, but reached end of EEPROM now. No further data allowed (except for 0 byte "end block").
currentAddress.isValid = false;
uartPutLine("OK STOP (end of EEPROM)");
}
else {
// Next block of data can be sent now.
uartPutLine("OK CONTINUE");
}
}
} while (block_length > 0);
// WRITE completed
uartPutLine("OK END");
}
void commandErase() {