Subversion Repositories HomeAutomation

Rev

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

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