Subversion Repositories HomeAutomation

Rev

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

Rev 141 Rev 260
1
/*********************************************************************
1
/*********************************************************************
2
 *
2
 *
3
 *                  Main application
3
 *                  Main application
4
 *
4
 *
5
 *********************************************************************
5
 *********************************************************************
6
 * FileName:        $HeadURL: https://svn.auml.se/svn/HomeAutomation/trunk/EmbeddedSoftware/PIC/canFreezer/Source/Main.c $
6
 * FileName:        $HeadURL: https://svn.auml.se/svn/HomeAutomation/trunk/EmbeddedSoftware/PIC/canFreezer/Source/Main.c $
7
 * Last changed:    $LastChangedDate: 2006-11-21 10:57:15 +0100 (Tue, 21 Nov 2006) $
7
 * Last changed:    $LastChangedDate: 2007-01-28 14:58:11 +0100 (Sun, 28 Jan 2007) $
8
 * By:              $LastChangedBy: johboh $
8
 * By:              $LastChangedBy: johboh $
9
 * Revision:        $Revision: 141 $
9
 * Revision:        $Revision: 260 $
10
 ********************************************************************/
10
 ********************************************************************/
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
#include <funcdefs.h>
16
#include <funcdefs.h>
17
#include <EEaccess.h>
-
 
18
 
-
 
19
#ifdef USE_CAN
-
 
20
    #include <CAN.h>
17
#include <CAN.h>
21
#endif
18
 
22
 
19
 
23
#ifdef USE_ADC
20
#ifdef USE_ADC
24
    #include <adc.h>
21
    #include <adc.h>
25
#endif
22
#endif
26
 
23
 
27
static void mainInit(void);
24
static void mainInit(void);
28
 
-
 
29
 
25
 
30
static int MY_ID = DEFAULT_ID;
-
 
31
static BYTE MY_NID = DEFAULT_NID;
-
 
32
 
26
 
