aboutsummaryrefslogtreecommitdiffstats
path: root/fw/tests/system/can_echo_systest.c
blob: f0e08d232bfd33a763f41f2f8d770b377d81ce93 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* Listen for a frame with ID 123h or 1234567h;
 * 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"

static const CanId mask0 = {
	.isExt = true,
	.eid = {0xFF, 0xFF, 0xFF, 0x1F}};
static const CanId filter0 = {
	.isExt = true,
	.eid = {0x67, 0x45, 0x23, 0x01}}; // 1234567h
static const CanId mask1 = {
	.isExt = false,
	.sid = {.lo = 0xFF, .hi = 0x7}};
static const CanId filter2 = {
	.isExt = false,
	.sid = {.lo = 0x23, .hi = 0x1}}; // 123h

void
main(void) {
	sysInit();
	spiInit();
	canInit();

	// Setup MCP2515 CAN controller
	canSetBitTiming(CAN_TIMING_10K);
	canSetMask0(&mask0); // RXB0
	canSetFilter0(&filter0);
	canSetMask1(&mask1); // RXB1
	canSetFilter2(&filter2);
	canIE(true); // enable interrupts on MCP2515's 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) {
	CanFrame frame;

	if (INTCONbits.INTF) {
		switch (canRxStatus() & 0x7) { // check filter match
		case 0u: // RXF0
			canReadRxb0(&frame);
			echo(&frame);
			break;
		case 2u: // RXF2
			canReadRxb1(&frame);
			echo(&frame);
			break;
		default:
			canReadRxb0(&frame); // clear interrupt flag
			canReadRxb1(&frame);
		}
		INTCONbits.INTF = 0;
	}
}