Rev 24 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 24 | Rev 25 | ||
|---|---|---|---|
| Line -... | Line 1... | ||
| - | 1 | /********************************************************************* |
|
| - | 2 | * |
|
| - | 3 | * CAN driver |
|
| - | 4 | * |
|
| - | 5 | ********************************************************************* |
|
| - | 6 | * FileName: CAN.c |
|
| - | 7 | * |
|
| - | 8 | * Author Date Comment |
|
| - | 9 | *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
| - | 10 | * Nilesh Rajbharti 8/20/01 Original (Rev 1.0) |
|
| - | 11 | ********************************************************************/ |
|
| - | 12 | ||
| 1 | #include "../Include/CANdefs.h" |
13 | #include "../Include/CANdefs.h" |
| 2 | #include "../Include/stackTasks.h" |
14 | #include "../Include/stackTasks.h" |
| 3 | #include "../Include/CAN.h" |
15 | #include "../Include/CAN.h" |
| 4 | #include "../Include/uart.h" |
16 | #include "../Include/uart.h" |
| 5 | 17 | ||
| 6 | static void canGetPacket(void); |
18 | static void canGetPacket(void); |
| 7 | 19 | ||
| 8 | #ifdef USE_CAN |
20 | #ifdef USE_CAN |
| 9 | 21 | ||
| 10 | 22 | ||
| 11 | /* |
23 | /* |
| 12 | * Function: canInit |
24 | * Function: canInit |
| 13 | * |
25 | * |
| 14 | * Input: none |
26 | * Input: none |
| 15 | * Output: none |
27 | * Output: none |
| Line 30... | Line 42... | ||
| 30 | BRGCON1bits.SJW1=0; |
42 | BRGCON1bits.SJW1=0; |
| 31 | BRGCON1bits.SJW0=1; |
43 | BRGCON1bits.SJW0=1; |
| 32 | 44 | ||
| 33 | // Setting BRP. |
45 | // Setting BRP. |
| 34 | BRGCON1=BRGCON1|CAN_BRP; |
46 | BRGCON1=BRGCON1|CAN_BRP; |
| 35 | 47 | ||
| 36 | // Maximum PHEG1 |
48 | // Maximum PHEG1 |
| 37 | BRGCON2bits.SEG2PHTS = 1; |
49 | BRGCON2bits.SEG2PHTS = 1; |
| 38 | 50 | ||
| 39 | // Phase_Seg1 = 2 x TQ = 1 |
51 | // Phase_Seg1 = 2 x TQ = 1 |
| 40 | BRGCON2bits.SEG1PH2 = 0; |
52 | BRGCON2bits.SEG1PH2 = 0; |
| 41 | BRGCON2bits.SEG1PH1 = 0; |
53 | BRGCON2bits.SEG1PH1 = 0; |
| 42 | BRGCON2bits.SEG1PH0 = 1; |
54 | BRGCON2bits.SEG1PH0 = 1; |
| 43 | 55 | ||
| 44 | // Prop_Seq = 2 x TQ = 1 |
56 | // Prop_Seq = 2 x TQ = 1 |
| 45 | BRGCON2bits.PRSEG2 = 0; |
57 | BRGCON2bits.PRSEG2 = 0; |
| 46 | BRGCON2bits.PRSEG1 = 0; |
58 | BRGCON2bits.PRSEG1 = 0; |
| 47 | BRGCON2bits.PRSEG0 = 1; |
59 | BRGCON2bits.PRSEG0 = 1; |
| 48 | 60 | ||
| 49 | // Enableing bus wake-up |
61 | // Enableing bus wake-up |
| 50 | BRGCON3bits.WAKDIS = 0; |
62 | BRGCON3bits.WAKDIS = 0; |
| 51 | 63 | ||
| 52 | // Dont use filter when wakeup |
64 | // Dont use filter when wakeup |
| 53 | BRGCON3bits.WAKFIL = 0; |
65 | BRGCON3bits.WAKFIL = 0; |
| Line 66... | Line 78... | ||
| 66 | RXB0CONbits.RXM1=1; |
78 | RXB0CONbits.RXM1=1; |
| 67 | RXB0CONbits.RXM0=0; |
79 | RXB0CONbits.RXM0=0; |
| 68 | 80 | ||
| 69 | 81 | ||
| 70 | // INTERRUPTS |
82 | // INTERRUPTS |
| 71 | 83 | ||
| 72 | // Diable transmit interrupt |
84 | // Diable transmit interrupt |
| 73 | TXBIE = 0; |
85 | TXBIE = 0; |
| 74 | 86 | ||
| 75 | //rx any interrupt |
87 | //rx any interrupt |
| 76 | PIR3 = 0; |
88 | PIR3 = 0; |
| Line 137... | Line 149... | ||
| 137 | * Depends: none. |
149 | * Depends: none. |
| 138 | */ |
150 | */ |
| 139 | void canGetPacket() |
151 | void canGetPacket() |
| 140 | { |
152 | { |
| 141 | CAN_MESSAGE cm; |
153 | CAN_MESSAGE cm; |
| 142 | 154 | ||
| 143 | // Set extended mode or not. |
155 | // Set extended mode or not. |
| 144 | cm.extended=(RXB0SIDLbits.EXID==0?FALSE:TRUE); |
156 | cm.extended=(RXB0SIDLbits.EXID==0?FALSE:TRUE); |
| 145 | 157 | ||
| 146 | // Clear |
158 | // Clear |
| 147 | cm.ident=0; |
159 | cm.ident=0; |
| Line 200... | Line 212... | ||
| 200 | * Affects: .. |
212 | * Affects: .. |
| 201 | * Depends: .. |
213 | * Depends: .. |
| 202 | */ |
214 | */ |
| 203 | BOOL canSendMessage(CAN_MESSAGE cm) |
215 | BOOL canSendMessage(CAN_MESSAGE cm) |
| 204 | { |
216 | { |
| 205 | - | ||
| 206 | - | ||
| 207 | if ( TXB0CONbits.TXREQ == 0 ) |
- | |
| 208 | { |
- | |
| 209 | uartPutrs("TXREQ not set."); |
- | |
| 210 | - | ||
| 211 | } |
- | |
| 212 | else { uartPutrs("TXREQ is set."); return FALSE;} |
- | |
| 213 | - | ||
| 214 | 217 | ||
| 215 | 218 | ||
| 216 | 219 | ||
| 217 | /* |
- | |
| 218 | if ( TXB0CONbits.TXREQ == 0 ) { ECANCON=(ECANCON&0b00000)|0b00011; } |
220 | if ( TXB0CONbits.TXREQ == 0 ) { ECANCON=(ECANCON&0b00000)|0b00011; } |
| 219 | if ( TXB1CONbits.TXREQ == 0 ) { ECANCON=(ECANCON&0b00000)|0b00100; } |
221 | if ( TXB1CONbits.TXREQ == 0 ) { ECANCON=(ECANCON&0b00000)|0b00100; } |
| 220 | if ( TXB2CONbits.TXREQ == 0 ) { ECANCON=(ECANCON&0b00000)|0b00101; } |
222 | if ( TXB2CONbits.TXREQ == 0 ) { ECANCON=(ECANCON&0b00000)|0b00101; } |
| 221 | // None of the transmit buffers were empty. |
223 | // None of the transmit buffers were empty. |
| 222 | else { return FALSE;} |
224 | else { return FALSE;} |
| 223 | 225 | ||
| 224 | 226 | ||
| 225 | // Set extended mode or not. |
227 | // Set extended mode or not. |
| 226 | RXB0SIDLbits.EXID=(cm.extended==TRUE?1:0); |
228 | RXB0SIDLbits.EXID=(cm.extended==TRUE?1:0); |
| 227 | 229 | ||
| 228 | 230 | ||
| 229 | if (cm.extended==TRUE) |
231 | if (cm.extended==TRUE) |
| Line 290... | Line 292... | ||
| 290 | bsf RXB0CON, 3, 0 |
292 | bsf RXB0CON, 3, 0 |
| 291 | _endasm |
293 | _endasm |
| 292 | 294 | ||
| 293 | 295 | ||
| 294 | return TRUE; |
296 | return TRUE; |
| 295 | */ |
297 | |
| 296 | } |
298 | } |
| 297 | 299 | ||
| 298 | 300 | ||
| 299 | 301 | ||
| 300 | #endif //USE_CAN |
302 | #endif //USE_CAN |