diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2025-09-06 13:51:25 -0400 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2025-09-06 13:51:25 -0400 |
| commit | 45a450f9aae7be5b9df14139989f82e1c665298d (patch) | |
| tree | bf02c55c707c661e9c718cc4ac4129e2da6bf679 /fw | |
| parent | 366330d3943ec4e066197a8d034d90caf8422a1d (diff) | |
| download | can-gauge-interface-45a450f9aae7be5b9df14139989f82e1c665298d.zip | |
fw: handle "r" eeprom read command
Diffstat (limited to 'fw')
| -rw-r--r-- | fw/usb.c | 49 |
1 files changed, 46 insertions, 3 deletions
@@ -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; } |