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-02-26 21:38:37 +0100 (Mon, 26 Feb 2007) $
8
 * By:              $LastChangedBy: johboh $
9
 * Revision:        $Revision: 342 $
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
 
260 johboh 19
TICK heartbeat;
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
 
85 johboh 150
    if (PIR3bits.RXBnIF || PIR3bits.RXB1IF || PIR3bits.RXB0IF)
151
    {
95 johboh 152
        ECANCON=(ECANCON&0b00000)|(0b10000|(CANCON&0x0F));
153
        if (RXB0CONbits.RXFUL) canGetPacket();
85 johboh 154
 
95 johboh 155
        if (PIR3bits.RXBnIF) {PIR3bits.RXBnIF = 0; return;}
156
        if (PIR3bits.RXB1IF) {PIR3bits.RXB1IF = 0; return;}
157
        if (PIR3bits.RXB0IF) {PIR3bits.RXB0IF = 0; return;}
85 johboh 158
    }
260 johboh 159
 
160
    tickUpdate();
161
 
162
    if ((tickGet()-heartbeat)>TICK_SECOND*5)
163
    {
164
        // Send alive heartbeat
339 johboh 165
        outCp.type=ptPGM;
166
        outCp.pgm.class=pcCTRL;
167
        outCp.pgm.id=pctHEARTBEAT;
168
        outCp.length=0;
169
        canSendMessage(outCp,PRIO_HIGH);
260 johboh 170
 
171
        heartbeat = tickGet();
172
    }
173
 
339 johboh 174
 
85 johboh 175
}
176
 
177
 
178
/*
179
*   Function: canGetPacket
180
*
181
*   Input:  none
182
*   Output: none
183
*   Pre-conditions: a call to canISR and an packet is available and banked to RXB0
184
*   Affects: Calls canParse() function in main for parsing received packet.
185
*   Depends: none.
186
*/
187
void canGetPacket()
188
{
339 johboh 189
        CAN_PACKET cp;
85 johboh 190
 
86 johboh 191
        //RXB0SIDH 28 27 26 25 24 23 22 21
192
        //RXB0SIDL 20 19 18 xx xx xx 17 16
193
        //RXB0EIDH 15 14 13 12 11 10 09 08
194
        //RXB0EIDL 07 06 05 04 03 02 01 00
85 johboh 195
 
196
 
342 johboh 197
        cp.type = (BYTE)((RXB0SIDH&0xC0)>>6);
85 johboh 198
 
199
 
339 johboh 200
        if (cp.type==ptBOOT)
201
        {
202
            cp.boot.type  = ((RXB0SIDH&0x38)>>3);
203
            cp.boot.offset= ((RXB0SIDH&0x7)<<5)+((RXB0SIDL&0xE0)>>3)+(RXB0SIDL&0x3);
204
            cp.boot.rid   = RXB0EIDH;
205
        }
206
        else
207
        {
208
            cp.pgm.class= ((RXB0SIDH&0x3C)>>2);
342 johboh 209
            cp.pgm.id   = (((WORD)RXB0SIDH&0x3)<<13)+(((WORD)RXB0SIDL&0xE0)<<5)+(((WORD)RXB0SIDL&0x3)<<8)+(WORD)RXB0EIDH;
339 johboh 210
        }
211
        cp.sid   = RXB0EIDL;
85 johboh 212
 
339 johboh 213
 
85 johboh 214
        // Data length
339 johboh 215
        cp.length=(RXB0DLCbits.DLC3<<3)+(RXB0DLCbits.DLC2<<2)+(RXB0DLCbits.DLC1<<1)+RXB0DLCbits.DLC0;
85 johboh 216
 
217
        // Data one bye one, for speed
339 johboh 218
        cp.data[0]=RXB0D0;  cp.data[1]=RXB0D1;
219
        cp.data[2]=RXB0D2;  cp.data[3]=RXB0D3;
220
        cp.data[4]=RXB0D4;  cp.data[5]=RXB0D5;
221
        cp.data[6]=RXB0D6;  cp.data[7]=RXB0D7;
85 johboh 222
 
223
        // Clear ful status
224
        RXB0CONbits.RXFUL=0;
225
 
260 johboh 226
 
227
 
339 johboh 228
        // Check if bootloader request specific.
260 johboh 229
        // reset and id change
339 johboh 230
        if (cp.type==ptBOOT)
260 johboh 231
        {
339 johboh 232
            if (cp.boot.rid==MY_ID)
260 johboh 233
            {
339 johboh 234
                if (cp.boot.type==btADDR) Reset(); // Reset me.
235
                if (cp.boot.type==btCHANGEID)
260 johboh 236
                {
237
                        // Change my ID and NID
339 johboh 238
                        MY_ID=cp.data[0];
239
                        EEWrite(NODE_ID_EE+1,cp.data[0]);
260 johboh 240
                        EEWrite(NODE_ID_EE,NODE_HAS_ID);
241
                        // Send ack with new id.
339 johboh 242
                        outCp.type=ptBOOT;
243
                        outCp.boot.type=btACK;
244
                        outCp.length=0;
245
                        while(!canSendMessage(outCp,PRIO_HIGH));
260 johboh 246
                }
247
            }
248
        }
249
 
250
 
85 johboh 251
        //Call main parser
339 johboh 252
        if (cp.type==ptPGM) canParse(cp);
85 johboh 253
}
254
 
