Subversion Repositories HomeAutomation

Rev

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

Rev 129 Rev 260
Line 1... Line 1...
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/canIRtrans/Source/Main.c $
6
 * FileName:        $HeadURL: https://svn.auml.se/svn/HomeAutomation/trunk/EmbeddedSoftware/PIC/canIRtrans/Source/Main.c $
7
 * Last changed:    $LastChangedDate: 2006-11-20 17:54:03 +0100 (Mon, 20 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: 129 $
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
-
 
22
 
18
 
23
static void mainInit(void);
19
static void mainInit(void);
24
 
-
 
25
 
-
 
26
static int MY_ID = DEFAULT_ID;
-
 
27
static BYTE MY_NID = DEFAULT_NID;
-
 
28
 
20
 
29
void main()
21
void main()
30
{
22
{
31
    static TICK t = 0;
23
    static TICK t = 0;
32
    CAN_MESSAGE outCm;
24
    CAN_MESSAGE outCm;
33
 
25
 
34
    // Inits
26
    // Inits
35
 
27
 
36
    mainInit();
28
    mainInit();
37
 
-
 
38
    #ifdef USE_CAN
-
 
39
        canInit();
29
    canInit();
40
    #endif
-
 
41
 
30
 
42
    tickInit();
-
 
43
 
-
 
44
    // Read ID and NID if exist.
-
 
45
    if (EERead(NODE_ID_EE)==NODE_HAS_ID)
-
 
46
    {
-
 
47
        MY_ID=(((WORD)EERead(NODE_ID_EE + 1))<<8)+EERead(NODE_ID_EE + 2);
-
 
48
        MY_NID=EERead(NODE_ID_EE + 3);
-
 
49
    }
-
 
50
 
-
 
51
    // Send user program startup heatbeat
-
 
52
    outCm.funct                     = FUNCT_BOOTLOADER;
-
 
53
    outCm.funcc                     = FUNCC_BOOT_HEARTBEAT;
-
 
54
    outCm.nid                       = MY_NID;
-
 
55
    outCm.sid                       = MY_ID;
-
 
56
    outCm.data_length               = 1;
-
 
57
    outCm.data[BOOT_DATA_HEARTBEAT_INDEX] = HEARTBEAT_USER_STARTUP;
-
 
58
    while(!canSendMessage(outCm,PRIO_HIGH));
-
 
59
 
-
 
60
 
31
 
61
    while(1)
32
    while(1)
62
    {
33
    {
63
        static TICK t = 0;
34
        static TICK t = 0;
64
        static TICK heartbeat = 0;
-
 
65
 
35
 
66
       
-
 
67
        if ((tickGet()-heartbeat)>TICK_SECOND*5)
-
 
68
        {
-
 
69
            // Send alive heartbeat
-
 
70
            outCm.funct                     = FUNCT_BOOTLOADER;
-
 
71
            outCm.funcc                     = FUNCC_BOOT_HEARTBEAT;
-
 
72
            outCm.nid                       = MY_NID;
-
 
73
            outCm.sid                       = MY_ID;
-
 
74
            outCm.data_length               = 1;
-
 
75
            outCm.data[BOOT_DATA_HEARTBEAT_INDEX] = HEARTBEAT_ALIVE;
-
 
76
            while(!canSendMessage(outCm,PRIO_HIGH));
-
 
77
           
-
 
78
            heartbeat = tickGet();
-
 
79
        }
-
 
80
        if ((tickGet()-t)>TICK_SECOND)
36
        if ((tickGet()-t)>TICK_SECOND)
81
        {
37
        {
82
            LED0_IO=~LED0_IO;
38
            LED0_IO=~LED0_IO;
83
            t = tickGet();
39
            t = tickGet();
84
        }
40
        }
Line 87... Line 43...
87
 
43
 
88
 
44
 
89
#pragma interrupt high_isr
45
#pragma interrupt high_isr
90
void high_isr(void)
46
void high_isr(void)
91
{
47
{
92
    ClrWdt();
-
 
93
 
-
 
94
    #ifdef USE_CAN
-
 
95
        canISR();
48
    canISR();
96
    #endif
-
 
97
 
-
 
98
    tickUpdate();
-
 
99
 
-
 
100
}
49
}
101
 
50
 
102
 
51
 
103
#pragma interrupt low_isr
52
#pragma interrupt low_isr
104
void low_isr(void)
53
void low_isr(void)
105
{
54
{
106
 
55
 
107
}
56
}
108
 
57
 
109
 
58
 
110
 
59
 
111
/*
60
/*
112
*   Function: mainInit
61
*   Function: mainInit
Line 122... Line 71...
122
    LED0_TRIS=0;   
71
    LED0_TRIS=0;   
123
 
72
 
124
    // Enable Interrupts
73
    // Enable Interrupts
125
    INTCONbits.GIEH = 1;
74
    INTCONbits.GIEH = 1;
126
    INTCONbits.GIEL = 1;
75
    INTCONbits.GIEL = 1;
127
 
76
 
128
    // Enable interrupt prirority
77
    // Enable interrupt prirority
129
    RCONbits.IPEN=1;
78
    RCONbits.IPEN=1;
130
   
79
   
131
 
80
 
132
}
81
}
Line 138... Line 87...
138
*   Output: none
87
*   Output: none
139
*   Pre-conditions: canInit and received packet.
88
*   Pre-conditions: canInit and received packet.
140
*   Affects: Sensors/actuators/etc. See code.
89
*   Affects: Sensors/actuators/etc. See code.
141
*   Depends: none.
90
*   Depends: none.
142
*/
91
*/
143
#ifdef USE_CAN
-
 
144
void canParse(CAN_MESSAGE cm)
92
void canParse(CAN_MESSAGE cm)
145
{
93
{
146
    CAN_MESSAGE outCm;
94
    CAN_MESSAGE outCm;
-
 
95
}
147
 
96
 
148
    ClrWdt();
-
 
149
 
-
 
150
    switch(cm.funct)
-
 
151
    {
-
 
152
        case FUNCT_BOOTLOADER:
-
 
153
            if (cm.nid==MY_NID && ((((unsigned int)cm.data[BOOT_DATA_RID_HIGH_INDEX])<<8)+cm.data[BOOT_DATA_RID_LOW_INDEX])==MY_ID)
-
 
154
            {
-
 
155
                switch(cm.funcc)
-
 
156
                {
-
 
157
                    case FUNCC_BOOT_INIT: Reset(); break; //User whant to program me, reset me.
-
 
158
                    case FUNCC_BOOT_SET_ID:
-
 
159
                        // Change my ID and NID
-
 
160
                        MY_ID=(((WORD)cm.data[BOOT_DATA_NEW_ID_HIGH_INDEX])<<8)+cm.data[BOOT_DATA_NEW_ID_LOW_INDEX];
-
 
161
                        MY_NID=cm.data[BOOT_DATA_NEW_NID_INDEX];
-
 
162
                        EEWrite(NODE_ID_EE+1,cm.data[BOOT_DATA_NEW_ID_HIGH_INDEX]);
-
 
163
                        EEWrite(NODE_ID_EE+2,cm.data[BOOT_DATA_NEW_ID_LOW_INDEX]);
-
 
164
                        EEWrite(NODE_ID_EE+3,cm.data[BOOT_DATA_NEW_NID_INDEX]);
-
 
165
                        EEWrite(NODE_ID_EE,NODE_HAS_ID);
-
 
166
                        // Send ack with new id.
-
 
167
                        outCm.funct                     = FUNCT_BOOTLOADER;
-
 
168
                        outCm.funcc                     = FUNCC_BOOT_ACK;
-
 
169
                        outCm.nid                       = MY_NID;
-
 
170
                        outCm.sid                       = MY_ID;
-
 
171
                        outCm.data_length               = 8;
-
 
172
                        outCm.data[BOOT_DATA_ERR_INDEX] = ACK_ERR_NO_ERROR;
-
 
173
                        while(!canSendMessage(outCm,PRIO_HIGH));
-
 
174
                    break;
-
 
175
                }
-
 
176
            }
-
 
177
 
-
 
178
 
-
 
179
        break;
-
 
180
    }
-
 
181
}
-
 
182
#endif
-
 
183
 
97
 
184
 
98
 
185
 
99
 
186
// Below are mandantory when using bootloader
100
// Below are mandantory when using bootloader
187
// define DEBUG_MODE to use without bootloader.
101
// define DEBUG_MODE to use without bootloader.