33
void main()
27
void main()
34
{
28
{
35
    static TICK t = 0;
29
    static TICK t = 0;
36
    CAN_MESSAGE outCm;
30
    CAN_MESSAGE outCm;
37
 
31
 
38
    // Inits
32
    // Inits
39
 
-
 
40
    mainInit();
33
    mainInit();
41
 
-
 
42
    #ifdef USE_CAN
-
 
43
        canInit();
34
    canInit();
44
    #endif
-
 
45
 
35
 
46
    #ifdef USE_ADC
36
    #ifdef USE_ADC
47
        adcInit(TRUE,0b1011);
37
        adcInit(TRUE,0b1011);
48
    #endif
38
    #endif
49
 
-
 
50
    tickInit();
-
 
51
 
-
 
52
    // Read ID and NID if exist.
-
 
53
    if (EERead(NODE_ID_EE)==NODE_HAS_ID)
-
 
54
    {
-
 
55
        MY_ID=(((WORD)EERead(NODE_ID_EE + 1))<<8)+EERead(NODE_ID_EE + 2);
-
 
56
        MY_NID=EERead(NODE_ID_EE + 3);
-
 
57
    }
-
 
58
 
-
 
59
    // Send user program startup heatbeat
-
 
60
    outCm.funct                     = FUNCT_BOOTLOADER;
-
 
61
    outCm.funcc                     = FUNCC_BOOT_HEARTBEAT;
-
 
62
    outCm.nid                       = MY_NID;
-
 
63
    outCm.sid                       = MY_ID;
-
 
64
    outCm.data_length               = 1;
-
 
65
    outCm.data[BOOT_DATA_HEARTBEAT_INDEX] = HEARTBEAT_USER_STARTUP;
-
 
66
    while(!canSendMessage(outCm,PRIO_HIGH));
-
 
67
 
-
 
68
 
39
 
69
    while(1)
40
    while(1)
70
    {
41
    {
71
        static TICK t = 0;
42
        static TICK t = 0;
72
        static TICK heartbeat = 0;
-
 
73
        static TICK temperature = 0;
43
        static TICK temperature = 0;
74
        static BYTE lastTemperature = TEMPERATURE_FREEZER;
44
        static BYTE lastTemperature = TEMPERATURE_FREEZER;
75
        static BYTE lastFreezerDoor = DOOR_CLOSED;
45
        static BYTE lastFreezerDoor = DOOR_CLOSED;
76
        static BYTE lastRefrigeratorDoor = DOOR_CLOSED;
46
        static BYTE lastRefrigeratorDoor = DOOR_CLOSED;
77
 
47
 
78
 
48
 
79
        //Poll doors freezer door
49
        //Poll doors freezer door
80
        if (lastFreezerDoor!=DOOR_FREEZER_IO)
50
        if (lastFreezerDoor!=DOOR_FREEZER_IO)
81
        {  
51
        {  
82
            lastFreezerDoor=DOOR_FREEZER_IO;
52
            lastFreezerDoor=DOOR_FREEZER_IO;
83
            // Send door status change
53
            // Send door status change
84
            outCm.funct                     = FUNCT_SENSORS;
54
            outCm.funct                     = FUNCT_SENSORS;
85
            outCm.funcc                     = FUNCC_SENSORS_DOORS_FREEZER;
55
            outCm.funcc                     = FUNCC_SENSORS_DOORS_FREEZER;
86
            outCm.nid                       = MY_NID;
-
 
87
            outCm.sid                       = MY_ID;
-
 
88
            outCm.data_length               = 1;
56
            outCm.data_length               = 1;
89
            outCm.data[0]                   = (lastFreezerDoor==DOOR_OPEN?0:1);
57
            outCm.data[0]                   = (lastFreezerDoor==DOOR_OPEN?0:1);
90
            while(!canSendMessage(outCm,PRIO_HIGH));
58
            while(!canSendMessage(outCm,PRIO_HIGH));
91
        }
59
        }
92
 
60
 
93
        //Poll doors refrigerator door
61
        //Poll doors refrigerator door
94
        if (lastRefrigeratorDoor!=DOOR_REFRIGERATOR_IO)
62
        if (lastRefrigeratorDoor!=DOOR_REFRIGERATOR_IO)
95
        {  
63
        {  
96
            lastRefrigeratorDoor=DOOR_REFRIGERATOR_IO;
64
            lastRefrigeratorDoor=DOOR_REFRIGERATOR_IO;
97
            // Send door status change
65
            // Send door status change
98
            outCm.funct                     = FUNCT_SENSORS;
66
            outCm.funct                     = FUNCT_SENSORS;
99
            outCm.funcc                     = FUNCC_SENSORS_DOORS_REFRIGERATOR;
67
            outCm.funcc                     = FUNCC_SENSORS_DOORS_REFRIGERATOR;
100
            outCm.nid                       = MY_NID;
-
 
101
            outCm.sid                       = MY_ID;
-
 
102
            outCm.data_length               = 1;
68
            outCm.data_length               = 1;
103
            outCm.data[0]                   = (lastRefrigeratorDoor==DOOR_OPEN?0:1);
69
            outCm.data[0]                   = (lastRefrigeratorDoor==DOOR_OPEN?0:1);
104
            while(!canSendMessage(outCm,PRIO_HIGH));
70
            while(!canSendMessage(outCm,PRIO_HIGH));
105
        }
-
 
106
 
-
 
107
       
-
 
108
        if ((tickGet()-heartbeat)>TICK_SECOND*5)
-
 
109
        {
-
 
110
            // Send alive heartbeat
-
 
111
            outCm.funct                     = FUNCT_BOOTLOADER;
-
 
112
            outCm.funcc                     = FUNCC_BOOT_HEARTBEAT;
-
 
113
            outCm.nid                       = MY_NID;
-
 
114
            outCm.sid                       = MY_ID;
-
 
115
            outCm.data_length               = 1;
-
 
116
            outCm.data[BOOT_DATA_HEARTBEAT_INDEX] = HEARTBEAT_ALIVE;
-
 
117
            while(!canSendMessage(outCm,PRIO_HIGH));
-
 
118
           
-
 
119
            heartbeat = tickGet();
-
 
120
        }
71
        }
121
 
72
 
122
        if ((tickGet()-t)>TICK_SECOND)
73
        if ((tickGet()-t)>TICK_SECOND)
123
        {
74
        {
124
            LED0_IO=~LED0_IO;
75
            LED0_IO=~LED0_IO;
125
            t = tickGet();
76
            t = tickGet();
126
        }
77
        }
127
 
78
 
128
        #ifdef USE_ADC
79
        #ifdef USE_ADC
129
        if ((tickGet()-temperature)>TICK_SECOND*2) // Each 2 second, read temperature.
80
        if ((tickGet()-temperature)>TICK_SECOND*2) // Each 2 second, read temperature.
130
        {
81
        {
131
            if (lastTemperature==TEMPERATURE_FREEZER) lastTemperature=TEMPERATURE_REFRIGERATOR; else lastTemperature=TEMPERATURE_FREEZER;
82
            if (lastTemperature==TEMPERATURE_FREEZER) lastTemperature=TEMPERATURE_REFRIGERATOR; else lastTemperature=TEMPERATURE_FREEZER;
132
            adcConvert(lastTemperature);
83
            adcConvert(lastTemperature);
133
            temperature = tickGet();
84
            temperature = tickGet();
134
        }
85
        }
135
        #endif
86
        #endif
136
 
87
 
137
 
88
 
138
        #ifdef USE_ADC
89
        #ifdef USE_ADC
139
        if (adcDone()) // Has temperature, send it to the bus
90
        if (adcDone()) // Has temperature, send it to the bus
140
        {
91
        {
141
            BYTE decimal;
92
            BYTE decimal;
142
            signed char tenth;
93
            signed char tenth;
143
            temperatureRead(&tenth,&decimal);
94
            temperatureRead(&tenth,&decimal);
144
 
95
 
145
            outCm.funct                     = FUNCT_SENSORS;
96
            outCm.funct                     = FUNCT_SENSORS;
146
            outCm.funcc                     = (lastTemperature==TEMPERATURE_FREEZER?FUNCC_SENSORS_TEMPERATURE_FREEZER:FUNCC_SENSORS_TEMPERATURE_REFRIGERATOR);
97
            outCm.funcc                     = (lastTemperature==TEMPERATURE_FREEZER?FUNCC_SENSORS_TEMPERATURE_FREEZER:FUNCC_SENSORS_TEMPERATURE_REFRIGERATOR);
147
            outCm.nid                       = MY_NID;
-
 
148
            outCm.sid                       = MY_ID;
-
 
149
            outCm.data_length               = 2;
98
            outCm.data_length               = 2;
150
            outCm.data[1]                   = tenth; //  signed 10 an 1 decimal.
99
            outCm.data[1]                   = tenth; //  signed 10 an 1 decimal.
151
            outCm.data[0]                   = decimal; //  Decimal value, 0-9
100
            outCm.data[0]                   = decimal; //  Decimal value, 0-9
152
            while(!canSendMessage(outCm,PRIO_HIGH));
101
            while(!canSendMessage(outCm,PRIO_HIGH));
153
        }
102
        }
154
        #endif
103
        #endif
155
 
104
 
156
    }
105
    }
157
}
106
}
158
 
