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