Subversion Repositories HomeAutomation

Rev

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

Rev 95 Rev 100
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/canBase/Source/Main.c $
6
 * FileName:        $HeadURL: https://svn.auml.se/svn/HomeAutomation/trunk/EmbeddedSoftware/PIC/canBase/Source/Main.c $
7
 * Last changed:    $LastChangedDate: 2006-11-14 21:11:28 +0100 (Tue, 14 Nov 2006) $
7
 * Last changed:    $LastChangedDate: 2006-11-15 09:40:52 +0100 (Wed, 15 Nov 2006) $
8
 * By:              $LastChangedBy: johboh $
8
 * By:              $LastChangedBy: johboh $
9
 * Revision:        $Revision: 95 $
9
 * Revision:        $Revision: 100 $
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>
17
#include <EEaccess.h>
18
 
18
 
19
#ifdef USE_CAN
19
#ifdef USE_CAN
20
    #include <CAN.h>
20
    #include <CAN.h>
21
#endif
21
#endif
22
 
22
 
23
static void mainInit(void);
23
static void mainInit(void);
24
 
24
 
-
 
25
 
-
 
26
static int MY_ID = DEFAULT_ID;
-
 
27
static BYTE MY_NID = DEFAULT_NID;
25
 
28
 
26
void main()
29
void main()
27
{
30
{
28
    static TICK t = 0;
31
    static TICK t = 0;
29
    CAN_MESSAGE cm2;
32
    CAN_MESSAGE cm2;
30
 
33
 
31
    // Inits
34
    // Inits
32
 
35
 
33
    mainInit();
36
    mainInit();
34
 
37
 
35
    #ifdef USE_CAN
38
    #ifdef USE_CAN
36
        canInit();
39
        canInit();
37
    #endif
40
    #endif
38
 
41
 
39
    tickInit();
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
    }
40
 
50
 
41
    // Dummy bootmessage
51
    // Dummy bootmessage
42
    cm2.data[0]=0x91;
52
    cm2.data[0]=0x91;
43
    cm2.data_length=1;
53
    cm2.data_length=1;
44
    canSendMessage(cm2,PRIO_HIGH);
54
    canSendMessage(cm2,PRIO_HIGH);
45
 
55
 
46
 
56
 
47
    while(1)
57
    while(1)
48
    {
58
    {
49
        static TICK t = 0;
59
        static TICK t = 0;
50
        if ((tickGet()-t)>TICK_SECOND)
60
        if ((tickGet()-t)>TICK_SECOND)
51
        {
61
        {
52
            LED0_IO=~LED0_IO;
62
            LED0_IO=~LED0_IO;
53
            t = tickGet();
63
            t = tickGet();
54
        }
64
        }
55
    }
65
    }
56
}
66
}
57
 
67
 
58
 
68
 
59
#pragma interrupt high_isr
69
#pragma interrupt high_isr
60
void high_isr(void)
70
void high_isr(void)
61
{
71
{
62
    ClrWdt();
72
    ClrWdt();
63
 
73
 
64
    #ifdef USE_CAN
74
    #ifdef USE_CAN
65
        canISR();
75
        canISR();
66
    #endif
76
    #endif
67
 
77
 
68
    tickUpdate();
78
    tickUpdate();
69
 
79
 
70
}
80
}
71
 
81
 
72
 
82
 
73
#pragma interrupt low_isr
83
#pragma interrupt low_isr
74
void low_isr(void)
84
void low_isr(void)
75
{
85
{
76
 
86
 
77
}
87
}
78
 
88
 
79
 
89
 
80
 
90
 
