Rev 139 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 139 | 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/canTemp/Source/Main.c $ |
6 | * FileName: $HeadURL: https://svn.auml.se/svn/HomeAutomation/trunk/EmbeddedSoftware/PIC/canTemp/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 |
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 | 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 | - | ||
| 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 | 39 | ||
| 68 | 40 | ||
| 69 | while(1) |
41 | while(1) |
| 70 | { |
42 | { |
| 71 | static TICK t = 0; |
43 | static TICK t = 0; |
| 72 | static TICK heartbeat = 0; |
- | |
| 73 | static TICK temperature = 0; |
44 | static TICK temperature = 0; |
| 74 | static BYTE lastTemperature = TEMPERATURE_INSIDE; |
45 | static BYTE lastTemperature = TEMPERATURE_INSIDE; |
| 75 | 46 | ||
| 76 | 47 | ||
| 77 | if ((tickGet()-heartbeat)>TICK_SECOND*5) |
- | |
| 78 | { |
- | |
| 79 | // Send alive heartbeat |
- | |
| 80 | outCm.funct = FUNCT_BOOTLOADER; |
- | |
| 81 | outCm.funcc = FUNCC_BOOT_HEARTBEAT; |
- | |
| 82 | outCm.nid = MY_NID; |
- | |
| 83 | outCm.sid = MY_ID; |
- | |
| 84 | outCm.data_length = 1; |
- | |
| 85 | outCm.data[BOOT_DATA_HEARTBEAT_INDEX] = HEARTBEAT_ALIVE; |
- | |
| 86 | while(!canSendMessage(outCm,PRIO_HIGH)); |
- | |
| 87 | - | ||
| 88 | heartbeat = tickGet(); |
- | |
| 89 | } |
- | |
| 90 | - | ||
| 91 | if ((tickGet()-t)>TICK_SECOND) |
48 | if ((tickGet()-t)>TICK_SECOND) |
| 92 | { |
49 | { |
| 93 | LED0_IO=~LED0_IO; |
50 | LED0_IO=~LED0_IO; |
| 94 | t = tickGet(); |
51 | t = tickGet(); |
| 95 | } |
52 | } |
| 96 | 53 | ||
| 97 | #ifdef USE_ADC |
54 | #ifdef USE_ADC |
| 98 | if ((tickGet()-temperature)>TICK_SECOND*2) // Each 2 second, read temperature. |
55 | if ((tickGet()-temperature)>TICK_SECOND*2) // Each 2 second, read temperature. |
| 99 | { |
56 | { |
| 100 | if (lastTemperature==TEMPERATURE_INSIDE) lastTemperature=TEMPERATURE_OUTSIDE; else lastTemperature=TEMPERATURE_INSIDE; |
57 | if (lastTemperature==TEMPERATURE_INSIDE) lastTemperature=TEMPERATURE_OUTSIDE; else lastTemperature=TEMPERATURE_INSIDE; |
| 101 | adcConvert(lastTemperature); |
58 | adcConvert(lastTemperature); |
| 102 | temperature = tickGet(); |
59 | temperature = tickGet(); |
| 103 | } |
60 | } |
| 104 | #endif |
61 | #endif |
| 105 | 62 | ||
| 106 | 63 | ||
| 107 | #ifdef USE_ADC |
64 | #ifdef USE_ADC |
| 108 | if (adcDone()) // Has temperature, send it to the bus |
65 | if (adcDone()) // Has temperature, send it to the bus |
| 109 | { |
66 | { |
| 110 | BYTE decimal; |
67 | BYTE decimal; |
| 111 | signed char tenth; |
68 | signed char tenth; |
| 112 | temperatureRead(&tenth,&decimal); |
69 | temperatureRead(&tenth,&decimal); |
| 113 | 70 | ||
| 114 | outCm.funct = FUNCT_SENSORS; |
71 | outCm.funct = FUNCT_SENSORS; |
| 115 | outCm.funcc = (lastTemperature==TEMPERATURE_INSIDE?FUNCC_SENSORS_TEMPERATURE_INSIDE:FUNCC_SENSORS_TEMPERATURE_OUTSIDE); |
72 | outCm.funcc = (lastTemperature==TEMPERATURE_INSIDE?FUNCC_SENSORS_TEMPERATURE_INSIDE:FUNCC_SENSORS_TEMPERATURE_OUTSIDE); |
| 116 | outCm.nid = MY_NID; |
- | |
| 117 | outCm.sid = MY_ID; |
- | |
| 118 | outCm.data_length = 2; |
73 | outCm.data_length = 2; |
| 119 | outCm.data[1] = tenth; // signed 10 an 1 decimal. |
74 | outCm.data[1] = tenth; // signed 10 an 1 decimal. |
| 120 | outCm.data[0] = decimal; // Decimal value, 0-9 |
75 | outCm.data[0] = decimal; // Decimal value, 0-9 |
| 121 | while(!canSendMessage(outCm,PRIO_HIGH)); |
76 | while(!canSendMessage(outCm,PRIO_HIGH)); |
| 122 | } |
77 | } |
| 123 | #endif |
78 | #endif |
| 124 | 79 | ||
| 125 | } |
80 | } |
| 126 | } |
81 | } |
| 127 | 82 | ||
| 128 | 83 | ||
| 129 | #pragma interrupt high_isr |
84 | #pragma interrupt high_isr |
| 130 | void high_isr(void) |
85 | void high_isr(void) |
| 131 | { |
86 | { |
| 132 | ClrWdt(); |
- | |
| 133 | - | ||
| 134 | #ifdef USE_CAN |
- | |
| 135 | canISR(); |
87 | canISR(); |
| 136 | #endif |
- | |
| 137 | - | ||
| 138 | tickUpdate(); |
- | |
| 139 | - | ||
| 140 | } |
88 | } |
| 141 | 89 | ||
| 142 | 90 | ||
| 143 | #pragma interrupt low_isr |
91 | #pragma interrupt low_isr |
| 144 | void low_isr(void) |
92 | void low_isr(void) |
| 145 | { |
93 | { |
| 146 | #ifdef USE_ADC |
94 | #ifdef USE_ADC |
| 147 | adcISR(); |
95 | adcISR(); |
| 148 | #endif |
96 | #endif |
| 149 | } |
97 | } |
| 150 | 98 | ||
| 151 | 99 | ||
| 152 | 100 | ||
| 153 | /* |
101 | /* |
| 154 | * Function: mainInit |
102 | * Function: mainInit |
| 155 | * |
103 | * |
| 156 | * Input: none |
104 | * Input: none |
| 157 | * Output: none |
105 | * Output: none |
| 158 | * Pre-conditions: none |
106 | * Pre-conditions: none |
| 159 | * Affects: Se code |
107 | * Affects: Se code |
| 160 | * Depends: none. |
108 | * Depends: none. |
| 161 | */ |
109 | */ |
| 162 | void mainInit() |
110 | void mainInit() |
| 163 | { |
111 | { |
| 164 | LED0_TRIS=0; |
112 | LED0_TRIS=0; |
| 165 | 113 | ||
| 166 | // Enable Interrupts |
114 | // Enable Interrupts |
| 167 | INTCONbits.GIEH = 1; |
115 | INTCONbits.GIEH = 1; |
| 168 | INTCONbits.GIEL = 1; |
116 | INTCONbits.GIEL = 1; |
| 169 | 117 | ||
| 170 | // Enable interrupt prirority |
118 | // Enable interrupt prirority |
| 171 | RCONbits.IPEN=1; |
119 | RCONbits.IPEN=1; |
| 172 | 120 | ||
| 173 | 121 | ||
| 174 | } |
122 | } |
| 175 | 123 | ||
| 176 | /* |
124 | /* |
| 177 | * Function: canParse |
125 | * Function: canParse |
| 178 | * |
126 | * |
| 179 | * Input: Received can message |
127 | * Input: Received can message |
| 180 | * Output: none |
128 | * Output: none |
| 181 | * Pre-conditions: canInit and received packet. |
129 | * Pre-conditions: canInit and received packet. |
| 182 | * Affects: Sensors/actuators/etc. See code. |
130 | * Affects: Sensors/actuators/etc. See code. |
| 183 | * Depends: none. |
131 | * Depends: none. |
| 184 | */ |
132 | */ |
| 185 | #ifdef USE_CAN |
133 | |
| 186 | void canParse(CAN_MESSAGE cm) |
134 | void canParse(CAN_MESSAGE cm) |
| 187 | { |
135 | { |
| 188 | CAN_MESSAGE outCm; |
136 | CAN_MESSAGE outCm; |
| 189 | 137 | ||
| 190 | ClrWdt(); |
- | |
| 191 | - | ||
| 192 | switch(cm.funct) |
- | |
| 193 | { |
- | |
| 194 | case FUNCT_BOOTLOADER: |
- | |
| 195 | if (cm.nid==MY_NID && ((((unsigned int)cm.data[BOOT_DATA_RID_HIGH_INDEX])<<8)+cm.data[BOOT_DATA_RID_LOW_INDEX])==MY_ID) |
- | |
| 196 | { |
- | |
| 197 | switch(cm.funcc) |
- | |
| 198 | { |
- | |
| 199 | case FUNCC_BOOT_INIT: Reset(); break; //User whant to program me, reset me. |
- | |
| 200 | case FUNCC_BOOT_SET_ID: |
- | |
| 201 | // Change my ID and NID |
- | |
| 202 | MY_ID=(((WORD)cm.data[BOOT_DATA_NEW_ID_HIGH_INDEX])<<8)+cm.data[BOOT_DATA_NEW_ID_LOW_INDEX]; |
- | |
| 203 | MY_NID=cm.data[BOOT_DATA_NEW_NID_INDEX]; |
- | |
| 204 | EEWrite(NODE_ID_EE+1,cm.data[BOOT_DATA_NEW_ID_HIGH_INDEX]); |
- | |
| 205 | EEWrite(NODE_ID_EE+2,cm.data[BOOT_DATA_NEW_ID_LOW_INDEX]); |
- | |
| 206 | EEWrite(NODE_ID_EE+3,cm.data[BOOT_DATA_NEW_NID_INDEX]); |
- | |
| 207 | EEWrite(NODE_ID_EE,NODE_HAS_ID); |
- | |
| 208 | // Send ack with new id. |
- | |
| 209 | outCm.funct = FUNCT_BOOTLOADER; |
- | |
| 210 | outCm.funcc = FUNCC_BOOT_ACK; |
- | |
| 211 | outCm.nid = MY_NID; |
- | |
| 212 | outCm.sid = MY_ID; |
- | |
| 213 | outCm.data_length = 8; |
- | |
| 214 | outCm.data[BOOT_DATA_ERR_INDEX] = ACK_ERR_NO_ERROR; |
- | |
| 215 | while(!canSendMessage(outCm,PRIO_HIGH)); |
- | |
| 216 | break; |
- | |
| 217 | } |
- | |
| 218 | } |
- | |
| 219 | - | ||
| 220 | - | ||
| 221 | break; |
- | |
| 222 | } |
- | |
| 223 | } |
138 | } |
| 224 | #endif |
- | |
| 225 | - | ||
| 226 | 139 | ||
| 227 | 140 | ||
| 228 | // Below are mandantory when using bootloader |
141 | // Below are mandantory when using bootloader |
| 229 | // define DEBUG_MODE to use without bootloader. |
142 | // define DEBUG_MODE to use without bootloader. |
| 230 | 143 | ||
| 231 | #ifndef DEBUG_MODE |
144 | #ifndef DEBUG_MODE |
| 232 | extern void _startup (void); |
145 | extern void _startup (void); |
| 233 | #pragma code _RESET_INTERRUPT_VECTOR = 0x001000 |
146 | #pragma code _RESET_INTERRUPT_VECTOR = 0x001000 |
| 234 | void _reset (void) |
147 | void _reset (void) |
| 235 | { |
148 | { |
| 236 | _asm goto _startup _endasm |
149 | _asm goto _startup _endasm |
| 237 | } |
150 | } |
| 238 | #pragma code |
151 | #pragma code |
| 239 | #endif |
152 | #endif |
| 240 | 153 | ||
| 241 | #pragma code _HIGH_INTERRUPT_VECTOR = 0x001008 |
154 | #pragma code _HIGH_INTERRUPT_VECTOR = 0x001008 |
| 242 | void _high_ISR (void) |
155 | void _high_ISR (void) |
| 243 | { |
156 | { |
| 244 | _asm GOTO high_isr _endasm |
157 | _asm GOTO high_isr _endasm |
| 245 | } |
158 | } |
| 246 | #pragma code |
159 | #pragma code |
| 247 | 160 | ||
| 248 | #pragma code _LOW_INTERRUPT_VECTOR = 0x001018 |
161 | #pragma code _LOW_INTERRUPT_VECTOR = 0x001018 |
| 249 | void _low_ISR (void) |
162 | void _low_ISR (void) |
| 250 | { |
163 | { |
| 251 | _asm GOTO low_isr _endasm |
164 | _asm GOTO low_isr _endasm |
| 252 | } |
165 | } |
| 253 | #pragma code |
166 | #pragma code |
| 254 | 167 | ||