Subversion Repositories HomeAutomation

Rev

Rev 25 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 25 Rev 26
1
/*********************************************************************
1
/*********************************************************************
2
 *
2
 *
3
 *                  CAN driver
3
 *                  CAN driver
4
 *
4
 *
5
 *********************************************************************
5
 *********************************************************************
6
 * FileName:        CAN.c
6
 * FileName:        CAN.c
7
 *
7
 *
8
 * Author               Date    Comment
8
 * Author               Date    Comment
9
 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9
 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10
 * Nilesh Rajbharti     8/20/01 Original        (Rev 1.0)
10
 * Johan Böhlin     21-10-06    Original        (Rev 1.0)
11
 ********************************************************************/
11
 ********************************************************************/
12
 
12
 
13
#include "../Include/CANdefs.h"
13
#include "../Include/CANdefs.h"
14
#include "../Include/stackTasks.h"
14
#include "../Include/stackTasks.h"
15
#include "../Include/CAN.h"
15
#include "../Include/CAN.h"
16
#include "../Include/uart.h"
16
#include "../Include/uart.h"
17
 
17
 
18
static void canGetPacket(void);
18
static void canGetPacket(void);
19
 
19
 
20
#ifdef USE_CAN
20
#ifdef USE_CAN
21
 
21
 
22
 
22
 
