aboutsummaryrefslogtreecommitdiffstats
path: root/fw
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2025-10-03 16:05:03 -0400
committerSam Anthony <sam@samanthony.xyz>2025-10-03 16:05:03 -0400
commit31937ab63b897b8092df64675a717a5b93d4d5dc (patch)
tree7dfb64fdc168a8e99a92c86f6761361e17feaf88 /fw
parente7baf2e96f1cec2071cb2e1e6a4735c36611bc26 (diff)
downloadcan-gauge-interface-31937ab63b897b8092df64675a717a5b93d4d5dc.zip
CAN bit timing
Diffstat (limited to 'fw')
-rw-r--r--fw/can.c79
-rw-r--r--fw/can.h26
-rw-r--r--fw/dac.h1
-rw-r--r--fw/eeprom.h2
4 files changed, 106 insertions, 2 deletions
diff --git a/fw/can.c b/fw/can.c
new file mode 100644
index 0000000..b005fc2
--- /dev/null
+++ b/fw/can.c
@@ -0,0 +1,79 @@
+#include <xc.h>
+
+#include <stdint.h>
+
+#include "types.h"
+
+#include "can.h"
+
+// Register addresses
+enum {
+ // Pin control and status
+ REG_BFPCTRL = 0x0C,
+
+ // Filter 0
+ REG_RXF0SIDH = 0x00, // standard ID high
+ REG_RXF0SIDL = 0x01, // standard ID low
+ REG_RXF0EID8 = 0x02, // extended ID high
+ REG_RXF0EID0 = 0x03, // extended ID low
+
+ // Filter 1
+ REG_RXF1SIDH = 0x04, // standard ID high
+ REG_RXF1SIDL = 0x05, // standard ID low
+ REG_RXF1EID8 = 0x06, // extended ID high
+ REG_RXF1EID0 = 0x07, // extended ID low
+
+ // Filter 2
+ REG_RXF2SIDH = 0x08, // standard ID high
+ REG_RXF2SIDL = 0x09, // standard ID low
+ REG_RXF2EID8 = 0x0A, // extended ID high
+ REG_RXF2EID0 = 0x0B, // extended ID low
+
+ // Filter 3
+ REG_RXF3SIDH = 0x10, // standard ID high
+ REG_RXF3SIDL = 0x11, // standard ID low
+ REG_RXF3EID8 = 0x12, // extended ID high
+ REG_RXF3EID0 = 0x13, // extended ID low
+
+ // Filter 4
+ REG_RXF4SIDH = 0x14, // standard ID high
+ REG_RXF4SIDL = 0x15, // standard ID low
+ REG_RXF4EID8 = 0x16, // extended ID high
+ REG_RXF4EID0 = 0x17, // extended ID low
+
+ // Filter 5
+ REG_RXF5SIDH = 0x18, // standard ID high
+ REG_RXF5SIDL = 0x19, // standard ID low
+ REG_RXF5EID8 = 0x1A, // extended ID high
+ REG_RXF5EID0 = 0x1B, // extended ID low
+
+ // Mask 0
+ REG_RXM0SIDH = 0x20, standard ID high
+ REG_RXM0SIDL = 0x21, // standard ID low
+ REG_RXM0EID8 = 0x22, // extended ID high
+ REG_RXM0EID0 = 0x23, // extended ID low
+
+ // Mask 1
+ REG_RXM1SIDH = 0x24, // standard ID high
+ REG_RXM1SIDL = 0x25, // standard ID low
+ REG_RXM1EID8 = 0x26. // extended ID high
+ REG_RXM1EID0 = 0x27, // extended ID low
+
+ // Receive buffer 0
+ REG_RXB0CTRL = 0x60, // control
+ REG_RXB0SIDH = 0x61, // standard ID high
+ REG_RXB0SIDL = 0x62, // standard ID low
+ REG_RXB0EID8 = 0x63, // extended ID high
+ REG_RXB0EID0 = 0x64, // extended ID low
+ REG_RXB0DLC = 0x65, // data length code
+ REG_RXB0DM = 0x66, // data byte <M> [66h+0, 66h+7]
+
+ // Receive buffer 1
+ REG_RXB1CTRL = 0x70, // control
+ REG_RXB1SIDH = 0x71, // standard ID high
+ REG_RXB1SIDL = 0x72, // standard ID low
+ REG_RXB1EID8 = 0x73, // extended ID high
+ REG_RXB1EID0 = 0x74, // extended ID low
+ REG_RXB1DLC = 0x75, // data length code
+ REG_RXB1DM = 0x76, // data byte <M> [76h+0, 76h+7]
+};
diff --git a/fw/can.h b/fw/can.h
new file mode 100644
index 0000000..320c020
--- /dev/null
+++ b/fw/can.h
@@ -0,0 +1,26 @@
+/* Microchip MCP2515 CAN Controller
+ *
+ * Device: PIC16F1459
+ * Compiler: XC8 v3.00
+ *
+ * MCP2515 FOSC = 12MHz (48MHz/4 from PIC's CLKOUT)
+ *
+ * Usage:
+ *
+ * #include <stdint.h>
+ * #include "types.h"
+ * #include "can.h"
+ */
+
+// Bit timings (CNF1, CNF2, CNF3)
+#define CAN_TIMINGS_10K 0xDE, 0xAD, 0x06 // BRP=30, PropSeg=6, PS1=6, PS2=7, SP=65%, SJW=4
+#define CAN_TIMINGS_20K 0xCF, 0xAD, 0x06 // BRP=15, PropSeg=6, PS1=6, PS2=7, SP=65%, SJW=4
+#define CAN_TIMINGS_50K 0xC6, 0xAD, 0x06 // BRP=6, PropSeg=6, PS1=6, PS2=7, SP=65%, SJW=4
+#define CAN_TIMINGS_100K 0xC3, 0xAD, 0x06 // BRP=3, PropSeg=6, PS1=6, PS2=7, SP=65%, SJW=4
+#define CAN_TIMINGS_125K 0xC4, 0x9A, 0x03 // BRP=4, PropSeg=3, PS1=4, PS2=4, SP=66.7%, SJW=4
+#define CAN_TIMINGS_250K 0xC2, 0x9A, 0x03 // BRP=2, PropSeg=3, PS1=4, PS2=4, SP=66.7%, SJW=4
+#define CAN_TIMINGS_500K 0xC1, 0x9A, 0x03 // BRP=1, PropSeg=3, PS1=4, PS2=4, SP=66.7, SJW=4
+#define CAN_TIMINGS_800K "800kbps unsupported" // not possible with 12MHz clock
+#define CAN_TIMINGS_1M "1Mbps unsupported" // not possible with 12MHz clock
+
+void canInit(void);
diff --git a/fw/dac.h b/fw/dac.h
index 7bbf66a..300a942 100644
--- a/fw/dac.h
+++ b/fw/dac.h
@@ -7,7 +7,6 @@
*
* #include <stdint.h>
* #include "types.h"
- * #include "spi.h"
* #include "dac.h"
*/
diff --git a/fw/eeprom.h b/fw/eeprom.h
index 9b8652e..9ad3cdd 100644
--- a/fw/eeprom.h
+++ b/fw/eeprom.h
@@ -2,7 +2,7 @@
*
* Device: PIC16F1459
* Compiler: XC8 v3.00
- *
+ *
* Usage:
*
* #include <stdint.h>