summaryrefslogtreecommitdiff
path: root/can.c
blob: 739ae68cdfbd837cb391155afa75438ed10056de (plain)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
			/* addrs: SSSEEEEE
			 *           8 -> extended addr
			 *          14 -> unused
			 */
#define CANA_TIME		0x10080000
#define CANA_DISCOVERY		0x4c080000
#define CANA_DISCOVERY_F(pg,dst) (CANA_DISCOVERY | (((uint32_t)(pg) & 0xf) << 12) | ((dst) & 0xfff))
#define CANA_LIGHT		0xcc080000
#define CANA_LIGHT_F(src,dst)	(CANA_LIGHT | (((src) & 0x3f) << 12) | ((dst) & 0xfff))
#define CANA_SENSOR		0xe6080000
#define CANA_SENSOR_F(src)	(CANA_SENSOR | ((src) & 0xfff))
#define CANA_DEBUG		0xe7000000

#define CANA_PROTOCOL		0xffe80000

#define can_rx_isext()		(can_rx_addr.b[1] & 0x08)
#define can_rx_ext_rr()		(can_rx_dlc & 0x40)
#define can_rx_len()		(can_rx_dlc & 0x0f)

#define can_rx_sublab_proto()	((can_rx_addr.b[0] << 8) | (can_rx_addr.b[1] & 0xe8))
#define can_rx_sublab_addr()	(((can_rx_addr.b[2] & 0x0f) << 8) | can_rx_addr.b[3])
#define can_rx_sublab_disco_page()	((can_rx_addr.b[2] & 0xf0) >> 4)

#ifndef R0KET
#define spi_ss(x)	PORTB = ((x) << B_SS) | 0x3;

static uint8_t spi_wrrd(uint8_t out)
{
	SPDR = out;
	while (!(SPSR & (1 << SPIF)))
		;
	return SPDR;
}

static void spi_performpgm(const uint8_t * PROGMEM cmds, uint8_t len)
{
	const uint8_t * PROGMEM end = cmds + len;
	uint8_t c;

	spi_ss(0);
	while (cmds < end) {
		c = pgm_read_byte(cmds);
		spi_wrrd(c);
		cmds++;
	}
	spi_ss(1);
}
#endif
#define spi_perform(...) do { \
	static const uint8_t _mycmds[] PROGMEM = { __VA_ARGS__ }; \
	spi_performpgm(_mycmds, sizeof(_mycmds)); } while (0)

/* CAN configuration:
 *
 * CNF1	SJW	= 1TQ	00xxxxxx
 * 	BRP	= /12	xx001011	(16 MHz assumed)
 * 	= 0x0b
 * CNF2 BTLMODE = cfg	1xxxxxxx
 * 	SAM	= once	x0xxxxxx
 * 	PS1	= 1TQ	xx000xxx
 * 	prop	= 2TQ	xxxxx001
 * 	= 0x81
 * CNF3	WAKFIL	= off	x0xxxxxx
 * 	PS2	= 2TQ	xxxxx001
 * 	= 0x01
 */

#define MCP2515_WRITE	0x02
#define MCP2515_READ	0x03
#define MCP2515_RTS	0x80
#define MCP2515_WRTXB	0x40
#define MCP2515_WRTXB_DATA	0x01
#define MCP2515_WRTXB_TXB0	0x00
#define MCP2515_WRTXB_TXB1	0x02
#define MCP2515_WRTXB_TXB2	0x04

#define A_CNF3		0x28
#define A_CANINTF	0x2c
#define A_CANCTRL	0x2f
#define A_TXB0CTRL	0x30
#define A_TXB0SIDH	0x31
#define A_RXB1CTRL	0x70

static void can_init(void)
{
	spi_perform(MCP2515_WRITE, A_CANCTRL,
		0x80);	/* CANCTRL: config mode */
#ifdef R0KET
#define CNF1	0x05	/* 8 MHz crystal, divide by 6 */
#else
#define CNF1	0x0b	/* 16 MHz crystal, divide by 12 */
#endif
	spi_perform(MCP2515_WRITE, A_CNF3,
		0x01,	/* CNF3 */
		0x81,	/* CNF2 */
		CNF1,	/* CNF1 */
		0xa7	/* CANINTE: MERRE, ERRIE, TX0IE, RX1IE, RX0IE */
	);
	spi_perform(MCP2515_WRITE, A_RXB1CTRL,
		0x60);	/* x, RXM, x, RXRTR, FILHIT */
	spi_perform(MCP2515_WRITE, A_CANCTRL,
		0x00);	/* CANCTRL: normal mode */
}

static uint8_t can_CANSTAT(void)
{
	uint8_t canstat;

	spi_ss(0);
	spi_wrrd(0x03);
	spi_wrrd(0x2e);	/* addr(CANCTRL) */
	canstat = spi_wrrd(0xff);	/* CANSTAT */
	spi_ss(1);
	uart_puts("can: CANSTAT ");
	uart_puthex(canstat);
	uart_puts("\n");
	return canstat;
}

