Subversion Repositories HomeAutomation

Rev

Rev 141 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 141 Rev 260
Line 2... Line 2...
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
        }
Line 142... Line 93...
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
        }
Line 158... Line 107...
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)
Line 214... Line 156...
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