23
/*
23
/*
24
*   Function: canInit
24
*   Function: canInit
25
*
25
*
26
*   Input:  none
26
*   Input:  none
27
*   Output: none
27
*   Output: none
28
*   Pre-conditions: none
28
*   Pre-conditions: none
29
*   Affects: Changes can registers.
29
*   Affects: Changes can registers.
30
*   Depends: none.
30
*   Depends: none.
31
*/
31
*/
32
void canInit()
32
void canInit()
33
{
33
{
34
    CANCON = 0x80; //request config mode
34
    CANCON = 0x80; //request config mode
35
    while ((CANSTAT & 0xE0) != 0x80 ); //wait for config mode request
35
    while ((CANSTAT & 0xE0) != 0x80 ); //wait for config mode request
36
 
36
 
37
    // BAUD AND TQS
37
    // BAUD AND TQS
38
 
38
 
39
    BRGCON1=0;
39
    BRGCON1=0;
40
   
40
   
41
    // Sync_Seq = 2 x TQ = 1
41
    // Sync_Seq = 2 x TQ = 1
42
    BRGCON1bits.SJW1=0;
42
    BRGCON1bits.SJW1=0;
43
    BRGCON1bits.SJW0=1;
43
    BRGCON1bits.SJW0=1;
44
   
44
   
45
    // Setting BRP.
45
    // Setting BRP.
46
    BRGCON1=BRGCON1|CAN_BRP;
46
    BRGCON1=BRGCON1|CAN_BRP;
47
   
47
   
48
    // Maximum PHEG1
48
    // Maximum PHEG1
49
    BRGCON2bits.SEG2PHTS = 1;
49
    BRGCON2bits.SEG2PHTS = 1;
50
   
50
   
51
    // Phase_Seg1 = 2 x TQ = 1
51
    // Phase_Seg1 = 2 x TQ = 1
52
    BRGCON2bits.SEG1PH2 = 0;
52
    BRGCON2bits.SEG1PH2 = 0;
53
    BRGCON2bits.SEG1PH1 = 0;
53
    BRGCON2bits.SEG1PH1 = 0;
54
    BRGCON2bits.SEG1PH0 = 1;
54
    BRGCON2bits.SEG1PH0 = 1;
55
   
55
   
56
    // Prop_Seq = 2 x TQ = 1
56
    // Prop_Seq = 2 x TQ = 1
57
    BRGCON2bits.PRSEG2 = 0;
57
    BRGCON2bits.PRSEG2 = 0;
58
    BRGCON2bits.PRSEG1 = 0;
58
    BRGCON2bits.PRSEG1 = 0;
59
    BRGCON2bits.PRSEG0 = 1;
59
    BRGCON2bits.PRSEG0 = 1;
60
   
60
   
61
    //  Enableing bus wake-up
61
    //  Enableing bus wake-up
62
    BRGCON3bits.WAKDIS = 0;
62
    BRGCON3bits.WAKDIS = 0;
63
   
63
   
64
    // Dont use filter when wakeup
64
    // Dont use filter when wakeup
65
    BRGCON3bits.WAKFIL = 0;
65
    BRGCON3bits.WAKFIL = 0;
66
   
66
   
67
    // Phase_Seg2 = 2 x TQ = 1
67
    // Phase_Seg2 = 2 x TQ = 1
68
    BRGCON3bits.SEG2PH2 = 0;
68
    BRGCON3bits.SEG2PH2 = 0;
69
    BRGCON3bits.SEG2PH1 = 0;
69
    BRGCON3bits.SEG2PH1 = 0;
70
    BRGCON3bits.SEG2PH0 = 1;
70
    BRGCON3bits.SEG2PH0 = 1;
71
 
71
 
72
    ECANCON=0;
72
    ECANCON=0;
73
    ECANCONbits.MDSEL1 = 0;
73
    ECANCONbits.MDSEL1 = 0;
74
    ECANCONbits.MDSEL0 = 1;
74
    ECANCONbits.MDSEL0 = 1;
75
 
75
 
76
 
76
 
77
    // FILTERS AND MASKS
77
    // FILTERS AND MASKS
78
    RXB0CONbits.RXM1=1;
78
    RXB0CONbits.RXM1=1;
79
    RXB0CONbits.RXM0=0;
79
    RXB0CONbits.RXM0=0;
80
 
80
 
81
 
81
 
82
    // INTERRUPTS
82
    // INTERRUPTS
83
   
83
   
84
    // Diable transmit interrupt
84
    // Diable transmit interrupt
85
    TXBIE = 0;
85
    TXBIE = 0;
86
 
86
 
87
    //rx any interrupt
87
    //rx any interrupt
88
    PIR3 = 0;
88
    PIR3 = 0;
89
    PIE3 = 0b00000011;
89
    PIE3 = 0b00000011;
90
 
90
 
91
    // High interrupt
91
    // High interrupt
92
    IPR3 =0b00000011;
92
    IPR3 =0b00000011;
93
 
93
 
94
    // Programmable buffers interrupt
94
    // Programmable buffers interrupt
95
    BIE0  = 0xFF;  
95
    BIE0  = 0xFF;  
96
 
96
 
97
 
97
 
98
    //loopback mode or not
98
    //loopback mode or not
99
    #ifdef CAN_LOOPBACK_MODE
99
    #ifdef CAN_LOOPBACK_MODE
100
        CANCON = 0x40;//request loopback mode
100
        CANCON = 0x40;//request loopback mode
101
        while ((CANSTAT & 0xE0) != 0x40 );//wait for loopback mode
101
        while ((CANSTAT & 0xE0) != 0x40 );//wait for loopback mode
102
    #else
102
    #else
103
        CANCON = 0x00;//request normal mode
103
        CANCON = 0x00;//request normal mode
104
        while ((CANSTAT & 0xE0) != 0x00 );//wait for normal mode
104
        while ((CANSTAT & 0xE0) != 0x00 );//wait for normal mode
105
    #endif
105
    #endif
106
 
106
 
107
}
107
}
108
 
108
 
109
 
109
 
