aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2025-09-06 13:51:25 -0400
committerSam Anthony <sam@samanthony.xyz>2025-09-06 13:51:25 -0400
commit45a450f9aae7be5b9df14139989f82e1c665298d (patch)
treebf02c55c707c661e9c718cc4ac4129e2da6bf679
parent366330d3943ec4e066197a8d034d90caf8422a1d (diff)
downloadcan-gauge-interface-45a450f9aae7be5b9df14139989f82e1c665298d.zip
fw: handle "r" eeprom read command
-rw-r--r--doc/protocol2
-rw-r--r--fw/usb.c49
2 files changed, 47 insertions, 4 deletions
diff --git a/doc/protocol b/doc/protocol
index f144c50..c7c4849 100644
--- a/doc/protocol
+++ b/doc/protocol
@@ -13,7 +13,7 @@ Write EEPROM:
Read EEPROM:
<opcode> ::= "r"
-<args> ::= <addr> " " <size>
+<args> ::= <addrHi> " " <addrLo> " " <size>
<retval> ::= <bytes>{size}
<addrHi> ::= <byte>
diff --git a/fw/usb.c b/fw/usb.c
index ae95de7..cf27a47 100644
--- a/fw/usb.c
+++ b/fw/usb.c
@@ -13,6 +13,10 @@
#include "usb.h"
+/***** Macros *****/
+
+#define min(a, b) (((a) < (b)) ? (a) : (b))
+
/***** Constants *****/
// Safety counter
@@ -50,8 +54,8 @@ static State *readEepromState(void);
/***** Global Variables *****/
-static U8 txBuf[CDC_DATA_IN_EP_SIZE];
static RxQueue rxBuf = {.len = 0u, .head = 0u};
+static U8 txBuf[CDC_DATA_IN_EP_SIZE];
/***** Function Definitions *****/
@@ -288,10 +292,49 @@ writeEepromState(void) {
static State *
readEepromState(void) {
static State state;
-
- // TODO
+ static U16 addr;
+ static U8 size = 0u;
+ U8 chunkSize;
state.next = idleState;
+
+ if (size == 0u) {
+ // First time called in this transaction
+
+ // Read command, including '\n'
+ if (parseU8(&addr.hi) != OK) {
+ nack();
+ return &state;
+ }
+ if (parseU8(&addr.lo) != OK) {
+ nack();
+ return &state;
+ }
+ if (parseU8(&size) != OK) {
+ nack();
+ return &state;
+ }
+ }
+
+ // Read from eeprom into buffer
+ chunkSize = min(size, sizeof(txBuf)-1u); // -1 to leave space for '\n'
+ eepromRead(addr, txBuf, chunkSize);
+ addU16(&addr, chunkSize);
+
+ // End of read?
+ size -= chunkSize;
+ if (size == 0u) {
+ // Done
+ txBuf[chunkSize] = '\n';
+ state.next = idleState;
+ } else {
+ // More data to read in next call
+ state.next = readEepromState;
+ }
+
+ // Flush buffer to USB
+ putUSBUSART(txBuf, chunkSize);
+
return &state;
}