Subversion Repositories HomeAutomation

Rev

Details | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
85 johboh 1
/*********************************************************************
2
 *
86 johboh 3
 *                  CAN module
85 johboh 4
 *
5
 *********************************************************************
86 johboh 6
 * FileName:        $HeadURL: https://svn.auml.se/svn/HomeAutomation/trunk/EmbeddedSoftware/PIC/canBase/Source/CAN.c $
7
 * Last changed:    $LastChangedDate: 2007-03-03 19:07:14 +0100 (Sat, 03 Mar 2007) $
8
 * By:              $LastChangedBy: johboh $
9
 * Revision:        $Revision: 353 $
85 johboh 10
 ********************************************************************/
11
 
12
#include <CANdefs.h>
13
#include <stackTasks.h>
14
#include <CAN.h>
260 johboh 15
#include <EEaccess.h>
16
#include <funcdefs.h>
17
#include <Tick.h>
85 johboh 18
 
353 johboh 19
static TICK heartbeat;
260 johboh 20
static int MY_ID = DEFAULT_ID;
339 johboh 21
static CAN_PACKET outCp;
260 johboh 22
 
85 johboh 23
static void canGetPacket(void);
24
 
25
 
26
/*
27
*   Function: canInit
28
*
29
*   Input:  none
30
*   Output: none
31
*   Pre-conditions: none
32
*   Affects: Changes can registers.
33
*   Depends: none.
34
*/
35
void canInit()
36
{
37
    CANCON = 0x80; //request config mode
38
    while ((CANSTAT & 0xE0) != 0x80 ); //wait for config mode request
39
 
40
    // BAUD AND TQS
41
 
260 johboh 42
    // 500Khz@40Mhz (new by the book)
43
    BRGCON1 = 0x04;
44
    BRGCON2 = 0xD1;
45
    BRGCON3 = 0x81;
46
 
47
    //BRGCON1=0;
85 johboh 48
 
49
    // Sync_Seq = 2 x TQ = 1
260 johboh 50
    //BRGCON1bits.SJW1=0;
51
    //BRGCON1bits.SJW0=1;
85 johboh 52
 
53
    // Setting BRP.
260 johboh 54
    //BRGCON1=BRGCON1|CAN_BRP;
85 johboh 55
 
56
    // Maximum PHEG1
260 johboh 57
    //BRGCON2bits.SEG2PHTS = 1;
85 johboh 58
 
59
    // Phase_Seg1 = 2 x TQ = 1
260 johboh 60
    //BRGCON2bits.SEG1PH2 = 0;
61
    //BRGCON2bits.SEG1PH1 = 0;
62
    //BRGCON2bits.SEG1PH0 = 1;
85 johboh 63
 
64
    // Prop_Seq = 2 x TQ = 1
260 johboh 65
    //BRGCON2bits.PRSEG2 = 0;
66
    //BRGCON2bits.PRSEG1 = 0;
67
    //BRGCON2bits.PRSEG0 = 1;
85 johboh 68
 
69
    //  Enableing bus wake-up
260 johboh 70
    //BRGCON3bits.WAKDIS = 0;
85 johboh 71
 
72
    // Dont use filter when wakeup
260 johboh 73
    //BRGCON3bits.WAKFIL = 0;
85 johboh 74
 
75
    // Phase_Seg2 = 2 x TQ = 1
260 johboh 76
    //BRGCON3bits.SEG2PH2 = 0;
77
    //BRGCON3bits.SEG2PH1 = 0;
78
    //BRGCON3bits.SEG2PH0 = 1;
85 johboh 79
 
80
    ECANCON=0;
95 johboh 81
    ECANCONbits.MDSEL1 = 1; // 0  For mode 1 
82
    ECANCONbits.MDSEL0 = 0; // 1  For mode 1 
85 johboh 83
 
84
 
85
    // FILTERS AND MASKS
86
    RXB0CONbits.RXM1=1;
87
    RXB0CONbits.RXM0=0;
88
 
89
 
90
    // INTERRUPTS
91
 
92
    // Diable transmit interrupt
93
    TXBIE = 0;
94
 
95
    //rx any interrupt
96
    PIR3 = 0;
97
    PIE3 = 0b00000011;
98
 
99
    // High interrupt
100
    IPR3 =0b00000011;
101
 
102
    // Programmable buffers interrupt
103
    BIE0  = 0xFF;  
104
 
105
 
106
    //loopback mode or not
107
    #ifdef CAN_LOOPBACK_MODE
108
        CANCON = 0x40;//request loopback mode
109
        while ((CANSTAT & 0xE0) != 0x40 );//wait for loopback mode
110
    #else
111
        CANCON = 0x00;//request normal mode
112
        while ((CANSTAT & 0xE0) != 0x00 );//wait for normal mode
113
    #endif
114
 
260 johboh 115
 
116
    // Init ticker service
117
    tickInit();
118
 
119
 
120
    // Read ID and NID if exist.
121
    if (EERead(NODE_ID_EE)==NODE_HAS_ID)
122
    {
339 johboh 123
        MY_ID=+EERead(NODE_ID_EE + 1);
260 johboh 124
    }
125
 
126
    // Send user program startup heatbeat
339 johboh 127
    outCp.type=ptPGM;
128
    outCp.pgm.class=pcCTRL;
129
    outCp.pgm.id=pctAPPBOOT;
342 johboh 130
    outCp.length=2;
131
    outCp.data[1]=(BYTE)((APP_VERSION & 0xFF00)>>8);
132
    outCp.data[0]=(BYTE)(APP_VERSION & 0xFF);
339 johboh 133
    while(!canSendMessage(outCp,PRIO_HIGH));
85 johboh 134
}
135
 
