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: 2006-11-14 11:20:48 +0100 (Tue, 14 Nov 2006) $ |
||
| 8 | * By: $LastChangedBy: johboh $ |
||
| 9 | * Revision: $Revision: 87 $ |
||
| 85 | johboh | 10 | ********************************************************************/ |
| 11 | |||
| 12 | #include <CANdefs.h> |
||
| 13 | #include <stackTasks.h> |
||
| 14 | #include <CAN.h> |
||
| 15 | |||
| 16 | static void canGetPacket(void); |
||
| 17 | |||
| 18 | #ifdef USE_CAN |
||
| 19 | |||
| 20 | |||
| 21 | /* |
||
| 22 | * Function: canInit |
||
| 23 | * |
||
| 24 | * Input: none |
||
| 25 | * Output: none |
||
| 26 | * Pre-conditions: none |
||
| 27 | * Affects: Changes can registers. |
||
| 28 | * Depends: none. |
||
| 29 | */ |
||
| 30 | void canInit() |
||
| 31 | { |
||
| 32 | CANCON = 0x80; //request config mode |
||
| 33 | while ((CANSTAT & 0xE0) != 0x80 ); //wait for config mode request |
||
| 34 | |||
| 35 | // BAUD AND TQS |
||
| 36 | |||
| 37 | BRGCON1=0; |
||
| 38 | |||
| 39 | // Sync_Seq = 2 x TQ = 1 |
||
| 40 | BRGCON1bits.SJW1=0; |
||
| 41 | BRGCON1bits.SJW0=1; |
||
| 42 | |||
| 43 | // Setting BRP. |
||
| 44 | BRGCON1=BRGCON1|CAN_BRP; |
||
| 45 | |||
| 46 | // Maximum PHEG1 |
||
| 47 | BRGCON2bits.SEG2PHTS = 1; |
||
| 48 | |||
| 49 | // Phase_Seg1 = 2 x TQ = 1 |
||
| 50 | BRGCON2bits.SEG1PH2 = 0; |
||
| 51 | BRGCON2bits.SEG1PH1 = 0; |
||
| 52 | BRGCON2bits.SEG1PH0 = 1; |
||
| 53 | |||
| 54 | // Prop_Seq = 2 x TQ = 1 |
||
| 55 | BRGCON2bits.PRSEG2 = 0; |
||
| 56 | BRGCON2bits.PRSEG1 = 0; |
||
| 57 | BRGCON2bits.PRSEG0 = 1; |
||
| 58 | |||
| 59 | // Enableing bus wake-up |
||
| 60 | BRGCON3bits.WAKDIS = 0; |
||
| 61 | |||
| 62 | // Dont use filter when wakeup |
||
| 63 | BRGCON3bits.WAKFIL = 0; |
||
| 64 | |||
| 65 | // Phase_Seg2 = 2 x TQ = 1 |
||
| 66 | BRGCON3bits.SEG2PH2 = 0; |
||
| 67 | BRGCON3bits.SEG2PH1 = 0; |
||
| 68 | BRGCON3bits.SEG2PH0 = 1; |
||
| 69 | |||
| 70 | ECANCON=0; |
||
| 71 | ECANCONbits.MDSEL1 = 0; |
||
| 72 | ECANCONbits.MDSEL0 = 1; |
||
| 73 | |||
| 74 | |||
| 75 | // FILTERS AND MASKS |
||
| 76 | RXB0CONbits.RXM1=1; |
||
| 77 | RXB0CONbits.RXM0=0; |
||
| 78 | |||
| 79 | |||
| 80 | // INTERRUPTS |
||
| 81 | |||
| 82 | // Diable transmit interrupt |
||
| 83 | TXBIE = 0; |
||
| 84 | |||
| 85 | //rx any interrupt |
||
| 86 | PIR3 = 0; |
||
| 87 | PIE3 = 0b00000011; |
||
| 88 | |||
| 89 | // High interrupt |
||
| 90 | IPR3 =0b00000011; |
||
| 91 | |||
| 92 | // Programmable buffers interrupt |
||
| 93 | BIE0 = 0xFF; |
||
| 94 | |||
| 95 | |||
| 96 | //loopback mode or not |
||
| 97 | #ifdef CAN_LOOPBACK_MODE |
||
| 98 | CANCON = 0x40;//request loopback mode |
||
| 99 | while ((CANSTAT & 0xE0) != 0x40 );//wait for loopback mode |
||
| 100 | #else |
||
| 101 | CANCON = 0x00;//request normal mode |
||
| 102 | while ((CANSTAT & 0xE0) != 0x00 );//wait for normal mode |
||
| 103 | #endif |
||
| 104 | |||
| 105 | } |
||
| 106 | |||
| 107 | |||
| 108 | /* |
||
| 109 | * Function: canISR |
||
| 110 | * |
||
| 111 | * Input: none |
||
| 112 | * Output: none |
||
| 113 | * Pre-conditions: a call to canInit() |
||
| 114 | * Affects: interrupt registers and receive data buffers |
||
| 115 | * Depends: .. |
||
| 116 | */ |
||
| 117 | void canISR() |
||
| 118 | { |
||
| 119 | |||
| 87 | johboh | 120 | |
| 85 | johboh | 121 | if (PIR3bits.RXBnIF || PIR3bits.RXB1IF || PIR3bits.RXB0IF) |
| 122 | { |
||
| 87 | johboh | 123 | |
| 124 | |||
| 85 | johboh | 125 | // Get which buffer that is ful. |
| 126 | if (RXB0CONbits.RXFUL) { ECANCON=(ECANCON&0b00000)|0b10000; canGetPacket(); } |
||
| 127 | else if (RXB1CONbits.RXFUL) { ECANCON=(ECANCON&0b00000)|0b10001; canGetPacket(); } |
||
| 128 | else if (B0CONbits.RXFUL) { ECANCON=(ECANCON&0b00000)|0b10010; canGetPacket(); } |
||
| 129 | else if (B1CONbits.RXFUL) { ECANCON=(ECANCON&0b00000)|0b10011; canGetPacket(); } |
||
| 130 | else if (B2CONbits.RXFUL) { ECANCON=(ECANCON&0b00000)|0b10100; canGetPacket(); } |
||
| 131 | else if (B3CONbits.RXFUL) { ECANCON=(ECANCON&0b00000)|0b10101; canGetPacket(); } |
||
| 132 | else if (B4CONbits.RXFUL) { ECANCON=(ECANCON&0b00000)|0b10110; canGetPacket(); } |
||
| 133 | else if (B5CONbits.RXFUL) { ECANCON=(ECANCON&0b00000)|0b10111; canGetPacket(); } |
||
| 134 | |||
| 135 | if (PIR3bits.RXBnIF) {PIR3bits.RXBnIF = 0;} |
||
| 136 | if (PIR3bits.RXB1IF) {PIR3bits.RXB1IF = 0;} |
||
| 137 | if (PIR3bits.RXB0IF) {PIR3bits.RXB0IF = 0;} |
||
| 138 | |||
| 139 | } |
||
| 140 | } |
||
| 141 | |||
| 142 | |||
| 143 | /* |
||
| 144 | * Function: canGetPacket |
||
| 145 | * |
||
| 146 | * Input: none |
||
| 147 | * Output: none |
||
| 148 | * Pre-conditions: a call to canISR and an packet is available and banked to RXB0 |
||
| 149 | * Affects: Calls canParse() function in main for parsing received packet. |
||
| 150 | * Depends: none. |
||
| 151 | */ |
||
| 152 | void canGetPacket() |
||
| 153 | { |
||
| 154 | CAN_MESSAGE cm; |
||
| 155 | |||
| 86 | johboh | 156 | //RXB0SIDH 28 27 26 25 24 23 22 21 |
| 157 | //RXB0SIDL 20 19 18 xx xx xx 17 16 |
||
| 158 | //RXB0EIDH 15 14 13 12 11 10 09 08 |
||
| 159 | //RXB0EIDL 07 06 05 04 03 02 01 00 |
||
| 85 | johboh | 160 | |
| 86 | johboh | 161 | //funct xx xx xx xx 28 27 26 25 |
| 162 | //funcc xx xx xx xx xx xx 24 23 22 21 20 19 18 17 16 15 |
||
| 163 | //nid xx xx 14 13 12 11 10 09 |
||
| 164 | //sid xx xx xx xx xx xx xx 08 07 06 05 04 03 02 01 00 |
||
| 85 | johboh | 165 | |
| 166 | |||
| 86 | johboh | 167 | cm.funct=((RXB0SIDH & 0xF0)>>4); |
| 168 | cm.funcc=(((WORD)(RXB0SIDH & 0x0F))<<6)+(((WORD)(RXB0SIDL & 0xE0))>>2)+(((WORD)(RXB0SIDL & 0x03))<<1)+(((WORD)(RXB0EIDH & 0x80))>>7); |
||
| 169 | cm.nid=((RXB0EIDH & 0x7E)>>1); |
||
| 170 | cm.sid=(((WORD)(RXB0EIDH & 0x01))<<8)+RXB0EIDL; |
||
| 85 | johboh | 171 | |
| 172 | |||
| 173 | // Data length |
||
| 174 | cm.data_length=(RXB0DLCbits.DLC3<<3)+(RXB0DLCbits.DLC2<<2)+(RXB0DLCbits.DLC1<<1)+RXB0DLCbits.DLC0; |
||
| 175 | |||
| 176 | // Data one bye one, for speed |
||
| 177 | cm.data[0]=RXB0D0; cm.data[1]=RXB0D1; |
||
| 178 | cm.data[2]=RXB0D2; cm.data[3]=RXB0D3; |
||
| 179 | cm.data[4]=RXB0D4; cm.data[5]=RXB0D5; |
||
| 180 | cm.data[6]=RXB0D6; cm.data[7]=RXB0D7; |
||
| 181 | |||
| 182 | // Clear ful status |
||
| 183 | RXB0CONbits.RXFUL=0; |
||
| 184 | |||
| 185 | //Call main parser |
||
| 186 | canParse(cm); |
||
| 187 | } |
||
| 188 | |||
| 189 | |||
| 190 | /* |
||
| 191 | * Function: canSendMessage |
||
| 192 | * |
||
| 193 | * Input: CAN_MESSAGE to send |
||
| 194 | * Output: True if packet scheduled sucess, false otherwise. |
||
| 195 | * Pre-conditions: canInit |
||
| 196 | * Affects: .. |
||
| 197 | * Depends: .. |
||
| 198 | */ |
||
| 199 | BOOL canSendMessage(CAN_MESSAGE cm,CAN_PRIORITY prio) |
||
| 200 | { |
||
| 201 | |||
| 202 | if ( TXB0CONbits.TXREQ == 0 ) { ECANCON=(ECANCON&0b00000)|0b00011; } |
||
| 203 | if ( TXB1CONbits.TXREQ == 0 ) { ECANCON=(ECANCON&0b00000)|0b00100; } |
||
| 204 | if ( TXB2CONbits.TXREQ == 0 ) { ECANCON=(ECANCON&0b00000)|0b00101; } |
||
| 205 | // None of the transmit buffers were empty. |
||
| 206 | else { return FALSE;} |
||
| 207 | |||
| 208 | if (prio<0 || prio>3) prio = 0; |
||
| 209 | |||
| 210 | |||
| 86 | johboh | 211 | //RXB0SIDH 28 27 26 25 24 23 22 21 |
| 212 | //RXB0SIDL 20 19 18 xx xx xx 17 16 |
||
| 213 | //RXB0EIDH 15 14 13 12 11 10 09 08 |
||
| 214 | //RXB0EIDL 07 06 05 04 03 02 01 00 |
||
| 85 | johboh | 215 | |
| 86 | johboh | 216 | //funct xx xx xx xx 28 27 26 25 |
| 217 | //funcc xx xx xx xx xx xx 24 23 22 21 20 19 18 17 16 15 |
||
| 218 | //nid xx xx 14 13 12 11 10 09 |
||
| 219 | //sid xx xx xx xx xx xx xx 08 07 06 05 04 03 02 01 00 |
||
| 85 | johboh | 220 | |
| 86 | johboh | 221 | RXB0SIDH = (BYTE)((cm.funct & 0x0F)<<4)+(BYTE)((cm.funcc & 0x03C0)>>6); |
| 222 | RXB0SIDL = (BYTE)((cm.funcc & 0x003F)<<2)+(BYTE)((cm.funcc & 0x0006)>>1); |
||
| 223 | RXB0EIDH = (BYTE)((cm.funcc & 0x0001)<<7)+(BYTE)((cm.nid & 0x3F)<<1)+(BYTE)((cm.sid & 0x0100)>>8); |
||
| 224 | RXB0EIDL = (BYTE)((cm.sid & 0x00FF)); |
||
| 85 | johboh | 225 | |
| 226 | // Data length |
||
| 227 | RXB0DLC = 0; |
||
| 228 | if (cm.data_length>8) RXB0DLC=8; else RXB0DLC=cm.data_length; |
||
| 229 | |||
| 230 | |||
| 86 | johboh | 231 | // NOT Remote request |
| 232 | _asm |
||
| 233 | bcf RXB0DLC, 6, 0 |
||
| 234 | _endasm |
||
| 235 | |||
| 236 | |||
| 85 | johboh | 237 | // Data one bye one, for speed |
| 238 | RXB0D0=cm.data[0]; RXB0D1=cm.data[1]; |
||
| 239 | RXB0D2=cm.data[2]; RXB0D3=cm.data[3]; |
||
| 240 | RXB0D4=cm.data[4]; RXB0D5=cm.data[5]; |
||
| 241 | RXB0D6=cm.data[6]; RXB0D7=cm.data[7]; |
||
| 242 | |||
| 86 | johboh | 243 | // set extended |
| 244 | RXB0SIDLbits.EXID=1; |
||
| 85 | johboh | 245 | |
| 246 | // Priority |
||
| 247 | RXB0CON = (RXB0CON & 0b11111100) | prio; |
||
| 248 | |||
| 249 | // mark as redy to transmit |
||
| 250 | _asm |
||
| 251 | bsf RXB0CON, 3, 0 |
||
| 252 | _endasm |
||
| 253 | |||
| 254 | return TRUE; |
||
| 255 | |||
| 256 | } |
||
| 257 | |||
| 258 | |||
| 259 | |||
| 260 | #endif //USE_CAN |