Rev 85 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 85 | Rev 86 | ||
|---|---|---|---|
| Line 1... | Line 1... | ||
| 1 | /********************************************************************* |
1 | /********************************************************************* |
| 2 | * |
2 | * |
| 3 | * CAN |
3 | * CAN module |
| 4 | * |
4 | * |
| 5 | ********************************************************************* |
5 | ********************************************************************* |
| 6 | * FileName: CAN.c |
6 | * FileName: $HeadURL: https://svn.auml.se/svn/HomeAutomation/trunk/EmbeddedSoftware/PIC/canBase/Source/CAN.c $ |
| 7 | * |
- | |
| 8 | * |
7 | * Last changed: $LastChangedDate: 2006-11-14 10:55:38 +0100 (Tue, 14 Nov 2006) $ |
| 9 | * |
8 | * By: $LastChangedBy: johboh $ |
| 10 | * |
9 | * Revision: $Revision: 86 $ |
| 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 | ||
| Line 30... | Line 29... | ||
| 30 | */ |
29 | */ |
| 31 | void canInit() |
30 | void canInit() |
| 32 | { |
31 | { |
| 33 | CANCON = 0x80; //request config mode |
32 | CANCON = 0x80; //request config mode |
| 34 | while ((CANSTAT & 0xE0) != 0x80 ); //wait for config mode request |
33 | while ((CANSTAT & 0xE0) != 0x80 ); //wait for config mode request |
| 35 | 34 | ||
| 36 | // BAUD AND TQS |
35 | // BAUD AND TQS |
| 37 | 36 | ||
| 38 | BRGCON1=0; |
37 | BRGCON1=0; |
| 39 | 38 | ||
| 40 | // Sync_Seq = 2 x TQ = 1 |
39 | // Sync_Seq = 2 x TQ = 1 |
| 41 | BRGCON1bits.SJW1=0; |
40 | BRGCON1bits.SJW1=0; |
| 42 | BRGCON1bits.SJW0=1; |
41 | BRGCON1bits.SJW0=1; |
| 43 | 42 | ||
| 44 | // Setting BRP. |
43 | // Setting BRP. |
| 45 | BRGCON1=BRGCON1|CAN_BRP; |
44 | BRGCON1=BRGCON1|CAN_BRP; |
| 46 | 45 | ||
| 47 | // Maximum PHEG1 |
46 | // Maximum PHEG1 |
| 48 | BRGCON2bits.SEG2PHTS = 1; |
47 | BRGCON2bits.SEG2PHTS = 1; |
| 49 | 48 | ||
| 50 | // Phase_Seg1 = 2 x TQ = 1 |
49 | // Phase_Seg1 = 2 x TQ = 1 |
| 51 | BRGCON2bits.SEG1PH2 = 0; |
50 | BRGCON2bits.SEG1PH2 = 0; |
| 52 | BRGCON2bits.SEG1PH1 = 0; |
51 | BRGCON2bits.SEG1PH1 = 0; |
| 53 | BRGCON2bits.SEG1PH0 = 1; |
52 | BRGCON2bits.SEG1PH0 = 1; |
| 54 | 53 | ||
| 55 | // Prop_Seq = 2 x TQ = 1 |
54 | // Prop_Seq = 2 x TQ = 1 |
| 56 | BRGCON2bits.PRSEG2 = 0; |
55 | BRGCON2bits.PRSEG2 = 0; |
| 57 | BRGCON2bits.PRSEG1 = 0; |
56 | BRGCON2bits.PRSEG1 = 0; |
| 58 | BRGCON2bits.PRSEG0 = 1; |
57 | BRGCON2bits.PRSEG0 = 1; |
| 59 | 58 | ||
| 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; |
| Line 100... | Line 99... | ||
| 100 | while ((CANSTAT & 0xE0) != 0x40 );//wait for loopback mode |
99 | while ((CANSTAT & 0xE0) != 0x40 );//wait for loopback mode |
| 101 | #else |
100 | #else |
| 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 150... | Line 149... | ||
| 150 | */ |
149 | */ |
| 151 | void canGetPacket() |
150 | void canGetPacket() |
| 152 | { |
151 | { |
| 153 | CAN_MESSAGE cm; |
152 | CAN_MESSAGE cm; |
| 154 | 153 | ||
| 155 | // |
154 | //RXB0SIDH 28 27 26 25 24 23 22 21 |
| 156 |
|
155 | //RXB0SIDL 20 19 18 xx xx xx 17 16 |
| 157 | - | ||
| 158 | // Clear |
- | |
| 159 | cm.ident=0; |
- | |
| 160 | - | ||
| 161 | - | ||
| 162 | if (cm.extended==TRUE) |
- | |
| 163 | { |
- | |
| 164 |
|
156 | //RXB0EIDH 15 14 13 12 11 10 09 08 |
| 165 | - | ||
| 166 |
|
157 | //RXB0EIDL 07 06 05 04 03 02 01 00 |
| 167 | 158 | ||
| 168 |
|
159 | //funct xx xx xx xx 28 27 26 25 |
| 169 |
|
160 | //funcc xx xx xx xx xx xx 24 23 22 21 20 19 18 17 16 15 |
| 170 |
|
161 | //nid xx xx 14 13 12 11 10 09 |
| 171 |
|
162 | //sid xx xx xx xx xx xx xx 08 07 06 05 04 03 02 01 00 |
| 172 | 163 | ||
| 173 | 164 | ||
| - | 165 | cm.funct=((RXB0SIDH & 0xF0)>>4); |
|
| 174 |
|
166 | cm.funcc=(((WORD)(RXB0SIDH & 0x0F))<<6)+(((WORD)(RXB0SIDL & 0xE0))>>2)+(((WORD)(RXB0SIDL & 0x03))<<1)+(((WORD)(RXB0EIDH & 0x80))>>7); |
| 175 | - | ||
| 176 | } |
- | |
| 177 | else |
- | |
| 178 | { |
- | |
| 179 | //Read standard ident. |
- | |
| 180 | - | ||
| 181 | //xx xx xx xx xx 10 09 08 07 06 05 04 03 02 01 00 |
- | |
| 182 | - | ||
| 183 |
|
167 | cm.nid=((RXB0EIDH & 0x7E)>>1); |
| 184 |
|
168 | cm.sid=(((WORD)(RXB0EIDH & 0x01))<<8)+RXB0EIDL; |
| 185 | 169 | ||
| 186 | cm.ident = (((DWORD)RXB0SIDH)<<3) + (((DWORD)RXB0SIDL)>>5); |
- | |
| 187 | } |
- | |
| 188 | 170 | ||
| 189 | // Data length |
171 | // Data length |
| 190 | cm.data_length=(RXB0DLCbits.DLC3<<3)+(RXB0DLCbits.DLC2<<2)+(RXB0DLCbits.DLC1<<1)+RXB0DLCbits.DLC0; |
172 | cm.data_length=(RXB0DLCbits.DLC3<<3)+(RXB0DLCbits.DLC2<<2)+(RXB0DLCbits.DLC1<<1)+RXB0DLCbits.DLC0; |
| 191 | - | ||
| 192 | // Remote request |
- | |
| 193 | cm.remote_request=(RXB0DLCbits.RXRTR==0?FALSE:TRUE); |
- | |
| 194 | 173 | ||
| 195 | // Data one bye one, for speed |
174 | // Data one bye one, for speed |
| 196 | cm.data[0]=RXB0D0; cm.data[1]=RXB0D1; |
175 | cm.data[0]=RXB0D0; cm.data[1]=RXB0D1; |
| 197 | cm.data[2]=RXB0D2; cm.data[3]=RXB0D3; |
176 | cm.data[2]=RXB0D2; cm.data[3]=RXB0D3; |
| 198 | cm.data[4]=RXB0D4; cm.data[5]=RXB0D5; |
177 | cm.data[4]=RXB0D4; cm.data[5]=RXB0D5; |
| Line 225... | Line 204... | ||
| 225 | else { return FALSE;} |
204 | else { return FALSE;} |
| 226 | 205 | ||
| 227 | if (prio<0 || prio>3) prio = 0; |
206 | if (prio<0 || prio>3) prio = 0; |
| 228 | 207 | ||
| 229 | 208 | ||
| 230 | if (cm.extended==TRUE) |
- | |
| 231 | { |
- | |
| 232 | //Write extended ident. |
- | |
| 233 | - | ||
| 234 | //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 |
- | |
| 235 | - | ||
| 236 |
|
209 | //RXB0SIDH 28 27 26 25 24 23 22 21 |
| 237 |
|
210 | //RXB0SIDL 20 19 18 xx xx xx 17 16 |
| 238 |
|
211 | //RXB0EIDH 15 14 13 12 11 10 09 08 |
| 239 |
|
212 | //RXB0EIDL 07 06 05 04 03 02 01 00 |
| 240 | - | ||
| 241 | - | ||
| 242 | RXB0SIDH = (BYTE)((cm.ident>>21)); |
- | |
| 243 | RXB0SIDL = ((BYTE)(cm.ident>>16) & 0x03) + ((BYTE)(cm.ident>>13) & 0xE0); |
- | |
| 244 | RXB0EIDH = (BYTE)(cm.ident>>8); |
- | |
| 245 | RXB0EIDL = (BYTE)cm.ident; |
- | |
| 246 | 213 | ||
| 247 | } |
- | |
| 248 | else |
- | |
| 249 | { |
- | |
| 250 |
|
214 | //funct xx xx xx xx 28 27 26 25 |
| 251 | - | ||
| 252 |
|
215 | //funcc xx xx xx xx xx xx 24 23 22 21 20 19 18 17 16 15 |
| 253 | - | ||
| 254 |
|
216 | //nid xx xx 14 13 12 11 10 09 |
| 255 |
|
217 | //sid xx xx xx xx xx xx xx 08 07 06 05 04 03 02 01 00 |
| 256 | - | ||
| 257 | RXB0SIDH= (BYTE)(cm.ident>>3); |
- | |
| 258 | RXB0SIDL= ((BYTE)(cm.ident<<5) & 0xE0); |
- | |
| 259 | 218 | ||
| 260 | } |
- | |
| - | 219 | RXB0SIDH = (BYTE)((cm.funct & 0x0F)<<4)+(BYTE)((cm.funcc & 0x03C0)>>6); |
|
| - | 220 | RXB0SIDL = (BYTE)((cm.funcc & 0x003F)<<2)+(BYTE)((cm.funcc & 0x0006)>>1); |
|
| - | 221 | RXB0EIDH = (BYTE)((cm.funcc & 0x0001)<<7)+(BYTE)((cm.nid & 0x3F)<<1)+(BYTE)((cm.sid & 0x0100)>>8); |
|
| - | 222 | RXB0EIDL = (BYTE)((cm.sid & 0x00FF)); |
|
| 261 | 223 | ||
| 262 | // Data length |
224 | // Data length |
| 263 | RXB0DLC = 0; |
225 | RXB0DLC = 0; |
| 264 | if (cm.data_length>8) RXB0DLC=8; else RXB0DLC=cm.data_length; |
226 | if (cm.data_length>8) RXB0DLC=8; else RXB0DLC=cm.data_length; |
| 265 | 227 | ||
| 266 | // Remote request |
- | |
| 267 | if (cm.remote_request==TRUE) |
- | |
| 268 | { |
228 | |
| 269 | _asm |
- | |
| 270 |
|
229 | // NOT Remote request |
| 271 | _endasm |
- | |
| 272 | } |
- | |
| 273 | else |
- | |
| 274 | { |
- | |
| 275 |
|
230 | _asm |
| 276 |
|
231 | bcf RXB0DLC, 6, 0 |
| 277 |
|
232 | _endasm |
| 278 |
|
233 | |
| 279 | 234 | ||
| 280 | // Data one bye one, for speed |
235 | // Data one bye one, for speed |
| 281 | RXB0D0=cm.data[0]; RXB0D1=cm.data[1]; |
236 | RXB0D0=cm.data[0]; RXB0D1=cm.data[1]; |
| 282 | RXB0D2=cm.data[2]; RXB0D3=cm.data[3]; |
237 | RXB0D2=cm.data[2]; RXB0D3=cm.data[3]; |
| 283 | RXB0D4=cm.data[4]; RXB0D5=cm.data[5]; |
238 | RXB0D4=cm.data[4]; RXB0D5=cm.data[5]; |
| 284 | RXB0D6=cm.data[6]; RXB0D7=cm.data[7]; |
239 | RXB0D6=cm.data[6]; RXB0D7=cm.data[7]; |
| 285 | 240 | ||
| 286 | // set extended |
241 | // set extended |
| 287 | RXB0SIDLbits.EXID= |
242 | RXB0SIDLbits.EXID=1; |
| 288 | 243 | ||
| 289 | // Priority |
244 | // Priority |
| 290 | RXB0CON = (RXB0CON & 0b11111100) | prio; |
245 | RXB0CON = (RXB0CON & 0b11111100) | prio; |
| 291 | 246 | ||
| 292 | // mark as redy to transmit |
247 | // mark as redy to transmit |