#ifndef R0KET
#define can_irq_cli()	EIMSK &= ~(1 << INT0)
#define can_irq_sei()	EIMSK |= (1 << INT0)
#else
#define can_irq_cli()	cli()
#define can_irq_sei()	sei()
#endif

/* daddr:
 *   31-24	ID 10:3
 *   23-16	ID 2:0, x, EXIDE, x, EID17:16
 *   15- 8	EID 15:8
 *    7- 0	EID  7:0
 */
static void can_send(uint32_t daddr, uint8_t len, const uint8_t *data)
{
	can_irq_cli();

	spi_ss(0);
	spi_wrrd(MCP2515_WRTXB | MCP2515_WRTXB_TXB0);
	spi_wrrd((daddr >> 24) & 0xff);
	spi_wrrd((daddr >> 16) & 0xff);
	spi_wrrd((daddr >>  8) & 0xff);
	spi_wrrd((daddr >>  0) & 0xff);
	spi_wrrd(len);
	while (len--)
		spi_wrrd(*data++);
	spi_ss(1);
	spi_perform(MCP2515_RTS | 0x01);

	can_irq_sei();
}

#ifndef R0KET
union {
	uint8_t b[4];
	uint32_t u;
} can_rx_addr;
uint8_t can_rx_dlc, can_rx_data[8];

#define can_rx_avail()		(can_rx_addr.u)
#define can_rx_pop()		do { can_rx_addr.u = 0; } while (0)

static void can_rxh(uint8_t buffer)
{
	uint8_t c, byte;

	if (buffer)
		uart_puts("can: RX1IF\n");
	else
		uart_puts("can: RX0IF\n");
	spi_ss(0);
	spi_wrrd(0x90 + 0x04 * buffer);

	if (can_rx_addr.u) {
		spi_wrrd(0xff);
		spi_ss(1);
		uart_puts("-- CAN RX overrun\n");
		return;
	};

	c = 0;
#define rdaddr() byte = spi_wrrd(0xff); uart_puthex(byte); can_rx_addr.b[c++] = byte
	rdaddr();
	rdaddr();
	rdaddr();
	rdaddr();
	can_rx_dlc = spi_wrrd(0xff);
	uart_puthex(can_rx_dlc);
	uart_puts("\n");
	for (c = 0; c < (can_rx_dlc & 0x0f); c++) {
		byte = spi_wrrd(0xff);
		can_rx_data[c] = byte;
		uart_puthex(byte);
	}
	uart_puts("\n");
	spi_ss(1);
}

ISR(INT0_vect)
{
	uint8_t canintf, eflg, canstat;

#ifdef HAVE_TICK
	uart_puttick();
#endif
//	dbg_dump_ret();
	uart_puts("can: irqh<");

	spi_ss(0);
	spi_wrrd(MCP2515_READ);
	spi_wrrd(A_CANINTF);
	canintf	= spi_wrrd(0xff);
	eflg	= spi_wrrd(0xff);
	canstat = spi_wrrd(0xff);
	spi_ss(1);

	uart_puthex(canintf);
	uart_puthex(eflg);
	uart_puthex(canstat);
	uart_puts(">\n");

	if (canintf & 0x80 || canintf & 0x04) {
		uint8_t txb0ctrl;
		spi_ss(0);
		spi_wrrd(MCP2515_READ);
		spi_wrrd(A_TXB0CTRL);
		txb0ctrl = spi_wrrd(0xff);
		spi_ss(1);

		/* clear RTS */
		if (txb0ctrl & 0x08) {
			spi_ss(0);
			spi_wrrd(MCP2515_WRITE);
			spi_wrrd(A_TXB0CTRL);
			spi_wrrd(0x00);
			spi_ss(1);
		}
	}
	if (canintf & 0x01)
		can_rxh(0);
	if (canintf & 0x02)
		can_rxh(1);

	spi_perform(MCP2515_WRITE, A_CANINTF, 0x00, 0x00);
}
#endif

static void can_preinit(void)
{

	spi_ss(1);
#ifndef R0KET
	can_rx_addr.u = 0;

	DDRB |= (1 << B_SCK) | (1 << B_MOSI) | (1 << B_SS);

	/* divisor: 0 0 0 = fosc / 4 = 2 MHz */
	SPCR = (1 << SPE) | (1 << MSTR);

	/* INT0 */
	EICRA = (1 << ISC01);
	EIMSK = (1 << INT0);

	_delay_ms(5);
#endif

	/* chip reset */
	spi_ss(0);
	spi_wrrd(0xc0);
	spi_ss(1);

	_delay_ms(5);

	spi_ss(0);
	spi_wrrd(0xb0);
	uint8_t status = spi_wrrd(0xff);
	spi_ss(1);
#if 0
	uart_puts("can: status ");
	uart_puthex(status);
	uart_puts("\n");
#endif
}