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