Compare commits
3 Commits
1f668b6032
...
213a143089
| Author | SHA1 | Date |
|---|---|---|
|
|
213a143089 | |
|
|
b5c5fd4ede | |
|
|
d898f6e518 |
|
|
@ -6,6 +6,7 @@
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <util/delay.h>
|
#include <util/delay.h>
|
||||||
|
|
||||||
|
|
@ -36,14 +37,6 @@ void executeCommand(CommandLine cmdLine) {
|
||||||
// ERASE command: Takes a hex address range as argument and writes 0x00 bytes to the specified range.
|
// ERASE command: Takes a hex address range as argument and writes 0x00 bytes to the specified range.
|
||||||
commandErase(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.
|
|
||||||
commandTestRead();
|
|
||||||
}
|
|
||||||
else if (strcmp(cmdLine.command, "TESTWRITE") == 0) {
|
|
||||||
// TESTWRITE command: for testing purposes, writes a few bytes
|
|
||||||
commandTestWrite(cmdLine.arg);
|
|
||||||
}
|
|
||||||
else {
|
else {
|
||||||
// unknown command: return error message
|
// unknown command: return error message
|
||||||
uartPutLine("ERROR invalid command");
|
uartPutLine("ERROR invalid command");
|
||||||
|
|
@ -79,11 +72,6 @@ void commandHelp() {
|
||||||
uartPutLine("HELP - READ 0000:0FFF");
|
uartPutLine("HELP - READ 0000:0FFF");
|
||||||
uartPutLine("HELP - WRITE 0000");
|
uartPutLine("HELP - WRITE 0000");
|
||||||
uartPutLine("HELP - ERASE 0000:0FFF");
|
uartPutLine("HELP - ERASE 0000:0FFF");
|
||||||
|
|
||||||
// TODO remove those
|
|
||||||
uartPutLine("HELP - TESTREAD (only for testing)");
|
|
||||||
uartPutLine("HELP - TESTWRITE (only for testing)");
|
|
||||||
|
|
||||||
uartPutLine("OK");
|
uartPutLine("OK");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -102,14 +90,12 @@ void commandRead(char* arg) {
|
||||||
|
|
||||||
uartPutLine("OK");
|
uartPutLine("OK");
|
||||||
|
|
||||||
uint8_t byteBuffer[DATA_BLOCK_SIZE];
|
// Create data buffer
|
||||||
DataBuffer buffer = {
|
DataBuffer buffer;
|
||||||
.data = byteBuffer,
|
|
||||||
.maxSize = DATA_BLOCK_SIZE,
|
|
||||||
.bytes = 0
|
|
||||||
};
|
|
||||||
|
|
||||||
do {
|
do {
|
||||||
|
buffer.bytes = 0;
|
||||||
|
|
||||||
// Read a single block with up to DATA_BLOCK_SIZE bytes
|
// Read a single block with up to DATA_BLOCK_SIZE bytes
|
||||||
Address nextAddress = eepromReadBlock(range, &buffer);
|
Address nextAddress = eepromReadBlock(range, &buffer);
|
||||||
range.isValid = nextAddress.isValid;
|
range.isValid = nextAddress.isValid;
|
||||||
|
|
@ -120,16 +106,16 @@ void commandRead(char* arg) {
|
||||||
// First the size of the package (1 byte) followed by the data bytes
|
// First the size of the package (1 byte) followed by the data bytes
|
||||||
uartPutChar(buffer.bytes);
|
uartPutChar(buffer.bytes);
|
||||||
|
|
||||||
for (int i = 0; i < buffer.bytes; i++) {
|
for (uint8_t i = 0; i < buffer.bytes; i++) {
|
||||||
uartPutChar(buffer.data[i]);
|
uartPutChar(buffer.data[i]);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Fancy ASCII output
|
// Fancy ASCII output
|
||||||
uartPutChar('<');
|
uartPutString("<0x");
|
||||||
uartPutInteger(buffer.bytes);
|
uartPutHexByte(buffer.bytes);
|
||||||
uartPutChar('>');
|
uartPutChar('>');
|
||||||
|
|
||||||
for (int i = 0; i < buffer.bytes; i++) {
|
for (uint8_t i = 0; i < buffer.bytes; i++) {
|
||||||
uartPutChar(' ');
|
uartPutChar(' ');
|
||||||
uartPutHexByte(buffer.data[i]);
|
uartPutHexByte(buffer.data[i]);
|
||||||
}
|
}
|
||||||
|
|
@ -158,17 +144,12 @@ void commandWrite(char* arg) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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.
|
// "OK" indicates that data can be sent now.
|
||||||
uartPutLine("OK START");
|
uartPutLine("OK START");
|
||||||
|
|
||||||
|
// Create data buffer
|
||||||
|
DataBuffer buffer;
|
||||||
|
|
||||||
Address currentAddress = startAddress;
|
Address currentAddress = startAddress;
|
||||||
uint8_t block_length = 0;
|
uint8_t block_length = 0;
|
||||||
|
|
||||||
|
|
@ -179,10 +160,8 @@ void commandWrite(char* arg) {
|
||||||
// Read and check block length
|
// Read and check block length
|
||||||
block_length = uartGetChar();
|
block_length = uartGetChar();
|
||||||
|
|
||||||
if (block_length > buffer.maxSize) {
|
if (block_length > DATA_BLOCK_SIZE) {
|
||||||
uartPutString("ERROR maximal block size is: ");
|
uartPutLine("ERROR maximal block size is: " STR(DATA_BLOCK_SIZE));
|
||||||
uartPutInteger(buffer.maxSize);
|
|
||||||
uartPutLine(NULL);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -195,9 +174,6 @@ void commandWrite(char* arg) {
|
||||||
if (buffer.bytes > 0) {
|
if (buffer.bytes > 0) {
|
||||||
currentAddress = eepromWriteBlock(currentAddress, buffer);
|
currentAddress = eepromWriteBlock(currentAddress, buffer);
|
||||||
|
|
||||||
// TODO wait necessary?
|
|
||||||
//_delay_ms(100);
|
|
||||||
|
|
||||||
if (!currentAddress.isValid) {
|
if (!currentAddress.isValid) {
|
||||||
uartPutLine("ERROR reached end of EEPROM while writing block");
|
uartPutLine("ERROR reached end of EEPROM while writing block");
|
||||||
return;
|
return;
|
||||||
|
|
@ -219,54 +195,28 @@ void commandWrite(char* arg) {
|
||||||
uartPutLine("OK END");
|
uartPutLine("OK END");
|
||||||
}
|
}
|
||||||
|
|
||||||
void commandErase() {
|
void commandErase(char* arg) {
|
||||||
uartPutLine("ERROR not implemented");
|
if (arg == NULL) {
|
||||||
|
uartPutLine("ERROR ERASE needs a start address");
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TESTREAD command: for testing purposes, reads a few bytes and returns them in a human readable format.
|
// Parse address(es)
|
||||||
void commandTestRead() {
|
AddressRange range = parseAddressRange(arg);
|
||||||
eepromSetReadMode();
|
if (!range.isValid || range.to < range.from) {
|
||||||
|
uartPutLine("ERROR invalid address format");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
for (uint8_t i = 0x00; i < 0x20; i++) {
|
uint32_t bytesErased = eepromEraseBlock(range);
|
||||||
uartPutString("TESTREAD 0x");
|
|
||||||
uartPutHexByte(i);
|
|
||||||
uartPutString(": ");
|
|
||||||
|
|
||||||
uint8_t byte = eepromReadByte(i);
|
if (bytesErased == 0) {
|
||||||
|
uartPutLine("ERROR 0 bytes erased");
|
||||||
if (byte >= 32 && byte <= 126) {
|
|
||||||
uartPutChar(byte);
|
|
||||||
} else {
|
} else {
|
||||||
uartPutChar('?');
|
char outBuffer[16];
|
||||||
}
|
snprintf(outBuffer, 16, "%lu", bytesErased);
|
||||||
|
uartPutString("OK erased ");
|
||||||
uartPutString(" (0x");
|
uartPutString(outBuffer);
|
||||||
uartPutHexByte(byte);
|
uartPutLine(" bytes");
|
||||||
uartPutLine(")");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TESTWRITE command: for testing purposes, writes a few bytes
|
|
||||||
void commandTestWrite(char* arg) {
|
|
||||||
char str[] = "Ohai world";
|
|
||||||
address_t addr = 0x00;
|
|
||||||
|
|
||||||
char* writeStr = str;
|
|
||||||
if (arg != NULL) {
|
|
||||||
writeStr = arg;
|
|
||||||
}
|
|
||||||
|
|
||||||
eepromSetWriteMode();
|
|
||||||
|
|
||||||
// write input line instead of static string
|
|
||||||
for (char* p = writeStr; *p != '\0'; p++) {
|
|
||||||
eepromWriteByte(addr, *p);
|
|
||||||
//_delay_ms(100);
|
|
||||||
addr++;
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO necessary?
|
|
||||||
_delay_ms(100);
|
|
||||||
|
|
||||||
uartPutLine("TESTWRITE success");
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -12,8 +12,4 @@ void commandRead(char* arg);
|
||||||
void commandWrite(char* arg);
|
void commandWrite(char* arg);
|
||||||
void commandErase();
|
void commandErase();
|
||||||
|
|
||||||
// TODO commands only for testing
|
|
||||||
void commandTestRead();
|
|
||||||
void commandTestWrite(char* arg);
|
|
||||||
|
|
||||||
#endif /* COMMANDS_H_ */
|
#endif /* COMMANDS_H_ */
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,10 @@
|
||||||
// Define macro for NOP instruction
|
// Define macro for NOP instruction
|
||||||
#define _NOP() __asm__ __volatile__ ("nop");
|
#define _NOP() __asm__ __volatile__ ("nop");
|
||||||
|
|
||||||
|
// Define macro to convert a preprocessor constant to a string literal
|
||||||
|
#define STR_(x) #x
|
||||||
|
#define STR(x) STR_(x)
|
||||||
|
|
||||||
// Define type for EEPROM addresses
|
// Define type for EEPROM addresses
|
||||||
typedef uint16_t address_t;
|
typedef uint16_t address_t;
|
||||||
|
|
||||||
|
|
@ -28,12 +32,11 @@ typedef struct {
|
||||||
} AddressRange;
|
} AddressRange;
|
||||||
|
|
||||||
// Define type for a block of data (bytes)
|
// Define type for a block of data (bytes)
|
||||||
|
#define DATA_BLOCK_SIZE 64
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
uint8_t* data;
|
uint8_t data[DATA_BLOCK_SIZE];
|
||||||
uint8_t maxSize;
|
|
||||||
uint8_t bytes;
|
uint8_t bytes;
|
||||||
} DataBuffer;
|
} DataBuffer;
|
||||||
|
|
||||||
#define DATA_BLOCK_SIZE 64
|
|
||||||
|
|
||||||
#endif /* COMMON_H_ */
|
#endif /* COMMON_H_ */
|
||||||
|
|
|
||||||
|
|
@ -132,7 +132,7 @@ Address eepromReadBlock(AddressRange addressRange, DataBuffer* buffer) {
|
||||||
highestAddress = HIGHEST_VALID_ADDRESS;
|
highestAddress = HIGHEST_VALID_ADDRESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (currentAddress <= highestAddress && buffer->bytes < buffer->maxSize) {
|
while (currentAddress <= highestAddress && buffer->bytes < DATA_BLOCK_SIZE) {
|
||||||
buffer->data[buffer->bytes++] = eepromReadByte(currentAddress++);
|
buffer->data[buffer->bytes++] = eepromReadByte(currentAddress++);
|
||||||
|
|
||||||
// Handle integer overflow
|
// Handle integer overflow
|
||||||
|
|
@ -172,3 +172,34 @@ Address eepromWriteBlock(Address startAddress, DataBuffer buffer) {
|
||||||
|
|
||||||
return (Address) {true, currentAddress};
|
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;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,10 +5,6 @@
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include <avr/io.h>
|
#include <avr/io.h>
|
||||||
|
|
||||||
// Define address length
|
|
||||||
// TODO 15 or 16?
|
|
||||||
#define ADDRESS_LENGTH 15
|
|
||||||
|
|
||||||
// Macros for IO pins
|
// Macros for IO pins
|
||||||
#define EEP_ADDR_LOW_PORT PORTC
|
#define EEP_ADDR_LOW_PORT PORTC
|
||||||
#define EEP_ADDR_LOW_DDR DDRC
|
#define EEP_ADDR_LOW_DDR DDRC
|
||||||
|
|
@ -46,4 +42,7 @@ Address eepromReadBlock(AddressRange addressRange, DataBuffer* buffer);
|
||||||
// Write multiple bytes from a buffer to EEPROM
|
// Write multiple bytes from a buffer to EEPROM
|
||||||
Address eepromWriteBlock(Address startAddress, DataBuffer buffer);
|
Address eepromWriteBlock(Address startAddress, DataBuffer buffer);
|
||||||
|
|
||||||
|
// Erase block of data on the EEPROM
|
||||||
|
uint32_t eepromEraseBlock(AddressRange addressRange);
|
||||||
|
|
||||||
#endif /* EEPROM_H_ */
|
#endif /* EEPROM_H_ */
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@
|
||||||
|
|
||||||
// Read and parse command line and dispatch command
|
// Read and parse command line and dispatch command
|
||||||
void parseNextCommand() {
|
void parseNextCommand() {
|
||||||
const int bufferLength = 80;
|
const int bufferLength = 40;
|
||||||
char buffer[bufferLength];
|
char buffer[bufferLength];
|
||||||
|
|
||||||
// Read next command
|
// Read next command
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,15 @@
|
||||||
#include <avr/io.h>
|
#include <avr/io.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
#include <util/setbaud.h>
|
#include <util/setbaud.h>
|
||||||
|
|
||||||
|
// Hexadecimal digit lookup table
|
||||||
|
static const char hexDigitLookupTable[] = {
|
||||||
|
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
|
||||||
|
};
|
||||||
|
|
||||||
// Initialize UART
|
// Initialize UART
|
||||||
void uartInit() {
|
void uartInit() {
|
||||||
// Set Baud register
|
// Set Baud register
|
||||||
|
|
@ -56,18 +63,12 @@ void uartPutLine(char* data) {
|
||||||
uartPutChar('\n');
|
uartPutChar('\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert an integer to decimal ASCII and transmit
|
|
||||||
void uartPutInteger(int value) {
|
|
||||||
char outBuffer[32];
|
|
||||||
snprintf(outBuffer, 32, "%d", value);
|
|
||||||
uartPutString(outBuffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Convert a byte to hexadecimal ASCII and transmit
|
// Convert a byte to hexadecimal ASCII and transmit
|
||||||
void uartPutHexByte(uint8_t byte) {
|
void uartPutHexByte(uint8_t byte) {
|
||||||
char outBuffer[8];
|
// First hex digit: most significant 4 bits
|
||||||
snprintf(outBuffer, 8, "%02hhX", byte);
|
uartPutChar(hexDigitLookupTable[(byte >> 4) & 0x0f]);
|
||||||
uartPutString(outBuffer);
|
// Second hex digit: least significant 4 bits
|
||||||
|
uartPutChar(hexDigitLookupTable[byte & 0x0f]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Receive a single character (blocking)
|
// Receive a single character (blocking)
|
||||||
|
|
|
||||||
|
|
@ -16,9 +16,6 @@ void uartPutString(char* data);
|
||||||
// Transmit a string followed by a line break
|
// Transmit a string followed by a line break
|
||||||
void uartPutLine(char* data);
|
void uartPutLine(char* data);
|
||||||
|
|
||||||
// Convert an integer to decimal ASCII and transmit
|
|
||||||
void uartPutInteger(int value);
|
|
||||||
|
|
||||||
// Convert a byte to hexadecimal ASCII and transmit
|
// Convert a byte to hexadecimal ASCII and transmit
|
||||||
void uartPutHexByte(uint8_t byte);
|
void uartPutHexByte(uint8_t byte);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue