Subversion Repositories HomeAutomation

Rev

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

Rev 125 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/canIR/Source/Main.c $
6
 * FileName:        $HeadURL: https://svn.auml.se/svn/HomeAutomation/trunk/EmbeddedSoftware/PIC/canIR/Source/Main.c $
7
 * Last changed:    $LastChangedDate: 2006-11-19 15:59:01 +0100 (Sun, 19 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: 125 $
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_IREC
20
#ifdef USE_IREC
24
    #include <IRec.h>
21
    #include <IRec.h>
25
#endif
22
#endif
26
 
23
 
27
static void mainInit(void);
24
static void mainInit(void);
28
 
25
 
29
 
-
 
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
 
33
 
40
    mainInit();
34
    mainInit();
41
 
-
 
42
    #ifdef USE_CAN
-
 
43
        canInit();
35
    canInit();
44
    #endif
-
 
45
 
36
 
46
    #ifdef USE_IREC
37
    #ifdef USE_IREC
47
        irecInit();
38
        irecInit();
48
    #endif
39
    #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
 
40
 
68
 
41
 
69
    while(1)
42
    while(1)
70
    {
43
    {
71
        static TICK t = 0;
44
        static TICK t = 0;
72
        static TICK heartbeat = 0;
-
 
73
 
45
 
74
       
-
 
75
        if ((tickGet()-heartbeat)>TICK_SECOND*5)
-
 
76
        {
-
 
77
            // Send alive heartbeat
-
 
78
            outCm.funct                     = FUNCT_BOOTLOADER;
-
 
79
            outCm.funcc                     = FUNCC_BOOT_HEARTBEAT;
-
 
80
            outCm.nid                       = MY_NID;
-
 
81
            outCm.sid                       = MY_ID;
-
 
82
            outCm.data_length               = 1;
-
 
83
            outCm.data[BOOT_DATA_HEARTBEAT_INDEX] = HEARTBEAT_ALIVE;
-
 
84
            while(!canSendMessage(outCm,PRIO_HIGH));
-
 
85
           
-
 
86
            heartbeat = tickGet();
-
 
87
        }
-
 
88
        if ((tickGet()-t)>TICK_SECOND)
46
        if ((tickGet()-t)>TICK_SECOND)
89
        {
47
        {
90
            LED0_IO=~LED0_IO;
48
            LED0_IO=~LED0_IO;
91
            t = tickGet();
49
            t = tickGet();
92
        }
50
        }
93
    }
51
    }
94
}
52
}
95
 
53
 
96
 
54
 
97
#pragma interrupt high_isr
55
#pragma interrupt high_isr
98
void high_isr(void)
56
void high_isr(void)
99
{
57
{
100
    ClrWdt();
-
 
101
 
-
 
102
    #ifdef USE_CAN
-
 
103
        canISR();
58
    canISR();
104
    #endif
-
 
105
 
59
 
106
    #ifdef USE_IREC
60
    #ifdef USE_IREC
107
        irecISR();
61
        irecISR();
108
    #endif
62
    #endif
109
 
-
 
110
    tickUpdate();
-
 
111
 
63
 
112
}
64
}
113
 
65
 
114
 
66
 
115
#pragma interrupt low_isr
67
#pragma interrupt low_isr
116
void low_isr(void)
68
void low_isr(void)
117
{
69
{
118
 
70
 
119
}
71
}
120
 
72
 
121
 
73
 
122
 
74
 
123
/*
75
/*
124
*   Function: mainInit
76
*   Function: mainInit
125
*
77
*
126
*   Input:  none
78
*   Input:  none
127
*   Output: none
79
*   Output: none
128
*   Pre-conditions: none
80
*   Pre-conditions: none
129
*   Affects: Se code
81
*   Affects: Se code
130
*   Depends: none.
82
*   Depends: none.
Line 150... Line 102...
150
*   Output: none
102
*   Output: none
151
*   Pre-conditions: canInit and received packet.
103
*   Pre-conditions: canInit and received packet.
152
*   Affects: Sensors/actuators/etc. See code.
104
*   Affects: Sensors/actuators/etc. See code.
153
*   Depends: none.
105
*   Depends: none.
154
*/
106
*/
155
#ifdef USE_CAN
107
 