107
 
159
 
108
 
160
#pragma interrupt high_isr
109
#pragma interrupt high_isr
161
void high_isr(void)
110
void high_isr(void)
162
{
111
{
163
    ClrWdt();
-
 
164
 
-
 
165
    #ifdef USE_CAN
-
 
166
        canISR();
112
    canISR();
167
    #endif
-
 
168
 
-
 
169
    tickUpdate();
-
 
170
 
-
 
171
}
113
}
172
 
114
 
173
 
115
 
174
#pragma interrupt low_isr
116
#pragma interrupt low_isr
175
void low_isr(void)
117
void low_isr(void)
176
{
118
{
177
    #ifdef USE_ADC
119
    #ifdef USE_ADC
178
        adcISR();
120
        adcISR();
179
    #endif
121
    #endif
180
}
122
}
181
 
123
 
182
 
124
 
183
 
125
 
184
/*
126
/*
185
*   Function: mainInit
127
*   Function: mainInit
186
*
128
*
187
*   Input:  none
129
*   Input:  none
188
*   Output: none
130
*   Output: none
189
*   Pre-conditions: none
131
*   Pre-conditions: none
190
*   Affects: Se code
132
*   Affects: Se code
191
*   Depends: none.
133
*   Depends: none.
192
*/
134
*/
193
void mainInit()
135
void mainInit()
194
{
136
{
195
    LED0_TRIS=0;   
137
    LED0_TRIS=0;   
196
 
138
 
197
    DOOR_FREEZER_TRIS=1;
139
    DOOR_FREEZER_TRIS=1;
198
    DOOR_REFRIGERATOR_TRIS=1;
140
    DOOR_REFRIGERATOR_TRIS=1;
199
 
141
 
200
    // Enable Interrupts
142
    // Enable Interrupts
201
    INTCONbits.GIEH = 1;
143
    INTCONbits.GIEH = 1;
202
    INTCONbits.GIEL = 1;
144
    INTCONbits.GIEL = 1;
203
 
145
 
204
    // Enable interrupt prirority
146
    // Enable interrupt prirority
205
    RCONbits.IPEN=1;
147
    RCONbits.IPEN=1;
206
   
148
   
207
 
149
 
208
}
150
}
209
 
151
 
