Subversion Repositories HomeAutomation

Rev

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

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