Subversion Repositories HomeAutomation

Rev

Rev 95 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 95 Rev 260
Line 2... Line 2...
2
 *
2
 *
3
 *                  CAN module
3
 *                  CAN module
4
 *
4
 *
5
 *********************************************************************
5
 *********************************************************************
6
 * FileName:        $HeadURL: https://svn.auml.se/svn/HomeAutomation/trunk/EmbeddedSoftware/PIC/canBase/Source/CAN.c $
6
 * FileName:        $HeadURL: https://svn.auml.se/svn/HomeAutomation/trunk/EmbeddedSoftware/PIC/canBase/Source/CAN.c $
7
 * Last changed:    $LastChangedDate: 2006-11-14 21:11:28 +0100 (Tue, 14 Nov 2006) $
7
 * Last changed:    $LastChangedDate: 2007-01-28 14:58:11 +0100 (Sun, 28 Jan 2007) $
8
 * By:              $LastChangedBy: johboh $
8
 * By:              $LastChangedBy: johboh $
9
 * Revision:        $Revision: 95 $
9
 * Revision:        $Revision: 260 $
10
 ********************************************************************/
10
 ********************************************************************/
11
 
11
 
12
#include <CANdefs.h>
12
#include <CANdefs.h>
13
#include <stackTasks.h>
13
#include <stackTasks.h>
14
#include <CAN.h>
14
#include <CAN.h>
-
 
15
#include <EEaccess.h>
-
 
16
#include <funcdefs.h>
-
 
17
#include <Tick.h>
-
 
18
 
-
 
19
TICK heartbeat;
-
 
20
static int MY_ID = DEFAULT_ID;
-
 
21
static BYTE MY_NID = DEFAULT_NID;
-
 
22
static CAN_MESSAGE outCm;
15
 
23
 
16
static void canGetPacket(void);
24
static void canGetPacket(void);
17
 
25
 
18
#ifdef USE_CAN
-
 
19
 
-
 
20
 
26
 