156
void canParse(CAN_MESSAGE cm)
108
void canParse(CAN_MESSAGE cm)
157
{
109
{
158
    CAN_MESSAGE outCm;
110
    CAN_MESSAGE outCm;
159
 
111
 
160
    ClrWdt();
-
 
161
 
-
 
162
    switch(cm.funct)
-
 
163
    {
-
 
164
        case FUNCT_BOOTLOADER:
-
 
165
            if (cm.nid==MY_NID && ((((unsigned int)cm.data[BOOT_DATA_RID_HIGH_INDEX])<<8)+cm.data[BOOT_DATA_RID_LOW_INDEX])==MY_ID)
-
 
166
            {
-
 
167
                switch(cm.funcc)
-
 
168
                {
-
 
169
                    case FUNCC_BOOT_INIT: Reset(); break; //User whant to program me, reset me.
-
 
170
                    case FUNCC_BOOT_SET_ID:
-
 
171
                        // Change my ID and NID
-
 
172
                        MY_ID=(((WORD)cm.data[BOOT_DATA_NEW_ID_HIGH_INDEX])<<8)+cm.data[BOOT_DATA_NEW_ID_LOW_INDEX];
-
 
173
                        MY_NID=cm.data[BOOT_DATA_NEW_NID_INDEX];
-
 
174
                        EEWrite(NODE_ID_EE+1,cm.data[BOOT_DATA_NEW_ID_HIGH_INDEX]);
-
 
175
                        EEWrite(NODE_ID_EE+2,cm.data[BOOT_DATA_NEW_ID_LOW_INDEX]);
-
 
176
                        EEWrite(NODE_ID_EE+3,cm.data[BOOT_DATA_NEW_NID_INDEX]);
-
 
177
                        EEWrite(NODE_ID_EE,NODE_HAS_ID);
-
 
178
                        // Send ack with new id.
-
 
179
                        outCm.funct                     = FUNCT_BOOTLOADER;
-
 
180
                        outCm.funcc                     = FUNCC_BOOT_ACK;
-
 
181
                        outCm.nid                       = MY_NID;
-
 
182
                        outCm.sid                       = MY_ID;
-
 
183
                        outCm.data_length               = 8;
-
 
184
                        outCm.data[BOOT_DATA_ERR_INDEX] = ACK_ERR_NO_ERROR;
-
 
185
                        while(!canSendMessage(outCm,PRIO_HIGH));
-
 
186
                    break;
-
 
187
                }
-
 
188
            }
-
 
189
 
-
 
190
 
-
 
191
        break;
-
 
192
    }
112
}
193
}
113
 
194
#endif
-
 
195
 
114
 
196
 
115
 
197
/*
116
/*
198
*   Function: irecParse
117
*   Function: irecParse
199
*
118
*
Line 207... Line 126...
207
void irecParse(IR_TYPE type, BYTE toggle, BYTE addr, BYTE data)
126
void irecParse(IR_TYPE type, BYTE toggle, BYTE addr, BYTE data)
208
{
127
{
209
    CAN_MESSAGE outCm;
128
    CAN_MESSAGE outCm;
210
    outCm.funct         = FUNCT_SENSORS;
129
    outCm.funct         = FUNCT_SENSORS;
211
    outCm.funcc         = FUNCC_SENSORS_IR;
130
    outCm.funcc         = FUNCC_SENSORS_IR;
212
    outCm.nid           = MY_NID;
-
 
213
    outCm.sid           = MY_ID;
-
 
214
    outCm.data_length   = 4;
131
    outCm.data_length   = 4;
215
    outCm.data[3]       = type;
132
    outCm.data[3]       = type;
216
    outCm.data[2]       = toggle;
133
    outCm.data[2]       = toggle;
217
    outCm.data[1]       = addr;
134
    outCm.data[1]       = addr;
218
    outCm.data[0]       = data;
135
    outCm.data[0]       = data;