aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--fw/eeprom.c55
-rw-r--r--fw/eeprom.h4
-rw-r--r--fw/serial.c69
-rw-r--r--fw/serial.h45
4 files changed, 114 insertions, 59 deletions
diff --git a/fw/eeprom.c b/fw/eeprom.c
index 2cc7807..c3e1ae7 100644
--- a/fw/eeprom.c
+++ b/fw/eeprom.c
@@ -6,7 +6,6 @@
#include "system.h"
#include "types.h"
#include "spi.h"
-#include "can.h"
#include "eeprom.h"
@@ -167,57 +166,3 @@ 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
- if (id->isExt) { // extended
- buf[0u] = (id->eid>>0u) & 0xFF;
- buf[1u] = (id->eid>>8u) & 0xFF;
- buf[2u] = (id->eid>>16u) & 0xFF;
- buf[3u] = (id->eid>>24u) & 0x1F;
- buf[3u] |= 0x80; // set EID flag in bit 31
- } else { // standard
- buf[0u] = (id->sid>>0u) & 0xFF;
- buf[1u] = (id->sid>>8u) & 0x07;
- buf[2u] = 0u;
- buf[3u] = 0u; // clear EID flag in bit 32
- }
-
- 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) {
- U8 buf[sizeof(U32)];
- Status status;
-
- // Read
- status = eepromRead(addr, buf, sizeof(buf));
- if (status != OK) {
- return FAIL;
- }
-
- // Unpack
- if (buf[3u] & 0x80) { // bit 31 is standard/extended flag
- id->isExt = true; // extended
- id->eid = ((U32)buf[0u] << 0u)
- | ((U32)buf[1u] << 8u)
- | ((U32)buf[2u] << 16u)
- | (((U32)buf[3u] & 0x1F) << 24u);
- } else {
- id->isExt = false; // standard
- id->sid = ((U16)buf[0u] << 0u)
- | (((U16)buf[1u] & 0x07) << 8u);
- }
-
- return OK;
-}
diff --git a/fw/eeprom.h b/fw/eeprom.h
index 47f02e6..96d8482 100644
--- a/fw/eeprom.h
+++ b/fw/eeprom.h
@@ -5,10 +5,8 @@
*
* Usage:
*
- * #include <stdbool>
* #include <stdint.h>
* #include "types.h"
- * #include "can.h"
* #include "eeprom.h"
*/
@@ -21,5 +19,3 @@ typedef U16 EepromAddr;
void eepromInit(void);
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);
diff --git a/fw/serial.c b/fw/serial.c
new file mode 100644
index 0000000..42a37b4
--- /dev/null
+++ b/fw/serial.c
@@ -0,0 +1,69 @@
+#include <xc.h>
+
+#include <stdbool.h>
+#include <stdint.h>
+
+#include "types.h"
+#include "eeprom.h"
+#include "can.h"
+#include "signal.h"
+
+#include "serial.h"
+
+Status
+serWriteCanId(U16 addr, const CanId *id) {
+ U8 buf[4u];
+
+ // Copy ID to buffer
+ if (id->isExt) { // extended
+ buf[0u] = (id->eid>>0u) & 0xFF;
+ buf[1u] = (id->eid>>8u) & 0xFF;
+ buf[2u] = (id->eid>>16u) & 0xFF;
+ buf[3u] = (id->eid>>24u) & 0x1F;
+ buf[3u] |= 0x80; // set EID flag in bit 31
+ } else { // standard
+ buf[0u] = (id->sid>>0u) & 0xFF;
+ buf[1u] = (id->sid>>8u) & 0x07;
+ buf[2u] = 0u;
+ buf[3u] = 0u; // clear EID flag in bit 32
+ }
+
+ return eepromWrite(addr, buf, sizeof(buf));
+}
+
+Status
+serReadCanId(U16 addr, CanId *id) {
+ U8 buf[sizeof(U32)];
+ Status status;
+
+ // Read
+ status = eepromRead(addr, buf, sizeof(buf));
+ if (status != OK) {
+ return FAIL;
+ }
+
+ // Unpack
+ if (buf[3u] & 0x80) { // bit 31 is standard/extended flag
+ id->isExt = true; // extended
+ id->eid = ((U32)buf[0u] << 0u)
+ | ((U32)buf[1u] << 8u)
+ | ((U32)buf[2u] << 16u)
+ | (((U32)buf[3u] & 0x1F) << 24u);
+ } else {
+ id->isExt = false; // standard
+ id->sid = ((U16)buf[0u] << 0u)
+ | (((U16)buf[1u] & 0x07) << 8u);
+ }
+
+ return OK;
+}
+
+Status
+serWriteSigFmt(EepromAddr addr, const SigFmt *sig) {
+ // TODO
+}
+
+Status
+serReadSigFmt(EepromAddr addr, SigFmt *sig) {
+ // TODO
+}
diff --git a/fw/serial.h b/fw/serial.h
new file mode 100644
index 0000000..5a6b495
--- /dev/null
+++ b/fw/serial.h
@@ -0,0 +1,45 @@
+/** Serialization of structures stored in the EEPROM.
+ *
+ * Device PIC16F1459
+ * Compiler: XC8 v3.00
+ *
+ * Usage:
+ *
+ * #include <stdbool.h>
+ * #include <stdint.h>
+ * #include "types.h"
+ * #include "eeprom.h"
+ * #include "can.h"
+ * #include "signal.h"
+ * #include "serial.h"
+ */
+
+// Size of structures stored in ROM
+enum {
+ SER_CANID_SIZE = sizeof(U32),
+ SER_SIGFMT_SIZE = 8,
+};
+
+// 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 serWriteCanId(EepromAddr addr, const CanId *id);
+
+// 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 serReadCanId(EepromAddr addr, CanId *id);
+
+// Write a SigFmt to the EEPROM.
+// SigFmts use 8 bytes of space.
+// The ID is stored little-endian in the first 4 bytes: 0--3.
+// Start and Size occupy bytes 4 and 5.
+// The Byte-order and Signedness flags are bits 0 and 1 of byte 6, respectively.
+// Byte order: [6][0] = {0=>LE, 1=>BE}.
+// Signedness: [6][1] = {0=>unsigned, 1=>signed}.
+// Byte 7 is unused -- it's for alignment.
+Status serWriteSigFmt(EepromAddr addr, const SigFmt *sig);
+
+// Read a SigFmt from the EEPROM.
+// See serWriteSigFmt for storage format.
+Status serReadSigFmt(EepromAddr addr, SigFmt *sig);