-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmcp2515.cpp
executable file
·364 lines (294 loc) · 8.46 KB
/
mcp2515.cpp
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/* Copyright (c) 2007 Fabian Greif
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
// ----------------------------------------------------------------------------
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include "Arduino.h"
#include "global.h"
#include "mcp2515.h"
#include "mcp2515_defs.h"
#include "defaults.h"
uint8_t spi_putc( uint8_t data )
{
// put byte in send-buffer
SPDR = data;
// wait until byte was sent
while( !( SPSR & (1<<SPIF) ) )
;
return SPDR;
}
// -------------------------------------------------------------------------
void mcp2515_write_register( uint8_t adress, uint8_t data )
{
RESET(MCP2515_CS);
spi_putc(SPI_WRITE);
spi_putc(adress);
spi_putc(data);
SET(MCP2515_CS);
}
// -------------------------------------------------------------------------
uint8_t mcp2515_read_register(uint8_t adress)
{
uint8_t data;
RESET(MCP2515_CS);
spi_putc(SPI_READ);
spi_putc(adress);
data = spi_putc(0xff);
SET(MCP2515_CS);
return data;
}
// -------------------------------------------------------------------------
void mcp2515_bit_modify(uint8_t adress, uint8_t mask, uint8_t data)
{
RESET(MCP2515_CS);
spi_putc(SPI_BIT_MODIFY);
spi_putc(adress);
spi_putc(mask);
spi_putc(data);
SET(MCP2515_CS);
}
// ----------------------------------------------------------------------------
uint8_t mcp2515_read_status(uint8_t type)
{
uint8_t data;
RESET(MCP2515_CS);
spi_putc(type);
data = spi_putc(0xff);
SET(MCP2515_CS);
return data;
}
// -------------------------------------------------------------------------
uint8_t mcp2515_init(uint8_t speed)
{
SET(MCP2515_CS);
SET_OUTPUT(MCP2515_CS);
RESET(P_SCK);
RESET(P_MOSI);
RESET(P_MISO);
SET_OUTPUT(P_SCK);
SET_OUTPUT(P_MOSI);
SET_INPUT(P_MISO);
SET_INPUT(MCP2515_INT);
SET(MCP2515_INT);
// active SPI master interface
SPCR = (1<<SPE)|(1<<MSTR) | (0<<SPR1)|(1<<SPR0);
SPSR = 0;
// reset MCP2515 by software reset.
// After this he is in configuration mode.
RESET(MCP2515_CS);
spi_putc(SPI_RESET);
SET(MCP2515_CS);
// wait a little bit until the MCP2515 has restarted
_delay_us(10);
// load CNF1..3 Register
RESET(MCP2515_CS);
spi_putc(SPI_WRITE);
spi_putc(CNF3);
// Bitrate 250 kbps at 16 MHz. (is this SPI speed??)
spi_putc((1<<PHSEG21));
spi_putc((1<<BTLMODE)|(1<<PHSEG11));
spi_putc(speed);
// activate interrupts
spi_putc((1<<RX1IE)|(1<<RX0IE));
SET(MCP2515_CS);
// test if we could read back the value => is the chip accessible?
if (mcp2515_read_register(CNF1) != speed) {
SET(LED2_HIGH);
return false;
}
// deaktivate the RXnBF Pins (High Impedance State)
mcp2515_write_register(BFPCTRL, 0);
// set TXnRTS as inputs
mcp2515_write_register(TXRTSCTRL, 0);
// turn off filters => receive any message
mcp2515_write_register(RXB0CTRL, (1<<RXM1)|(1<<RXM0));
mcp2515_write_register(RXB1CTRL, (1<<RXM1)|(1<<RXM0));
// reset device to normal mode
mcp2515_write_register(CANCTRL, 0);
// SET(LED2_HIGH);
return true;
}
// -------------------------------------------------------------------------
void mcp2515_set_standard_mode(void) {
mcp2515_bit_modify(CANCTRL, (1<<REQOP2)|(1<<REQOP1)|(1<<REQOP0), 0);
}
// -------------------------------------------------------------------------
void mcp2515_set_loopback_mode(void) {
mcp2515_bit_modify(CANCTRL, (1<<REQOP2)|(1<<REQOP1)|(1<<REQOP0), 0x40);
}
// -------------------------------------------------------------------------
void mcp2515_set_configuration_mode(void) {
mcp2515_bit_modify(CANCTRL, 0xe0, (1<<REQOP2));
while ((mcp2515_read_register(CANSTAT) & 0xe0) != (1<<REQOP2))
;
}
// -------------------------------------------------------------------------
void mcp2515_turn_on_filters(void) {
mcp2515_write_register(RXB0CTRL, (1<<BUKT));
mcp2515_write_register(RXB1CTRL, 0);
}
// -------------------------------------------------------------------------
void mcp2515_turn_off_filters(void) {
mcp2515_write_register(RXB0CTRL, (1<<RXM1)|(1<<RXM0));
mcp2515_write_register(RXB1CTRL, (1<<RXM1)|(1<<RXM0));
}
// -------------------------------------------------------------------------
void mcp2515_static_filter(const prog_uint8_t *filter)
{
mcp2515_set_configuration_mode();
mcp2515_turn_on_filters();
uint8_t i, j;
for (i = 0; i < 0x30; i += 0x10)
{
RESET(MCP2515_CS);
spi_putc(SPI_WRITE);
spi_putc(i);
for (j = 0; j < 12; j++)
{
if (i == 0x20 && j >= 0x08)
break;
spi_putc(pgm_read_byte(filter++));
}
SET(MCP2515_CS);
}
mcp2515_set_standard_mode();
}
// ----------------------------------------------------------------------------
// check if there are any new messages waiting
uint8_t mcp2515_check_message(void) {
return (!IS_SET(MCP2515_INT));
}
// ----------------------------------------------------------------------------
// check if there is a free buffer to send messages
uint8_t mcp2515_check_free_buffer(void)
{
uint8_t status = mcp2515_read_status(SPI_READ_STATUS);
if ((status & 0x54) == 0x54) {
// all buffers used
return false;
}
return true;
}
// ----------------------------------------------------------------------------
uint8_t mcp2515_get_message(tCAN *message)
{
// read status
uint8_t status = mcp2515_read_status(SPI_RX_STATUS);
uint8_t addr;
uint8_t t;
if (bit_is_set(status,6)) {
// message in buffer 0
addr = SPI_READ_RX;
}
else if (bit_is_set(status,7)) {
// message in buffer 1
addr = SPI_READ_RX | 0x04;
}
else {
// Error: no message available
return 0;
}
RESET(MCP2515_CS);
spi_putc(addr);
// read id
message->id = (uint16_t) spi_putc(0xff) << 3;
message->id |= spi_putc(0xff) >> 5;
spi_putc(0xff);
spi_putc(0xff);
// read DLC
uint8_t length = spi_putc(0xff) & 0x0f;
message->header.length = length;
message->header.rtr = (bit_is_set(status, 3)) ? 1 : 0;
// read data
for (t=0;t<length;t++) {
message->data[t] = spi_putc(0xff);
}
SET(MCP2515_CS);
// clear interrupt flag
if (bit_is_set(status, 6)) {
mcp2515_bit_modify(CANINTF, (1<<RX0IF), 0);
}
else {
mcp2515_bit_modify(CANINTF, (1<<RX1IF), 0);
}
return (status & 0x07) + 1;
}
// ----------------------------------------------------------------------------
uint8_t mcp2515_send_message(tCAN *message)
{
uint8_t status = mcp2515_read_status(SPI_READ_STATUS);
/* Statusbyte:
*
* Bit Function
* 2 TXB0CNTRL.TXREQ
* 4 TXB1CNTRL.TXREQ
* 6 TXB2CNTRL.TXREQ
*/
uint8_t address;
uint8_t t;
// SET(LED2_HIGH);
if (bit_is_clear(status, 2)) {
address = 0x00;
}
else if (bit_is_clear(status, 4)) {
address = 0x02;
}
else if (bit_is_clear(status, 6)) {
address = 0x04;
}
else {
// all buffer used => could not send message
return 0;
}
RESET(MCP2515_CS);
spi_putc(SPI_WRITE_TX | address);
spi_putc(message->id >> 3);
spi_putc(message->id << 5);
spi_putc(0);
spi_putc(0);
uint8_t length = message->header.length & 0x0f;
if (message->header.rtr) {
// a rtr-frame has a length, but contains no data
spi_putc((1<<RTR) | length);
}
else {
// set message length
spi_putc(length);
// data
for (t=0;t<length;t++) {
spi_putc(message->data[t]);
}
}
SET(MCP2515_CS);
_delay_us(1);
// send message
RESET(MCP2515_CS);
address = (address == 0) ? 1 : address;
spi_putc(SPI_RTS | address);
SET(MCP2515_CS);
return address;
}