Subversion Repositories HomeAutomation

Rev

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
 
32 johboh 29
static BOOL heartBeatEnabled = FALSE;
31 johboh 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);
33 johboh 149
    for(i=0;i<8;i++) uartPutc(cm.data[i]);
31 johboh 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
{
32 johboh 168
    BYTE i;
169
    unsigned int wait = 0;
170
    CAN_MESSAGE cm;
171
    static BOOL waitingMessage = FALSE;
33 johboh 172
    static TICK timeout=0;
173
    static BYTE count=0;
24 johboh 174
 
32 johboh 175
    if (waitingMessage==TRUE && (tickGet()-timeout)>TICK_SECOND/20)
24 johboh 176
    {
32 johboh 177
        waitingMessage=FALSE;
26 johboh 178
    }
179
 
32 johboh 180
    if (waitingMessage==TRUE)
26 johboh 181
    {
32 johboh 182
        timeout=tickGet();
24 johboh 183
 
32 johboh 184
        // UART END
185
        if(count>=14)
186
        {
187
            if (c==UART_END_BYTE)
188
            {
189
                while(!canSendMessage(cm));
190
            }
191
            waitingMessage=FALSE;
192
            return;
193
        }
26 johboh 194
 
32 johboh 195
        // data
196
        if(count>=6)
197
        {
198
            cm.data[count-6]=c;
199
            count++;
200
            return;
201
        }
24 johboh 202
 
32 johboh 203
        // data length
204
        if(count>=5)
205
        {
206
            cm.data_length=c;
207
            count++;
208
            return;
209
        }
31 johboh 210
 
32 johboh 211
        // extended
212
        if(count>=4)
213
        {
214
            cm.extended=c;
215
            count++;
216
            return;
217
        }
218
 
219
        // ident
220
        if(count>=0)
221
        {
222
            cm.ident+=((DWORD)c<<(count*8));
223
            count++;
224
            return;
225
        }
226
 
31 johboh 227
    }
228
 
32 johboh 229
 
230
    if (c==UART_START_BYTE && waitingMessage==FALSE)
24 johboh 231
    {
32 johboh 232
            waitingMessage=TRUE;
233
            timeout=tickGet();
234
            count=0;
235
            cm.ident=0;
236
            return;
24 johboh 237
    }
32 johboh 238
 
24 johboh 239
 
32 johboh 240
    if(c=='1' && waitingMessage==FALSE)
241
    {  
242
            cm.ident=0x00000123;
243
            cm.extended=FALSE;
244
            cm.data_length=4;
33 johboh 245
            cm.data[3]='H';
246
            cm.data[2]='e';
247
            cm.data[1]='j';
32 johboh 248
            cm.data[0]='!';
33 johboh 249
 
32 johboh 250
            while(!canSendMessage(cm));
24 johboh 251
    }
252
 
32 johboh 253
    if(c=='2' && waitingMessage==FALSE)  
24 johboh 254
    {
32 johboh 255
            cm.ident=0x0ABCDEF0;
256
            cm.extended=TRUE;
257
            cm.data_length=8;
33 johboh 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';
32 johboh 264
            cm.data[1]='g';
33 johboh 265
            cm.data[0]='!';    
32 johboh 266
 
267
            while(!canSendMessage(cm));
31 johboh 268
    }
269
 
32 johboh 270
    if(c=='3' && waitingMessage==FALSE)
31 johboh 271
    {
32 johboh 272
            cm.ident=0x00000010;
273
            cm.extended=FALSE;
274
            cm.data_length=6;
33 johboh 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';
32 johboh 280
            cm.data[0]='0';
33 johboh 281
 
32 johboh 282
            while(!canSendMessage(cm));
24 johboh 283
    }
284
 
32 johboh 285
 
286
 
24 johboh 287
}
288
#endif