Implement READ command (for now with ASCII output)

This commit is contained in:
Lexi / Zoe 2021-04-04 17:38:37 +02:00
parent 55b08b7630
commit 1e644b9a5e
Signed by: binaryDiv
GPG Key ID: F8D4956E224DA232
4 changed files with 80 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>
@ -57,8 +58,33 @@ void commandRead(char* arg) {
return;
}
// TODO read data and output it
uartPutString("ERR not implemented\n");
uartPutString("OK\n");
uint8_t byteBuffer[DATA_BLOCK_SIZE];
DataBuffer buffer = {
.data = byteBuffer,
.maxSize = DATA_BLOCK_SIZE,
.bytes = 0
};
do {
// Read a single block with up to DATA_BLOCK_SIZE bytes
range.from = eepromReadBlock(range, &buffer);
// TODO binary output
// ASCII output
char outBuffer[20];
sprintf(outBuffer, "<%d>", buffer.bytes);
uartPutString(outBuffer);
for (int i = 0; i < buffer.bytes; i++) {
sprintf(outBuffer, " %02X", buffer.data[i]);
uartPutString(outBuffer);
}
uartPutChar('\n');
} while (buffer.bytes > 0);
}
void commandWrite(char* arg) {

View File

@ -11,6 +11,9 @@
// Define type for EEPROM addresses
typedef uint16_t address_t;
// Define highest valid address for the EEPROM
#define HIGHEST_VALID_ADDRESS 0x7fff
// Type definition for address ranges (from-to)
typedef struct {
bool isValid;
@ -18,4 +21,13 @@ typedef struct {
address_t to;
} AddressRange;
// Define type for a block of data (bytes)
typedef struct {
uint8_t* data;
uint8_t maxSize;
uint8_t bytes;
} DataBuffer;
#define DATA_BLOCK_SIZE 64
#endif /* COMMON_H_ */

View File

@ -113,3 +113,37 @@ void eepromWriteByte(address_t address, uint8_t data) {
// Write pulse width high (50ns)
_NOP();
}
// Read multiple bytes from EEPROM into a buffer
address_t eepromReadBlock(AddressRange addressRange, DataBuffer* buffer) {
// Set read mode
eepromSetReadMode();
buffer->bytes = 0;
address_t currentAddress = addressRange.from;
address_t highestAddress = addressRange.to;
if (highestAddress > HIGHEST_VALID_ADDRESS) {
highestAddress = HIGHEST_VALID_ADDRESS;
}
while (currentAddress <= highestAddress && buffer->bytes < buffer->maxSize) {
buffer->data[buffer->bytes++] = eepromReadByte(currentAddress++);
}
return currentAddress;
}
// // Write multiple bytes from a buffer to EEPROM
// address_t eepromWriteBlock(address_t currentAddress, DataBuffer buffer) {
// // Set write mode
// eepromSetWriteMode();
// for (uint8_t i = 0; currentAddress <= HIGHEST_VALID_ADDRESS && i < buffer.bytes; i++) {
// eepromWriteByte(currentAddress++, buffer.data[i]);
// // TODO address overflow
// }
// return currentAddress;
// }

View File

@ -40,4 +40,10 @@ uint8_t eepromReadByte(address_t address);
// Write byte (assumes we're in write mode)
void eepromWriteByte(address_t address, uint8_t data);
// Read multiple bytes from EEPROM into a buffer
address_t eepromReadBlock(AddressRange addressRange, DataBuffer* buffer);
// // Write multiple bytes from a buffer to EEPROM
// address_t eepromWriteBlock(address_t addressRange, DataBuffer buffer);
#endif /* EEPROM_H_ */