136
 
137
/*
138
*   Function: canISR
139
*
140
*   Input:  none
141
*   Output: none
142
*   Pre-conditions: a call to canInit()
143
*   Affects: interrupt registers and receive data buffers
144
*   Depends: ..
145
*/
146
void canISR()
147
{
339 johboh 148
    ClrWdt();
149
 
353 johboh 150
    tickUpdate();
151
 
85 johboh 152
    if (PIR3bits.RXBnIF || PIR3bits.RXB1IF || PIR3bits.RXB0IF)
153
    {
95 johboh 154
        ECANCON=(ECANCON&0b00000)|(0b10000|(CANCON&0x0F));
155
        if (RXB0CONbits.RXFUL) canGetPacket();
85 johboh 156
 
95 johboh 157
        if (PIR3bits.RXBnIF) {PIR3bits.RXBnIF = 0; return;}
158
        if (PIR3bits.RXB1IF) {PIR3bits.RXB1IF = 0; return;}
159
        if (PIR3bits.RXB0IF) {PIR3bits.RXB0IF = 0; return;}
85 johboh 160
    }
260 johboh 161
 
353 johboh 162
 
260 johboh 163
 
353 johboh 164
    if ((tickGet()-heartbeat)>TICK_1S*5)
260 johboh 165
    {
166
        // Send alive heartbeat
339 johboh 167
        outCp.type=ptPGM;
168
        outCp.pgm.class=pcCTRL;
169
        outCp.pgm.id=pctHEARTBEAT;
170
        outCp.length=0;
171
        canSendMessage(outCp,PRIO_HIGH);
260 johboh 172
 
173
        heartbeat = tickGet();
174
    }
175
 
339 johboh 176
 
85 johboh 177
}
178
 
179
 
180
/*
181
*   Function: canGetPacket
182
*
183
*   Input:  none
184
*   Output: none
185
*   Pre-conditions: a call to canISR and an packet is available and banked to RXB0
186
*   Affects: Calls canParse() function in main for parsing received packet.
187
*   Depends: none.
188
*/
189
void canGetPacket()
190
{
339 johboh 191
        CAN_PACKET cp;
85 johboh 192
 
86 johboh 193
        //RXB0SIDH 28 27 26 25 24 23 22 21
194
        //RXB0SIDL 20 19 18 xx xx xx 17 16
195
        //RXB0EIDH 15 14 13 12 11 10 09 08
196
        //RXB0EIDL 07 06 05 04 03 02 01 00
85 johboh 197
 
198
 
342 johboh 199
        cp.type = (BYTE)((RXB0SIDH&0xC0)>>6);
85 johboh 200
 
201
 
339 johboh 202
        if (cp.type==ptBOOT)
203
        {
204
            cp.boot.type  = ((RXB0SIDH&0x38)>>3);
205
            cp.boot.offset= ((RXB0SIDH&0x7)<<5)+((RXB0SIDL&0xE0)>>3)+(RXB0SIDL&0x3);
206
            cp.boot.rid   = RXB0EIDH;
207
        }
208
        else
209
        {
210
            cp.pgm.class= ((RXB0SIDH&0x3C)>>2);
342 johboh 211
            cp.pgm.id   = (((WORD)RXB0SIDH&0x3)<<13)+(((WORD)RXB0SIDL&0xE0)<<5)+(((WORD)RXB0SIDL&0x3)<<8)+(WORD)RXB0EIDH;
339 johboh 212
        }
213
        cp.sid   = RXB0EIDL;
85 johboh 214
 
339 johboh 215
 
85 johboh 216
        // Data length
339 johboh 217
        cp.length=(RXB0DLCbits.DLC3<<3)+(RXB0DLCbits.DLC2<<2)+(RXB0DLCbits.DLC1<<1)+RXB0DLCbits.DLC0;
85 johboh 218
 
219
        // Data one bye one, for speed
339 johboh 220
        cp.data[0]=RXB0D0;  cp.data[1]=RXB0D1;
221
        cp.data[2]=RXB0D2;  cp.data[3]=RXB0D3;
222
        cp.data[4]=RXB0D4;  cp.data[5]=RXB0D5;
223
        cp.data[6]=RXB0D6;  cp.data[7]=RXB0D7;
85 johboh 224
 
225
        // Clear ful status
226
        RXB0CONbits.RXFUL=0;
227
 
260 johboh 228
 
229
 
339 johboh 230
        // Check if bootloader request specific.
260 johboh 231
        // reset and id change
339 johboh 232
        if (cp.type==ptBOOT)
260 johboh 233
        {
339 johboh 234
            if (cp.boot.rid==MY_ID)
260 johboh 235
            {
339 johboh 236
                if (cp.boot.type==btADDR) Reset(); // Reset me.
237
                if (cp.boot.type==btCHANGEID)
260 johboh 238
                {
239
                        // Change my ID and NID
339 johboh 240
                        MY_ID=cp.data[0];
241
                        EEWrite(NODE_ID_EE+1,cp.data[0]);
260 johboh 242
                        EEWrite(NODE_ID_EE,NODE_HAS_ID);
243
                        // Send ack with new id.
339 johboh 244
                        outCp.type=ptBOOT;
245
                        outCp.boot.type=btACK;
246
                        outCp.length=0;
247
                        while(!canSendMessage(outCp,PRIO_HIGH));
260 johboh 248
                }
249
            }
250
        }
251
 
252
 
85 johboh 253
        //Call main parser
339 johboh 254
        if (cp.type==ptPGM) canParse(cp);
85 johboh 255
}
256
 