81
/*
91
/*
82
*   Function: mainInit
92
*   Function: mainInit
83
*
93
*
84
*   Input:  none
94
*   Input:  none
85
*   Output: none
95
*   Output: none
86
*   Pre-conditions: none
96
*   Pre-conditions: none
87
*   Affects: Se code
97
*   Affects: Se code
88
*   Depends: none.
98
*   Depends: none.
89
*/
99
*/
90
void mainInit()
100
void mainInit()
91
{
101
{
92
    LED0_TRIS=0;   
102
    LED0_TRIS=0;   
93
 
103
 
94
    // Enable Interrupts
104
    // Enable Interrupts
95
    INTCONbits.GIEH = 1;
105
    INTCONbits.GIEH = 1;
96
    INTCONbits.GIEL = 1;
106
    INTCONbits.GIEL = 1;
97
 
107
 
98
    // Enable interrupt prirority
108
    // Enable interrupt prirority
99
    RCONbits.IPEN=1;
109
    RCONbits.IPEN=1;
100
   
110
   
101
 
111
 
102
}
112
}
103
 
113
 
104
/*
114
/*
105
*   Function: canParse
115
*   Function: canParse
106
*
116
*
107
*   Input:  Received can message
117
*   Input:  Received can message
108
*   Output: none
118
*   Output: none
109
*   Pre-conditions: canInit and received packet.
119
*   Pre-conditions: canInit and received packet.
110
*   Affects: Sensors/actuators/etc. See code.
120
*   Affects: Sensors/actuators/etc. See code.
111
*   Depends: none.
121
*   Depends: none.
112
*/
122
*/
113
#ifdef USE_CAN
123
#ifdef USE_CAN
114
void canParse(CAN_MESSAGE cm)
124
void canParse(CAN_MESSAGE cm)
115
{
125
{
116
    ClrWdt();
126
    ClrWdt();
117
 
127
 
118
    switch(cm.funct)
128
    switch(cm.funct)
119
    {
129
    {
120
        case FUNCT_BOOTLOADER:
130
        case FUNCT_BOOTLOADER:
121
            if (cm.funcc==FUNCC_BOOT_INIT) { Reset(); }
131
            if (cm.funcc==FUNCC_BOOT_INIT && cm.nid==MY_NID && ((((unsigned int)cm.data[BOOT_DATA_RID_HIGH_INDEX])<<8)+cm.data[BOOT_DATA_RID_LOW_INDEX])==MY_ID) { Reset(); }
122
        break;
132
        break;
123
    }
133
    }
124
}
134
}
125
#endif
135
#endif
126
 
136
 
127
 
137
 
128
// Below are mandantory when using bootloader
138
// Below are mandantory when using bootloader
129
// define DEBUG_MODE to use without bootloader.
139
// define DEBUG_MODE to use without bootloader.
130
 
140
 
131
#ifndef DEBUG_MODE
141
#ifndef DEBUG_MODE
132
extern void _startup (void);
142
extern void _startup (void);
133
#pragma code _RESET_INTERRUPT_VECTOR = 0x001000
143
#pragma code _RESET_INTERRUPT_VECTOR = 0x001000
134
void _reset (void)
144
void _reset (void)
135
{
145
{
136
    _asm goto _startup _endasm
146
    _asm goto _startup _endasm
137
}
147
}
138
#pragma code
148
#pragma code
139
#endif
149
#endif
140
 
150
 
141
#pragma code _HIGH_INTERRUPT_VECTOR = 0x001008
151
#pragma code _HIGH_INTERRUPT_VECTOR = 0x001008
142
void _high_ISR (void)
152
void _high_ISR (void)
143
{
153
{
144
    _asm GOTO high_isr _endasm
154
    _asm GOTO high_isr _endasm
145
}
155
}
146
#pragma code
156
#pragma code
147
 
157
 
148
#pragma code _LOW_INTERRUPT_VECTOR = 0x001018
158
#pragma code _LOW_INTERRUPT_VECTOR = 0x001018
149
void _low_ISR (void)
159
void _low_ISR (void)
150
{
160
{
151
    _asm GOTO low_isr _endasm
161
    _asm GOTO low_isr _endasm
152
}
162
}
153
#pragma code
163
#pragma code
154
 
164