Subversion Repositories HomeAutomation

Rev

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

Rev 73 Rev 97
Line 1... Line 1...
1
/*********************************************************************
1
/*********************************************************************
2
 *
2
 *
3
 *                  CAN driver
3
 *                  CAN driver
4
 *
4
 *
5
 *********************************************************************
5
 *********************************************************************
6
 * FileName:        CAN.c
6
 * FileName:        $HeadURL: https://svn.auml.se/svn/HomeAutomation/trunk/EmbeddedSoftware/PIC/canToSerial/Source/CAN.c $
7
 *
-
 
8
 * Author               Date    Comment
7
 * Last changed:    $LastChangedDate: 2006-11-14 21:22:17 +0100 (Tue, 14 Nov 2006) $
9
 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8
 * By:              $LastChangedBy: johboh $
10
 * Johan Böhlin     21-10-06    Original        (Rev 1.0)
9
 * Revision:        $Revision: 97 $
11
 ********************************************************************/
10
 ********************************************************************/
12
 
11
 
13
#include <CANdefs.h>
12
#include <CANdefs.h>
14
#include <stackTasks.h>
13
#include <stackTasks.h>
15
#include <CAN.h>
14
#include <CAN.h>
16
 
15
 
17
static void canGetPacket(void);
16
static void canGetPacket(void);
18
 
17
 
19
#ifdef USE_CAN
18
#ifdef USE_CAN
20
 
19
 
21
 
20
 
22
/*
21
/*
23
*   Function: canInit
22
*   Function: canInit
24
*
23
*
25
*   Input:  none
24
*   Input:  none
26
*   Output: none
25
*   Output: none
27
*   Pre-conditions: none
26
*   Pre-conditions: none
28
*   Affects: Changes can registers.
27
*   Affects: Changes can registers.
29
*   Depends: none.
28
*   Depends: none.
Line 60... Line 59...
60
    //  Enableing bus wake-up
59
    //  Enableing bus wake-up
61
    BRGCON3bits.WAKDIS = 0;
60
    BRGCON3bits.WAKDIS = 0;
62
   
61
   
63
    // Dont use filter when wakeup
62
    // Dont use filter when wakeup
64
    BRGCON3bits.WAKFIL = 0;
63
    BRGCON3bits.WAKFIL = 0;
65
   
64
   
66
    // Phase_Seg2 = 2 x TQ = 1
65
    // Phase_Seg2 = 2 x TQ = 1
67
    BRGCON3bits.SEG2PH2 = 0;
66
    BRGCON3bits.SEG2PH2 = 0;
68
    BRGCON3bits.SEG2PH1 = 0;
67
    BRGCON3bits.SEG2PH1 = 0;
69
    BRGCON3bits.SEG2PH0 = 1;
68
    BRGCON3bits.SEG2PH0 = 1;
70
 
69
 
71
    ECANCON=0;
70
    ECANCON=0;
72
    ECANCONbits.MDSEL1 = 0;
71
    ECANCONbits.MDSEL1 = 1;
73
    ECANCONbits.MDSEL0 = 1;
72
    ECANCONbits.MDSEL0 = 0;
74
 
73
 
75
 
74
 
76
    // FILTERS AND MASKS
75
    // FILTERS AND MASKS
77
    RXB0CONbits.RXM1=1;
76
    RXB0CONbits.RXM1=1;
78
    RXB0CONbits.RXM0=0;
77
    RXB0CONbits.RXM0=0;
Line 102... Line 101...
102
        CANCON = 0x00;//request normal mode
101
        CANCON = 0x00;//request normal mode
103
        while ((CANSTAT & 0xE0) != 0x00 );//wait for normal mode
102
        while ((CANSTAT & 0xE0) != 0x00 );//wait for normal mode
104
    #endif
103
    #endif
105
 
104
 
106
}
105
}
107
 
106
 
108
 
107
 
109
/*
108
/*
110
*   Function: canISR
109
*   Function: canISR
111
*
110
*
112
*   Input:  none
111
*   Input:  none
113
*   Output: none
112
*   Output: none
Line 115... Line 114...
115
*   Affects: interrupt registers and receive data buffers
114
*   Affects: interrupt registers and receive data buffers
116
*   Depends: ..
115
*   Depends: ..
117
*/
116
*/
118
void canISR()
117
void canISR()
119
{
118
{
120
   
-
 
121
 
-
 
122
    if (PIR3bits.RXBnIF || PIR3bits.RXB1IF || PIR3bits.RXB0IF)
119
    if (PIR3bits.RXBnIF || PIR3bits.RXB1IF || PIR3bits.RXB0IF)
123
    {
120
    {
124
        // Get which buffer that is ful.
-
 
125
             if (RXB0CONbits.RXFUL) { ECANCON=(ECANCON&0b00000)|0b10000; canGetPacket(); }
121
        ECANCON=(ECANCON&0b00000)|(0b10000|(CANCON&0x0F));
126
        else if (RXB1CONbits.RXFUL) { ECANCON=(ECANCON&0b00000)|0b10001; canGetPacket(); }
122
        if (RXB0CONbits.RXFUL) canGetPacket();
127
        else if (B0CONbits.RXFUL)   { ECANCON=(ECANCON&0b00000)|0b10010; canGetPacket(); }
-
 
128
        else if (B1CONbits.RXFUL)   { ECANCON=(ECANCON&0b00000)|0b10011; canGetPacket(); }
-
 
129
        else if (B2CONbits.RXFUL)   { ECANCON=(ECANCON&0b00000)|0b10100; canGetPacket(); }
-
 
130
        else if (B3CONbits.RXFUL)   { ECANCON=(ECANCON&0b00000)|0b10101; canGetPacket(); }
-
 
131
        else if (B4CONbits.RXFUL)   { ECANCON=(ECANCON&0b00000)|0b10110; canGetPacket(); }
-
 
132
        else if (B5CONbits.RXFUL)   { ECANCON=(ECANCON&0b00000)|0b10111; canGetPacket(); }
-
 
133
   
-
 
134
        if (PIR3bits.RXBnIF) {PIR3bits.RXBnIF = 0;}
-
 
135
        if (PIR3bits.RXB1IF) {PIR3bits.RXB1IF = 0;}
-
 
136
        if (PIR3bits.RXB0IF) {PIR3bits.RXB0IF = 0;}
-
 
137
   
123
   
-
 
124
        if (PIR3bits.RXBnIF) {PIR3bits.RXBnIF = 0; return;}
-
 
125
        if (PIR3bits.RXB1IF) {PIR3bits.RXB1IF = 0; return;}
-
 
126
        if (PIR3bits.RXB0IF) {PIR3bits.RXB0IF = 0; return;}
138
    }
127
    }
139
}
128
}
140
 
129
 
141
 
130
 
142
/*
131
/*