255
 
256
/*
257
*   Function: canSendMessage
258
*
259
*   Input:  CAN_MESSAGE to send
260
*   Output: True if packet scheduled sucess, false otherwise.
261
*   Pre-conditions: canInit
262
*   Affects: ..
263
*   Depends: ..
264
*/
339 johboh 265
BOOL canSendMessage(CAN_PACKET cp, CAN_PRIORITY prio)
85 johboh 266
{
267
 
268
    if ( TXB0CONbits.TXREQ == 0 )  { ECANCON=(ECANCON&0b00000)|0b00011; }
269
    if ( TXB1CONbits.TXREQ == 0 )  { ECANCON=(ECANCON&0b00000)|0b00100; }
270
    if ( TXB2CONbits.TXREQ == 0 )  { ECANCON=(ECANCON&0b00000)|0b00101; }
271
    // None of the transmit buffers were empty. 
272
    else { return FALSE;}
273
 
274
    if (prio<0 || prio>3) prio = 0;
275
 
276
 
339 johboh 277
    //RXB0SIDH 28 27 26 25 24 23 22 21
278
    //RXB0SIDL 20 19 18 xx xx xx 17 16
279
    //RXB0EIDH 15 14 13 12 11 10 09 08
280
    //RXB0EIDL 07 06 05 04 03 02 01 00
260 johboh 281
 
85 johboh 282
 
339 johboh 283
    if (cp.type==ptBOOT)
284
    {
285
        RXB0SIDH = ((cp.type&0x3)<<6)+((cp.boot.type&0x7)<<3)+((cp.boot.offset&0xE0)>>5);
286
        RXB0SIDL = ((cp.boot.offset&0x1C)<<3)+(cp.boot.offset&0x3);
287
        RXB0EIDH = cp.boot.rid;
288
    }
289
    else
290
    {
291
        RXB0SIDH = ((cp.type&0x3)<<6)+((cp.pgm.class&0xF)<<2)+((cp.pgm.id&0x6000)>>13);
292
        RXB0SIDL = ((cp.pgm.id&0x1C00)>>5)+((cp.pgm.id&0x300)>>8);
293
        RXB0EIDH = (cp.pgm.id&0xFF);
294
    }
295
    RXB0EIDL = MY_ID;
85 johboh 296
 
339 johboh 297
    // Data length
298
    RXB0DLC = 0;
299
    if (cp.length>8) RXB0DLC=8; else RXB0DLC=cp.length;
300
 
301
    // NOT Remote request
302
    _asm
303
        bcf RXB0DLC, 6, 0
304
    _endasm
305
 
306
 
307
    // Data one bye one, for speed
308
    RXB0D0=cp.data[0];  RXB0D1=cp.data[1];
309
    RXB0D2=cp.data[2];  RXB0D3=cp.data[3];
310
    RXB0D4=cp.data[4];  RXB0D5=cp.data[5];
311
    RXB0D6=cp.data[6];  RXB0D7=cp.data[7];
312
    // set extended
313
    RXB0SIDLbits.EXID=1;
314
 
315
    // Priority
316
    RXB0CON = (RXB0CON & 0b11111100) | prio;
317
 
318
    // mark as redy to transmit
319
    _asm
320
        bsf RXB0CON, 3, 0
321
    _endasm
322
 
323
    return TRUE;
85 johboh 324
 
325
}