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 | |||
| 31 | johboh | 29 | static BOOL heartBeatEnabled = TRUE; |
| 30 | |||
| 24 | johboh | 31 | void main() |
| 32 | { |
||
| 33 | static TICK t = 0; |
||
| 34 | |||
| 35 | // Inits |
||
| 36 | |||
| 37 | mainInit(); |
||
| 38 | |||
| 39 | #ifdef USE_CAN |
||
| 40 | canInit(); |
||
| 41 | #endif |
||
| 42 | |||
| 43 | #ifdef USE_UART |
||
| 44 | uartInit(); |
||
| 45 | #endif |
||
| 46 | |||
| 47 | tickInit(); |
||
| 48 | |||
| 49 | while(1) |
||
| 50 | { |
||
| 31 | johboh | 51 | if ((tickGet()-t)>=5*TICK_SECOND && heartBeatEnabled==TRUE) |
| 24 | johboh | 52 | { |
| 53 | CAN_MESSAGE cm; |
||
| 54 | |||
| 55 | t = tickGet(); |
||
| 56 | |||
| 57 | // Send heartbeat |
||
| 31 | johboh | 58 | cm.ident=0x00000666; |
| 24 | johboh | 59 | cm.extended=FALSE; |
| 60 | cm.data_length=1; |
||
| 61 | cm.data[0]='H'; |
||
| 62 | |||
| 63 | while(!canSendMessage(cm)); |
||
| 64 | |||
| 65 | |||
| 66 | } |
||
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 70 | |||
| 71 | #pragma interruptlow HighISR |
||
| 72 | void HighISR(void) |
||
| 73 | { |
||
| 74 | #ifdef USE_CAN |
||
| 75 | canISR(); |
||
| 76 | #endif |
||
| 77 | |||
| 78 | #ifdef USE_UART |
||
| 79 | uartISR(); |
||
| 80 | #endif |
||
| 81 | |||
| 82 | tickUpdate(); |
||
| 83 | } |
||
| 84 | #pragma code highVector=0x08 |
||
| 85 | void HighVector (void) |
||
| 86 | { |
||
| 87 | _asm goto HighISR _endasm |
||
| 88 | } |
||
| 89 | #pragma code // Return to default code section |
||
| 90 | |||
| 91 | |||
| 92 | |||
| 93 | #pragma interruptlow LowISR |
||
| 94 | void LowISR(void) |
||
| 95 | { |
||
| 96 | |||
| 97 | } |
||
| 98 | #pragma code lowVector=0x18 |
||
| 99 | void LowVector (void) |
||
| 100 | { |
||
| 101 | _asm goto LowISR _endasm |
||
| 102 | } |
||
| 103 | #pragma code // Return to default code section |
||
| 104 | |||
| 105 | |||
| 106 | /* |
||
| 107 | * Function: mainInit |
||
| 108 | * |
||
| 109 | * Input: none |
||
| 110 | * Output: none |
||
| 111 | * Pre-conditions: none |
||
| 112 | * Affects: Se code |
||
| 113 | * Depends: none. |
||
| 114 | */ |
||
| 115 | void mainInit() |
||
| 116 | { |
||
| 117 | LED0_TRIS=0; |
||
| 118 | |||
| 119 | // Enable internal PORTB pull-ups |
||
| 120 | INTCON2bits.RBPU = 0; |
||
| 121 | |||
| 122 | // Enable Interrupts |
||
| 123 | INTCONbits.GIEH = 1; |
||
| 124 | INTCONbits.GIEL = 1; |
||
| 125 | |||
| 126 | } |
||
| 127 | |||
| 128 | /* |
||
| 129 | * Function: canParse |
||
| 130 | * |
||
| 131 | * Input: Received can message |
||
| 132 | * Output: none |
||
| 133 | * Pre-conditions: canInit and received packet. |
||
| 134 | * Affects: Sensors/actuators/etc. See code. |
||
| 135 | * Depends: none. |
||
| 136 | */ |
||
| 137 | #ifdef USE_CAN |
||
| 138 | void canParse(CAN_MESSAGE cm) |
||
| 139 | { |
||
| 140 | int i=0; |
||
| 31 | johboh | 141 | |
| 142 | uartPutc(UART_START_BYTE); |
||
| 143 | uartPutc((BYTE)(cm.ident)); |
||
| 24 | johboh | 144 | uartPutc((BYTE)(cm.ident>>8)); |
| 31 | johboh | 145 | uartPutc((BYTE)(cm.ident>>16)); |
| 146 | uartPutc((BYTE)(cm.ident>>24)); |
||
| 147 | uartPutc(cm.extended); |
||
| 148 | uartPutc(cm.data_length); |
||
| 149 | for(i=0;i<8;i++) uartPutc(cm.data[cm.data_length-i-1]); |
||
| 150 | uartPutc(UART_END_BYTE); |
||
| 24 | johboh | 151 | |
| 152 | } |
||
| 153 | #endif |
||
| 154 | |||
| 155 | |||
| 156 | /* |
||
| 157 | * Function: uartParse |
||
| 158 | * |
||
| 159 | * Input: Received uart message |
||
| 160 | * Output: none |
||
| 161 | * Pre-conditions: uartInit and received byte. |
||
| 162 | * Affects: Sensors/actuators/etc. See code. |
||
| 163 | * Depends: none. |
||
| 164 | */ |
||
| 165 | #ifdef USE_UART |
||
| 166 | void uartParse(BYTE c) |
||
| 167 | { |
||
| 168 | |||
| 169 | |||
| 170 | if (c=='1') |
||
| 171 | { |
||
| 172 | CAN_MESSAGE cm; |
||
| 26 | johboh | 173 | cm.ident=0x00000123; |
| 174 | cm.extended=FALSE; |
||
| 175 | cm.data_length=4; |
||
| 176 | cm.data[0]='!'; |
||
| 177 | cm.data[1]='j'; |
||
| 178 | cm.data[2]='e'; |
||
| 179 | cm.data[3]='H'; |
||
| 180 | |||
| 181 | while(!canSendMessage(cm)); |
||
| 182 | } |
||
| 183 | |||
| 184 | if (c=='2') |
||
| 185 | { |
||
| 186 | CAN_MESSAGE cm; |
||
| 187 | cm.ident=0x0ABCDEF0; |
||
| 24 | johboh | 188 | cm.extended=TRUE; |
| 26 | johboh | 189 | cm.data_length=8; |
| 190 | cm.data[0]='!'; |
||
| 191 | cm.data[1]='g'; |
||
| 192 | cm.data[2]='a'; |
||
| 24 | johboh | 193 | cm.data[3]='d'; |
| 26 | johboh | 194 | cm.data[4]=' '; |
| 195 | cm.data[5]='d'; |
||
| 196 | cm.data[6]='o'; |
||
| 197 | cm.data[7]='G'; |
||
| 24 | johboh | 198 | |
| 26 | johboh | 199 | |
| 24 | johboh | 200 | while(!canSendMessage(cm)); |
| 201 | } |
||
| 202 | |||
| 31 | johboh | 203 | if (c=='3') |
| 204 | { |
||
| 205 | CAN_MESSAGE cm; |
||
| 206 | cm.ident=0x00000010; |
||
| 207 | cm.extended=FALSE; |
||
| 208 | cm.data_length=6; |
||
| 209 | cm.data[0]='0'; |
||
| 210 | cm.data[1]='1'; |
||
| 211 | cm.data[2]='2'; |
||
| 212 | cm.data[3]='3'; |
||
| 213 | cm.data[4]='4'; |
||
| 214 | cm.data[5]='5'; |
||
| 215 | |||
| 216 | while(!canSendMessage(cm)); |
||
| 217 | } |
||
| 218 | |||
| 24 | johboh | 219 | if (c=='d') |
| 220 | { |
||
| 31 | johboh | 221 | //uartPutc('D'); |
| 24 | johboh | 222 | } |
| 223 | |||
| 224 | if (c=='D') |
||
| 225 | { |
||
| 31 | johboh | 226 | //uartPutrs("Debug? Debug!"); |
| 24 | johboh | 227 | } |
| 228 | |||
| 31 | johboh | 229 | if (c=='T') |
| 24 | johboh | 230 | { |
| 31 | johboh | 231 | heartBeatEnabled=(heartBeatEnabled==TRUE?FALSE:TRUE); |
| 232 | } |
||
| 233 | |||
| 234 | if (c=='4') |
||
| 235 | { |
||
| 236 | /* |
||
| 24 | johboh | 237 | uartPutc(RXB0CON); |
| 238 | uartPutc(RXB1CON); |
||
| 239 | uartPutc(123); |
||
| 240 | uartPutc(B0CON); |
||
| 241 | uartPutc(B1CON); |
||
| 242 | uartPutc(B2CON); |
||
| 243 | uartPutc(123); |
||
| 244 | uartPutc(B3CON); |
||
| 245 | uartPutc(B4CON); |
||
| 246 | uartPutc(B5CON); |
||
| 247 | uartPutc(123); |
||
| 248 | uartPutc(CANSTAT); |
||
| 249 | uartPutc(COMSTAT); |
||
| 250 | uartPutc(ECANCON); |
||
| 251 | uartPutc(123); |
||
| 252 | uartPutc(TXB0CON); |
||
| 253 | uartPutc(TXB1CON); |
||
| 254 | uartPutc(TXB2CON); |
||
| 255 | uartPutc(123); |
||
| 256 | uartPutc(PIR3); |
||
| 257 | uartPutc(PIE3); |
||
| 258 | uartPutc(123); |
||
| 259 | uartPutc(BRGCON1); |
||
| 260 | uartPutc(BRGCON2); |
||
| 261 | uartPutc(BRGCON3); |
||
| 262 | uartPutc(123); |
||
| 263 | uartPutc(RXFCON0); |
||
| 264 | uartPutc(RXFCON1); |
||
| 265 | uartPutc(123); |
||
| 266 | uartPutc(RXFBCON0); |
||
| 267 | uartPutc(RXFBCON1); |
||
| 268 | uartPutc(RXFBCON2); |
||
| 269 | uartPutc(RXFBCON3); |
||
| 270 | uartPutc(RXFBCON4); |
||
| 271 | uartPutc(RXFBCON5); |
||
| 272 | uartPutc(RXFBCON6); |
||
| 273 | uartPutc(RXFBCON7); |
||
| 274 | uartPutc(123); |
||
| 275 | uartPutc(TXBIE); |
||
| 276 | uartPutc(BIE0); |
||
| 31 | johboh | 277 | */ |
| 24 | johboh | 278 | } |
| 279 | |||
| 280 | } |
||
| 281 | #endif |