aboutsummaryrefslogtreecommitdiffstats
path: root/fw/tests
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2025-10-24 09:42:49 -0400
committerSam Anthony <sam@samanthony.xyz>2025-10-24 09:42:49 -0400
commit32dafa1088e2f2428e9b67bf848faf2433216011 (patch)
treed1ee08ceb3e16d80784680fbc6ac2a2df3b580ab /fw/tests
parent94cdc9ec4e75fc5e55deb394eda4820ba672e3e4 (diff)
downloadcan-gauge-interface-32dafa1088e2f2428e9b67bf848faf2433216011.zip
CAN echo systest
Diffstat (limited to 'fw/tests')
-rw-r--r--fw/tests/system/can_echo_systest.c85
-rw-r--r--fw/tests/system/can_tx_systest.c4
2 files changed, 88 insertions, 1 deletions
diff --git a/fw/tests/system/can_echo_systest.c b/fw/tests/system/can_echo_systest.c
new file mode 100644
index 0000000..798e45f
--- /dev/null
+++ b/fw/tests/system/can_echo_systest.c
@@ -0,0 +1,85 @@
+/* Listen for a standard DATA FRAME with ID 123h;
+ * echo it back with each byte of the DATA FIELD incremented by 1.
+ */
+
+#include <xc.h>
+
+#include <stdbool.h>
+#include <stdint.h>
+
+#include "system.h"
+#include "types.h"
+#include "spi.h"
+#include "can.h"
+
+void
+main(void) {
+ sysInit();
+ spiInit();
+ canInit();
+
+ // Setup MCP2515 CAN controller
+ canSetBitTiming(CAN_TIMING_10K);
+ CanId id = {
+ .type=CAN_ID_STD,
+ .sid = {0xFF, 0xFF},
+ };
+ canSetMask0(&id); // set masks
+ canSetMask1(&id);
+ canSetFilter1(&id); // set unused filters
+ canSetFilter2(&id);
+ canSetFilter3(&id);
+ canSetFilter4(&id);
+ canSetFilter5(&id);
+ id.sid = (U16){0x01, 0x23}; // listen for message on filter 0
+ canSetFilter0(&id);
+ canIE(true); // enable interrupts on INT pin
+ canSetMode(CAN_MODE_NORMAL);
+
+ // Enable interrupts
+ INTCON = 0x00; // clear flags
+ OPTION_REGbits.INTEDG = 0; // interrupt on falling edge
+ INTCONbits.INTE = 1; // enable INT pin
+ INTCONbits.PEIE = 1; // enable peripheral interrupts
+ INTCONbits.GIE = 1; // enable global interrupts
+
+ for (;;) {
+
+ }
+}
+
+// Add 1 to each data byte of a CAN frame and transmit it to the bus.
+static void
+echo(CanFrame *frame) {
+ U8 k;
+
+ for (k = 0u; k < frame->dlc; k++) {
+ frame->data[k]++;
+ }
+
+ canTx(frame);
+}
+
+void
+__interrupt() isr(void) {
+ U8 status;
+ CanFrame frame;
+
+ if (INTCONbits.INTF) {
+ status = canRxStatus();
+ switch (status & 0x7) { // check filter match
+ case 0u: // RXF0
+ canReadRxb0(&frame);
+ echo(&frame);
+ break;
+ case 1u: // RXF1
+ canReadRxb0(&frame); // clear interrupt flag
+ break;
+ default:
+ // Message in RXB1
+ canReadRxb1(&frame); // clear interrupt flag
+ }
+
+ INTCONbits.INTF = 0;
+ }
+}
diff --git a/fw/tests/system/can_tx_systest.c b/fw/tests/system/can_tx_systest.c
index b1dec17..ef1d7b0 100644
--- a/fw/tests/system/can_tx_systest.c
+++ b/fw/tests/system/can_tx_systest.c
@@ -1,3 +1,5 @@
+/* Periodically transmit a frame to the CAN bus. */
+
#include <xc.h>
#include <stdbool.h>
@@ -14,9 +16,9 @@ static const CanFrame frame = {
.type = CAN_ID_STD,
.sid = {0x01, 0x23},
},
+ .rtr = false,
.dlc = 8u,
.data = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77},
- .rtr = false,
};
static U8 ctr = 0u; // timer