257
 
258
/*
259
*   Function: canSendMessage
260
*
261
*   Input:  CAN_MESSAGE to send
262
*   Output: True if packet scheduled sucess, false otherwise.
263
*   Pre-conditions: canInit
264
*   Affects: ..
265
*   Depends: ..
266
*/
339 johboh 267
BOOL canSendMessage(CAN_PACKET cp, CAN_PRIORITY prio)
85 johboh 268
{
269
 
270
    if ( TXB0CONbits.TXREQ == 0 )  { ECANCON=(ECANCON&0b00000)|0b00011; }
353 johboh 271
    else if ( TXB1CONbits.TXREQ == 0 )  { ECANCON=(ECANCON&0b00000)|0b00100; }
272
    else if ( TXB2CONbits.TXREQ == 0 )  { ECANCON=(ECANCON&0b00000)|0b00101; }
273
    else { return FALSE;}  // None of the transmit buffers were empty. 
85 johboh 274
 
275
    if (prio<0 || prio>3) prio = 0;
276
 
277
 
339 johboh 278
    //RXB0SIDH 28 27 26 25 24 23 22 21
279
    //RXB0SIDL 20 19 18 xx xx xx 17 16
280
    //RXB0EIDH 15 14 13 12 11 10 09 08
281
    //RXB0EIDL 07 06 05 04 03 02 01 00
260 johboh 282
 
85 johboh 283
 
339 johboh 284
    if (cp.type==ptBOOT)
285
    {
286
        RXB0SIDH = ((cp.type&0x3)<<6)+((cp.boot.type&0x7)<<3)+((cp.boot.offset&0xE0)>>5);
287
        RXB0SIDL = ((cp.boot.offset&0x1C)<<3)+(cp.boot.offset&0x3);
288
        RXB0EIDH = cp.boot.rid;
289
    }
290
    else
291
    {
292
        RXB0SIDH = ((cp.type&0x3)<<6)+((cp.pgm.class&0xF)<<2)+((cp.pgm.id&0x6000)>>13);
293
        RXB0SIDL = ((cp.pgm.id&0x1C00)>>5)+((cp.pgm.id&0x300)>>8);
294
        RXB0EIDH = (cp.pgm.id&0xFF);
295
    }
296
    RXB0EIDL = MY_ID;
85 johboh 297
 
339 johboh 298
    // Data length
299
    RXB0DLC = 0;
300
    if (cp.length>8) RXB0DLC=8; else RXB0DLC=cp.length;
301
 
302
    // NOT Remote request
303
    _asm
304
        bcf RXB0DLC, 6, 0
305
    _endasm
306
 
307
 
308
    // Data one bye one, for speed
309
    RXB0D0=cp.data[0];  RXB0D1=cp.data[1];
310
    RXB0D2=cp.data[2];  RXB0D3=cp.data[3];
311
    RXB0D4=cp.data[4];  RXB0D5=cp.data[5];
312
    RXB0D6=cp.data[6];  RXB0D7=cp.data[7];
313
    // set extended
314
    RXB0SIDLbits.EXID=1;
315
 
316
    // Priority
317
    RXB0CON = (RXB0CON & 0b11111100) | prio;
318
 
319
    // mark as redy to transmit
320
    _asm
321
        bsf RXB0CON, 3, 0
322
    _endasm
323
 
324
    return TRUE;
85 johboh 325
 
326
}