LTlib LaurTec Library  4.0.0 Beta
Open Source C Library for Microchip Microcontrollers based on XC8 Compiler
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
module_CAN.c
Go to the documentation of this file.
1 /*******************************************************************************
2 
3 Autore : Mauro Laurenti
4 Versione : 1.2
5 
6 Created on Date : 4/11/2007
7 Last update : 15/01/2016
8 
9 CopyRight 2006-2015 all rights are reserved
10 
11 ********************************************************
12 SOFTWARE LICENSE AGREEMENT
13 ********************************************************
14 
15 The usage of the supplied software imply the acceptance of the following license.
16 
17 The software supplied herewith by Mauro Laurenti (the Author) is intended for
18 use solely and exclusively on Microchip PIC Microcontroller (registered mark).
19 The software is owned by the Author, and is protected under applicable
20 copyright laws. All rights are reserved.
21 Any use in violation of the foregoing restrictions may subject the
22 user to criminal sanctions under applicable laws, as well as to civil liability
23 for the breach of the terms and conditions of this license.
24 Commercial use is forbidden without a written acknowledgement with the Author.
25 Personal or educational use is allowed if the application containing the
26 following software doesn't aim to commercial use or monetary earning of any kind.
27 
28 THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTIES,
29 WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
30 TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
31 PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE AUTHOR SHALL NOT,
32 IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
33 CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
34 
35 ********************************************************
36 PURPOSES
37 ********************************************************
38 
39 This library is supposed to be used for CAN bus
40 applications. All the basic functions are included.
41 Standard and Extended messages are supported
42 
43 *******************************************************************************/
44 
45 #include "module_CAN.h"
46 
47 
48 
49 //*********************************************
50 // Initialize the CAN bus
51 //*********************************************
52 
53 void CAN_open (BYTE propSeg, BYTE phaseSeg1, BYTE phaseSeg2,BYTE SJW, BYTE BRP, enum CAN_CONFIG_FLAGS flags) {
54 
55  BYTE FilterConfig1;
56  BYTE FilterConfig2;
57 
58  //I/O pin configuraton
59 
60  CAN1_TX_LINE_TRIS = 0x00;
61  CAN1_RX_LINE_TRIS = 0x01;
62 
63  //set configuration mode
65 
66  //cleaning the value
67  BRGCON1 = 0x00;
68  BRGCON2 = 0x00;
69  BRGCON3 = 0x00;
70 
71  SJW= (SJW -1) << 6;
72  BRGCON1 |= SJW;
73  BRGCON1 |= BRP;
74 
75  BRGCON2 |= propSeg -1;
76  phaseSeg1 = (phaseSeg1 -1) <<3;
77  BRGCON2 |= phaseSeg1;
78 
79  //SAM
80  if ( !(flags & CAN_CONFIG_SAMPLE_BIT) )
81  BRGCON2bits.SAM= 1;
82 
83  if ( flags & CAN_CONFIG_PHSEG2_PRG_BIT )
84  BRGCON2bits.SEG2PHTS = 1;
85 
86  BRGCON3 |= phaseSeg2 -1;
87 
88  if ( flags & CAN_CONFIG_LINE_FILTER_BIT )
89  BRGCON3bits.WAKFIL = 1;
90 
91 
92  //Receiver Settings
93  RXB0CON = flags & CAN_CONFIG_MSG_BITS;
95  RXB0CONbits.RXB0DBEN = 1;
96 
97  RXB1CON = RXB0CON;
98 
99  //Setting the mask to receive all the messages
102 
103 
104  switch( (flags & CAN_CONFIG_MSG_BITS) | ~CAN_CONFIG_MSG_BITS ) {
105 
107  FilterConfig1 = CAN_CONFIG_XTD_MSG;
108  FilterConfig2 = CAN_CONFIG_XTD_MSG;
109  break;
110 
112  FilterConfig1 = CAN_CONFIG_STD_MSG;
113  FilterConfig2 = CAN_CONFIG_STD_MSG;
114  break;
115  default:
116  //Buffer 1 will receive the Standard messages
117  FilterConfig1 = CAN_CONFIG_STD_MSG;
118  //Buffer 2 will receive the Extended messages
119  FilterConfig2 = CAN_CONFIG_XTD_MSG;
120  break;
121  }
122 
123  CAN_set_filter (CAN_FILTER_B1_F1, 0, FilterConfig1);
124  CAN_set_filter (CAN_FILTER_B1_F2, 0, FilterConfig1);
125  CAN_set_filter (CAN_FILTER_B2_F1, 0, FilterConfig2);
126  CAN_set_filter (CAN_FILTER_B2_F2, 0, FilterConfig2);
127  CAN_set_filter (CAN_FILTER_B2_F3, 0, FilterConfig2);
128  CAN_set_filter (CAN_FILTER_B2_F4, 0, FilterConfig2);
129 
130  //set normal mode
132 }
133 
134 
135 //*********************************************
136 // CAN_close
137 //*********************************************
138 void CAN_close (void){
139  //set SLEEP mode
141 }
142 
143 
144 //*********************************************
145 // Send a message
146 //*********************************************
147 void CAN_write_message (unsigned long identifier, BYTE *data, BYTE dataLength, enum CAN_TX_MSG_FLAGS flags) {
148 
149  //used to compute the identifier
150  unsigned long identifier_math;
151 
152  if (TXB0CONbits.TXREQ == 0) {
153 
154  // set the data length
155  TXB0DLC = dataLength;
156 
157  //set the RTR bit
158  if (0b01000000 & flags)
159  TXB0DLCbits.TXRTR = 0x01;
160  else
161  TXB0DLCbits.TXRTR = 0x00;
162 
163  if (CAN_TX_FRAME_MASK & flags) {
164 
165  //EID0 - EID7 setting
166  identifier_math = identifier & 0x000000FF;
167  TXB0EIDL = (unsigned char) identifier_math;
168 
169  //EID8 - EID15 setting
170  identifier_math = identifier & 0x0000FF00;
171  identifier_math = identifier_math >> 8;
172  TXB0EIDH = (unsigned char) identifier_math;
173 
174  TXB0SIDL = 0x00;
175  TXB0SIDLbits.EXIDE = 0x01;
176 
177  if (identifier & 0x00010000)
178  TXB0SIDLbits.EID16 = 0x01;
179  if (identifier & 0x00020000)
180  TXB0SIDLbits.EID17 = 0x01;
181  if (identifier & 0x00040000)
182  TXB0SIDLbits.SID0 = 0x01;
183  if (identifier & 0x00080000)
184  TXB0SIDLbits.SID1 = 0x01;
185  if (identifier & 0x00100000)
186  TXB0SIDLbits.SID2 = 0x01;
187 
188  identifier_math = (identifier >> 21);
189  identifier_math = identifier_math & 0x000000FF;
190  TXB0SIDH = (unsigned char) identifier_math;
191  }
192 
193  else {
194 
195  //enable standard messages
196  TXB0SIDLbits.EXIDE = 0x00;
197 
198  identifier_math = (identifier >> 3);
199  identifier_math = identifier_math & 0x000000FF;
200  TXB0SIDH = (unsigned char) identifier_math;
201 
202  identifier_math = identifier & 0x00000007;
203  identifier_math = identifier_math << 5;
204  TXB0SIDL = TXB0SIDL & 0b00011111;
205  TXB0SIDL = (unsigned char) identifier_math;
206  }
207 
208  //set transmission priority
209  if (0b00000001 & flags)
210  TXB0CONbits.TXPRI0 = 0x01;
211  else
212  TXB0CONbits.TXPRI0 = 0x00;
213  if (0b00000010 & flags)
214  TXB0CONbits.TXPRI1 = 0x01;
215  else
216  TXB0CONbits.TXPRI1 = 0x00;
217 
218  TXB0D0 = data[0];
219  TXB0D1 = data[1];
220  TXB0D2 = data[2];
221  TXB0D3 = data[3];
222  TXB0D4 = data[4];
223  TXB0D5 = data[5];
224  TXB0D6 = data[6];
225  TXB0D7 = data[7];
226 
227  //enable trasmission
228  TXB0CONbits.TXREQ = 0x01;
229  return;
230  }
231 
232 
233  // Second TX Buffer setting
234  if (TXB1CONbits.TXREQ == 0) {
235 
236  // set the data length
237  TXB1DLC = dataLength;
238 
239  //set the RTR bit
240  if (0b01000000 & flags)
241  TXB1DLCbits.TXRTR = 0x01;
242  else
243  TXB1DLCbits.TXRTR = 0x00;
244 
245  if (CAN_TX_FRAME_MASK & flags) {
246 
247  //EID0 - EID7 setting
248  identifier_math = identifier & 0x000000FF;
249  TXB1EIDL = (unsigned char) identifier_math;
250 
251  //EID8 - EID15 setting
252  identifier_math = identifier & 0x0000FF00;
253  identifier_math = identifier_math >> 8;
254  TXB1EIDH = (unsigned char) identifier_math;
255 
256  TXB1SIDL = 0x00;
257  TXB1SIDLbits.EXIDE = 0x01;
258 
259  if (identifier & 0x00010000)
260  TXB1SIDLbits.EID16 = 0x01;
261  if (identifier & 0x00020000)
262  TXB1SIDLbits.EID17 = 0x01;
263  if (identifier & 0x00040000)
264  TXB1SIDLbits.SID0 = 0x01;
265  if (identifier & 0x00080000)
266  TXB1SIDLbits.SID1 = 0x01;
267  if (identifier & 0x00100000)
268  TXB1SIDLbits.SID2 = 0x01;
269 
270  identifier_math = (identifier >> 21);
271  identifier_math = identifier_math & 0x000000FF;
272  TXB1SIDH = (unsigned char) identifier_math;
273  }
274 
275  else {
276  //enable standard messages
277  TXB1SIDLbits.EXIDE = 0x00;
278 
279  identifier_math = (identifier >> 3);
280  identifier_math = identifier_math & 0x000000FF;
281  TXB1SIDH = (unsigned char) identifier_math;
282 
283  identifier_math = identifier & 0x00000007;
284  identifier_math = identifier_math << 5;
285  TXB1SIDL = TXB1SIDL & 0b00011111;
286  TXB1SIDL = (unsigned char) identifier_math;
287  }
288 
289  //set transmission priority
290  if (0b00000001 & flags)
291  TXB1CONbits.TXPRI0 = 0x01;
292  else
293  TXB1CONbits.TXPRI0 = 0x00;
294  if (0b00000010 & flags)
295  TXB1CONbits.TXPRI1 = 0x01;
296  else
297  TXB1CONbits.TXPRI1 = 0x00;
298 
299  TXB1D0 = data[0];
300  TXB1D1 = data[1];
301  TXB1D2 = data[2];
302  TXB1D3 = data[3];
303  TXB1D4 = data[4];
304  TXB1D5 = data[5];
305  TXB1D6 = data[6];
306  TXB1D7 = data[7];
307 
308  //enable trasmission
309  TXB1CONbits.TXREQ = 0x01;
310 
311  return;
312  }
313 
314 
315  // Third TX Buffer setting
316  if (TXB2CONbits.TXREQ == 0) {
317 
318  // set the data length
319  TXB2DLC = dataLength;
320 
321  //set the RTR bit
322  if (0b01000000 & flags)
323  TXB2DLCbits.TXRTR = 0x01;
324  else
325  TXB2DLCbits.TXRTR = 0x00;
326 
327  if (CAN_TX_FRAME_MASK & flags) {
328 
329  //EID0 - EID7 setting
330  identifier_math = identifier & 0x000000FF;
331  TXB2EIDL = (unsigned char) identifier_math;
332 
333  //EID8 - EID15 setting
334  identifier_math = identifier & 0x0000FF00;
335  identifier_math = identifier_math >> 8;
336  TXB2EIDH = (unsigned char) identifier_math;
337 
338  TXB2SIDL = 0x00;
339  TXB2SIDLbits.EXIDE = 0x01;
340 
341  if (identifier & 0x00010000)
342  TXB2SIDLbits.EID16 = 0x01;
343  if (identifier & 0x00020000)
344  TXB2SIDLbits.EID17 = 0x01;
345  if (identifier & 0x00040000)
346  TXB2SIDLbits.SID0 = 0x01;
347  if (identifier & 0x00080000)
348  TXB2SIDLbits.SID1 = 0x01;
349  if (identifier & 0x00100000)
350  TXB2SIDLbits.SID2 = 0x01;
351 
352  identifier_math = (identifier >> 21);
353  identifier_math = identifier_math & 0x000000FF;
354  TXB2SIDH = (unsigned char) identifier_math;
355  }
356 
357  else {
358 
359  //enable standard messages
360  TXB2SIDLbits.EXIDE = 0x00;
361 
362  identifier_math = (identifier >> 3);
363  identifier_math = identifier_math & 0x000000FF;
364  TXB2SIDH = (unsigned char) identifier_math;
365 
366  identifier_math = identifier & 0x00000007;
367  identifier_math = identifier_math << 5;
368  TXB2SIDL = TXB2SIDL & 0b00011111;
369  TXB2SIDL = (unsigned char) identifier_math;
370  }
371 
372  //set transmission priority
373  if (0b00000001 & flags)
374  TXB2CONbits.TXPRI0 = 0x01;
375  else
376  TXB2CONbits.TXPRI0 = 0x00;
377  if (0b00000010 & flags)
378  TXB2CONbits.TXPRI1 = 0x01;
379  else
380  TXB2CONbits.TXPRI1 = 0x00;
381 
382  TXB2D0 = data[0];
383  TXB2D1 = data[1];
384  TXB2D2 = data[2];
385  TXB2D3 = data[3];
386  TXB2D4 = data[4];
387  TXB2D5 = data[5];
388  TXB2D6 = data[6];
389  TXB2D7 = data[7];
390 
391  //enable transmission
392  TXB2CONbits.TXREQ = 0x01;
393 
394  return;
395  }
396 }
397 
398 //*********************************************
399 // Read the message from the RX buffer
400 //*********************************************
401 
402 unsigned char CAN_read_message (CANmessage *msg) {
403 
404  BYTE tamp;
405  unsigned char error = 0;
406 
407  //check for buffers overflows
408  if (COMSTATbits.RXB0OVFL == 0x01)
409  error |= CAN_RX_BUFFER_1_OVFL;
410 
411  if (COMSTATbits.RXB1OVFL == 0x01)
412  error |= CAN_RX_BUFFER_2_OVFL;
413 
414  //RX Buffer 1 is read
415  if (RXB1CONbits.RXFUL ==0x01) {
416 
417 
418  msg->identifier = 0;
419 
420  //retrieve the data
421  msg->data[0] =RXB1D0;
422  msg->data[1] =RXB1D1;
423  msg->data[2] =RXB1D2;
424  msg->data[3] =RXB1D3;
425  msg->data[4] =RXB1D4;
426  msg->data[5] =RXB1D5;
427  msg->data[6] =RXB1D6;
428  msg->data[7] =RXB1D7;
429 
430  //retrieve the RTR bit
431  msg->RTR = RXB1DLCbits.RXRTR;
432 
433  //retrieve the length
434  msg->length = RXB1DLC & 0x0F;
435 
436  //retrieve the format (standard or extended)
437  msg->type = RXB1SIDLbits.EXID;
438 
439  //reading the identifier standard format
440  if (RXB1SIDLbits.EXID == 0) {
441  msg->identifier = ((unsigned long)RXB1SIDH)<< 3;
442  tamp = (RXB1SIDL >> 5 ) & 0x07;
443  msg->identifier = msg->identifier + tamp;
444  }
445 
446  // reading the identifier extended format
447  else {
448 
449  //retrieve EID0-EID7
450  msg->identifier = (unsigned long) RXB1EIDL;
451  //retrieve EID8-EID15
452  msg->identifier += ((unsigned long) RXB1EIDH) << 8;
453 
454  if (RXB1SIDLbits.EID16)
455  msg->identifier |= 0x00010000;
456  if (RXB1SIDLbits.EID17)
457  msg->identifier |= 0x00020000;
458  if (RXB1SIDLbits.SID0)
459  msg->identifier |= 0x00040000;
460  if (RXB1SIDLbits.SID1)
461  msg->identifier |= 0x00080000;
462  if (RXB1SIDLbits.SID2)
463  msg->identifier |= 0x00100000;
464 
465  msg->identifier |= ((unsigned long) RXB1SIDH) << 21;
466  }
467 
468  //release the RX buffer for new messages
469  RXB1CONbits.RXFUL = 0x00;
470  return (error);
471  }
472 
473  //RX Buffer 0 is read
474  if (RXB0CONbits.RXFUL ==0x01) {
475 
476  msg->identifier = 0;
477 
478  //retrieve the data
479  msg->data[0] =RXB0D0;
480  msg->data[1] =RXB0D1;
481  msg->data[2] =RXB0D2;
482  msg->data[3] =RXB0D3;
483  msg->data[4] =RXB0D4;
484  msg->data[5] =RXB0D5;
485  msg->data[6] =RXB0D6;
486  msg->data[7] =RXB0D7;
487 
488  //retrieve the RTR bit
489  msg->RTR = RXB0DLCbits.RXRTR;
490 
491  //retrieve the length
492  msg->length = RXB0DLC & 0x0F;
493 
494  //retrieve the format (standard or extended)
495  msg->type = RXB0SIDLbits.EXID;
496 
497  //reading the identifier standard format
498  if (RXB0SIDLbits.EXID == 0)
499  {
500  msg->identifier = ((unsigned long)RXB0SIDH)<< 3;
501  tamp = (RXB0SIDL >> 5 ) & 0x07;
502  msg->identifier = msg->identifier + tamp;
503  }
504  else {
505 
506  // reading the identifier extended format
507  //retrieve EID0-EID7
508  msg->identifier = (unsigned long) RXB0EIDL;
509  //retrieve EID8-EID15
510  msg->identifier += ((unsigned long) RXB0EIDH) << 8;
511 
512  if (RXB0SIDLbits.EID16)
513  msg->identifier |= 0x00010000;
514  if (RXB0SIDLbits.EID17)
515  msg->identifier |= 0x00020000;
516  if (RXB0SIDLbits.SID0)
517  msg->identifier |= 0x00040000;
518  if (RXB0SIDLbits.SID1)
519  msg->identifier |= 0x00080000;
520  if (RXB0SIDLbits.SID2)
521  msg->identifier |= 0x00100000;
522 
523  msg->identifier |= ((unsigned long) RXB0SIDH) << 21;
524 
525  }
526 
527  //release the RX buffer for new messages
528  RXB0CONbits.RXFUL = 0x00;
529 
530  return (error);
531  }
532  return (error);
533 }
534 
535 //*********************************************
536 // Set the RX MASK
537 //*********************************************
538 
539 void CAN_set_mask (enum CAN_MASK mask_number, unsigned long mask, enum CAN_CONFIG_FLAGS type) {
540 
541 //used to compute the identifier
542 unsigned long identifier_math;
543 
544  //Standard MASK RX BUFFER 1
545  if ((mask_number == CAN_MASK_B1) && (type == CAN_CONFIG_STD_MSG)) {
546  identifier_math = (mask >> 3);
547  identifier_math = identifier_math & 0x000000FF;
548  RXM0SIDH = (unsigned char) identifier_math;
549 
550  identifier_math = mask & 0x00000007;
551  identifier_math = identifier_math << 5;
552  RXM0SIDL = (unsigned char) identifier_math;
553  }
554 
555  //Standard MASK RX BUFFER2
556  if ((mask_number == CAN_MASK_B2) && (type == CAN_CONFIG_STD_MSG)) {
557  identifier_math = (mask >> 3);
558  identifier_math = identifier_math & 0x000000FF;
559  RXM1SIDH = (unsigned char) identifier_math;
560 
561  identifier_math = mask & 0x00000007;
562  identifier_math = identifier_math << 5;
563  RXM1SIDL = (unsigned char) identifier_math;
564  }
565 
566  //Extended MASK RX BUFFER 1
567  if ((mask_number == CAN_MASK_B1) && (type == CAN_CONFIG_XTD_MSG)) {
568  //EID0 - EID7 setting
569  identifier_math = mask & 0x000000FF;
570  RXM0EIDL = (unsigned char) identifier_math;
571 
572  //EID8 - EID15 setting
573  identifier_math = mask & 0x0000FF00;
574  identifier_math = identifier_math >> 8;
575  RXM0EIDH = (unsigned char) identifier_math;
576 
577  if (mask & 0x00010000)
578  RXM0SIDLbits.EID16 = 0x01;
579  else
580  RXM0SIDLbits.EID16 = 0x00;
581 
582  if (mask & 0x00020000)
583  RXM0SIDLbits.EID17 = 0x01;
584  else
585  RXM0SIDLbits.EID17 = 0x00;
586 
587  if (mask & 0x00040000)
588  RXM0SIDLbits.SID0 = 0x01;
589  else
590  RXM0SIDLbits.SID0 = 0x00;
591 
592  if (mask & 0x00080000)
593  RXM0SIDLbits.SID1 = 0x01;
594  else
595  RXM0SIDLbits.SID1 = 0x00;
596 
597  if (mask & 0x00100000)
598  RXM0SIDLbits.SID2 = 0x01 ;
599  else
600  RXM0SIDLbits.SID2 = 0x00 ;
601 
602  identifier_math = (mask >> 21);
603  identifier_math = identifier_math & 0x000000FF;
604  RXM0SIDH = (unsigned char) identifier_math;
605  }
606 
607  //Extended MASK BUFFER 2
608  if ((mask_number == CAN_MASK_B2) && (type == CAN_CONFIG_XTD_MSG)) {
609  //EID0 - EID7 setting
610  identifier_math = mask & 0x000000FF;
611  RXM1EIDL = (unsigned char) identifier_math;
612 
613  //EID8 - EID15 setting
614  identifier_math = mask & 0x0000FF00;
615  identifier_math = identifier_math >> 8;
616  RXM1EIDH = (unsigned char) identifier_math;
617 
618  if (mask & 0x00010000)
619  RXM1SIDLbits.EID16 = 0x01;
620  else
621  RXM1SIDLbits.EID16 = 0x00;
622 
623  if (mask & 0x00020000)
624  RXM1SIDLbits.EID17 = 0x01;
625  else
626  RXM1SIDLbits.EID17 = 0x00;
627 
628  if (mask & 0x00040000)
629  RXM1SIDLbits.SID0 = 0x01;
630  else
631  RXM1SIDLbits.SID0 = 0x00;
632 
633  if (mask & 0x00080000)
634  RXM1SIDLbits.SID1 = 0x01;
635  else
636  RXM1SIDLbits.SID1 = 0x00;
637 
638  if (mask & 0x00100000)
639  RXM1SIDLbits.SID2 = 0x01 ;
640  else
641  RXM1SIDLbits.SID2 = 0x00 ;
642 
643  identifier_math = (mask >> 21);
644  identifier_math = identifier_math & 0x000000FF;
645  RXM1SIDH = (unsigned char) identifier_math;
646  }
647 }
648 
649 //*********************************************
650 // Set the RX Filters
651 //*********************************************
652 
653 void CAN_set_filter (enum CAN_FILTER filter_number, unsigned long filter, enum CAN_CONFIG_FLAGS type) {
654 
655  unsigned long tamp;
656 
657  // STANDARD FILTER 1 BUFFER 1
658  if ((filter_number == CAN_FILTER_B1_F1) && (type == CAN_CONFIG_STD_MSG)) {
659 
660  //standard filter
661  RXF0SIDLbits.EXIDEN = 0x00;
662 
663  tamp = (filter >> 3);
664  tamp = tamp & 0x000000FF;
665  RXF0SIDH = (unsigned char) tamp;
666 
667  tamp = filter & 0x00000007;
668  tamp = tamp << 5;
669  RXF0SIDL = (unsigned char) tamp;
670  }
671 
672  // STANDARD FILTER 2 BUFFER 1
673  if ((filter_number == CAN_FILTER_B1_F2) && (type == CAN_CONFIG_STD_MSG)) {
674 
675  //standard filter
676  RXF1SIDLbits.EXIDEN = 0x00;
677 
678  tamp = (filter >> 3);
679  tamp = tamp & 0x000000FF;
680  RXF1SIDH = (unsigned char) tamp;
681 
682  tamp = filter & 0x00000007;
683  tamp = tamp << 5;
684  RXF1SIDL = (unsigned char) tamp;
685  }
686 
687  // STANDARD FILTER 1 BUFFER 2
688  if ((filter_number == CAN_FILTER_B2_F1) && (type == CAN_CONFIG_STD_MSG)) {
689 
690  //standard filter
691  RXF2SIDLbits.EXIDEN = 0x00;
692 
693  tamp = (filter >> 3);
694  tamp = tamp & 0x000000FF;
695  RXF2SIDH = (unsigned char) tamp;
696 
697  tamp = filter & 0x00000007;
698  tamp = tamp << 5;
699  RXF2SIDL = (unsigned char) tamp;
700  }
701 
702  // STANDARD FILTER 2 BUFFER 2
703  if ((filter_number == CAN_FILTER_B2_F2) && (type == CAN_CONFIG_STD_MSG)) {
704 
705  //standard filter
706  RXF3SIDLbits.EXIDEN = 0x00;
707 
708  tamp = (filter >> 3);
709  tamp = tamp & 0x000000FF;
710  RXF3SIDH = (unsigned char) tamp;
711 
712  tamp = filter & 0x00000007;
713  tamp = tamp << 5;
714  RXF3SIDL = (unsigned char) tamp;
715  }
716 
717  // STANDARD FILTER 3 BUFFER 2
718  if ((filter_number == CAN_FILTER_B2_F3) && (type == CAN_CONFIG_STD_MSG)) {
719 
720  //standard filter
721  RXF4SIDLbits.EXIDEN = 0x00;
722 
723  tamp = (filter >> 3);
724  tamp = tamp & 0x000000FF;
725  RXF4SIDH = (unsigned char) tamp;
726 
727  tamp = filter & 0x00000007;
728  tamp = tamp << 5;
729  RXF4SIDL = (unsigned char) tamp;
730  }
731 
732  // STANDARD FILTER 4 BUFFER 2
733  if ((filter_number == CAN_FILTER_B2_F4) && (type == CAN_CONFIG_STD_MSG)) {
734 
735  //standard filter
736  RXF5SIDLbits.EXIDEN = 0x00;
737 
738  tamp = (filter >> 3);
739  tamp = tamp & 0x000000FF;
740  RXF5SIDH = (unsigned char) tamp;
741 
742  tamp = filter & 0x00000007;
743  tamp = tamp << 5;
744  RXF5SIDL = (unsigned char) tamp;
745  }
746 
747  // EXTENDED FILTER 1 BUFFER 1
748  if ((filter_number == CAN_FILTER_B1_F1) && (type == CAN_CONFIG_XTD_MSG)) {
749 
750  //standard filter
751  RXF0SIDLbits.EXIDEN = 0x01;
752 
753  //EID0 - EID7 setting
754  tamp = filter & 0x000000FF;
755  RXF0EIDL = (unsigned char) tamp;
756 
757  //EID8 - EID15 setting
758  tamp = filter & 0x0000FF00;
759  tamp = tamp >> 8;
760  RXF0EIDH = (unsigned char) tamp;
761 
762  if (filter & 0x00010000)
763  RXF0SIDLbits.EID16 = 0x01;
764  else
765  RXF0SIDLbits.EID16 = 0x00;
766 
767  if (filter & 0x00020000)
768  RXF0SIDLbits.EID17 = 0x01;
769  else
770  RXF0SIDLbits.EID17 = 0x00;
771 
772  if (filter & 0x00040000)
773  RXF0SIDLbits.SID0 = 0x01;
774  else
775  RXF0SIDLbits.SID0 = 0x00;
776 
777  if (filter & 0x00080000)
778  RXF0SIDLbits.SID1 = 0x01;
779  else
780  RXF0SIDLbits.SID1 = 0x00;
781 
782  if (filter & 0x00100000)
783  RXF0SIDLbits.SID2 = 0x01 ;
784  else
785  RXF0SIDLbits.SID2 = 0x00 ;
786 
787  tamp = (filter >> 21);
788  tamp = tamp & 0x000000FF;
789  RXF0SIDH = (unsigned char) tamp;
790  }
791 
792  // EXTENDED FILTER 2 BUFFER 1
793  if ((filter_number == CAN_FILTER_B1_F2) && (type == CAN_CONFIG_XTD_MSG)) {
794 
795  //standard filter
796  RXF1SIDLbits.EXIDEN = 0x01;
797 
798  //EID0 - EID7 setting
799  tamp = filter & 0x000000FF;
800  RXF1EIDL = (unsigned char) tamp;
801 
802  //EID8 - EID15 setting
803  tamp = filter & 0x0000FF00;
804  tamp = tamp >> 8;
805  RXF1EIDH = (unsigned char) tamp;
806 
807  if (filter & 0x00010000)
808  RXF1SIDLbits.EID16 = 0x01;
809  else
810  RXF1SIDLbits.EID16 = 0x00;
811 
812  if (filter & 0x00020000)
813  RXF1SIDLbits.EID17 = 0x01;
814  else
815  RXF1SIDLbits.EID17 = 0x00;
816 
817  if (filter & 0x00040000)
818  RXF1SIDLbits.SID0 = 0x01;
819  else
820  RXF1SIDLbits.SID0 = 0x00;
821 
822  if (filter & 0x00080000)
823  RXF1SIDLbits.SID1 = 0x01;
824  else
825  RXF1SIDLbits.SID1 = 0x00;
826 
827  if (filter & 0x00100000)
828  RXF1SIDLbits.SID2 = 0x01 ;
829  else
830  RXF1SIDLbits.SID2 = 0x00 ;
831 
832  tamp = (filter >> 21);
833  tamp = tamp & 0x000000FF;
834  RXF1SIDH = (unsigned char) tamp;
835  }
836 
837 
838  // EXTENDED FILTER 1 BUFFER 2
839  if ((filter_number == CAN_FILTER_B2_F1) && (type == CAN_CONFIG_XTD_MSG)) {
840 
841  //standard filter
842  RXF2SIDLbits.EXIDEN = 0x01;
843 
844  //EID0 - EID7 setting
845  tamp = filter & 0x000000FF;
846  RXF2EIDL = (unsigned char) tamp;
847 
848  //EID8 - EID15 setting
849  tamp = filter & 0x0000FF00;
850  tamp = tamp >> 8;
851  RXF2EIDH = (unsigned char) tamp;
852 
853  if (filter & 0x00010000)
854  RXF2SIDLbits.EID16 = 0x01;
855  else
856  RXF2SIDLbits.EID16 = 0x00;
857 
858  if (filter & 0x00020000)
859  RXF2SIDLbits.EID17 = 0x01;
860  else
861  RXF2SIDLbits.EID17 = 0x00;
862 
863  if (filter & 0x00040000)
864  RXF2SIDLbits.SID0 = 0x01;
865  else
866  RXF2SIDLbits.SID0 = 0x00;
867 
868  if (filter & 0x00080000)
869  RXF2SIDLbits.SID1 = 0x01;
870  else
871  RXF2SIDLbits.SID1 = 0x00;
872 
873  if (filter & 0x00100000)
874  RXF2SIDLbits.SID2 = 0x01 ;
875  else
876  RXF2SIDLbits.SID2 = 0x00 ;
877 
878  tamp = (filter >> 21);
879  tamp = tamp & 0x000000FF;
880  RXF2SIDH = (unsigned char) tamp;
881  }
882 
883  // EXTENDED FILTER 2 BUFFER 2
884  if ((filter_number == CAN_FILTER_B2_F2) && (type == CAN_CONFIG_XTD_MSG)) {
885 
886  //standard filter
887  RXF3SIDLbits.EXIDEN = 0x01;
888 
889  //EID0 - EID7 setting
890  tamp = filter & 0x000000FF;
891  RXF3EIDL = (unsigned char) tamp;
892 
893  //EID8 - EID15 setting
894  tamp = filter & 0x0000FF00;
895  tamp = tamp >> 8;
896  RXF3EIDH = (unsigned char) tamp;
897 
898  if (filter & 0x00010000)
899  RXF3SIDLbits.EID16 = 0x01;
900  else
901  RXF3SIDLbits.EID16 = 0x00;
902 
903  if (filter & 0x00020000)
904  RXF3SIDLbits.EID17 = 0x01;
905  else
906  RXF3SIDLbits.EID17 = 0x00;
907 
908  if (filter & 0x00040000)
909  RXF3SIDLbits.SID0 = 0x01;
910  else
911  RXF3SIDLbits.SID0 = 0x00;
912 
913  if (filter & 0x00080000)
914  RXF3SIDLbits.SID1 = 0x01;
915  else
916  RXF3SIDLbits.SID1 = 0x00;
917 
918  if (filter & 0x00100000)
919  RXF3SIDLbits.SID2 = 0x01 ;
920  else
921  RXF3SIDLbits.SID2 = 0x00 ;
922 
923  tamp = (filter >> 21);
924  tamp = tamp & 0x000000FF;
925  RXF3SIDH = (unsigned char) tamp;
926  }
927 
928  // EXTENDED FILTER 3 BUFFER 2
929  if ((filter_number == CAN_FILTER_B2_F3) && (type == CAN_CONFIG_XTD_MSG)) {
930 
931  //standard filter
932  RXF4SIDLbits.EXIDEN = 0x01;
933 
934  //EID0 - EID7 setting
935  tamp = filter & 0x000000FF;
936  RXF4EIDL = (unsigned char) tamp;
937 
938  //EID8 - EID15 setting
939  tamp = filter & 0x0000FF00;
940  tamp = tamp >> 8;
941  RXF4EIDH = (unsigned char) tamp;
942 
943  if (filter & 0x00010000)
944  RXF4SIDLbits.EID16 = 0x01;
945  else
946  RXF4SIDLbits.EID16 = 0x00;
947 
948  if (filter & 0x00020000)
949  RXF4SIDLbits.EID17 = 0x01;
950  else
951  RXF4SIDLbits.EID17 = 0x00;
952 
953  if (filter & 0x00040000)
954  RXF4SIDLbits.SID0 = 0x01;
955  else
956  RXF4SIDLbits.SID0 = 0x00;
957 
958  if (filter & 0x00080000)
959  RXF4SIDLbits.SID1 = 0x01;
960  else
961  RXF4SIDLbits.SID1 = 0x00;
962 
963  if (filter & 0x00100000)
964  RXF4SIDLbits.SID2 = 0x01 ;
965  else
966  RXF4SIDLbits.SID2 = 0x00 ;
967 
968  tamp = (filter >> 21);
969  tamp = tamp & 0x000000FF;
970  RXF4SIDH = (unsigned char) tamp;
971  }
972 
973  // EXTENDED FILTER 4 BUFFER 2
974  if ((filter_number == CAN_FILTER_B2_F4) && (type == CAN_CONFIG_XTD_MSG)) {
975 
976  //standard filter
977  RXF5SIDLbits.EXIDEN = 0x01;
978 
979  //EID0 - EID7 setting
980  tamp = filter & 0x000000FF;
981  RXF5EIDL = (unsigned char) tamp;
982 
983  //EID8 - EID15 setting
984  tamp = filter & 0x0000FF00;
985  tamp = tamp >> 8;
986  RXF5EIDH = (unsigned char) tamp;
987 
988  if (filter & 0x00010000)
989  RXF5SIDLbits.EID16 = 0x01;
990  else
991  RXF5SIDLbits.EID16 = 0x00;
992 
993  if (filter & 0x00020000)
994  RXF5SIDLbits.EID17 = 0x01;
995  else
996  RXF5SIDLbits.EID17 = 0x00;
997 
998  if (filter & 0x00040000)
999  RXF5SIDLbits.SID0 = 0x01;
1000  else
1001  RXF5SIDLbits.SID0 = 0x00;
1002 
1003  if (filter & 0x00080000)
1004  RXF5SIDLbits.SID1 = 0x01;
1005  else
1006  RXF5SIDLbits.SID1 = 0x00;
1007 
1008  if (filter & 0x00100000)
1009  RXF5SIDLbits.SID2 = 0x01 ;
1010  else
1011  RXF5SIDLbits.SID2 = 0x00 ;
1012 
1013  tamp = (filter >> 21);
1014  tamp = tamp & 0x000000FF;
1015  RXF5SIDH = (unsigned char) tamp;
1016  }
1017 
1018 }
1019 
1020 //*********************************************
1021 //Set the CAN engine mode
1022 //*********************************************
1023 
1025  CANCON = mode;
1026  while((CANSTAT & 0b11100000) != mode );
1027 }
1028 
1029 
1030 //*********************************************
1031 // Abort all the pending messages
1032 //*********************************************
1033 
1034 void CAN_abort_messages (void) {
1035  CANCONbits.ABAT = 1;
1036 }
1037 
1038 
1039 //*********************************************
1040 // Return 1 if the Bus is OFF
1041 //*********************************************
1043  return (COMSTATbits.TXBO);
1044 }
1045 
1046 //*********************************************
1047 // Return 1 if there is a TX passive status
1048 //*********************************************
1050  return (COMSTATbits.TXBP);
1051 }
1052 
1053 
1054 //*********************************************
1055 // Return 1 if there is a TX passive status
1056 //*********************************************
1058  return (COMSTATbits.RXBP);
1059 }
1060 
1061 //*********************************************
1062 // Return 1 if TX warning is ON
1063 //*********************************************
1065  return (COMSTATbits.TXWARN);
1066 }
1067 
1068 //*********************************************
1069 // Return 1 if RX warning is ON
1070 //*********************************************
1072  return (COMSTATbits.RXWARN);
1073 }
1074 
1075 //*********************************************
1076 // Return TX error Count
1077 //*********************************************
1078 
1080  return (TXERRCNT);
1081 }
1082 
1083 //*********************************************
1084 // Return RX error Count
1085 //*********************************************
1086 
1088  return (RXERRCNT);
1089 }
1090 
1091 //*********************************************
1092 // Return 1 if at least 1 TX buffer is empty
1093 //*********************************************
1094 
1096  //if at least one flag is 0, it returns return 1
1097  return (!TXB0CONbits.TXREQ || !TXB1CONbits.TXREQ || !TXB2CONbits.TXREQ);
1098 }
1099 
1100 //*********************************************
1101 // Return 1 if one or more RX buffer are full
1102 //*********************************************
1103 
1105  return (RXB0CONbits.RXFUL || RXB1CONbits.RXFUL);
1106 }