-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcan.c
453 lines (387 loc) · 14.6 KB
/
can.c
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
/////////////////////////////////////////////////////////////////////////
//// can-18xxx8.c ////
//// CAN Library routines for Microchip's PIC18Cxx8 and 18Fxx8 line ////
//// ////
//// This library provides the following functions: ////
//// (for more information on these functions see the comment ////
//// header above each function) ////
//// ////
//// can_init - Configures the PIC18xxx8 CAN peripheral ////
//// ////
//// can_set_baud - Sets the baud rate control registers ////
//// ////
//// can_set_mode - Sets the CAN module into a specific mode ////
//// ////
//// can_set_id - Sets the standard and extended ID ////
//// ////
//// can_get_id - Gets the standard and extended ID ////
//// ////
//// can_putd - Sends a message/request with specified ID ////
//// ////
//// can_getd - Returns specifid message/request and ID ////
//// ////
//// can_kbhit - Returns true if there is data in one of the ////
//// receive buffers ////
//// ////
//// can_tbe - Returns true if the transmit buffer is ready to ////
//// send more data ////
//// ////
//// can_abort - Aborts all pending transmissions ////
//// ////
//// PIN_B3 is CANRX, and PIN_B2 is CANTX. You will need a CAN ////
//// transeiver to connect these pins to CANH and CANL bus lines. ////
//// ////
//// CCS provides an example, ex_can.c, which shows how to use this ////
//// library. ////
//// ////
/////////////////////////////////////////////////////////////////////////
//// ////
//// Version History ////
//// ////
//// Jul 27 04 - can_init() uses CAN_USE_EXTENDED_ID instead of ////
//// setting all RX filters to extended. ////
//// ////
//// Feb 24 04 - can_get_id() fixed for EID<18:20>. ////
//// ////
//// Mar 30 09 - added CANTX2 code for PIC18F6585/8585/6680/8680 ////
//// ////
//// May 18 10 - changed variable types to unsigned incase compiled ////
//// with #device ANSI, which makes variables signed ////
//// ////
//// Apr 20 11 - updated for new PIC18FxxK80 chips ////
//// ////
/////////////////////////////////////////////////////////////////////////
//// (C) Copyright 1996,2011 Custom Computer Services ////
//// This source code may only be used by licensed users of the CCS ////
//// C compiler. This source code may only be distributed to other ////
//// licensed users of the CCS C compiler. No other use, ////
//// reproduction or distribution is permitted without written ////
//// permission. Derivative programs created using this software ////
//// in object code form are not restricted in any way. ////
/////////////////////////////////////////////////////////////////////////
#include "can.h"
#if CAN_DO_DEBUG
#define can_debug printf
#else
#define can_debug
#endif
//macros
#define can_kbhit() (RXB0CON.rxful || RXB1CON.rxful)
#define can_tbe() (!TXB0CON.txreq || !TXB1CON.txreq || !TXB2CON.txreq)
#define can_abort() (CANCON.abat=1)
////////////////////////////////////////////////////////////////////////
//
// can_init()
//
// Initializes PIC18xxx8 CAN peripheral. Sets the RX filter and masks so the
// CAN peripheral will receive all incoming IDs. Configures both RX buffers
// to only accept valid valid messages (as opposed to all messages, or all
// extended message, or all standard messages). Also sets the tri-state
// setting of B2 to output, and B3 to input (apparently the CAN peripheral
// doesn't keep track of this)
//
// The constants (CAN_USE_RX_DOUBLE_BUFFER, CAN_ENABLE_DRIVE_HIGH,
// CAN_ENABLE_CAN_CAPTURE) are given a default define in the can-18xxx8.h file.
// These default values can be overwritten in the main code, but most
// applications will be fine with these defaults.
//
//////////////////////////////////////////////////////////////////////////////
void can_init(void) {
can_set_mode(CAN_OP_CONFIG); //must be in config mode before params can be set
can_set_baud();
RXB0CON=0;
RXB0CON.rxm=CAN_RX_VALID;
RXB0CON.rxb0dben=CAN_USE_RX_DOUBLE_BUFFER;
RXB1CON=RXB0CON;
CIOCON.endrhi=CAN_ENABLE_DRIVE_HIGH;
CIOCON.cancap=CAN_ENABLE_CAN_CAPTURE;
CIOCON.tx2src=CAN_CANTX2_SOURCE; //added 3/30/09 for PIC18F6585/8585/6680/8680
CIOCON.tx2en=CAN_ENABLE_CANTX2; //added 3/30/09 for PIC18F6585/8585/6680/8680
can_set_id(RX0MASK, CAN_MASK_ACCEPT_ALL, CAN_USE_EXTENDED_ID); //set mask 0
can_set_id(RX0FILTER0, 0, CAN_USE_EXTENDED_ID); //set filter 0 of mask 0
can_set_id(RX0FILTER1, 0, CAN_USE_EXTENDED_ID); //set filter 1 of mask 0
can_set_id(RX1MASK, CAN_MASK_ACCEPT_ALL, CAN_USE_EXTENDED_ID); //set mask 1
can_set_id(RX1FILTER2, 0, CAN_USE_EXTENDED_ID); //set filter 0 of mask 1
can_set_id(RX1FILTER3, 0, CAN_USE_EXTENDED_ID); //set filter 1 of mask 1
can_set_id(RX1FILTER4, 0, CAN_USE_EXTENDED_ID); //set filter 2 of mask 1
can_set_id(RX1FILTER5, 0, CAN_USE_EXTENDED_ID); //set filter 3 of mask 1
set_tris_b((*0xF93 & 0xFB ) | 0x08); //b3 is out, b2 is in
can_set_mode(CAN_OP_NORMAL);
}
////////////////////////////////////////////////////////////////////////
//
// can_set_baud()
//
// Configures the baud rate control registers. All the defines here
// are defaulted in the can-18xxx8.h file. These defaults can, and
// probably should, be overwritten in the main code.
//
// Current defaults are set to work with Microchip's MCP250xxx CAN
// Developers Kit if this PIC is running at 20Mhz.
//
////////////////////////////////////////////////////////////////////////
void can_set_baud(void) {
/* 250 kbps from 8 MHz oscillator */
BRGCON1=0x00;
BRGCON2=0xB8;
BRGCON3=0x05;
#if 0
BRGCON1.brp=CAN_BRG_PRESCALAR;
BRGCON1.sjw=CAN_BRG_SYNCH_JUMP_WIDTH;
BRGCON2.prseg=CAN_BRG_PROPAGATION_TIME;
BRGCON2.seg1ph=CAN_BRG_PHASE_SEGMENT_1;
BRGCON2.sam=CAN_BRG_SAM;
BRGCON2.seg2phts=CAN_BRG_SEG_2_PHASE_TS;
BRGCON3.seg2ph=CAN_BRG_PHASE_SEGMENT_2;
BRGCON3.wakfil=CAN_BRG_WAKE_FILTER;
#endif
}
void can_set_mode(CAN_OP_MODE mode) {
CANCON.reqop=mode;
while( (CANSTAT.opmode) != mode );
}
////////////////////////////////////////////////////////////////////////
//
// can_set_id()
//
// Configures the xxxxEIDL, xxxxEIDH, xxxxSIDL and xxxxSIDH registers to
// configure the defined buffer to use the specified ID
//
// Paramaters:
// addr - pointer to first byte of ID register, starting with xxxxEIDL.
// For example, a pointer to RXM1EIDL
// id - ID to set buffer to
// ext - Set to TRUE if this buffer uses an extended ID, FALSE if not
//
////////////////////////////////////////////////////////////////////////
void can_set_id(unsigned int* addr, unsigned int32 id, int1 ext) {
int *ptr;
ptr=addr;
if (ext) { //extended
//eidl
*ptr=make8(id,0); //0:7
//eidh
ptr--;
*ptr=make8(id,1); //8:15
//sidl
ptr--;
*ptr=make8(id,2) & 0x03; //16:17
*ptr|=(make8(id,2) << 3) & 0xE0; //18:20
*ptr|=0x08;
//sidh
ptr--;
*ptr=((make8(id,2) >> 5) & 0x07 ); //21:23
*ptr|=((make8(id,3) << 3) & 0xF8);//24:28
}
else { //standard
//eidl
*ptr=0;
//eidh
ptr--;
*ptr=0;
//sidl
ptr--;
*ptr=(make8(id,0) << 5) & 0xE0;
//sidh
ptr--;
*ptr=(make8(id,0) >> 3) & 0x1F;
*ptr|=(make8(id,1) << 5) & 0xE0;
}
}
////////////////////////////////////////////////////////////////////////
//
// can_get_id()
//
// Returns the ID of the specified buffer. (The opposite of can_set_id())
// This is used after receiving a message, to see which ID sent the message.
//
// Paramaters:
// addr - pointer to first byte of ID register, starting with xxxxEIDL.
// For example, a pointer to RXM1EIDL
// ext - Set to TRUE if this buffer uses an extended ID, FALSE if not
//
// Returns:
// The ID of the buffer
//
////////////////////////////////////////////////////////////////////////
unsigned int32 can_get_id(unsigned int * addr, int1 ext) {
unsigned int32 ret;
unsigned int * ptr;
ret=0;
ptr=addr;
if (ext) {
ret=*ptr; //eidl
ptr--; //eidh
ret|=((unsigned int32)*ptr << 8);
ptr--; //sidl
ret|=((unsigned int32)*ptr & 0x03) << 16;
ret|=((unsigned int32)*ptr & 0xE0) << 13;
ptr--; //sidh
ret|=((unsigned int32)*ptr << 21);
}
else {
ptr-=2; //sidl
ret=((unsigned int32)*ptr & 0xE0) >> 5;
ptr--; //sidh
ret|=((unsigned int32)*ptr << 3);
}
return(ret);
}
////////////////////////////////////////////////////////////////////////
//
// can_putd()
//
// Puts data on a transmit buffer, at which time the CAN peripheral will
// send when the CAN bus becomes available.
//
// Paramaters:
// id - ID to transmit data as
// data - pointer to data to send
// len - length of data to send
// priority - priority of message. The higher the number, the
// sooner the CAN peripheral will send the message.
// Numbers 0 through 3 are valid.
// ext - TRUE to use an extended ID, FALSE if not
// rtr - TRUE to set the RTR (request) bit in the ID, false if NOT
//
// Returns:
// If successful, it will return TRUE
// If un-successful, will return FALSE
//
////////////////////////////////////////////////////////////////////////
int1 can_putd(unsigned int32 id, unsigned int * data, unsigned int len, unsigned int priority, int1 ext, int1 rtr) {
unsigned int i;
unsigned int * txd0;
unsigned int port;
txd0=&TXRXBaD0;
// find emtpy transmitter
//map access bank addresses to empty transmitter
if (!TXB0CON.txreq) {
CANCON.win=CAN_WIN_TX0;
port=0;
}
else if (!TXB1CON.txreq) {
CANCON.win=CAN_WIN_TX1;
port=1;
}
else if (!TXB2CON.txreq) {
CANCON.win=CAN_WIN_TX2;
port=2;
}
else {
#if CAN_DO_DEBUG
can_debug("\r\nCAN_PUTD() FAIL: NO OPEN TX BUFFERS\r\n");
#endif
return(0);
}
//set priority.
TXBaCON.txpri=priority;
//set tx mask
can_set_id(TXRXBaID, id, ext);
//set tx data count
TXBaDLC=len;
TXBaDLC.rtr=rtr;
for (i=0; i<len; i++) {
*txd0=*data;
txd0++;
data++;
}
//enable transmission
TXBaCON.txreq=1;
CANCON.win=CAN_WIN_RX0;
#if CAN_DO_DEBUG
can_debug("\r\nCAN_PUTD(): BUFF=%U ID=%LX LEN=%U PRI=%U EXT=%U RTR=%U\r\n", port, id, len, priority, ext, rtr);
if ((len)&&(!rtr)) {
data-=len;
can_debug(" DATA = ");
for (i=0;i<len;i++) {
can_debug("%X ",*data);
data++;
}
can_debug("\r\n");
}
#endif
return(1);
}
////////////////////////////////////////////////////////////////////////
//
// can_getd()
//
// Gets data from a receive buffer, if the data exists
//
// Returns:
// id - ID who sent message
// data - pointer to array of data
// len - length of received data
// stat - structure holding some information (such as which buffer
// recieved it, ext or standard, etc)
//
// Returns:
// Function call returns a TRUE if there was data in a RX buffer, FALSE
// if there was none.
//
////////////////////////////////////////////////////////////////////////
int1 can_getd(unsigned int32 & id, unsigned int * data, unsigned int & len, struct rx_stat & stat)
{
unsigned int i;
unsigned int * ptr;
if (RXB0CON.rxful) {
CANCON.win=CAN_WIN_RX0;
stat.buffer=0;
CAN_INT_RXB0IF=0;
stat.err_ovfl=COMSTAT.rx0ovfl;
COMSTAT.rx0ovfl=0;
if (RXB0CON.rxb0dben) {
stat.filthit=RXB0CON.filthit0;
}
}
else if ( RXB1CON.rxful )
{
CANCON.win=CAN_WIN_RX1;
stat.buffer=1;
CAN_INT_RXB1IF=0;
stat.err_ovfl=COMSTAT.rx1ovfl;
COMSTAT.rx1ovfl=0;
stat.filthit=RXB1CON.filthit;
}
else {
#if CAN_DO_DEBUG
can_debug("\r\nFAIL ON CAN_GETD(): NO MESSAGE IN BUFFER\r\n");
#endif
return (0);
}
len = RXBaDLC.dlc;
stat.rtr=RXBaDLC.rtr;
stat.ext=TXRXBaSIDL.ext;
id=can_get_id(TXRXBaID,stat.ext);
ptr = &TXRXBaD0;
for ( i = 0; i < len; i++ ) {
*data = *ptr;
data++;
ptr++;
}
// return to default addressing
CANCON.win=CAN_WIN_RX0;
stat.inv=CAN_INT_IRXIF;
CAN_INT_IRXIF = 0;
if (stat.buffer) {
RXB1CON.rxful=0;
}
else {
RXB0CON.rxful=0;
}
#if CAN_DO_DEBUG
can_debug("\r\nCAN_GETD(): BUFF=%U ID=%LX LEN=%U OVF=%U ", stat.buffer, id, len, stat.err_ovfl);
can_debug("FILT=%U RTR=%U EXT=%U INV=%U", stat.filthit, stat.rtr, stat.ext, stat.inv);
if ((len)&&(!stat.rtr)) {
data-=len;
can_debug("\r\n DATA = ");
for (i=0;i<len;i++) {
can_debug("%X ",*data);
data++;
}
}
can_debug("\r\n");
#endif
return(1);
}