Rev 96 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 96 | Rev 101 | ||
|---|---|---|---|
| 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/canBoot/Source/Main.c $ |
6 | * FileName: $HeadURL: https://svn.auml.se/svn/HomeAutomation/trunk/EmbeddedSoftware/PIC/canBoot/Source/Main.c $ |
| 7 | * Last changed: $LastChangedDate: 2006-11- |
7 | * Last changed: $LastChangedDate: 2006-11-15 09:41:08 +0100 (Wed, 15 Nov 2006) $ |
| 8 | * By: $LastChangedBy: johboh $ |
8 | * By: $LastChangedBy: johboh $ |
| 9 | * Revision: $Revision: |
9 | * Revision: $Revision: 101 $ |
| 10 | ********************************************************************/ |
10 | ********************************************************************/ |
| 11 | 11 | ||
| 12 | #include <compiler.h> |
12 | #include <compiler.h> |
| 13 | #include <stackTasks.h> |
13 | #include <stackTasks.h> |
| 14 | #include <CANdefs.h> |
14 | #include <CANdefs.h> |
| 15 | #include <CAN.h> |
15 | #include <CAN.h> |
| 16 | #include <boot.h> |
16 | #include <boot.h> |
| 17 | #include <funcdefs.h> |
17 | #include <funcdefs.h> |
| - | 18 | #include <EEaccess.h> |
|
| 18 | 19 | ||
| 19 | /** V E C T O R R E M A P P I N G *******************************************/ |
20 | /** V E C T O R R E M A P P I N G *******************************************/ |
| 20 | 21 | ||
| 21 | #pragma code _HIGH_INTERRUPT_VECTOR = 0x000008 |
22 | #pragma code _HIGH_INTERRUPT_VECTOR = 0x000008 |
| 22 | void _high_ISR (void) |
23 | void _high_ISR (void) |
| Line 36... | Line 37... | ||
| 36 | #ifdef USE_CAN |
37 | #ifdef USE_CAN |
| 37 | #include <CAN.h> |
38 | #include <CAN.h> |
| 38 | #endif |
39 | #endif |
| 39 | 40 | ||
| 40 | static CAN_PROTO_MESSAGE cm; |
41 | static CAN_PROTO_MESSAGE cm; |
| 41 | static BYTE ful=0; |
- | |
| 42 | static DWORD cnt =0; |
42 | static DWORD cnt =0; |
| 43 | 43 | ||
| 44 | static void mainInit(void); |
44 | static void mainInit(void); |
| 45 | static BOOL canGetPacket(void); |
45 | static BOOL canGetPacket(void); |
| 46 | void TBLWT_Func(void); |
46 | void TBLWT_Func(void); |
| Line 53... | Line 53... | ||
| 53 | 53 | ||
| 54 | DWORD tickCounter = 0; |
54 | DWORD tickCounter = 0; |
| 55 | DWORD tickSubCounter = 0; |
55 | DWORD tickSubCounter = 0; |
| 56 | 56 | ||
| 57 | DWORD tickGet(void); |
57 | DWORD tickGet(void); |
| - | 58 | ||
| - | 59 | static int MY_ID = DEFAULT_ID; |
|
| - | 60 | static BYTE MY_NID = DEFAULT_NID; |
|
| 58 | 61 | ||
| 59 | void main() |
62 | void main() |
| 60 | { |
63 | { |
| 61 | static PROGRAM_STATE pgs = pgsWATING_START; |
64 | static PROGRAM_STATE pgs = pgsWATING_START; |
| 62 | static DWORD t = 0; |
65 | static DWORD t = 0; |
| 63 | static WORD programmerSid=0; |
66 | static WORD programmerSid=0; |
| 64 | 67 | ||
| 65 | static BYTE errMsg = |
68 | static BYTE errMsg = ACK_ERR_NO_ERROR; |
| 66 | static PROGRAM_STATE afterAckPgs = pgsWATING_START; |
69 | static PROGRAM_STATE afterAckPgs = pgsWATING_START; |
| 67 | static DWORD pgmAddress = 0; |
70 | static DWORD pgmAddress = 0; |
| 68 | static BYTE bytesReceived = 0; |
71 | static BYTE bytesReceived = 0; |
| 69 | 72 | ||
| 70 | DWORD tBoot; |
73 | DWORD tBoot; |
| 71 | 74 | ||
| 72 | static BYTE pgmData[64]; |
75 | static BYTE pgmData[64]; |
| 73 | 76 | ||
| 74 | static BYTE tmp; |
- | |
| 75 | - | ||
| 76 | // Inits |
77 | // Inits |
| 77 | mainInit(); |
78 | mainInit(); |
| 78 | 79 | ||
| 79 | #ifdef USE_CAN |
80 | #ifdef USE_CAN |
| 80 | canInit(); |
81 | canInit(); |
| 81 | #endif |
82 | #endif |
| 82 | 83 | ||
| 83 | tBoot = tickGet(); |
84 | tBoot = tickGet(); |
| - | 85 | ||
| - | 86 | // Read ID and NID if exist. |
|
| - | 87 | if (EERead(NODE_ID_EE)==NODE_HAS_ID) |
|
| - | 88 | { |
|
| - | 89 | MY_ID=(((WORD)EERead(NODE_ID_EE + 1))<<8)+EERead(NODE_ID_EE + 2); |
|
| - | 90 | MY_NID=EERead(NODE_ID_EE + 3); |
|
| - | 91 | } |
|
| 84 | 92 | ||
| 85 | 93 | ||
| 86 | for(;;) |
94 | for(;;) |
| 87 | { |
95 | { |
| 88 | BYTE i; |
96 | BYTE i; |
| Line 99... | Line 107... | ||
| 99 | 107 | ||
| 100 | switch(pgs) |
108 | switch(pgs) |
| 101 | { |
109 | { |
| 102 | case pgsWATING_START: |
110 | case pgsWATING_START: |
| 103 | // Check if there is bootloader start packet |
111 | // Check if there is bootloader start packet |
| 104 | if (hasPacket==TRUE && cm.funct==FUNCT_BOOTLOADER && cm.funcc==FUNCC_BOOT_INIT && ((((unsigned int)cm.data[ |
112 | if (hasPacket==TRUE && cm.funct==FUNCT_BOOTLOADER && cm.funcc==FUNCC_BOOT_INIT && ((((unsigned int)cm.data[BOOT_DATA_RID_HIGH_INDEX])<<8)+cm.data[BOOT_DATA_RID_LOW_INDEX])==MY_ID && cm.nid==MY_NID) |
| 105 | { |
113 | { |
| 106 | // Has new start packet |
114 | // Has new start packet |
| 107 | pgmAddress = (((DWORD)cm.data[ |
115 | pgmAddress = (((DWORD)cm.data[BOOT_DATA_ADDRU_INDEX])<<16)+(((DWORD)cm.data[BOOT_DATA_ADDRH_INDEX])<<8)+((DWORD)cm.data[BOOT_DATA_ADDRL_INDEX]); |
| 108 | programmerSid = cm.sid; |
116 | programmerSid = cm.sid; |
| 109 | t = tickGet(); |
117 | t = tickGet(); |
| 110 | errMsg = |
118 | errMsg = ACK_ERR_NO_ERROR; |
| 111 | afterAckPgs = pgsWAIT_FIRST_PGM_PACKET; |
119 | afterAckPgs = pgsWAIT_FIRST_PGM_PACKET; |
| 112 | pgs = pgsSEND_ACK; |
120 | pgs = pgsSEND_ACK; |
| 113 | // save pgm adress. |
121 | // save pgm adress. |
| 114 | 122 | ||
| 115 | } |
123 | } |
| Line 126... | Line 134... | ||
| 126 | outCm.funct = FUNCT_BOOTLOADER; |
134 | outCm.funct = FUNCT_BOOTLOADER; |
| 127 | outCm.funcc = FUNCC_BOOT_ACK; |
135 | outCm.funcc = FUNCC_BOOT_ACK; |
| 128 | outCm.nid = MY_NID; |
136 | outCm.nid = MY_NID; |
| 129 | outCm.sid = MY_ID; |
137 | outCm.sid = MY_ID; |
| 130 | outCm.data_length = 8; |
138 | outCm.data_length = 8; |
| 131 | outCm.data[ |
139 | outCm.data[BOOT_DATA_ERR_INDEX] = errMsg; |
| 132 | t = tickGet(); |
140 | t = tickGet(); |
| 133 | pgs = afterAckPgs; |
141 | pgs = afterAckPgs; |
| 134 | 142 | ||
| 135 | while(!canSendMessage(outCm,PRIO_HIGH)); |
143 | while(!canSendMessage(outCm,PRIO_HIGH)); |
| 136 | break; |
144 | break; |
| Line 139... | Line 147... | ||
| 139 | case pgsWAIT_FIRST_PGM_PACKET: |
147 | case pgsWAIT_FIRST_PGM_PACKET: |
| 140 | // wait for first pgm packet. |
148 | // wait for first pgm packet. |
| 141 | if ((tickGet()-t)>PACKET_TIMEOUT) |
149 | if ((tickGet()-t)>PACKET_TIMEOUT) |
| 142 | { |
150 | { |
| 143 | // Timeout, resend ack. |
151 | // Timeout, resend ack. |
| 144 | errMsg = |
152 | errMsg = ACK_ERR_NO_ERROR; |
| 145 | afterAckPgs = pgsWAIT_FIRST_PGM_PACKET; |
153 | afterAckPgs = pgsWAIT_FIRST_PGM_PACKET; |
| 146 | pgs = pgsSEND_ACK; |
154 | pgs = pgsSEND_ACK; |
| 147 | } |
155 | } |
| 148 | 156 | ||
| 149 | // If programming packet... |
157 | // If programming packet... |
| Line 181... | Line 189... | ||
| 181 | // If adress packet (control packet).. |
189 | // If adress packet (control packet).. |
| 182 | if (hasPacket==TRUE && cm.funct==FUNCT_BOOTLOADER && cm.funcc==FUNCC_BOOT_ADDR && cm.nid==MY_NID && cm.sid==programmerSid) |
190 | if (hasPacket==TRUE && cm.funct==FUNCT_BOOTLOADER && cm.funcc==FUNCC_BOOT_ADDR && cm.nid==MY_NID && cm.sid==programmerSid) |
| 183 | { |
191 | { |
| 184 | // Send ack and goto wait first packet. |
192 | // Send ack and goto wait first packet. |
| 185 | // save adress. |
193 | // save adress. |
| 186 | errMsg = |
194 | errMsg = ACK_ERR_NO_ERROR; |
| 187 | afterAckPgs = pgsWAIT_FIRST_PGM_PACKET; |
195 | afterAckPgs = pgsWAIT_FIRST_PGM_PACKET; |
| 188 | pgs = pgsSEND_ACK; |
196 | pgs = pgsSEND_ACK; |
| 189 | } |
197 | } |
| 190 | 198 | ||
| 191 | // If end packet... |
199 | // If end packet... |
| Line 197... | Line 205... | ||
| 197 | // If 64, write. |
205 | // If 64, write. |
| 198 | 206 | ||
| 199 | if (bytesReceived==0) |
207 | if (bytesReceived==0) |
| 200 | { |
208 | { |
| 201 | // No received, done. |
209 | // No received, done. |
| 202 | errMsg = |
210 | errMsg = ACK_ERR_NO_ERROR; |
| 203 | afterAckPgs = pgsDONE; |
211 | afterAckPgs = pgsDONE; |
| 204 | pgs = pgsSEND_ACK; |
212 | pgs = pgsSEND_ACK; |
| 205 | } |
213 | } |
| 206 | else if (bytesReceived!=64) |
214 | else if (bytesReceived!=64) |
| 207 | { |
215 | { |
| 208 | errMsg = |
216 | errMsg = ACK_ERR_ERROR; |
| 209 | afterAckPgs = pgsWAIT_FIRST_PGM_PACKET; |
217 | afterAckPgs = pgsWAIT_FIRST_PGM_PACKET; |
| 210 | pgs = pgsSEND_ACK; |
218 | pgs = pgsSEND_ACK; |
| 211 | } |
219 | } |
| 212 | else |
220 | else |
| 213 | { |
221 | { |
| Line 236... | Line 244... | ||
| 236 | // Write program and send ack. |
244 | // Write program and send ack. |
| 237 | 245 | ||
| 238 | // Check addr limit |
246 | // Check addr limit |
| 239 | if (pgmAddress<RM_RESET_VECTOR) |
247 | if (pgmAddress<RM_RESET_VECTOR) |
| 240 | { |
248 | { |
| 241 | errMsg = |
249 | errMsg = ACK_ERR_ERROR; |
| 242 | pgs = pgsSEND_ACK; |
250 | pgs = pgsSEND_ACK; |
| 243 | //Not allowed to write here! |
251 | //Not allowed to write here! |
| 244 | 252 | ||
| 245 | } |
253 | } |
| 246 | 254 | ||
| Line 303... | Line 311... | ||
| 303 | // disable write to memory |
311 | // disable write to memory |
| 304 | EECON1bits.WREN = 0; |
312 | EECON1bits.WREN = 0; |
| 305 | 313 | ||
| 306 | pgmAddress += 64; |
314 | pgmAddress += 64; |
| 307 | bytesReceived = 0; |
315 | bytesReceived = 0; |
| 308 | errMsg = |
316 | errMsg = ACK_ERR_NO_ERROR; |
| 309 | pgs = pgsSEND_ACK; |
317 | pgs = pgsSEND_ACK; |
| 310 | 318 | ||
| 311 | break; |
319 | break; |
| 312 | 320 | ||
| 313 | 321 | ||