-
Notifications
You must be signed in to change notification settings - Fork 5
/
can.c
830 lines (680 loc) · 20.9 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
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
/*
* The MIT License (MIT)
*
* Copyright (c) [2015] [Marco Russi]
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/*
* This file can.c represents the only source file of the CAN bus driver for
* PIC18 microcontrollers.
*
* Author : Marco Russi
*
* Evolution of the file:
* 06/08/2015 - File created - Marco Russi
*
*/
/* -------------- System and library files included ------------- */
#include "common.h"
#include "can.h"
/* ---------------- Other files included ---------------------- */
/* None */
/*-------------- Definition of local constants ---------------- */
/* Length of TX messages buffer */
#define TX_BUFFER_LENGTH_MESSAGES UC_5
/* Length of RX messages buffer */
#define RX_BUFFER_LENGTH_MESSAGES UC_5
/* Max number of acceptance filter */
#define UC_MAX_NUM_OF_ACCEPTANCE_FILTERS UC_16
/* -------------- Definition of local macros ---------------- */
/* None */
/* --------------- Definition of local types ---------------- */
/* Module info structure */
typedef struct
{
Boolean fgInitialised;
uchar ucTxErrorCounter;
uchar ucRxErrorCounter;
uchar ucWriteRequestIndex;
uchar ucWriteExecutionIndex;
uchar ucReadRequestIndex;
uchar ucReadExecutionIndex;
uchar ucWriteBufferCounter;
uchar ucReadBufferCounter;
} stInfo;
/* ------------ Definition of local constant data ------------- */
/* None */
/* ------------- Declaration of local variables -------------- */
/* Message FIFO write buffer. Range: 0 - TX_BUFFER_LENGTH_MESSAGES */
CAN_stCanMessage stTxBuffer[TX_BUFFER_LENGTH_MESSAGES];
/* Message FIFO read buffer. Range: 0 - RX_BUFFER_LENGTH_MESSAGES */
CAN_stCanMessage stRxBuffer[RX_BUFFER_LENGTH_MESSAGES];
/* Store module informations */
stInfo stModuleInfo;
/* Array of first register addres of acceptance filters */
uchar *apucFilterPointer[UC_MAX_NUM_OF_ACCEPTANCE_FILTERS] =
{
&RXF0SIDH
,&RXF1SIDH
,&RXF2SIDH
,&RXF3SIDH
,&RXF4SIDH
,&RXF5SIDH
,&RXF6SIDH
,&RXF7SIDH
,&RXF8SIDH
,&RXF9SIDH
,&RXF10SIDH
,&RXF11SIDH
,&RXF12SIDH
,&RXF13SIDH
,&RXF14SIDH
,&RXF15SIDH
}
/* ------------------ Functions prototypes ------------------ */
LOCAL Boolean receiveCanMessage( CAN_stCanMessage * );
LOCAL Boolean sendCanMessage( CAN_stCanMessage * );
LOCAL void setCanConfigurationMode( void );
LOCAL void setCanNormalMode( void );
/* ------------------- Exported Functions -------------------- */
/* Initialise CAN module */
EXPORTED CAN_eCanResponseCodes CAN_InitCanModule( CAN_stCanInitParameters stParameters )
{
CAN_eCanResponseCodes eResponseCode;
setCanConfigurationMode();
/* Reset status flags */
COMSTAT = 0x00;
/* Select Enhanced Legacy mode (Mode 1) */
ECANCON |= 0b01000000;
/* Set channels priority levels */
/* TXB0 priority = 2 */
TXB0CONbits.TXPRI0 = 0;
TXB0CONbits.TXPRI1 = 1;
/* TXB0 priority = 1 */
TXB1CONbits.TXPRI0 = 1;
TXB1CONbits.TXPRI1 = 0;
/* TXB0 priority = 0 */
TXB2CONbits.TXPRI0 = 0;
TXB2CONbits.TXPRI1 = 0;
/* Configuration */
BRGCON1 |= ((uchar)(stParameters.eSynchJumpWidth) & 0x03) << UC_6;
BRGCON1 |= (uchar)(stParameters.ucBaudRate) & 0x3F;
if(stParameters.ePhaseSeg2 == CAN_PHASE_SEGMENT_2_MAXIMUM_OF_PHEG1)
{
BRGCON2bits.SEG2PHTS = 0;
}
else
{
BRGCON2bits.SEG2PHTS = 1;
BRGCON3 |= (uchar)stParameters.ePhaseSeg2 & 0x07;
}
BRGCON2bits.SAM = stParameters.eBusSample;
BRGCON2 |= ((uchar)stParameters.ePhaseSeg1 & 0x07) << UC_3;
BRGCON2 |= (uchar)stParameters.ePropagation & 0x07;
BRGCON3bits.WAKFIL = stParameters.eWakeUpFilter;
BRGCON3bits.WAKDIS = 0; /* Enable CAN bus activity wake-up feature */
/*
sync jump : 1 tq
phase seg 1 : 8 tq
phase seg 2 : 8 tq
prog seg : 3 tq
bit time = ( 1 + 8 + 8 + 3 ) * TQ = 20 TQ
TQ @ 100 Kbit/s = 10 us / 20 = 500 ns
BRP = 0
*/
BRGCON1 = 0x00;
BRGCON2 = 0xBA;
BRGCON3 = 0x07;
/* Reset rxb0 control register. Receive all valid messages as per acceptance filters */
RXB0CON = 0b00000000;
/* Reset rxb1 control register. Receive all valid messages as per acceptance filters */
RXB1CON = 0b00000000;
/* No bits will be compared with incoming data bits */
SDFLC = 0x00;
/* All acceptance filters are associated with RXB0 */
RXFBCON0 = 0x00;
RXFBCON1 = 0x00;
RXFBCON2 = 0x00;
RXFBCON3 = 0x00;
RXFBCON4 = 0x00;
RXFBCON5 = 0x00;
RXFBCON6 = 0x00;
RXFBCON7 = 0x00;
/* No mask are used */
MSEL0 = 0xFF;
MSEL1 = 0xFF;
MSEL2 = 0xFF;
MSEL3 = 0xFF;
CIOCON = 0b00100000;
/* ||||||||__ - : Unimplemented (read as 0) */
/* |||||||___ - : Unimplemented (read as 0) */
/* ||||||____ - : Unimplemented (read as 0) */
/* |||||_____ - : Unimplemented (read as 0) */
/* ||||______ CANCAP: Disable CAN capture, RC2/CCP1 input to CCP1 module */
/* |||_______ ENDRHI: CANTX pin will drive VDD when recessive */
/* ||________ - : Unimplemented (read as 0) */
/* |_________ - : Unimplemented (read as 0) */
setCanNormalMode();
IPR3 = 0x00; /* All interrupts have low priority */
PIR3 = 0x00; /* Reset all interrupts flags */
PIE3 = 0x01; /* Enable rxb0 interrupts only */
/* Reset module status */
stModuleInfo.fgInitialised = FG_TRUE;
stModuleInfo.ucTxErrorCounter = UC_NULL;
stModuleInfo.ucRxErrorCounter = UC_NULL;
stModuleInfo.ucWriteRequestIndex = UC_NULL;
stModuleInfo.ucWriteExecutionIndex = UC_NULL;
stModuleInfo.ucReadRequestIndex = UC_NULL;
stModuleInfo.ucReadExecutionIndex = UC_NULL;
stModuleInfo.ucWriteBufferCounter = UC_NULL;
stModuleInfo.ucReadBufferCounter = UC_NULL;
eResponseCode = CAN_MODULE_INITIALISED;
return eResponseCode;
}
/* Check the module status */
EXPORTED CAN_eCanResponseCodes CAN_CheckModuleStatus( void )
{
CAN_eCanResponseCodes eResponseCode;
/* If the module is initialised */
if(stModuleInfo.fgInitialised == FG_TRUE)
{
/* Errors check */
if((stModuleInfo.ucTxErrorCounter == UC_NULL) && (stModuleInfo.ucRxErrorCounter == UC_NULL))
{
eResponseCode = CAN_MODULE_INITIALISED;
}
else
{
eResponseCode = CAN_ERRORS;
}
}
else
{
eResponseCode = CAN_MODULE_NOT_INITIALISED;
}
return eResponseCode;
}
/* Send a message */
EXPORTED CAN_eCanResponseCodes CAN_SendMessage( CAN_stCanMessage *pstMessage )
{
CAN_eCanResponseCodes eResponseCode;
/* If write buffer is not full */
if(stModuleInfo.ucWriteBufferCounter < TX_BUFFER_LENGTH_MESSAGES)
{
/* Copy the message */
stTxBuffer[stModuleInfo.ucWriteRequestIndex] = *pstMessage;
/* Increment write buffer index */
stModuleInfo.ucWriteRequestIndex++;
if(stModuleInfo.ucWriteRequestIndex == TX_BUFFER_LENGTH_MESSAGES)
{
stModuleInfo.ucWriteRequestIndex = UC_NULL;
}
else
{
}
/* Increment write requests counter */
stModuleInfo.ucWriteBufferCounter++;
eResponseCode = CAN_NO_ERRORS;
}
else
{
eResponseCode = CAN_TX_BUFFER_FULL;
}
return eResponseCode;
}
/* Read a message */
EXPORTED CAN_eCanResponseCodes CAN_ReceiveMessage( CAN_stCanMessage *pstMessage )
{
CAN_eCanResponseCodes eResponseCode;
/* If read buffer is not empty */
if(stModuleInfo.ucReadBufferCounter > UC_NULL)
{
/* Copy the message */
*pstMessage = stRxBuffer[stModuleInfo.ucReadExecutionIndex];
/* Increment read buffer index */
stModuleInfo.ucReadExecutionIndex++;
if(stModuleInfo.ucReadExecutionIndex == RX_BUFFER_LENGTH_MESSAGES)
{
stModuleInfo.ucReadExecutionIndex = UC_NULL;
}
else
{
}
/* Decrement read requests counter */
stModuleInfo.ucReadBufferCounter--;
eResponseCode = CAN_NO_ERRORS;
}
else
{
eResponseCode = CAN_RX_BUFFER_EMPTY;
}
return eResponseCode;
}
/* Manage the can module queue */
EXPORTED CAN_eCanResponseCodes CAN_ManageQueue( void )
{
CAN_eCanResponseCodes eResponseCode;
Boolean fgSendSuccess = FG_FALSE;
/* If there are not tx errors */
if(stModuleInfo.ucTxErrorCounter == UC_NULL)
{
/* If there are write requests to be executed */
if(stModuleInfo.ucWriteBufferCounter > UC_NULL)
{
/* Send the message pointed by current write execution index */
fgSendSuccess = sendCanMessage(&stTxBuffer[stModuleInfo.ucWriteExecutionIndex]);
/* If one can channel was free */
if(fgSendSuccess == FG_TRUE)
{
/* Increment write execution index */
stModuleInfo.ucWriteExecutionIndex++;
if(stModuleInfo.ucWriteExecutionIndex == TX_BUFFER_LENGTH_MESSAGES)
{
stModuleInfo.ucWriteExecutionIndex = UC_NULL;
}
else
{
}
/* Decrement write requests counter */
stModuleInfo.ucWriteBufferCounter--;
}
else
{
/* Do nothing */
}
}
else
{
/* Do nothing */
}
eResponseCode = CAN_NO_ERRORS;
}
else
{
eResponseCode = CAN_ERRORS;
}
return eResponseCode;
}
/* Manage RX message interrupt */
EXPORTED CAN_eCanResponseCodes CAN_ManageRxMessageInterrupt( void )
{
CAN_eCanResponseCodes eResponseCode;
Boolean fgReceiveSuccess = FG_FALSE;
/* If there are not tx errors */
if(stModuleInfo.ucRxErrorCounter == UC_NULL)
{
/* If the read buffer is not full */
if(stModuleInfo.ucReadBufferCounter < RX_BUFFER_LENGTH_MESSAGES)
{
/* Read the message pointed by current read request index */
fgReceiveSuccess = receiveCanMessage(&stRxBuffer[stModuleInfo.ucReadRequestIndex]);
/* If a channel was read successfully */
if(fgReceiveSuccess == FG_TRUE)
{
/* Increment read request index */
stModuleInfo.ucReadRequestIndex++;
if(stModuleInfo.ucReadRequestIndex == RX_BUFFER_LENGTH_MESSAGES)
{
stModuleInfo.ucReadRequestIndex = UC_NULL;
}
else
{
}
/* Increment read requests counter */
stModuleInfo.ucReadBufferCounter++;
}
else
{
/* Do nothing */
}
}
else
{
/* Overwrite the message pointed by current read request index */
fgReceiveSuccess = receiveCanMessage(&stRxBuffer[stModuleInfo.ucReadRequestIndex]);
}
eResponseCode = CAN_NO_ERRORS;
}
else
{
eResponseCode = CAN_ERRORS;
}
return eResponseCode;
}
/* Set a ID message to receive */
EXPORTED CAN_eCanResponseCodes CAN_SetIDMessageToReceive( uchar ucIDIndex, ulong ulIdentifier, Boolean fgExide)
{
CAN_eCanResponseCodes eResponseCode = CAN_NO_ERRORS;
uchar *pucFilterPointer;
/* Copy the first register address of the required acceptance filter */
pucFilterPointer = apucFilterPointer[ucIDIndex];
/* Set configuration mode */
setCanConfigurationMode();
/* Copy identifier */
if(fgExide == B_TRUE)
{
*pucFilterPointer = (uchar)(ulIdentifier >> UL_SHIFT_21);
pucFilterPointer++;
*pucFilterPointer = (uchar)(ulIdentifier >> UL_SHIFT_16);
(*pucFilterPointer) &= 0x03;
(*pucFilterPointer) |= (uchar)(ulIdentifier >> UL_SHIFT_13);
/* Set EXIDE bit */
(*pucFilterPointer) |= 0x08;
pucFilterPointer++;
*pucFilterPointer = (uchar)(ulIdentifier >> UL_SHIFT_8);
pucFilterPointer++;
*pucFilterPointer = (uchar)(ulIdentifier);
}
else
{
*pucFilterPointer = (uchar)(ulIdentifier >> UL_SHIFT_3);
pucFilterPointer++;
*pucFilterPointer = (uchar)(ulIdentifier << UL_SHIFT_5);
(*pucFilterPointer) &= 0xE0;
pucFilterPointer++;
pucFilterPointer++;
}
/* Set corrispective filter enable bit */
switch(ucIDIndex)
{
case UC_0:
{
RXFCON0bits.RXF0EN = 1;
break;
}
case UC_1:
{
RXFCON0bits.RXF1EN = 1;
break;
}
case UC_2:
{
RXFCON0bits.RXF2EN = 1;
break;
}
case UC_3:
{
RXFCON0bits.RXF3EN = 1;
break;
}
case UC_4:
{
RXFCON0bits.RXF4EN = 1;
break;
}
case UC_5:
{
RXFCON0bits.RXF5EN = 1;
break;
}
case UC_6:
{
RXFCON0bits.RXF6EN = 1;
break;
}
case UC_7:
{
RXFCON0bits.RXF7EN = 1;
break;
}
case UC_8:
{
RXFCON1bits.RXF8EN = 1;
break;
}
case UC_9:
{
RXFCON1bits.RXF9EN = 1;
break;
}
case UC_10:
{
RXFCON1bits.RXF10EN = 1;
break;
}
case UC_11:
{
RXFCON1bits.RXF11EN = 1;
break;
}
case UC_12:
{
RXFCON1bits.RXF12EN = 1;
break;
}
case UC_13:
{
RXFCON1bits.RXF13EN = 1;
break;
}
case UC_14:
{
RXFCON1bits.RXF14EN = 1;
break;
}
case UC_15:
{
RXFCON1bits.RXF15EN = 1;
break;
}
default:
{
break;
}
}
/* Set normal mode */
setCanNormalMode();
return eResponseCode;
}
/* ------------------- Local Functions --------------------- */
/* Receive a message from CAN channels */
LOCAL Boolean receiveCanMessage( CAN_stCanMessage *pstMessage )
{
uchar * pucChannelControlRegister;
uchar * pucChannelPointer;
uchar ucDataIndex;
Boolean fgSuccess = FG_TRUE;
/* Check the full channel */
if(RXB0CONbits.RXFUL == 1)
{
pucChannelControlRegister = &RXB0CON;
}
else if(RXB1CONbits.RXFUL == 1)
{
pucChannelControlRegister = &RXB1CON;
}
else
{
/* There are not full channels */
fgSuccess = FG_FALSE;
}
if(fgSuccess == FG_TRUE)
{
/* The control register is the first register order by address */
pucChannelPointer = pucChannelControlRegister;
pucChannelPointer++;
pucChannelPointer++;
/* Copy identifier */
/* if extended identifier */
if(((*pucChannelPointer) & 0x08) == 0x08)
{
pucChannelPointer--;
pstMessage->ulIdentifier = ((ulong)(*pucChannelPointer) & 0x000000FF) << UL_SHIFT_21;
pucChannelPointer++;
pstMessage->ulIdentifier |= ((ulong)(*pucChannelPointer) & 0x000000E0) << UL_SHIFT_13;
pstMessage->ulIdentifier |= ((ulong)(*pucChannelPointer) & 0x00000003) << UL_SHIFT_16;
pucChannelPointer++;
pstMessage->ulIdentifier |= (*pucChannelPointer) << UL_SHIFT_8;
pucChannelPointer++;
pstMessage->ulIdentifier |= (*pucChannelPointer);
/* Set EXIDE bit */
pstMessage->fgExide = FG_TRUE;
}
else
{
pucChannelPointer--;
pstMessage->ulIdentifier = ((ulong)(*pucChannelPointer) & 0x000000FF) << UL_SHIFT_3;
pucChannelPointer++;
pstMessage->ulIdentifier |= ((ulong)(*pucChannelPointer) & 0x000000E0) >> UL_SHIFT_5;
pucChannelPointer++;
pucChannelPointer++;
/* Clear EXIDE bit */
pstMessage->fgExide = FG_FALSE;
}
pucChannelPointer++;
/* Data Length Code update */
pstMessage->ucDataLength = (*pucChannelPointer) & 0x0F;
/* Remote flag update */
if(((*pucChannelControlRegister) & 0x04) == 0x04)
{
pstMessage->fgRemote = FG_TRUE;
}
else
{
pstMessage->fgRemote = FG_FALSE;
}
/* Copy data */
for(ucDataIndex = UC_NULL; ucDataIndex < pstMessage->ucDataLength; ucDataIndex++)
{
pucChannelPointer++;
pstMessage->ucvData[ucDataIndex] = (*pucChannelPointer);
}
/* Clear buffer full flag */
(*pucChannelControlRegister) &= 0x7F;
}
else
{
/* Do nothing */
}
return fgSuccess;
}
/* Send a message via CAN channels */
LOCAL Boolean sendCanMessage( CAN_stCanMessage *pstMessage )
{
/* Save pointed message indentifier and DLC */
ulong ulIdentifier = pstMessage->ulIdentifier;
uchar ucDataLength = pstMessage->ucDataLength;
uchar *pucChannelControlRegister;
uchar *pucChannelPointer;
uchar ucDataIndex;
Boolean fgSuccess = FG_TRUE;
/* Data length control */
if(ucDataLength > CAN_MESSAGE_LENGTH_BYTES)
{
ucDataLength = CAN_MESSAGE_LENGTH_BYTES;
}
else
{
/* Do nothing */
}
/* Check a free channel for send a message according to priority level order */
if(TXB0CONbits.TXREQ == 0)
{
pucChannelControlRegister = &TXB0CON;
}
else if(TXB1CONbits.TXREQ == 0)
{
pucChannelControlRegister = &TXB1CON;
}
else if(TXB2CONbits.TXREQ == 0)
{
pucChannelControlRegister = &TXB2CON;
}
else
{
/* There are not free channels */
fgSuccess = FG_FALSE;
}
if(fgSuccess == FG_TRUE)
{
/* The control register is the first register order by address */
pucChannelPointer = pucChannelControlRegister;
pucChannelPointer++;
/* Copy identifier */
if(pstMessage->fgExide == B_TRUE)
{
*pucChannelPointer = (uchar)(ulIdentifier >> UL_SHIFT_21);
pucChannelPointer++;
*pucChannelPointer = (uchar)(ulIdentifier >> UL_SHIFT_16);
(*pucChannelPointer) &= 0x03;
(*pucChannelPointer) |= (uchar)(ulIdentifier >> UL_SHIFT_13);
/* Set EXIDE bit */
(*pucChannelPointer) |= 0x08;
pucChannelPointer++;
*pucChannelPointer = (uchar)(ulIdentifier >> UL_SHIFT_8);
pucChannelPointer++;
*pucChannelPointer = (uchar)(ulIdentifier);
}
else
{
*pucChannelPointer = (uchar)(ulIdentifier >> UL_SHIFT_3);
pucChannelPointer++;
*pucChannelPointer = (uchar)(ulIdentifier << UL_SHIFT_5);
(*pucChannelPointer) &= 0xE0;
pucChannelPointer++;
pucChannelPointer++;
}
pucChannelPointer++;
/* Data Length Code update */
*pucChannelPointer = ucDataLength;
/* Remote flag update */
if(pstMessage->fgRemote == FG_FALSE)
{
(*pucChannelPointer) |= 0x40;
}
else
{
(*pucChannelPointer) &= 0x0F;
}
/* Copy data */
for(ucDataIndex = UC_NULL; ucDataIndex < ucDataLength; ucDataIndex++)
{
pucChannelPointer++;
*pucChannelPointer = pstMessage->ucvData[ucDataIndex];
}
/* Send request */
(*pucChannelControlRegister) |= 0x08;
}
else
{
/* Do nothing */
}
return fgSuccess;
}
/* Set CAN registers in configuration mode */
LOCAL void setCanConfigurationMode( void )
{
/* Configuration mode request */
CANCONbits.REQOP0 = 0;
CANCONbits.REQOP1 = 0;
CANCONbits.REQOP2 = 1;
/* wait configuration mode request */
while((CANSTAT & 0xE0) != 0b10000000);
}
/* Set CAN registers in normal mode */
LOCAL void setCanNormalMode( void )
{
/* Normal mode request */
CANCONbits.REQOP0 = 0;
CANCONbits.REQOP1 = 0;
CANCONbits.REQOP2 = 0;
/* wait normal mode request */
while((CANSTAT & 0xE0) != 0b00000000);
}
/* End of file */