#include "commands.h" #include "common.h" #include "parsing.h" #include "uart.h" #include "eeprom.h" #include #include #include #include #include // Internal function prototypes void _uartPutAsciiBlock(DataBuffer buffer); // Mode: ASCII (false) or binary (true) bool binary_mode = false; // Execute a single parsed command void executeCommand(CommandLine cmdLine) { // Parse command if (strcmp(cmdLine.command, "INIT") == 0) { // INIT command: Initializes connection. commandInit(cmdLine.arg); } else if (strcmp(cmdLine.command, "READ") == 0) { // READ command: Takes a hex address range (or single address) as argument, // reads data and returns it in hexadecimal ASCII format. commandRead(cmdLine.arg); } 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); } 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 { // unknown command: return error message uartPutLine("ERR invalid command"); } } void commandInit(char* arg) { // (Re-)initialize EEPROM functions eepromInit(); // Default mode: ASCII binary_mode = false; if (arg != NULL) { if (strcmp(arg, "BINARY") == 0) { binary_mode = true; } else if (strcmp(arg, "ASCII") == 0 || strcmp(arg, "TEXT") == 0) { binary_mode = false; } } if (binary_mode) { uartPutLine("OK (binary mode)"); } else { uartPutLine("OK (ASCII mode)"); } } void commandRead(char* arg) { if (arg == NULL) { uartPutLine("ERR READ needs an address or address range"); return; } // Parse address(es) AddressRange range = parseAddressRange(arg); if (!range.isValid) { uartPutLine("ERR invalid address format"); return; } uartPutLine("OK"); 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); if (binary_mode) { // Send block as binary "package": // First the size of the package (1 byte) followed by the data bytes uartPutChar(buffer.bytes); for (int i = 0; i < buffer.bytes; i++) { uartPutChar(buffer.data[i]); } } else { // Fancy ASCII output _uartPutAsciiBlock(buffer); } } while (buffer.bytes > 0); } void _uartPutAsciiBlock(DataBuffer buffer) { 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); } uartPutLine(NULL); } void commandWrite(char* arg) { if (arg == NULL) { uartPutLine("ERR WRITE needs a start address"); return; } // Parse address AddressRange range = parseSingleAddress(arg); if (!range.isValid) { uartPutLine("ERR invalid address format"); return; } // Only binary mode if (!binary_mode) { uartPutLine("ERR WRITE in ASCII mode is not implemented"); return; } // TODO read data from input and write to EEPROM uartPutLine("ERR not implemented"); } void commandErase() { // TODO uartPutLine("ERR not implemented"); } // TESTREAD command: for testing purposes, reads a few bytes and returns them in a human readable format. void commandTestRead() { uint8_t byte; char outBuffer[20]; eepromSetReadMode(); for (int i = 0x00; i < 0x20; i++) { itoa(i, outBuffer, 16); uartPutString("TESTREAD 0x"); uartPutString(outBuffer); uartPutString(": "); byte = eepromReadByte(i); itoa(byte, outBuffer, 16); uartPutChar(byte); uartPutString(" (0x"); uartPutString(outBuffer); uartPutString(")"); uartPutLine(NULL); } } // 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"); }