Rev 129 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 129 | 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/canDimmer/Source/Main.c $ |
6 | * FileName: $HeadURL: https://svn.auml.se/svn/HomeAutomation/trunk/EmbeddedSoftware/PIC/canDimmer/Source/Main.c $ |
| 7 | * Last changed: $LastChangedDate: |
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: |
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 | 20 | ||
| 25 | 21 | ||
| - | 22 | /* |
|
| - | 23 | Features (support upto 8 dimmers). |
|
| - | 24 | ||
| - | 25 | need to define funcc to each channel. |
|
| - | 26 | need to define funcc to all in this node. |
|
| - | 27 | ||
| - | 28 | channel = 0 to 7 |
|
| - | 29 | value = 0 to 255 |
|
| - | 30 | place = 1 to 10 |
|
| - | 31 | ||
| - | 32 | NO FADE |
|
| - | 33 | set channel to value. |
|
| - | 34 | set channel off. |
|
| - | 35 | set channel last. |
|
| - | 36 | set channel on. |
|
| - | 37 | set all to value. |
|
| - | 38 | set all off. |
|
| - | 39 | set all last. |
|
| - | 40 | set all on. |
|
| - | 41 | ||
| - | 42 | FADE |
|
| - | 43 | fade channel to value. |
|
| - | 44 | fade channel off. |
|
| - | 45 | fade channel last. |
|
| - | 46 | fade channel on. |
|
| - | 47 | fade all to value. |
|
| - | 48 | fade all off. |
|
| - | 49 | fade all last. |
|
| - | 50 | fade all on. |
|
| - | 51 | ||
| - | 52 | MEMORY: |
|
| - | 53 | Save current values to M[place]. |
|
| 26 |
|
54 | Set current values to M[place]. |
| 27 |
|
55 | Fade current values to M[place]. |
| - | 56 | ||
| - | 57 | ||
| - | 58 | settings: |
|
| - | 59 | buttons, default fadespeed, stepping. (Get and set) |
|
| - | 60 | */ |
|
| - | 61 | ||
| - | 62 | ||
| 28 | 63 | ||
| 29 | void main() |
64 | void main() |
| 30 | { |
65 | { |
| 31 | static TICK t = 0; |
66 | static TICK t = 0; |
| 32 | CAN_MESSAGE outCm; |
67 | CAN_MESSAGE outCm; |
| 33 | 68 | ||
| 34 | // Inits |
69 | // Inits |
| - | 70 | mainInit(); |
|
| 35 | 71 | ||
| 36 | mainInit(); |
- | |
| 37 | - | ||
| 38 | #ifdef USE_CAN |
- | |
| 39 | canInit(); |
72 | canInit(); |
| 40 | #endif |
- | |
| 41 | 73 | ||
| 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 | 74 | ||
| 61 | while(1) |
75 | while(1) |
| 62 | { |
76 | { |
| 63 | static TICK t = 0; |
77 | static TICK t = 0; |
| 64 | static TICK heartbeat = 0; |
- | |
| 65 | 78 | ||
| 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) |
79 | if ((tickGet()-t)>TICK_SECOND) |
| 81 | { |
80 | { |
| 82 | LED0_IO=~LED0_IO; |
81 | LED0_IO=~LED0_IO; |
| 83 | t = tickGet(); |
82 | t = tickGet(); |
| 84 | } |
83 | } |
| 85 | } |
84 | } |
| 86 | } |
85 | } |
| 87 | 86 | ||
| 88 | 87 | ||
| 89 | #pragma interrupt high_isr |
88 | #pragma interrupt high_isr |
| 90 | void high_isr(void) |
89 | void high_isr(void) |
| 91 | { |
90 | { |
| 92 | ClrWdt(); |
- | |
| 93 | - | ||
| 94 | #ifdef USE_CAN |
- | |
| 95 | canISR(); |
91 | canISR(); |
| 96 | #endif |
- | |
| 97 | - | ||
| 98 | tickUpdate(); |
- | |
| 99 | - | ||
| 100 | } |
92 | } |
| 101 | 93 | ||
| 102 | 94 | ||
| 103 | #pragma interrupt low_isr |
95 | #pragma interrupt low_isr |
| 104 | void low_isr(void) |
96 | void low_isr(void) |
| 105 | { |
97 | { |
| 106 | 98 | ||
| 107 | } |
99 | } |
| 108 | 100 | ||
| 109 | 101 | ||
| 110 | 102 | ||
| 111 | /* |
103 | /* |
| 112 | * Function: mainInit |
104 | * Function: mainInit |
| 113 | * |
105 | * |
| 114 | * Input: none |
106 | * Input: none |
| 115 | * Output: none |
107 | * Output: none |
| 116 | * Pre-conditions: none |
108 | * Pre-conditions: none |
| 117 | * Affects: Se code |
109 | * Affects: Se code |
| 118 | * Depends: none. |
110 | * Depends: none. |
| 119 | */ |
111 | */ |
| 120 | void mainInit() |
112 | void mainInit() |
| 121 | { |
113 | { |
| 122 | LED0_TRIS=0; |
114 | LED0_TRIS=0; |
| 123 | 115 | ||
| 124 | // Enable Interrupts |
116 | // Enable Interrupts |
| 125 | INTCONbits.GIEH = 1; |
117 | INTCONbits.GIEH = 1; |
| 126 | INTCONbits.GIEL = 1; |
118 | INTCONbits.GIEL = 1; |
| 127 | 119 | ||
| 128 | // Enable interrupt prirority |
120 | // Enable interrupt prirority |
| 129 | RCONbits.IPEN=1; |
121 | RCONbits.IPEN=1; |
| 130 | 122 | ||
| 131 | 123 | ||
| 132 | } |
124 | } |
| 133 | 125 | ||
| 134 | /* |
126 | /* |
| 135 | * Function: canParse |
127 | * Function: canParse |
| 136 | * |
128 | * |
| 137 | * Input: Received can message |
129 | * Input: Received can message |
| 138 | * Output: none |
130 | * Output: none |
| 139 | * Pre-conditions: canInit and received packet. |
131 | * Pre-conditions: canInit and received packet. |
| 140 | * Affects: Sensors/actuators/etc. See code. |
132 | * Affects: Sensors/actuators/etc. See code. |
| 141 | * Depends: none. |
133 | * Depends: none. |
| 142 | */ |
134 | */ |
| 143 | #ifdef USE_CAN |
- | |
| 144 | void canParse(CAN_MESSAGE cm) |
135 | void canParse(CAN_MESSAGE cm) |
| 145 | { |
136 | { |
| 146 | CAN_MESSAGE outCm; |
137 | CAN_MESSAGE outCm; |
| 147 | 138 | ||
| 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 | } |
139 | } |
| 182 | #endif |
- | |
| 183 | - | ||
| 184 | 140 | ||
| 185 | 141 | ||
| 186 | // Below are mandantory when using bootloader |
142 | // Below are mandantory when using bootloader |
| 187 | // define DEBUG_MODE to use without bootloader. |
143 | // define DEBUG_MODE to use without bootloader. |
| 188 | 144 | ||
| 189 | #ifndef DEBUG_MODE |
145 | #ifndef DEBUG_MODE |
| 190 | extern void _startup (void); |
146 | extern void _startup (void); |
| 191 | #pragma code _RESET_INTERRUPT_VECTOR = 0x001000 |
147 | #pragma code _RESET_INTERRUPT_VECTOR = 0x001000 |
| 192 | void _reset (void) |
148 | void _reset (void) |
| 193 | { |
149 | { |
| 194 | _asm goto _startup _endasm |
150 | _asm goto _startup _endasm |
| 195 | } |
151 | } |
| 196 | #pragma code |
152 | #pragma code |
| 197 | #endif |
153 | #endif |
| 198 | 154 | ||
| 199 | #pragma code _HIGH_INTERRUPT_VECTOR = 0x001008 |
155 | #pragma code _HIGH_INTERRUPT_VECTOR = 0x001008 |
| 200 | void _high_ISR (void) |
156 | void _high_ISR (void) |
| 201 | { |
157 | { |
| 202 | _asm GOTO high_isr _endasm |
158 | _asm GOTO high_isr _endasm |
| 203 | } |
159 | } |
| 204 | #pragma code |
160 | #pragma code |
| 205 | 161 | ||
| 206 | #pragma code _LOW_INTERRUPT_VECTOR = 0x001018 |
162 | #pragma code _LOW_INTERRUPT_VECTOR = 0x001018 |
| 207 | void _low_ISR (void) |
163 | void _low_ISR (void) |
| 208 | { |
164 | { |
| 209 | _asm GOTO low_isr _endasm |
165 | _asm GOTO low_isr _endasm |
| 210 | } |
166 | } |
| 211 | #pragma code |
167 | #pragma code |
| 212 | 168 | ||