110
/*
110
/*
111
*   Function: canISR
111
*   Function: canISR
112
*
112
*
113
*   Input:  none
113
*   Input:  none
114
*   Output: none
114
*   Output: none
115
*   Pre-conditions: a call to canInit()
115
*   Pre-conditions: a call to canInit()
116
*   Affects: interrupt registers and receive data buffers
116
*   Affects: interrupt registers and receive data buffers
117
*   Depends: ..
117
*   Depends: ..
118
*/
118
*/
119
void canISR()
119
void canISR()
120
{
120
{
121
    if (PIR3bits.RXBnIF || PIR3bits.RXB1IF || PIR3bits.RXB0IF)
121
    if (PIR3bits.RXBnIF || PIR3bits.RXB1IF || PIR3bits.RXB0IF)
122
    {
122
    {
123
 
123
 
124
        // Get which buffer that is ful.
124
        // Get which buffer that is ful.
125
             if (RXB0CONbits.RXFUL) { ECANCON=(ECANCON&0b00000)|0b10000; canGetPacket(); }
125
             if (RXB0CONbits.RXFUL) { ECANCON=(ECANCON&0b00000)|0b10000; canGetPacket(); }
126
        else if (RXB1CONbits.RXFUL) { ECANCON=(ECANCON&0b00000)|0b10001; canGetPacket(); }
126
        else if (RXB1CONbits.RXFUL) { ECANCON=(ECANCON&0b00000)|0b10001; canGetPacket(); }
127
        else if (B0CONbits.RXFUL)   { ECANCON=(ECANCON&0b00000)|0b10010; canGetPacket(); }
127
        else if (B0CONbits.RXFUL)   { ECANCON=(ECANCON&0b00000)|0b10010; canGetPacket(); }
128
        else if (B1CONbits.RXFUL)   { ECANCON=(ECANCON&0b00000)|0b10011; canGetPacket(); }
128
        else if (B1CONbits.RXFUL)   { ECANCON=(ECANCON&0b00000)|0b10011; canGetPacket(); }
129
        else if (B2CONbits.RXFUL)   { ECANCON=(ECANCON&0b00000)|0b10100; canGetPacket(); }
129
        else if (B2CONbits.RXFUL)   { ECANCON=(ECANCON&0b00000)|0b10100; canGetPacket(); }
130
        else if (B3CONbits.RXFUL)   { ECANCON=(ECANCON&0b00000)|0b10101; canGetPacket(); }
130
        else if (B3CONbits.RXFUL)   { ECANCON=(ECANCON&0b00000)|0b10101; canGetPacket(); }
131
        else if (B4CONbits.RXFUL)   { ECANCON=(ECANCON&0b00000)|0b10110; canGetPacket(); }
131
        else if (B4CONbits.RXFUL)   { ECANCON=(ECANCON&0b00000)|0b10110; canGetPacket(); }
132
        else if (B5CONbits.RXFUL)   { ECANCON=(ECANCON&0b00000)|0b10111; canGetPacket(); }
132
        else if (B5CONbits.RXFUL)   { ECANCON=(ECANCON&0b00000)|0b10111; canGetPacket(); }
133
   
133
   
134
        if (PIR3bits.RXBnIF) {PIR3bits.RXBnIF = 0;}
134
        if (PIR3bits.RXBnIF) {PIR3bits.RXBnIF = 0;}
135
        if (PIR3bits.RXB1IF) {PIR3bits.RXB1IF = 0;}
135
        if (PIR3bits.RXB1IF) {PIR3bits.RXB1IF = 0;}
136
        if (PIR3bits.RXB0IF) {PIR3bits.RXB0IF = 0;}
136
        if (PIR3bits.RXB0IF) {PIR3bits.RXB0IF = 0;}
137
   
137
   
138
    }
138
    }
139
}
139
}
140
 
140
 
141
 
141
 
