Details | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 51 | 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 | |||
| 53 | johboh | 13 | #include <compiler.h> |
| 14 | #include <stackTasks.h> |
||
| 15 | #include <Tick.h> |
||
| 51 | johboh | 16 | |
| 17 | #ifdef USE_CAN |
||
| 53 | johboh | 18 | #include <CAN.h> |
| 51 | johboh | 19 | #endif |
| 20 | |||
| 21 | #ifdef USE_UART |
||
| 53 | johboh | 22 | #include <uart.h> |
| 51 | johboh | 23 | #endif |
| 24 | |||
| 25 | |||
| 26 | |||
| 27 | static void mainInit(void); |
||
| 28 | |||
| 29 | void main() |
||
| 30 | { |
||
| 31 | static TICK t = 0; |
||
| 32 | |||
| 33 | // Inits |
||
| 34 | |||
| 35 | mainInit(); |
||
| 36 | |||
| 37 | #ifdef USE_CAN |
||
| 38 | canInit(); |
||
| 39 | #endif |
||
| 40 | |||
| 41 | #ifdef USE_UART |
||
| 42 | uartInit(); |
||
| 43 | #endif |
||
| 44 | |||
| 45 | tickInit(); |
||
| 46 | |||
| 64 | johboh | 47 | |
| 48 | |||
| 51 | johboh | 49 | while(1) |
| 64 | johboh | 50 | { |
| 51 | johboh | 51 | } |
| 52 | |||
| 53 | } |
||
| 54 | |||
| 55 | |||
| 56 | #pragma interruptlow HighISR |
||
| 57 | void HighISR(void) |
||
| 58 | { |
||
| 59 | |||
| 60 | |||
| 61 | #ifdef USE_UART |
||
| 62 | uartISR(); |
||
| 63 | #endif |
||
| 64 | |||
| 65 | #ifdef USE_CAN |
||
| 66 | canISR(); |
||
| 67 | #endif |
||
| 68 | |||
| 69 | |||
| 70 | tickUpdate(); |
||
| 71 | } |
||
| 72 | #pragma code highVector=0x08 |
||
| 73 | void HighVector (void) |
||
| 74 | { |
||
| 75 | _asm goto HighISR _endasm |
||
| 76 | } |
||
| 77 | #pragma code // Return to default code section |
||
| 78 | |||
| 79 | |||
| 80 | |||
| 81 | #pragma interruptlow LowISR |
||
| 82 | void LowISR(void) |
||
| 83 | { |
||
| 84 | |||
| 85 | } |
||
| 86 | #pragma code lowVector=0x18 |
||
| 87 | void LowVector (void) |
||
| 88 | { |
||
| 89 | _asm goto LowISR _endasm |
||
| 90 | } |
||
| 91 | #pragma code // Return to default code section |
||
| 92 | |||
| 93 | |||
| 94 | /* |
||
| 95 | * Function: mainInit |
||
| 96 | * |
||
| 97 | * Input: none |
||
| 98 | * Output: none |
||
| 99 | * Pre-conditions: none |
||
| 100 | * Affects: Se code |
||
| 101 | * Depends: none. |
||
| 102 | */ |
||
| 103 | void mainInit() |
||
| 104 | { |
||
| 105 | LED0_TRIS=0; |
||
| 106 | |||
| 107 | // Enable internal PORTB pull-ups |
||
| 108 | INTCON2bits.RBPU = 0; |
||
| 109 | |||
| 110 | // Enable Interrupts |
||
| 111 | INTCONbits.GIEH = 1; |
||
| 112 | INTCONbits.GIEL = 1; |
||
| 113 | |||
| 114 | |||
| 115 | // Enable interrupt prirority |
||
| 116 | RCONbits.IPEN=1; |
||
| 117 | |||
| 118 | |||
| 119 | } |
||
| 120 | |||
| 121 | /* |
||
| 122 | * Function: canParse |
||
| 123 | * |
||
| 124 | * Input: Received can message |
||
| 125 | * Output: none |
||
| 126 | * Pre-conditions: canInit and received packet. |
||
| 127 | * Affects: Sensors/actuators/etc. See code. |
||
| 128 | * Depends: none. |
||
| 129 | */ |
||
| 130 | #ifdef USE_CAN |
||
| 131 | void canParse(CAN_MESSAGE cm) |
||
| 132 | { |
||
| 133 | int i=0; |
||
| 134 | |||
| 135 | uartPutc(UART_START_BYTE); |
||
| 136 | uartPutc((BYTE)(cm.ident)); |
||
| 137 | uartPutc((BYTE)(cm.ident>>8)); |
||
| 138 | uartPutc((BYTE)(cm.ident>>16)); |
||
| 139 | uartPutc((BYTE)(cm.ident>>24)); |
||
| 140 | uartPutc(cm.extended); |
||
| 56 | johboh | 141 | uartPutc(cm.remote_request); |
| 51 | johboh | 142 | uartPutc(cm.data_length); |
| 143 | for(i=0;i<8;i++) uartPutc(cm.data[i]); |
||
| 144 | uartPutc(UART_END_BYTE); |
||
| 145 | |||
| 146 | } |
||
| 147 | #endif |
||
| 148 | |||
| 149 | |||
| 150 | /* |
||
| 151 | * Function: uartParse |
||
| 152 | * |
||
| 153 | * Input: Received uart message |
||
| 154 | * Output: none |
||
| 155 | * Pre-conditions: uartInit and received byte. |
||
| 156 | * Affects: Sensors/actuators/etc. See code. |
||
| 157 | * Depends: none. |
||
| 158 | */ |
||
| 159 | #ifdef USE_UART |
||
| 160 | void uartParse(BYTE c) |
||
| 161 | { |
||
| 162 | BYTE i; |
||
| 163 | unsigned int wait = 0; |
||
| 164 | CAN_MESSAGE cm; |
||
| 165 | static BOOL waitingMessage = FALSE; |
||
| 166 | static TICK timeout=0; |
||
| 167 | static BYTE count=0; |
||
| 168 | |||
| 169 | if (waitingMessage==TRUE && (tickGet()-timeout)>TICK_SECOND/20) |
||
| 170 | { |
||
| 171 | waitingMessage=FALSE; |
||
| 172 | } |
||
| 173 | |||
| 174 | |||
| 175 | if (waitingMessage==TRUE) |
||
| 176 | { |
||
| 177 | |||
| 178 | timeout=tickGet(); |
||
| 179 | |||
| 56 | johboh | 180 | |
| 51 | johboh | 181 | // UART END |
| 56 | johboh | 182 | if(count>=15) |
| 51 | johboh | 183 | { |
| 184 | if (c==UART_END_BYTE) |
||
| 185 | { |
||
| 56 | johboh | 186 | LED0_IO=~LED0_IO; |
| 64 | johboh | 187 | //canParse(cm); |
| 188 | while(!canSendMessage(cm,PRIO_HIGEST)); |
||
| 51 | johboh | 189 | } |
| 190 | waitingMessage=FALSE; |
||
| 191 | return; |
||
| 192 | } |
||
| 193 | |||
| 194 | // data |
||
| 56 | johboh | 195 | if(count>=7) |
| 51 | johboh | 196 | { |
| 56 | johboh | 197 | cm.data[count-7]=c; |
| 51 | johboh | 198 | count++; |
| 199 | return; |
||
| 200 | } |
||
| 201 | |||
| 202 | // data length |
||
| 56 | johboh | 203 | if(count>=6) |
| 51 | johboh | 204 | { |
| 205 | cm.data_length=c; |
||
| 206 | count++; |
||
| 207 | return; |
||
| 208 | } |
||
| 209 | |||
| 56 | johboh | 210 | // remote request |
| 211 | if(count>=5) |
||
| 212 | { |
||
| 213 | cm.remote_request=c; |
||
| 214 | count++; |
||
| 215 | return; |
||
| 216 | } |
||
| 217 | |||
| 51 | johboh | 218 | // extended |
| 219 | if(count>=4) |
||
| 220 | { |
||
| 221 | cm.extended=c; |
||
| 222 | count++; |
||
| 223 | return; |
||
| 224 | } |
||
| 225 | |||
| 226 | // ident |
||
| 227 | if(count>=0) |
||
| 228 | { |
||
| 229 | cm.ident+=((DWORD)c<<(count*8)); |
||
| 230 | count++; |
||
| 231 | return; |
||
| 232 | } |
||
| 233 | |||
| 234 | } |
||
| 235 | |||
| 236 | |||
| 237 | if (c==UART_START_BYTE && waitingMessage==FALSE) |
||
| 238 | { |
||
| 239 | waitingMessage=TRUE; |
||
| 240 | timeout=tickGet(); |
||
| 241 | count=0; |
||
| 242 | cm.ident=0; |
||
| 243 | return; |
||
| 244 | } |
||
| 245 | |||
| 246 | |||
| 247 | } |
||
| 248 | #endif |