210
/*
152
/*
211
*   Function: canParse
153
*   Function: canParse
212
*
154
*
213
*   Input:  Received can message
155
*   Input:  Received can message
214
*   Output: none
156
*   Output: none
215
*   Pre-conditions: canInit and received packet.
157
*   Pre-conditions: canInit and received packet.
216
*   Affects: Sensors/actuators/etc. See code.
158
*   Affects: Sensors/actuators/etc. See code.
217
*   Depends: none.
159
*   Depends: none.
218
*/
160
*/
219
#ifdef USE_CAN
-
 
220
void canParse(CAN_MESSAGE cm)
161
void canParse(CAN_MESSAGE cm)
221
{
162
{
222
    CAN_MESSAGE outCm;
163
    CAN_MESSAGE outCm;
223
 
-
 
224
    ClrWdt();
-
 
225
 
-
 
226
    switch(cm.funct)
-
 
227
    {
-
 
228
        case FUNCT_BOOTLOADER:
-
 
229
            if (cm.nid==MY_NID && ((((unsigned int)cm.data[BOOT_DATA_RID_HIGH_INDEX])<<8)+cm.data[BOOT_DATA_RID_LOW_INDEX])==MY_ID)
-
 
230
            {
-
 
231
                switch(cm.funcc)
-
 
232
                {
-
 
233
                    case FUNCC_BOOT_INIT: Reset(); break; //User whant to program me, reset me.
-
 
234
                    case FUNCC_BOOT_SET_ID:
-
 
235
                        // Change my ID and NID
-
 
236
                        MY_ID=(((WORD)cm.data[BOOT_DATA_NEW_ID_HIGH_INDEX])<<8)+cm.data[BOOT_DATA_NEW_ID_LOW_INDEX];
-
 
237
                        MY_NID=cm.data[BOOT_DATA_NEW_NID_INDEX];
-
 
238
                        EEWrite(NODE_ID_EE+1,cm.data[BOOT_DATA_NEW_ID_HIGH_INDEX]);
-
 
239
                        EEWrite(NODE_ID_EE+2,cm.data[BOOT_DATA_NEW_ID_LOW_INDEX]);
-
 
240
                        EEWrite(NODE_ID_EE+3,cm.data[BOOT_DATA_NEW_NID_INDEX]);
-
 
241
                        EEWrite(NODE_ID_EE,NODE_HAS_ID);
-
 
242
                        // Send ack with new id.
-
 
243
                        outCm.funct                     = FUNCT_BOOTLOADER;
-
 
244
                        outCm.funcc                     = FUNCC_BOOT_ACK;
-
 
245
                        outCm.nid                       = MY_NID;
-
 
246
                        outCm.sid                       = MY_ID;
-
 
247
                        outCm.data_length               = 8;
-
 
248
                        outCm.data[BOOT_DATA_ERR_INDEX] = ACK_ERR_NO_ERROR;
-
 
249
                        while(!canSendMessage(outCm,PRIO_HIGH));
-
 
250
                    break;
-
 
251
                }
-
 
252
            }
-
 
253
 
-
 
254
 
-
 
255
        break;
-
 
256
    }
-
 
257
}
164
}
258
#endif
-
 
259
 
-
 
260
 
165
 
261
 
166
 
262
// Below are mandantory when using bootloader
167
// Below are mandantory when using bootloader
263
// define DEBUG_MODE to use without bootloader.
168
// define DEBUG_MODE to use without bootloader.
264
 
169
 
265
#ifndef DEBUG_MODE
170
#ifndef DEBUG_MODE
266
extern void _startup (void);
171
extern void _startup (void);
267
#pragma code _RESET_INTERRUPT_VECTOR = 0x001000
172
#pragma code _RESET_INTERRUPT_VECTOR = 0x001000
268
void _reset (void)
173
void _reset (void)
269
{
174
{
270
    _asm goto _startup _endasm
175
    _asm goto _startup _endasm
271
}
176
}
272
#pragma code
177
#pragma code
273
#endif
178
#endif
274
 
179
 
275
#pragma code _HIGH_INTERRUPT_VECTOR = 0x001008
180
#pragma code _HIGH_INTERRUPT_VECTOR = 0x001008
276
void _high_ISR (void)
181
void _high_ISR (void)
277
{
182
{
278
    _asm GOTO high_isr _endasm
183
    _asm GOTO high_isr _endasm
279
}
184
}
280
#pragma code
185
#pragma code
281
 
186
 
282
#pragma code _LOW_INTERRUPT_VECTOR = 0x001018
187
#pragma code _LOW_INTERRUPT_VECTOR = 0x001018
283
void _low_ISR (void)
188
void _low_ISR (void)
284
{
189
{
285
    _asm GOTO low_isr _endasm
190
    _asm GOTO low_isr _endasm
286
}
191
}
287
#pragma code
192
#pragma code
288
 
193