Subversion Repositories HomeAutomation

Rev

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

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