diff options
Diffstat (limited to 'fw')
| -rw-r--r-- | fw/eeprom.c | 53 | ||||
| -rw-r--r-- | fw/eeprom.h | 10 | ||||
| -rw-r--r-- | fw/table.c | 2 | ||||
| -rw-r--r-- | fw/table.h | 14 | ||||
| -rw-r--r-- | fw/tests/system/eeprom_can_systest.c | 2 | ||||
| -rw-r--r-- | fw/tests/system/eeprom_systest.c | 2 |
6 files changed, 74 insertions, 9 deletions
diff --git a/fw/eeprom.c b/fw/eeprom.c index fdce167..71a53b1 100644 --- a/fw/eeprom.c +++ b/fw/eeprom.c @@ -2,10 +2,12 @@ #include <stdbool.h> #include <stdint.h> +#include <string.h> #include "system.h" #include "types.h" #include "spi.h" +#include "can.h" #include "eeprom.h" @@ -166,3 +168,54 @@ eepromRead(U16 addr, U8 *data, U8 size) { return OK; } + +// Write a CAN ID to the EEPROM. +// CAN IDs are stored in the lower 11 or 29 bits of a little-endian U32. +// Bit 31 indicates standard/extended: 0=>std, 1=>ext. +Status +eepromWriteCanId(U16 addr, const CanId *id) { + U8 buf[4u]; + + // Copy ID to buffer + memset(buf, 0u, sizeof(buf)); + switch (id->type) { + case CAN_ID_STD: + buf[0u] = id->sid.lo; + buf[1u] = id->sid.hi & 0x7; + break; + case CAN_ID_EXT: + memmove(buf, id->eid, sizeof(buf)); + buf[3u] = (buf[3u] & 0x1F) | 0x80; // set EID flag + break; + default: + return FAIL; // unreachable + } + + return eepromWrite(addr, buf, sizeof(buf)); +} + +// Read a CAN ID from the EEPROM. +// CAN IDs are stored in the lower 11 or 29 bits of a little-endian U32. +// Bit 31 indicates standard/extended: 0=>std, 1=>ext. +Status +eepromReadCanId(U16 addr, CanId *id) { + Status status; + + // Read + status = eepromRead(addr, id->eid, sizeof(id->eid)); + if (status != OK) { + return FAIL; + } + + // Unpack + if (id->eid[3u] & 0x80) { // bit 31 is standard/extended flag + id->type = CAN_ID_EXT; // extended + id->eid[3u] &= 0x1F; + } else { + id->type = CAN_ID_STD; // standard + id->sid.lo = id->eid[0u]; + id->sid.hi = id->eid[1u] & 0x7; + } + + return OK; +} diff --git a/fw/eeprom.h b/fw/eeprom.h index ab701d7..47f02e6 100644 --- a/fw/eeprom.h +++ b/fw/eeprom.h @@ -5,8 +5,10 @@ * * Usage: * + * #include <stdbool> * #include <stdint.h> * #include "types.h" + * #include "can.h" * #include "eeprom.h" */ @@ -14,6 +16,10 @@ #define EEPROM_CS_TRIS TRISCbits.TRISC5 #define EEPROM_CS LATCbits.LATC5 +typedef U16 EepromAddr; + void eepromInit(void); -Status eepromWrite(U16 addr, U8 data[], U8 size); -Status eepromRead(U16 addr, U8 data[], U8 size); +Status eepromWrite(EepromAddr addr, U8 data[], U8 size); +Status eepromRead(EepromAddr addr, U8 data[], U8 size); +Status eepromWriteCanId(EepromAddr addr, const CanId *id); +Status eepromReadCanId(EepromAddr addr, CanId *id); @@ -1,8 +1,10 @@ #include <xc.h> +#include <stdbool.h> #include <stdint.h> #include "types.h" +#include "can.h" #include "eeprom.h" #include "table.h" @@ -1,8 +1,8 @@ /* Table datastructure for storing calibrations in the EEPROM. * * A table has a fixed number of rows that define a key/value mapping. - * Keys and values are each U16. - * T: U16->U16 + * Keys and values are each U32. + * T: U32->U32 * Numbers are stored little-endian. * * Keys must be monotonically increasing. @@ -12,21 +12,23 @@ * * Usage: * + * #include <stdbool.h> * #include <stdint.h> * #include "types.h" + * #include "can.h" + * #include "eeprom.h" * #include "table.h" */ enum { - TAB_KEY_SIZE = 2, // U16 - TAB_VAL_SIZE = 2, // U16 + TAB_KEY_SIZE = 4, // U32 + TAB_VAL_SIZE = 4, // U32 TAB_ROWS = 32, TAB_ROW_SIZE = TAB_KEY_SIZE + TAB_VAL_SIZE, - TAB_SIZE = TAB_ROWS * TAB_ROW_SIZE, }; typedef struct { - U16 offset; // starting address + EepromAddr offset; // starting address } Table; // Set the key and value of row k. diff --git a/fw/tests/system/eeprom_can_systest.c b/fw/tests/system/eeprom_can_systest.c index 29c29e0..14e8f2d 100644 --- a/fw/tests/system/eeprom_can_systest.c +++ b/fw/tests/system/eeprom_can_systest.c @@ -14,8 +14,8 @@ #include "system.h" #include "types.h" #include "spi.h" -#include "eeprom.h" #include "can.h" +#include "eeprom.h" static const CanId id = { .type = CAN_ID_STD, diff --git a/fw/tests/system/eeprom_systest.c b/fw/tests/system/eeprom_systest.c index 8285e5c..5261532 100644 --- a/fw/tests/system/eeprom_systest.c +++ b/fw/tests/system/eeprom_systest.c @@ -1,10 +1,12 @@ #include <xc.h> +#include <stdbool.h> #include <stdint.h> #include "system.h" #include "types.h" #include "spi.h" +#include "can.h" #include "eeprom.h" static const U16 addr = {0x00, 0x0A}; |