21
/*
27
/*
22
*   Function: canInit
28
*   Function: canInit
23
*
29
*
24
*   Input:  none
30
*   Input:  none
25
*   Output: none
31
*   Output: none
Line 31... Line 37...
31
{
37
{
32
    CANCON = 0x80; //request config mode
38
    CANCON = 0x80; //request config mode
33
    while ((CANSTAT & 0xE0) != 0x80 ); //wait for config mode request
39
    while ((CANSTAT & 0xE0) != 0x80 ); //wait for config mode request
34
 
40
 
35
    // BAUD AND TQS
41
    // BAUD AND TQS
36
 
42
 
-
 
43
    // 500Khz@40Mhz (new by the book)
-
 
44
    BRGCON1 = 0x04;
-
 
45
    BRGCON2 = 0xD1;
-
 
46
    BRGCON3 = 0x81;
-
 
47
 
37
    BRGCON1=0;
48
    //BRGCON1=0;
38
   
49
   
39
    // Sync_Seq = 2 x TQ = 1
50
    // Sync_Seq = 2 x TQ = 1
40
    BRGCON1bits.SJW1=0;
51
    //BRGCON1bits.SJW1=0;
41
    BRGCON1bits.SJW0=1;
52
    //BRGCON1bits.SJW0=1;
42
   
53
   
43
    // Setting BRP.
54
    // Setting BRP.
44
    BRGCON1=BRGCON1|CAN_BRP;
55
    //BRGCON1=BRGCON1|CAN_BRP;
45
   
56
   
46
    // Maximum PHEG1
57
    // Maximum PHEG1
47
    BRGCON2bits.SEG2PHTS = 1;
58
    //BRGCON2bits.SEG2PHTS = 1;
48
   
59
   
49
    // Phase_Seg1 = 2 x TQ = 1
60
    // Phase_Seg1 = 2 x TQ = 1
50
    BRGCON2bits.SEG1PH2 = 0;
61
    //BRGCON2bits.SEG1PH2 = 0;
51
    BRGCON2bits.SEG1PH1 = 0;
62
    //BRGCON2bits.SEG1PH1 = 0;
52
    BRGCON2bits.SEG1PH0 = 1;
63
    //BRGCON2bits.SEG1PH0 = 1;
53
   
64
   
54
    // Prop_Seq = 2 x TQ = 1
65
    // Prop_Seq = 2 x TQ = 1
55
    BRGCON2bits.PRSEG2 = 0;
66
    //BRGCON2bits.PRSEG2 = 0;
56
    BRGCON2bits.PRSEG1 = 0;
67
    //BRGCON2bits.PRSEG1 = 0;
57
    BRGCON2bits.PRSEG0 = 1;
68
    //BRGCON2bits.PRSEG0 = 1;
58
   
69
   
59
    //  Enableing bus wake-up
70
    //  Enableing bus wake-up
60
    BRGCON3bits.WAKDIS = 0;
71
    //BRGCON3bits.WAKDIS = 0;
61
   
72
   
62
    // Dont use filter when wakeup
73
    // Dont use filter when wakeup
63
    BRGCON3bits.WAKFIL = 0;
74
    //BRGCON3bits.WAKFIL = 0;
64
   
75
   
65
    // Phase_Seg2 = 2 x TQ = 1
76
    // Phase_Seg2 = 2 x TQ = 1
66
    BRGCON3bits.SEG2PH2 = 0;
77
    //BRGCON3bits.SEG2PH2 = 0;
67
    BRGCON3bits.SEG2PH1 = 0;
78
    //BRGCON3bits.SEG2PH1 = 0;
68
    BRGCON3bits.SEG2PH0 = 1;
79
    //BRGCON3bits.SEG2PH0 = 1;
69
 
80
 
70
    ECANCON=0;
81
    ECANCON=0;
71
    ECANCONbits.MDSEL1 = 1; // 0  For mode 1 
82
    ECANCONbits.MDSEL1 = 1; // 0  For mode 1 
72
    ECANCONbits.MDSEL0 = 0; // 1  For mode 1 
83
    ECANCONbits.MDSEL0 = 0; // 1  For mode 1 
73
 
84
 
Line 99... Line 110...
99
        while ((CANSTAT & 0xE0) != 0x40 );//wait for loopback mode
110
        while ((CANSTAT & 0xE0) != 0x40 );//wait for loopback mode
100
    #else
111
    #else
101
        CANCON = 0x00;//request normal mode
112
        CANCON = 0x00;//request normal mode
102
        while ((CANSTAT & 0xE0) != 0x00 );//wait for normal mode
113
        while ((CANSTAT & 0xE0) != 0x00 );//wait for normal mode
103
    #endif
114
    #endif
-
 
115
 
-
 
116
 
-
 
117
    // Init ticker service
-
 
118
    tickInit();
-
 
119
 
-
 
120
 
-
 
121
    // Read ID and NID if exist.
-
 
122
    if (EERead(NODE_ID_EE)==NODE_HAS_ID)
-
 
123
    {
-
 
124
        MY_ID=(((WORD)EERead(NODE_ID_EE + 1))<<8)+EERead(NODE_ID_EE + 2);
-
 
125
        MY_NID=EERead(NODE_ID_EE + 3);
-
 
126
    }
-
 
127
 
-
 
128
    // Send user program startup heatbeat
-
 
129
    outCm.funct                     = FUNCT_BOOTLOADER;
-
 
130
    outCm.funcc                     = FUNCC_BOOT_HEARTBEAT;
-
 
131
    outCm.data_length               = 1;
-
 
132
    outCm.data[BOOT_DATA_HEARTBEAT_INDEX] = HEARTBEAT_USER_STARTUP;
-
 
133
    while(!canSendMessage(outCm,PRIO_HIGH));
104
 
134
 
105
}
135
}
106
 
136
 
107
 
137
 
108
/*
138
/*
Line 123... Line 153...
123
   
153
   
124
        if (PIR3bits.RXBnIF) {PIR3bits.RXBnIF = 0; return;}
154
        if (PIR3bits.RXBnIF) {PIR3bits.RXBnIF = 0; return;}
125
        if (PIR3bits.RXB1IF) {PIR3bits.RXB1IF = 0; return;}
155
        if (PIR3bits.RXB1IF) {PIR3bits.RXB1IF = 0; return;}
126
        if (PIR3bits.RXB0IF) {PIR3bits.RXB0IF = 0; return;}
156
        if (PIR3bits.RXB0IF) {PIR3bits.RXB0IF = 0; return;}
127
    }
157
    }
-
 
158
 
-
 
159
    tickUpdate();
-
 
160
 
-
 
161
    if ((tickGet()-heartbeat)>TICK_SECOND*5)
-
 
162
    {
-
 
163
        // Send alive heartbeat
-
 
164
        outCm.funct                     = FUNCT_BOOTLOADER;
-
 
165
        outCm.funcc                     = FUNCC_BOOT_HEARTBEAT;
-
 
166
        outCm.data_length               = 1;
-
 
167
        outCm.data[BOOT_DATA_HEARTBEAT_INDEX] = HEARTBEAT_ALIVE;
-
 
168
        while(!canSendMessage(outCm,PRIO_HIGH));
-
 
169
           
-
 
170
        heartbeat = tickGet();
-
 
171
    }
-
 
172
 
-
 
173
    ClrWdt();
128
}
174
}
129
 
175
 
130
 
176
 
131
/*
177
/*
132
*   Function: canGetPacket
178
*   Function: canGetPacket
Line 167... Line 213...
167
        cm.data[4]=RXB0D4;  cm.data[5]=RXB0D5;
213
        cm.data[4]=RXB0D4;  cm.data[5]=RXB0D5;
168
        cm.data[6]=RXB0D6;  cm.data[7]=RXB0D7;
214
        cm.data[6]=RXB0D6;  cm.data[7]=RXB0D7;
169
 
215
 
170
        // Clear ful status
216
        // Clear ful status
171
        RXB0CONbits.RXFUL=0;
217
        RXB0CONbits.RXFUL=0;
-
 
218
 
-
 
219
 
-
 
220
 
-
 
221
        // Check if FUNCT_BOOTLOADER specific.
-
 
222
        // reset and id change
-
 
223
        if (cm.funct==FUNCT_BOOTLOADER)
-
 
224
        {
-
 
225
            if (cm.nid==MY_NID && ((((unsigned int)cm.data[BOOT_DATA_RID_HIGH_INDEX])<<8)+cm.data[BOOT_DATA_RID_LOW_INDEX])==MY_ID)
-
 
226
            {
-
 
227
                if (cm.funcc==FUNCC_BOOT_INIT) Reset(); // Reset me.
-
 
228
                if (cm.funcc==FUNCC_BOOT_SET_ID)
-
 
229
                {
-
 
230
                        // Change my ID and NID
-
 
231
                        MY_ID=(((WORD)cm.data[BOOT_DATA_NEW_ID_HIGH_INDEX])<<8)+cm.data[BOOT_DATA_NEW_ID_LOW_INDEX];
-
 
232
                        MY_NID=cm.data[BOOT_DATA_NEW_NID_INDEX];
-
 
233
                        EEWrite(NODE_ID_EE+1,cm.data[BOOT_DATA_NEW_ID_HIGH_INDEX]);
-
 
234
                        EEWrite(NODE_ID_EE+2,cm.data[BOOT_DATA_NEW_ID_LOW_INDEX]);
-
 
235
                        EEWrite(NODE_ID_EE+3,cm.data[BOOT_DATA_NEW_NID_INDEX]);
-
 
236
                        EEWrite(NODE_ID_EE,NODE_HAS_ID);
-
 
237
                        // Send ack with new id.
-
 
238
                        outCm.funct                     = FUNCT_BOOTLOADER;
-
 
239
                        outCm.funcc                     = FUNCC_BOOT_ACK;
-
 
240
                        outCm.data_length               = 8;
-
 
241
                        outCm.data[BOOT_DATA_ERR_INDEX] = ACK_ERR_NO_ERROR;
-
 
242
                        while(!canSendMessage(outCm,PRIO_HIGH));
-
 
243
                }
-
 
244
            }
-
 
245
        }
-
 
246
 
172
 
247
 
173
        //Call main parser
248
        //Call main parser
174
        canParse(cm);
249
        canParse(cm);
175
}
250
}
176
 
251
 
Line 192... Line 267...
192
    if ( TXB2CONbits.TXREQ == 0 )  { ECANCON=(ECANCON&0b00000)|0b00101; }
267
    if ( TXB2CONbits.TXREQ == 0 )  { ECANCON=(ECANCON&0b00000)|0b00101; }
193
    // None of the transmit buffers were empty. 
268
    // None of the transmit buffers were empty. 
194
    else { return FALSE;}
269
    else { return FALSE;}
195
 
270
 
196
    if (prio<0 || prio>3) prio = 0;
271
    if (prio<0 || prio>3) prio = 0;
-
 
272
 
-
 
273
        // Append my id and nid
-
 
274
        cm.nid  = MY_NID;
-
 
275
        cm.sid  = MY_ID;
197
 
276
 
198
 
277
 
199
        //RXB0SIDH 28 27 26 25 24 23 22 21
278
        //RXB0SIDH 28 27 26 25 24 23 22 21
200
        //RXB0SIDL 20 19 18 xx xx xx 17 16
279
        //RXB0SIDL 20 19 18 xx xx xx 17 16
201
        //RXB0EIDH 15 14 13 12 11 10 09 08
280
        //RXB0EIDH 15 14 13 12 11 10 09 08
Line 240... Line 319...
240
        _endasm
319
        _endasm
241
 
320
 
242
        return TRUE;
321
        return TRUE;
243
 
322
 
244
}
323
}
245
 
-
 
246
 
-
 
247
 
-
 
248
#endif //USE_CAN
-