Details | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 25 | johboh | 1 | /********************************************************************* |
| 2 | * |
||
| 3 | * Main application |
||
| 4 | * |
||
| 5 | ********************************************************************* |
||
| 6 | * FileName: Main.c |
||
| 7 | * |
||
| 8 | * Author Date Comment |
||
| 9 | *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||
| 10 | * Johan Böhlin 21-10-06 Original (Rev 1.0) |
||
| 11 | ********************************************************************/ |
||
| 12 | |||
| 24 | johboh | 13 | #include "../Include/compiler.h" |
| 14 | #include "../Include/stackTasks.h" |
||
| 15 | #include "../Include/Tick.h" |
||
| 16 | |||
| 17 | #ifdef USE_CAN |
||
| 18 | #include "../Include/CAN.h" |
||
| 19 | #endif |
||
| 20 | |||
| 21 | #ifdef USE_UART |
||
| 22 | #include "../Include/uart.h" |
||
| 23 | #endif |
||
| 24 | |||
| 25 | |||
| 26 | |||
| 27 | static void mainInit(void); |
||
| 28 | |||
| 32 | johboh | 29 | static BOOL heartBeatEnabled = FALSE; |
| 48 | johboh | 30 | BYTE ldc = 50; |
| 31 | johboh | 31 | |
| 24 | johboh | 32 | void main() |
| 33 | { |
||
| 34 | static TICK t = 0; |
||
| 35 | |||
| 36 | // Inits |
||
| 37 | |||
| 38 | mainInit(); |
||
| 39 | |||
| 40 | #ifdef USE_CAN |
||
| 41 | canInit(); |
||
| 42 | #endif |
||
| 43 | |||
| 44 | #ifdef USE_UART |
||
| 45 | uartInit(); |
||
| 46 | #endif |
||
| 47 | |||
| 48 | tickInit(); |
||
| 49 | |||
| 50 | while(1) |
||
| 51 | { |
||
| 48 | johboh | 52 | #ifdef USE_CAN |
| 31 | johboh | 53 | if ((tickGet()-t)>=5*TICK_SECOND && heartBeatEnabled==TRUE) |
| 24 | johboh | 54 | { |
| 55 | CAN_MESSAGE cm; |
||
| 56 | |||
| 57 | t = tickGet(); |
||
| 58 | |||
| 59 | // Send heartbeat |
||
| 31 | johboh | 60 | cm.ident=0x00000666; |
| 24 | johboh | 61 | cm.extended=FALSE; |
| 62 | cm.data_length=1; |
||
| 63 | cm.data[0]='H'; |
||
| 64 | |||
| 65 | while(!canSendMessage(cm)); |
||
| 66 | |||
| 67 | |||
| 68 | } |
||
| 48 | johboh | 69 | #endif |
| 24 | johboh | 70 | } |
| 71 | } |
||
| 72 | |||
| 73 | |||
| 74 | #pragma interruptlow HighISR |
||
| 75 | void HighISR(void) |
||
| 76 | { |
||
| 48 | johboh | 77 | //if(ldc--<=1) {LED0_IO=~LED0_IO; ldc=50; } |
| 78 | |||
| 24 | johboh | 79 | #ifdef USE_CAN |
| 80 | canISR(); |
||
| 81 | #endif |
||
| 82 | |||
| 83 | #ifdef USE_UART |
||
| 84 | uartISR(); |
||
| 85 | #endif |
||
| 86 | |||
| 48 | johboh | 87 | if (INTCONbits.INT0IE && INTCONbits.INT0IF) |
| 88 | { |
||
| 89 | static TICK t = 0; |
||
| 90 | CAN_MESSAGE cm; |
||
| 91 | |||
| 92 | if ((tickGet()-t)>TICK_SECOND/2) |
||
| 93 | { |
||
| 94 | |||
| 95 | LED0_IO=~LED0_IO; |
||
| 96 | |||
| 97 | // Send heartbeat |
||
| 98 | cm.ident=0x00000666; |
||
| 99 | cm.extended=FALSE; |
||
| 100 | cm.data_length=1; |
||
| 101 | cm.data[0]='H'; |
||
| 102 | |||
| 103 | while(!canSendMessage(cm)); |
||
| 104 | |||
| 105 | t=tickGet(); |
||
| 106 | } |
||
| 107 | |||
| 108 | INTCONbits.INT0IF=0; |
||
| 109 | } |
||
| 110 | |||
| 111 | //if (RCSTAbits.OERR) { RCSTAbits.CREN=0; RCSTAbits.CREN=1; } |
||
| 112 | |||
| 24 | johboh | 113 | tickUpdate(); |
| 114 | } |
||
| 115 | #pragma code highVector=0x08 |
||
| 116 | void HighVector (void) |
||
| 117 | { |
||
| 118 | _asm goto HighISR _endasm |
||
| 119 | } |
||
| 120 | #pragma code // Return to default code section |
||
| 121 | |||
| 122 | |||
| 123 | |||
| 124 | #pragma interruptlow LowISR |
||
| 125 | void LowISR(void) |
||
| 126 | { |
||
| 127 | |||
| 128 | } |
||
| 129 | #pragma code lowVector=0x18 |
||
| 130 | void LowVector (void) |
||
| 131 | { |
||
| 132 | _asm goto LowISR _endasm |
||
| 133 | } |
||
| 134 | #pragma code // Return to default code section |
||
| 135 | |||
| 136 | |||
| 137 | /* |
||
| 138 | * Function: mainInit |
||
| 139 | * |
||
| 140 | * Input: none |
||
| 141 | * Output: none |
||
| 142 | * Pre-conditions: none |
||
| 143 | * Affects: Se code |
||
| 144 | * Depends: none. |
||
| 145 | */ |
||
| 146 | void mainInit() |
||
| 147 | { |
||
| 148 | LED0_TRIS=0; |
||
| 149 | |||
| 150 | // Enable internal PORTB pull-ups |
||
| 151 | INTCON2bits.RBPU = 0; |
||
| 152 | |||
| 153 | // Enable Interrupts |
||
| 154 | INTCONbits.GIEH = 1; |
||
| 155 | INTCONbits.GIEL = 1; |
||
| 156 | |||
| 48 | johboh | 157 | |
| 158 | // RB0 interrupt |
||
| 159 | INTCON2bits.INTEDG0 = 1; //rising edge |
||
| 160 | INTCONbits.INT0IE = 1; |
||
| 161 | |||
| 162 | // Enable interrupt prirority |
||
| 163 | RCONbits.IPEN=1; |
||
| 164 | |||
| 165 | |||
| 24 | johboh | 166 | } |
| 167 | |||
| 168 | /* |
||
| 169 | * Function: canParse |
||
| 170 | * |
||
| 171 | * Input: Received can message |
||
| 172 | * Output: none |
||
| 173 | * Pre-conditions: canInit and received packet. |
||
| 174 | * Affects: Sensors/actuators/etc. See code. |
||
| 175 | * Depends: none. |
||
| 176 | */ |
||
| 177 | #ifdef USE_CAN |
||
| 178 | void canParse(CAN_MESSAGE cm) |
||
| 179 | { |
||
| 180 | int i=0; |
||
| 31 | johboh | 181 | |
| 182 | uartPutc(UART_START_BYTE); |
||
| 183 | uartPutc((BYTE)(cm.ident)); |
||
| 24 | johboh | 184 | uartPutc((BYTE)(cm.ident>>8)); |
| 31 | johboh | 185 | uartPutc((BYTE)(cm.ident>>16)); |
| 186 | uartPutc((BYTE)(cm.ident>>24)); |
||
| 187 | uartPutc(cm.extended); |
||
| 188 | uartPutc(cm.data_length); |
||
| 33 | johboh | 189 | for(i=0;i<8;i++) uartPutc(cm.data[i]); |
| 31 | johboh | 190 | uartPutc(UART_END_BYTE); |
| 24 | johboh | 191 | |
| 192 | } |
||
| 193 | #endif |
||
| 194 | |||
| 195 | |||
| 196 | /* |
||
| 197 | * Function: uartParse |
||
| 198 | * |
||
| 199 | * Input: Received uart message |
||
| 200 | * Output: none |
||
| 201 | * Pre-conditions: uartInit and received byte. |
||
| 202 | * Affects: Sensors/actuators/etc. See code. |
||
| 203 | * Depends: none. |
||
| 204 | */ |
||
| 205 | #ifdef USE_UART |
||
| 206 | void uartParse(BYTE c) |
||
| 207 | { |
||
| 32 | johboh | 208 | BYTE i; |
| 209 | unsigned int wait = 0; |
||
| 210 | CAN_MESSAGE cm; |
||
| 211 | static BOOL waitingMessage = FALSE; |
||
| 33 | johboh | 212 | static TICK timeout=0; |
| 213 | static BYTE count=0; |
||
| 24 | johboh | 214 | |
| 48 | johboh | 215 | |
| 32 | johboh | 216 | if (waitingMessage==TRUE && (tickGet()-timeout)>TICK_SECOND/20) |
| 24 | johboh | 217 | { |
| 32 | johboh | 218 | waitingMessage=FALSE; |
| 26 | johboh | 219 | } |
| 220 | |||
| 48 | johboh | 221 | |
| 32 | johboh | 222 | if (waitingMessage==TRUE) |
| 26 | johboh | 223 | { |
| 48 | johboh | 224 | |
| 32 | johboh | 225 | timeout=tickGet(); |
| 24 | johboh | 226 | |
| 32 | johboh | 227 | // UART END |
| 228 | if(count>=14) |
||
| 229 | { |
||
| 230 | if (c==UART_END_BYTE) |
||
| 231 | { |
||
| 232 | while(!canSendMessage(cm)); |
||
| 233 | } |
||
| 234 | waitingMessage=FALSE; |
||
| 235 | return; |
||
| 236 | } |
||
| 26 | johboh | 237 | |
| 32 | johboh | 238 | // data |
| 239 | if(count>=6) |
||
| 240 | { |
||
| 241 | cm.data[count-6]=c; |
||
| 242 | count++; |
||
| 243 | return; |
||
| 244 | } |
||
| 24 | johboh | 245 | |
| 32 | johboh | 246 | // data length |
| 247 | if(count>=5) |
||
| 248 | { |
||
| 249 | cm.data_length=c; |
||
| 250 | count++; |
||
| 251 | return; |
||
| 252 | } |
||
| 31 | johboh | 253 | |
| 32 | johboh | 254 | // extended |
| 255 | if(count>=4) |
||
| 256 | { |
||
| 257 | cm.extended=c; |
||
| 258 | count++; |
||
| 259 | return; |
||
| 260 | } |
||
| 261 | |||
| 262 | // ident |
||
| 263 | if(count>=0) |
||
| 264 | { |
||
| 265 | cm.ident+=((DWORD)c<<(count*8)); |
||
| 266 | count++; |
||
| 267 | return; |
||
| 268 | } |
||
| 269 | |||
| 31 | johboh | 270 | } |
| 271 | |||
| 32 | johboh | 272 | |
| 273 | if (c==UART_START_BYTE && waitingMessage==FALSE) |
||
| 24 | johboh | 274 | { |
| 32 | johboh | 275 | waitingMessage=TRUE; |
| 276 | timeout=tickGet(); |
||
| 277 | count=0; |
||
| 278 | cm.ident=0; |
||
| 279 | return; |
||
| 24 | johboh | 280 | } |
| 281 | |||
| 32 | johboh | 282 | if(c=='1' && waitingMessage==FALSE) |
| 283 | { |
||
| 284 | cm.ident=0x00000123; |
||
| 285 | cm.extended=FALSE; |
||
| 286 | cm.data_length=4; |
||
| 33 | johboh | 287 | cm.data[3]='H'; |
| 288 | cm.data[2]='e'; |
||
| 289 | cm.data[1]='j'; |
||
| 32 | johboh | 290 | cm.data[0]='!'; |
| 33 | johboh | 291 | |
| 32 | johboh | 292 | while(!canSendMessage(cm)); |
| 24 | johboh | 293 | } |
| 294 | |||
| 32 | johboh | 295 | if(c=='2' && waitingMessage==FALSE) |
| 24 | johboh | 296 | { |
| 32 | johboh | 297 | cm.ident=0x0ABCDEF0; |
| 298 | cm.extended=TRUE; |
||
| 299 | cm.data_length=8; |
||
| 33 | johboh | 300 | cm.data[7]='G'; |
| 301 | cm.data[6]='o'; |
||
| 302 | cm.data[5]='d'; |
||
| 303 | cm.data[4]=' '; |
||
| 304 | cm.data[3]='d'; |
||
| 305 | cm.data[2]='a'; |
||
| 32 | johboh | 306 | cm.data[1]='g'; |
| 33 | johboh | 307 | cm.data[0]='!'; |
| 32 | johboh | 308 | |
| 309 | while(!canSendMessage(cm)); |
||
| 31 | johboh | 310 | } |
| 311 | |||
| 32 | johboh | 312 | if(c=='3' && waitingMessage==FALSE) |
| 31 | johboh | 313 | { |
| 32 | johboh | 314 | cm.ident=0x00000010; |
| 315 | cm.extended=FALSE; |
||
| 316 | cm.data_length=6; |
||
| 33 | johboh | 317 | cm.data[5]='5'; |
| 318 | cm.data[4]='4'; |
||
| 319 | cm.data[3]='3'; |
||
| 320 | cm.data[2]='2'; |
||
| 321 | cm.data[1]='1'; |
||
| 32 | johboh | 322 | cm.data[0]='0'; |
| 33 | johboh | 323 | |
| 32 | johboh | 324 | while(!canSendMessage(cm)); |
| 24 | johboh | 325 | } |
| 326 | |||
| 32 | johboh | 327 | |
| 48 | johboh | 328 | |
| 24 | johboh | 329 | } |
| 330 | #endif |