142
/*
142
/*
143
*   Function: canGetPacket
143
*   Function: canGetPacket
144
*
144
*
145
*   Input:  none
145
*   Input:  none
146
*   Output: none
146
*   Output: none
147
*   Pre-conditions: a call to canISR and an packet is available and banked to RXB0
147
*   Pre-conditions: a call to canISR and an packet is available and banked to RXB0
148
*   Affects: Calls canParse() function in main for parsing received packet.
148
*   Affects: Calls canParse() function in main for parsing received packet.
149
*   Depends: none.
149
*   Depends: none.
150
*/
150
*/
151
void canGetPacket()
151
void canGetPacket()
152
{
152
{
153
        CAN_MESSAGE cm;
153
        CAN_MESSAGE cm;
154
 
154
 
155
        // Set extended mode or not.
155
        // Set extended mode or not.
156
        cm.extended=(RXB0SIDLbits.EXID==0?FALSE:TRUE);
156
        cm.extended=(RXB0SIDLbits.EXID==0?FALSE:TRUE);
157
 
157
 
158
        // Clear
158
        // Clear
159
        cm.ident=0;
159
        cm.ident=0;
160
 
160
 
161
 
161
 
162
        if (cm.extended==TRUE)
162
        if (cm.extended==TRUE)
163
        {
163
        {
164
            //Read extended ident.
164
            //Read extended ident.
165
 
165
 
166
            //xx xx xx 28 27 26 25 24   23 22 21 20 19 18 17 16   15 14 13 12 11 10 09 08   07 06 05 04 03 02 01 00
166
            //xx xx xx 28 27 26 25 24   23 22 21 20 19 18 17 16   15 14 13 12 11 10 09 08   07 06 05 04 03 02 01 00
167
 
167
 
168
            //RXB0SIDH 28 27 26 25 24 23 22 21
168
            //RXB0SIDH 28 27 26 25 24 23 22 21
169
            //RXB0SIDL 20 19 18 xx xx xx 17 16
169
            //RXB0SIDL 20 19 18 xx xx xx 17 16
170
            //RXB0EIDH 15 14 13 12 11 10 09 08
170
            //RXB0EIDH 15 14 13 12 11 10 09 08
171
            //RXB0EIDL 07 06 05 04 03 02 01 00
171
            //RXB0EIDL 07 06 05 04 03 02 01 00
172
 
172
 
173
 
173
 
174
            cm.ident = (((DWORD)RXB0SIDH)<<21) + (((DWORD)(RXB0SIDL & 0xE0))<<13) + (((DWORD)(RXB0SIDL & 0x03))<<16) + (((DWORD)RXB0EIDH)<<8) + (BYTE)RXB0EIDL;
174
            cm.ident = (((DWORD)RXB0SIDH)<<21) + (((DWORD)(RXB0SIDL & 0xE0))<<13) + (((DWORD)(RXB0SIDL & 0x03))<<16) + (((DWORD)RXB0EIDH)<<8) + (BYTE)RXB0EIDL;
175
 
175
 
176
        }
176
        }
177
        else
177
        else
178
        {
178
        {
179
            //Read standard ident.
179
            //Read standard ident.
180
 
180
 
181
            //xx xx xx xx xx 10 09 08   07 06 05 04 03 02 01 00
181
            //xx xx xx xx xx 10 09 08   07 06 05 04 03 02 01 00
182
 
182
 
183
            //RXB0SIDH 10 09 08 07 06 05 04 03
183
            //RXB0SIDH 10 09 08 07 06 05 04 03
184
            //RXB0SIDL 02 01 00 xx xx xx xx xx
184
            //RXB0SIDL 02 01 00 xx xx xx xx xx
185
 
185
 
186
            cm.ident = (((DWORD)RXB0SIDH)<<3) + (((DWORD)RXB0SIDL)>>5);
186
            cm.ident = (((DWORD)RXB0SIDH)<<3) + (((DWORD)RXB0SIDL)>>5);
187
        }
187
        }
188
 
188
 
189
        // Data length
189
        // Data length
190
        cm.data_length=RXB0DLC;
190
        cm.data_length=RXB0DLC;
191
 
191
 
192
        // Data one bye one, for speed
192
        // Data one bye one, for speed
193
        cm.data[0]=RXB0D0;  cm.data[1]=RXB0D1;
193
        cm.data[0]=RXB0D0;  cm.data[1]=RXB0D1;
194
        cm.data[2]=RXB0D2;  cm.data[3]=RXB0D3;
194
        cm.data[2]=RXB0D2;  cm.data[3]=RXB0D3;
195
        cm.data[4]=RXB0D4;  cm.data[5]=RXB0D5;
195
        cm.data[4]=RXB0D4;  cm.data[5]=RXB0D5;
196
        cm.data[6]=RXB0D6;  cm.data[7]=RXB0D7;
196
        cm.data[6]=RXB0D6;  cm.data[7]=RXB0D7;
197
 
197
 
198
        // Clear ful status
198
        // Clear ful status
199
        RXB0CONbits.RXFUL=0;
199
        RXB0CONbits.RXFUL=0;
200
 
200
 
201
        //Call main parser
201
        //Call main parser
202
        canParse(cm);
202
        canParse(cm);
203
}
203
}
204
 
