-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcan.c
More file actions
503 lines (463 loc) · 16.3 KB
/
can.c
File metadata and controls
503 lines (463 loc) · 16.3 KB
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
/*
* Tritium MCP2515 CAN Interface
* Copyright (c) 2006, Tritium Pty Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* - 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.
* - Neither the name of Tritium Pty Ltd nor the names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT OWNER 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.
*
* Last Modified: J.Kennedy, Tritium Pty Ltd, 18 December 2006
*
* - Implements the following CAN interface functions
* - can_init
* - can_transmit
* - can_receive
*
*/
// Include files
#include <msp430x13x.h>
#include "tri63.h"
#include "can.h"
#include "spi.h"
// Public variables
can_variables can;
// Private variables
unsigned char buffer[16];
/**************************************************************************************************
* PUBLIC FUNCTIONS
*************************************************************************************************/
/*
* Initialises MCP2515 CAN controller
* - Resets MCP2515 via SPI port (switches to config mode, clears errors)
* - Changes CLKOUT to /4 rate (4 MHz)
* - Sets up bit timing
* - originally 1 Mbit operation
* - modified for 250 kbps operation (buffer[2] setting below)
* - Sets up receive filters and masks
* - Rx Filter 0 = Motor controller velocity
* - Rx Filter 1 = Unused
* - Rx Filter 2 = Driver controls packets (for remote frame requests)
* - Rx Filter 3 = Unused
* - Rx Filter 4 = Unused
* - Rx Filter 5 = Unused
* - Rx Mask 0 = Exact message must match (all 11 bits)
* - Rx Mask 1 = Block address must match (upper 6 bits)
* - Enables ERROR and RX interrupts on IRQ pin
* - Switches to normal (operating) mode
*/
void can_init( void )
{
// Set up reset and clocking
can_reset();
can_mod( CANCTRL, 0x03, 0x02 ); // CANCTRL register, modify lower 2 bits, CLK = /4
// Set up bit timing & interrupts
buffer[0] = 0x02; // CNF3 register: PHSEG2 = 3Tq, No wakeup, CLKOUT = CLK
buffer[1] = 0xC9; // CNF2 register: set PHSEG2 in CNF3, Triple sample, PHSEG1= 2Tq, PROP = 2Tq
// buffer[2] = 0x00; // CNF1 register: SJW = 1Tq, BRP = 0 > 1Mbps
buffer[2] = 0x03; // CNF1 register: SJW = 1Tq, BRP = 3 > 250 kbps
buffer[3] = 0x23; // CANINTE register: enable ERROR, RX0 & RX1 interrupts on IRQ pin
buffer[4] = 0x00; // CANINTF register: clear all IRQ flags
buffer[5] = 0x00; // EFLG register: clear all user-changable error flags
can_write( CNF3, &buffer[0], 6); // Write to registers
// Set up receive filtering & masks
// RXF0 - Buffer 0
buffer[ 0] = (unsigned char)((MC_CAN_BASE1 + MC_VELOCITY) >> 3);
buffer[ 1] = (unsigned char)((MC_CAN_BASE1 + MC_VELOCITY) << 5);
buffer[ 2] = 0x00;
buffer[ 3] = 0x00;
// RXF1 - Buffer 0
buffer[ 4] = (unsigned char)((BP_CAN_BASE + BP_PCDONE) >> 3);
buffer[ 5] = (unsigned char)((BP_CAN_BASE + BP_PCDONE) << 5);
buffer[ 6] = 0x00;
buffer[ 7] = 0x00;
// RXF2 - Buffer 1
buffer[ 8] = (unsigned char)(DC_CAN_BASE >> 3);
buffer[ 9] = (unsigned char)(DC_CAN_BASE << 5);
buffer[10] = 0x00;
buffer[11] = 0x00;
can_write( RXF0SIDH, &buffer[0], 12 );
// RXF3 - Buffer 1
buffer[ 0] = 0x00;
buffer[ 1] = 0x00;
buffer[ 2] = 0x00;
buffer[ 3] = 0x00;
// RXF4 - Buffer 1
buffer[ 4] = 0x00;
buffer[ 5] = 0x00;
buffer[ 6] = 0x00;
buffer[ 7] = 0x00;
// RXF5 - Buffer 1
buffer[ 8] = 0x00;
buffer[ 9] = 0x00;
buffer[10] = 0x00;
buffer[11] = 0x00;
can_write( RXF3SIDH, &buffer[0], 12 );
// RXM0 - Buffer 0
buffer[ 0] = 0xFB; // Match entire 11 bit ID (ID is left-justified in 32-bit mask register)
buffer[ 1] = 0xE0; // Match upper 5 and lower 5 bits
buffer[ 2] = 0x00;
buffer[ 3] = 0x00;
// RXM1 - Buffer 1
buffer[ 4] = 0xFC; // Match upper 6 bits of ID - don't care about lower 5 bits (block address)
buffer[ 5] = 0x00;
buffer[ 6] = 0x00;
buffer[ 7] = 0x00;
can_write( RXM0SIDH, &buffer[0], 8 );
// Switch out of config mode into normal operating mode
can_mod( CANCTRL, 0xE0, 0x00 ); // CANCTRL register, modify upper 3 bits, mode = Normal
}
/*
* Receives a CAN message from the MCP2515
* - Run this routine when an IRQ is received
* - Query the controller to identify the source of the IRQ
* - If it was an ERROR IRQ, read & clear the Error Flag register, and return it
* - If it was an RX IRQ, read the message and address, and return them
* - If both, handle the error preferentially to the message
* - Clear the appropriate IRQ flag bits
*/
void can_receive( void )
{
unsigned char flags;
// Read out the interrupt flags register
can_read( CANINTF, &flags, 1 );
// Check for errors
if(( flags & MCP_IRQ_ERR ) != 0x00 ){
// Read error flags and counters
can_read( EFLAG, &buffer[0], 1 );
can_read( TEC, &buffer[1], 2 );
// Clear error flags
can_mod( EFLAG, buffer[0], 0x00 ); // Modify (to '0') all bits that were set
// Return error code, a blank address field, and error registers in data field
can.status = CAN_ERROR;
can.address = 0x0000;
can.data.data_u8[0] = flags; // CANINTF
can.data.data_u8[1] = buffer[0]; // EFLG
can.data.data_u8[2] = buffer[1]; // TEC
can.data.data_u8[3] = buffer[2]; // REC
// Clear the IRQ flag
can_mod( CANINTF, MCP_IRQ_ERR, 0x00 );
}
// No error, check for received messages, buffer 0
else if(( flags & MCP_IRQ_RXB0 ) != 0x00 ){
// Read in the info, address & message data
can_read( RXB0CTRL, &buffer[0], 14 );
// Fill out return structure
// check for Remote Frame requests and indicate the status correctly
if(( buffer[0] & MCP_RXB0_RTR ) == 0x00 ){
// We've received a standard data packet
can.status = CAN_OK;
// Fill in the data
can.data.data_u8[0] = buffer[ 6];
can.data.data_u8[1] = buffer[ 7];
can.data.data_u8[2] = buffer[ 8];
can.data.data_u8[3] = buffer[ 9];
can.data.data_u8[4] = buffer[10];
can.data.data_u8[5] = buffer[11];
can.data.data_u8[6] = buffer[12];
can.data.data_u8[7] = buffer[13];
}
else{
// We've received a remote frame request
// Data is irrelevant with an RTR
can.status = CAN_RTR;
}
// Fill in the address
can.address = buffer[1];
can.address = can.address << 3;
buffer[2] = buffer[2] >> 5;
can.address = can.address | buffer[2];
// Clear the IRQ flag
can_mod( CANINTF, MCP_IRQ_RXB0, 0x00 );
}
// No error, check for received messages, buffer 1
else if(( flags & MCP_IRQ_RXB1 ) != 0x00 ){
// Read in the info, address & message data
can_read( RXB1CTRL, &buffer[0], 14 );
// Fill out return structure
// check for Remote Frame requests and indicate the status correctly
if(( buffer[0] & MCP_RXB1_RTR ) == 0x00 ){
// We've received a standard data packet
can.status = CAN_OK;
// Fill in the data
can.data.data_u8[0] = buffer[ 6];
can.data.data_u8[1] = buffer[ 7];
can.data.data_u8[2] = buffer[ 8];
can.data.data_u8[3] = buffer[ 9];
can.data.data_u8[4] = buffer[10];
can.data.data_u8[5] = buffer[11];
can.data.data_u8[6] = buffer[12];
can.data.data_u8[7] = buffer[13];
}
else{
// We've received a remote frame request
// Data is irrelevant with an RTR
can.status = CAN_RTR;
}
// Fill in the address
can.address = buffer[1];
can.address = can.address << 3;
buffer[2] = buffer[2] >> 5;
can.address = can.address | buffer[2];
// Clear the IRQ flag
can_mod( CANINTF, MCP_IRQ_RXB1, 0x00 );
}
else{
can.status = CAN_ERROR;
can.address = 0x0001;
can.data.data_u8[0] = flags; // CANINTF
}
}
/*
* Transmits a CAN message to the bus
* - Accepts address and data payload via can_interface structure
* - Busy waits while message is sent to CAN controller
* - Uses all available transmit buffers (3 available in CAN controller) to maximise throughput
* - Only modifies address information if it's different from what is already set up in CAN controller
* - Assumes constant 8-byte data length value
*/
void can_transmit( void )
{
static unsigned int buf_addr[3] = {0xFFFF, 0xFFFF, 0xFFFF};
// Fill data into buffer, it's used by any address
// Allow room at the start of the buffer for the address info if needed
buffer[ 5] = can.data.data_u8[0];
buffer[ 6] = can.data.data_u8[1];
buffer[ 7] = can.data.data_u8[2];
buffer[ 8] = can.data.data_u8[3];
buffer[ 9] = can.data.data_u8[4];
buffer[10] = can.data.data_u8[5];
buffer[11] = can.data.data_u8[6];
buffer[12] = can.data.data_u8[7];
// Check if the incoming address has already been configured in a mailbox
if( can.address == buf_addr[0] ){
// Mailbox 0 setup matches our new message
// Write to TX Buffer 0, start at data registers, and initiate transmission
can_write_tx( 0x01, &buffer[5] );
can_rts( 0 );
}
else if( can.address == buf_addr[1] ){
// Mailbox 1 setup matches our new message
// Write to TX Buffer 1, start at data registers, and initiate transmission
can_write_tx( 0x03, &buffer[5] );
can_rts( 1 );
}
else if( can.address == buf_addr[2] ){
// Mailbox 2 setup matches our new message
// Write to TX Buffer 2, start at data registers, and initiate transmission
can_write_tx( 0x05, &buffer[5] );
can_rts( 2 );
}
else{
// No matches in existing mailboxes
// No mailboxes already configured, so we'll need to load an address - set it up
buffer[0] = (unsigned char)(can.address >> 3);
buffer[1] = (unsigned char)(can.address << 5);
buffer[2] = 0x00; // EID8
buffer[3] = 0x00; // EID0
buffer[4] = 0x08; // DLC = 8 bytes
// Check if we've got any un-setup mailboxes free and use them
// Otherwise, find a non-busy mailbox and set it up with our new address
if( buf_addr[0] == 0xFFFF ){ // Mailbox 0 is free
// Write to TX Buffer 0, start at address registers, and initiate transmission
can_write_tx( 0x00, &buffer[0] );
can_rts( 0 );
buf_addr[0] = can.address;
}
else if( buf_addr[1] == 0xFFFF ){ // Mailbox 1 is free
// Write to TX Buffer 1, start at address registers, and initiate transmission
can_write_tx( 0x02, &buffer[0] );
can_rts( 1 );
buf_addr[1] = can.address;
}
else if( buf_addr[2] == 0xFFFF ){ // Mailbox 2 is free
// Write to TX Buffer 2, start at address registers, and initiate transmission
can_write_tx( 0x04, &buffer[0] );
can_rts( 2 );
buf_addr[2] = can.address;
}
else {
// No mailboxes free, wait until at least one is not busy
while(( can_read_status() & 0x54 ) == 0x54);
// Is it mailbox 0?
if(( can_read_status() & 0x04 ) == 0x00) {
// Setup mailbox 0 and send the message
can_write_tx( 0x00, &buffer[0] );
can_rts( 0 );
buf_addr[0] = can.address;
}
// Is it mailbox 1?
else if(( can_read_status() & 0x10 ) == 0x00) {
// Setup mailbox 1 and send the message
can_write_tx( 0x02, &buffer[0] );
can_rts( 1 );
buf_addr[1] = can.address;
}
// Is it mailbox 2?
else if(( can_read_status() & 0x40 ) == 0x00) {
// Setup mailbox 2 and send the message
can_write_tx( 0x04, &buffer[0] );
can_rts( 2 );
buf_addr[2] = can.address;
}
}
}
}
/**************************************************************************************************
* PRIVATE FUNCTIONS
*************************************************************************************************/
/*
* Resets MCP2515 CAN controller via SPI port
* - SPI port must be already initialised
*/
void can_reset( void )
{
can_select;
spi_transmit( MCP_RESET );
can_deselect;
}
/*
* Reads data bytes from the MCP2515
* - Pass in starting address, pointer to array of bytes for return data, and number of bytes to read
*/
void can_read( unsigned char address, unsigned char *ptr, unsigned char bytes )
{
unsigned char i;
can_select;
spi_transmit( MCP_READ );
spi_transmit( address );
for( i = 0; i < bytes; i++ ) *ptr++ = spi_exchange( 0x00 );
can_deselect;
}
/*
* Reads data bytes from receive buffers
* - Pass in buffer number and start position as defined in MCP2515 datasheet
* - For starting at data, returns 8 bytes
* - For starting at address, returns 13 bytes
*/
void can_read_rx( unsigned char address, unsigned char *ptr )
{
unsigned char i;
address &= 0x03; // Force upper bits of address to be zero (they're invalid)
address <<= 1; // Shift input bits to correct location in command byte
address |= MCP_READ_RX; // Construct command byte for MCP2515
can_select;
spi_transmit( address );
if(( address & 0x02 ) == 0x00 ){ // Start at address registers
for( i = 0; i < 13; i++ ){
*ptr++ = spi_exchange( 0x00 );
}
}
else{ // Start at data registers
for( i = 0; i < 8; i++ ){
*ptr++ = spi_exchange( 0x00 );
}
}
can_deselect;
}
/*
* Writes data bytes to the MCP2515
* - Pass in starting address, pointer to array of bytes, and number of bytes to write
*/
void can_write( unsigned char address, unsigned char *ptr, unsigned char bytes )
{
unsigned char i;
can_select;
spi_transmit( MCP_WRITE );
spi_transmit( address );
for( i = 0; i < (bytes-1); i++ ){
spi_transmit( *ptr++ );
}
spi_transmit( *ptr );
can_deselect;
}
/*
* Writes data bytes to transmit buffers
* - Pass in buffer number and start position as defined in MCP2515 datasheet
* - For starting at data, accepts 8 bytes
* - For starting at address, accepts 13 bytes
*/
void can_write_tx( unsigned char address, unsigned char *ptr )
{
unsigned char i;
address &= 0x07; // Force upper bits of address to be zero (they're invalid)
address |= MCP_WRITE_TX; // Construct command byte for MCP2515
can_select;
spi_transmit( address );
if(( address & 0x01 ) == 0x00 ){ // Start at address registers
for( i = 0; i < 13; i++ ){
spi_transmit( *ptr++ );
}
}
else{ // Start at data registers
for( i = 0; i < 8; i++ ){
spi_transmit( *ptr++ );
}
}
can_deselect;
}
/*
* Request to send selected transmit buffer
* - Pass in address of buffer to transmit: 0, 1 or 2
*/
void can_rts( unsigned char address )
{
unsigned char i;
// Set up address bits in command byte
i = MCP_RTS;
if( address == 0 ) i |= 0x01;
else if( address == 1 ) i |= 0x02;
else if( address == 2 ) i |= 0x04;
// Write command
can_select;
spi_transmit( i );
can_deselect;
}
/*
* Reads MCP2515 status register
*/
unsigned char can_read_status( void )
{
unsigned char status;
can_select;
spi_transmit( MCP_STATUS );
status = spi_exchange( 0x00 );
can_deselect;
return status;
}
/*
* Reads MCP2515 RX status (filter match) register
*/
unsigned char can_read_filter( void )
{
unsigned char status;
can_select;
spi_transmit( MCP_FILTER );
status = spi_exchange( 0x00 );
can_deselect;
return status;
}
/*
* Modifies selected register in MCP2515
* - Pass in register to be modified, bit mask, and bit data
*/
void can_mod( unsigned char address, unsigned char mask, unsigned char data )
{
can_select;
spi_transmit( MCP_MODIFY );
spi_transmit( address );
spi_transmit( mask );
spi_transmit( data );
can_deselect;
}