Subversion Repositories HomeAutomation

Rev

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

Rev 53 Rev 56
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 <compiler.h>
13
#include <compiler.h>
14
#include <stackTasks.h>
14
#include <stackTasks.h>
15
#include <Tick.h>
15
#include <Tick.h>
16
 
16
 
17
#ifdef USE_CAN
17
#ifdef USE_CAN
18
    #include <CAN.h>
18
    #include <CAN.h>
19
#endif
19
#endif
20
 
20
 
21
#ifdef USE_UART
21
#ifdef USE_UART
22
    #include <uart.h>
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.remote_request);
140
    uartPutc(cm.data_length);
141
    uartPutc(cm.data_length);
141
    for(i=0;i<8;i++) uartPutc(cm.data[i]);
142
    for(i=0;i<8;i++) uartPutc(cm.data[i]);
142
    uartPutc(UART_END_BYTE);
143
    uartPutc(UART_END_BYTE);
143
 
144
 
144
}
145
}
145
#endif
146
#endif
146
 
147
 
147
 
148
 
148
/*
149
/*
149
*   Function: uartParse
150
*   Function: uartParse
150
*
151
*
151
*   Input:  Received uart message
152
*   Input:  Received uart message
152
*   Output: none
153
*   Output: none
153
*   Pre-conditions: uartInit and received byte.
154
*   Pre-conditions: uartInit and received byte.
154
*   Affects: Sensors/actuators/etc. See code.
155
*   Affects: Sensors/actuators/etc. See code.
155
*   Depends: none.
156
*   Depends: none.
156
*/
157
*/
157
#ifdef USE_UART
158
#ifdef USE_UART
158
void uartParse(BYTE c)
159
void uartParse(BYTE c)
159
{
160
{
160
    BYTE i;
161
    BYTE i;
161
    unsigned int wait = 0;
162
    unsigned int wait = 0;
162
    CAN_MESSAGE cm;
163
    CAN_MESSAGE cm;
163
    static BOOL waitingMessage = FALSE;
164
    static BOOL waitingMessage = FALSE;
164
    static TICK timeout=0;
165
    static TICK timeout=0;
165
    static BYTE count=0;
166
    static BYTE count=0;
166
 
167
 
167
 
168
 
168
    if (waitingMessage==TRUE && (tickGet()-timeout)>TICK_SECOND/20)
169
    if (waitingMessage==TRUE && (tickGet()-timeout)>TICK_SECOND/20)
169
    {
170
    {
170
        waitingMessage=FALSE;
171
        waitingMessage=FALSE;
171
    }
172
    }
172
 
173
 
173
 
174
 
174
    if (waitingMessage==TRUE)
175
    if (waitingMessage==TRUE)
175
    {
176
    {
176
 
177
 
177
        timeout=tickGet();
178
        timeout=tickGet();
-
 
179
 
178
 
180
 
179
        // UART END
181
        // UART END
180
        if(count>=14)
182
        if(count>=15)
181
        {
183
        {
182
            if (c==UART_END_BYTE)
184
            if (c==UART_END_BYTE)
183
            {
185
            {
-
 
186
                LED0_IO=~LED0_IO;
184
                while(!canSendMessage(cm));
187
                while(!canSendMessage(cm,PRIO_LOWEST));
185
            }
188
            }
186
            waitingMessage=FALSE;
189
            waitingMessage=FALSE;
187
            return;
190
            return;
188
        }
191
        }
189
 
192
 
190
        // data
193
        // data
-
 
194
        if(count>=7)
-
 
195
        {
-
 
196
            cm.data[count-7]=c;
-
 
197
            count++;
-
 
198
            return;
-
 
199
        }
-
 
200
 
-
 
201
        // data length
191
        if(count>=6)
202
        if(count>=6)
192
        {
203
        {
193
            cm.data[count-6]=c;
204
            cm.data_length=c;
194
            count++;
205
            count++;
195
            return;
206
            return;
196
        }
207
        }
197
 
208
 
198
        // data length
209
        // remote request
199
        if(count>=5)
210
        if(count>=5)
200
        {
211
        {
201
            cm.data_length=c;
212
            cm.remote_request=c;
202
            count++;
213
            count++;
203
            return;
214
            return;
204
        }
215
        }
205
 
216
 
206
        // extended
217
        // extended
207
        if(count>=4)
218
        if(count>=4)
208
        {
219
        {
209
            cm.extended=c;
220
            cm.extended=c;
210
            count++;
221
            count++;
211
            return;
222
            return;
212
        }
223
        }
213
 
224
 
214
        // ident
225
        // ident
215
        if(count>=0)
226
        if(count>=0)
216
        {
227
        {
217
            cm.ident+=((DWORD)c<<(count*8));
228
            cm.ident+=((DWORD)c<<(count*8));
218
            count++;
229
            count++;
219
            return;
230
            return;
220
        }
231
        }
221
   
232
   
222
    }
233
    }
223
 
234
 
224
 
235
 
225
    if (c==UART_START_BYTE && waitingMessage==FALSE)
236
    if (c==UART_START_BYTE && waitingMessage==FALSE)
226
    {
237
    {
227
            waitingMessage=TRUE;
238
            waitingMessage=TRUE;
228
            timeout=tickGet();
239
            timeout=tickGet();
229
            count=0;
240
            count=0;
230
            cm.ident=0;
241
            cm.ident=0;
231
            return;
242
            return;
232
    }
243
    }
233
 
244
 
234
 
245
 
235
}
246
}
236
#endif
247
#endif
237
 
248