Rev 112 | Rev 151 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 112 | Rev 113 | ||
---|---|---|---|
Line 172... | Line 172... | ||
172 | /**************************************************************/ |
172 | /**************************************************************/ |
173 | void MKProtocol_CollectSerialFrame(Buffer_t* pRxBuff, u8 c) |
173 | void MKProtocol_CollectSerialFrame(Buffer_t* pRxBuff, u8 c) |
174 | { |
174 | { |
175 | if(pRxBuff->Locked == FALSE) |
175 | if(pRxBuff->Locked == FALSE) |
176 | { // rx buffer not locked |
176 | { // rx buffer not locked |
177 | if((pRxBuff->Position == 0) && (c == '#')) // if rxd buffer is empty and syncronisation character is received |
177 | if(c == '#') // if syncronisation character is received |
178 | { |
178 | { |
- | 179 | pRxBuff->Position = 0; // reset buffer |
|
179 | pRxBuff->pData[pRxBuff->Position++] = c; // copy 1st byte to buffer |
180 | pRxBuff->pData[pRxBuff->Position++] = c; // copy 1st byte to buffer |
180 | pRxBuff->DataBytes = 1; |
181 | pRxBuff->DataBytes = 1; |
- | 182 | } |
|
- | 183 | else if (pRxBuff->Position < pRxBuff->Size) // rx buffer not full |
|
- | 184 | { |
|
- | 185 | if (c != '\r') // no termination character received |
|
- | 186 | { |
|
- | 187 | pRxBuff->pData[pRxBuff->Position++] = c; // copy byte to rxd buffer |
|
- | 188 | pRxBuff->DataBytes++; |
|
181 | } |
189 | } |
182 | else if (pRxBuff->Position < pRxBuff->Size) // rx buffer not full |
190 | else // termination character received |
183 | { |
191 | { |
184 | if (c != '\r') // no termination character received |
192 | // calculate checksum from transmitted data |
185 | { |
- | |
186 | pRxBuff->pData[pRxBuff->Position++] = c; // copy byte to rxd buffer |
- | |
187 | pRxBuff->DataBytes++; |
193 | u16 crc = 0, i; |
188 | } |
194 | u8 crc1, crc2; |
189 | else // termination character received |
195 | for(i = 0; i < (pRxBuff->Position-2); i++) |
190 | { |
196 | { |
191 | // calculate checksum from transmitted data |
- | |
192 | u16 crc = 0, i; |
- | |
193 | u8 crc1, crc2; |
- | |
194 | for(i = 0; i < (pRxBuff->Position-2); i++) |
- | |
195 | { |
- | |
196 | crc += pRxBuff->pData[i]; |
197 | crc += pRxBuff->pData[i]; |
197 | } |
198 | } |
198 | crc %= 4096; |
199 | crc %= 4096; |
199 | crc1 = '=' + crc / 64; |
200 | crc1 = '=' + crc / 64; |
200 | crc2 = '=' + crc % 64; |
201 | crc2 = '=' + crc % 64; |
201 | // compare checksum to transmitted checksum bytes |
202 | // compare checksum to transmitted checksum bytes |
202 | if((crc1 == pRxBuff->pData[pRxBuff->Position-2]) && (crc2 == pRxBuff->pData[pRxBuff->Position-1])) |
203 | if((crc1 == pRxBuff->pData[pRxBuff->Position-2]) && (crc2 == pRxBuff->pData[pRxBuff->Position-1])) |
203 | { // checksum is valid |
204 | { // checksum is valid |
204 | pRxBuff->Locked = TRUE; // lock the rxd buffer |
- | |
205 | pRxBuff->pData[pRxBuff->Position++] = '\r'; // set termination character |
205 | pRxBuff->pData[pRxBuff->Position++] = '\r'; // set termination character |
206 | pRxBuff->DataBytes = pRxBuff->Position; |
206 | pRxBuff->DataBytes = pRxBuff->Position; |
207 | pRxBuff->Position = 0; |
207 | pRxBuff->Position = 0; |
- | 208 | pRxBuff->Locked = TRUE; // lock the rxd buffer |
|
208 | // if 2nd byte is an 'R' start bootloader |
209 | // if 2nd byte is an 'R' start bootloader |
209 | if(pRxBuff->pData[2] == 'R') |
210 | if(pRxBuff->pData[2] == 'R') |
210 | { |
211 | { |
211 | PowerOff(); |
212 | PowerOff(); |
212 | VIC_DeInit(); |
213 | VIC_DeInit(); |
213 | Execute_Bootloader(); // Reset-Commando - Bootloader starten |
214 | Execute_Bootloader(); // Reset-Commando - Bootloader starten |
214 | } |
215 | } |
215 | } // eof checksum valid |
216 | } // eof checksum valid |
216 | else |
217 | else |
217 | { // checksum is invalid |
218 | { // checksum is invalid |
218 | Buffer_Clear(pRxBuff); |
219 | Buffer_Clear(pRxBuff); |
219 | } // eof checksum invalid |
220 | } // eof checksum invalid |
220 | } // eof termination character received |
221 | } // eof termination character received |
221 | } // rxd buffer not full |
222 | } // rxd buffer not full |
222 | else // rxd buffer overrun |
223 | else // rxd buffer overrun |
223 | { |
224 | { |
224 | Buffer_Clear(pRxBuff); |
225 | Buffer_Clear(pRxBuff); |
225 | } // eof rxd buffer overrrun |
226 | } // eof rxd buffer overrrun |
226 | } |
227 | } |
227 | } |
228 | } |
Line 228... | Line 229... | ||
228 | 229 | ||
229 | /**************************************************************/ |
230 | /**************************************************************/ |