Subversion Repositories HomeAutomation

Rev

Rev 32 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

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