204
 
205
 
205
 
206
/*
206
/*
207
*   Function: canSendMessage
207
*   Function: canSendMessage
208
*
208
*
209
*   Input:  CAN_MESSAGE to send
209
*   Input:  CAN_MESSAGE to send
210
*   Output: True if packet scheduled sucess, false otherwise.
210
*   Output: True if packet scheduled sucess, false otherwise.
211
*   Pre-conditions: canInit
211
*   Pre-conditions: canInit
212
*   Affects: ..
212
*   Affects: ..
213
*   Depends: ..
213
*   Depends: ..
214
*/
214
*/
215
BOOL canSendMessage(CAN_MESSAGE cm)
215
BOOL canSendMessage(CAN_MESSAGE cm)
216
{
216
{
217
 
-
 
218
 
-
 
219
 
-
 
220
    if ( TXB0CONbits.TXREQ == 0 )  { ECANCON=(ECANCON&0b00000)|0b00011; }
217
    if ( TXB0CONbits.TXREQ == 0 )  { ECANCON=(ECANCON&0b00000)|0b00011; }
221
    if ( TXB1CONbits.TXREQ == 0 )  { ECANCON=(ECANCON&0b00000)|0b00100; }
218
    if ( TXB1CONbits.TXREQ == 0 )  { ECANCON=(ECANCON&0b00000)|0b00100; }
222
    if ( TXB2CONbits.TXREQ == 0 )  { ECANCON=(ECANCON&0b00000)|0b00101; }
219
    if ( TXB2CONbits.TXREQ == 0 )  { ECANCON=(ECANCON&0b00000)|0b00101; }
223
    // None of the transmit buffers were empty. 
220
    // None of the transmit buffers were empty. 
224
    else { return FALSE;}
221
    else { return FALSE;}
225
 
222
 
226
   
-
 
227
    // Set extended mode or not.
-
 
228
    RXB0SIDLbits.EXID=(cm.extended==TRUE?1:0);
-
 
229
 
-
 
230
 
-
 
231
        if (cm.extended==TRUE)
-
 
232
        {
-
 
233
            RXB0SIDL=RXB0SIDL|0b00001000;
-
 
234
        //  _asm 
-
 
235
        //      bsf RXB0SIDL, 3, 0
-
 
236
        //  _endasm 
-
 
237
        }
-
 
238
        else
-
 
239
        {
-
 
240
            RXB0SIDL=RXB0SIDL&0b11110111;
-
 
241
        //  _asm 
-
 
242
        //      bcf RXB0SIDL, 3, 0
-
 
243
        //  _endasm 
-
 
244
        }
-
 
245
 
-
 
246
       
-
 
247
 
-
 
248
 
223
 
249
        if (cm.extended==TRUE)
224
        if (cm.extended==TRUE)
250
        {
225
        {
251
            //Write extended ident.
226
            //Write extended ident.
252
   
227
   
253
            //xx xx xx 28 27 26 25 24   23 22 21 20 19 18 17 16   15 14 13 12 11 10 09 08   07 06 05 04 03 02 01 00
228
            //xx xx xx 28 27 26 25 24   23 22 21 20 19 18 17 16   15 14 13 12 11 10 09 08   07 06 05 04 03 02 01 00
254
 
229
 
255
            //RXB0SIDH 28 27 26 25 24 23 22 21
230
            //RXB0SIDH 28 27 26 25 24 23 22 21
256
            //RXB0SIDL 20 19 18 xx xx xx 17 16
231
            //RXB0SIDL 20 19 18 xx xx xx 17 16
257
            //RXB0EIDH 15 14 13 12 11 10 09 08
232
            //RXB0EIDH 15 14 13 12 11 10 09 08
258
            //RXB0EIDL 07 06 05 04 03 02 01 00
233
            //RXB0EIDL 07 06 05 04 03 02 01 00
259
 
234
 
260
 
235
 
261
            RXB0SIDH =  (BYTE)((cm.ident>>21));
236
            RXB0SIDH =  (BYTE)((cm.ident>>21));
262
            RXB0SIDL = ((BYTE)(cm.ident>>16) & 0x03) + ((BYTE)(cm.ident>>13) & 0xE0);
237
            RXB0SIDL = ((BYTE)(cm.ident>>16) & 0x03) + ((BYTE)(cm.ident>>13) & 0xE0);
263
            RXB0EIDH =  (BYTE)(cm.ident>>8);
238
            RXB0EIDH =  (BYTE)(cm.ident>>8);
264
            RXB0EIDL =  (BYTE)cm.ident;
239
            RXB0EIDL =  (BYTE)cm.ident;
265
 
240
 
266
        }
241
        }
267
        else
242
        else
268
        {
243
        {
269
            //Write standard ident.
244
            //Write standard ident.
270
       
245
       
271
            //xx xx xx xx xx 10 09 08   07 06 05 04 03 02 01 00
246
            //xx xx xx xx xx 10 09 08   07 06 05 04 03 02 01 00
272
 
247
 
273
            //RXB0SIDH 10 09 08 07 06 05 04 03
248
            //RXB0SIDH 10 09 08 07 06 05 04 03
274
            //RXB0SIDL 02 01 00 xx xx xx xx xx
249
            //RXB0SIDL 02 01 00 xx xx xx xx xx
275
 
250
 
276
            RXB0SIDH=  (BYTE)(cm.ident>>3);
251
            RXB0SIDH=  (BYTE)(cm.ident>>3);
277
            RXB0SIDL= ((BYTE)(cm.ident<<5) & 0xE0);
252
            RXB0SIDL= ((BYTE)(cm.ident<<5) & 0xE0);
278
 
253
 
279
        }
254
        }
280
 
255
 
281
        // Data length
256
        // Data length
282
        if (cm.data_length>8) RXB0DLC=8; else RXB0DLC=cm.data_length;
257
        if (cm.data_length>8) RXB0DLC=8; else RXB0DLC=cm.data_length;
283
 
258
 
284
        // Data one bye one, for speed
259
        // Data one bye one, for speed
285
        RXB0D0=cm.data[0];  RXB0D1=cm.data[1];
260
        RXB0D0=cm.data[0];  RXB0D1=cm.data[1];
286
        RXB0D2=cm.data[2];  RXB0D3=cm.data[3];
261
        RXB0D2=cm.data[2];  RXB0D3=cm.data[3];
287
        RXB0D4=cm.data[4];  RXB0D5=cm.data[5];
262
        RXB0D4=cm.data[4];  RXB0D5=cm.data[5];
288
        RXB0D6=cm.data[6];  RXB0D7=cm.data[7];
263
        RXB0D6=cm.data[6];  RXB0D7=cm.data[7];
-
 
264
 
-
 
265
        // set extended or not
-
 
266
        RXB0SIDLbits.EXID=(cm.extended==TRUE?1:0);
-
 
267
 
289
 
268
 
290
        // mark as redy to transmit
269
        // mark as redy to transmit
291
        _asm
270
        _asm
292
            bsf RXB0CON, 3, 0
271
            bsf RXB0CON, 3, 0
293
        _endasm
272
        _endasm
294
 
273
 
295
 
274
 
296
        return TRUE;
275
        return TRUE;
297
 
276
 
298
}
277
}
299
 
278
 
300
 
279
 
301
 
280
 
302
#endif //USE_CAN
281
#endif //USE_CAN
303
 
282