Rev 110 | Rev 113 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 110 | Rev 112 | ||
---|---|---|---|
Line 59... | Line 59... | ||
59 | #include "91x_lib.h" |
59 | #include "91x_lib.h" |
60 | #include "mkprotocol.h" |
60 | #include "mkprotocol.h" |
61 | #include "ramfunc.h" |
61 | #include "ramfunc.h" |
62 | #include "usb.h" |
62 | #include "usb.h" |
Line -... | Line 63... | ||
- | 63 | ||
- | 64 | void Buffer_Clear(Buffer_t* pBuffer) |
|
- | 65 | { |
|
- | 66 | pBuffer->DataBytes = 0; |
|
- | 67 | pBuffer->Position = 0; |
|
- | 68 | pBuffer->Locked = FALSE; |
|
- | 69 | } |
|
- | 70 | ||
- | 71 | void Buffer_Init(Buffer_t* pBuffer, u8* pDataBuffer, u16 DataBufferSize) |
|
- | 72 | { |
|
- | 73 | pBuffer->pData = pDataBuffer; |
|
- | 74 | pBuffer->Size = DataBufferSize; |
|
- | 75 | Buffer_Clear(pBuffer); |
|
Line 63... | Line 76... | ||
63 | 76 | } |
|
64 | 77 | ||
65 | /**************************************************************/ |
78 | /**************************************************************/ |
66 | /* Create serial output frame */ |
79 | /* Create serial output frame */ |
Line 77... | Line 90... | ||
77 | int len = 0; |
90 | int len = 0; |
Line 78... | Line 91... | ||
78 | 91 | ||
79 | if(pTxBuff->Locked == FALSE) |
92 | if(pTxBuff->Locked == FALSE) |
80 | { // tx-buffer is not in use |
93 | { // tx-buffer is not in use |
81 | pTxBuff->Position = 0; |
94 | pTxBuff->Position = 0; |
82 | pTxBuff->pData[pTxBuff->Position++] = '#'; // Start character |
95 | pTxBuff->pData[pTxBuff->Position++] = '#'; // Start character |
83 | pTxBuff->pData[pTxBuff->Position++] = 'a' + Address; // Address (a=0; b=1,...) |
96 | pTxBuff->pData[pTxBuff->Position++] = 'a' + Address; // Address (a=0; b=1,...) |
Line 84... | Line 97... | ||
84 | pTxBuff->pData[pTxBuff->Position++] = CmdID; // Command |
97 | pTxBuff->pData[pTxBuff->Position++] = CmdID; // Command |
85 | 98 | ||
Line 145... | Line 158... | ||
145 | } |
158 | } |
146 | tmpCRC %= 4096; |
159 | tmpCRC %= 4096; |
147 | pTxBuff->pData[pTxBuff->Position++] = '=' + tmpCRC / 64; |
160 | pTxBuff->pData[pTxBuff->Position++] = '=' + tmpCRC / 64; |
148 | pTxBuff->pData[pTxBuff->Position++] = '=' + tmpCRC % 64; |
161 | pTxBuff->pData[pTxBuff->Position++] = '=' + tmpCRC % 64; |
149 | pTxBuff->pData[pTxBuff->Position++] = '\r'; |
162 | pTxBuff->pData[pTxBuff->Position++] = '\r'; |
- | 163 | pTxBuff->DataBytes = pTxBuff->Position; |
|
150 | pTxBuff->Position = 0; // reset buffer position for transmision |
164 | pTxBuff->Position = 0; // reset buffer position for transmision |
151 | pTxBuff->Locked = TRUE; // mark as used |
165 | pTxBuff->Locked = TRUE; // mark as used |
152 | } // EOF buffer not in use |
166 | } // EOF buffer not in use |
153 | } |
167 | } |
Line 161... | Line 175... | ||
161 | if(pRxBuff->Locked == FALSE) |
175 | if(pRxBuff->Locked == FALSE) |
162 | { // rx buffer not locked |
176 | { // rx buffer not locked |
163 | if((pRxBuff->Position == 0) && (c == '#')) // if rxd buffer is empty and syncronisation character is received |
177 | if((pRxBuff->Position == 0) && (c == '#')) // if rxd buffer is empty and syncronisation character is received |
164 | { |
178 | { |
165 | pRxBuff->pData[pRxBuff->Position++] = c; // copy 1st byte to buffer |
179 | pRxBuff->pData[pRxBuff->Position++] = c; // copy 1st byte to buffer |
166 | //crc = c; // init crc |
180 | pRxBuff->DataBytes = 1; |
167 | } |
181 | } |
168 | else if (pRxBuff->Position < pRxBuff->Size) // rx buffer not full |
182 | else if (pRxBuff->Position < pRxBuff->Size) // rx buffer not full |
169 | { |
183 | { |
170 | if (c != '\r') // no termination character received |
184 | if (c != '\r') // no termination character received |
171 | { |
185 | { |
172 | pRxBuff->pData[pRxBuff->Position++] = c; // copy byte to rxd buffer |
186 | pRxBuff->pData[pRxBuff->Position++] = c; // copy byte to rxd buffer |
173 | //crc += c; // update crc |
187 | pRxBuff->DataBytes++; |
174 | } |
188 | } |
175 | else // termination character received |
189 | else // termination character received |
176 | { |
190 | { |
177 | // calculate checksum from transmitted data |
191 | // calculate checksum from transmitted data |
178 | u16 crc = 0, i; |
192 | u16 crc = 0, i; |
Line 186... | Line 200... | ||
186 | crc2 = '=' + crc % 64; |
200 | crc2 = '=' + crc % 64; |
187 | // compare checksum to transmitted checksum bytes |
201 | // compare checksum to transmitted checksum bytes |
188 | if((crc1 == pRxBuff->pData[pRxBuff->Position-2]) && (crc2 == pRxBuff->pData[pRxBuff->Position-1])) |
202 | if((crc1 == pRxBuff->pData[pRxBuff->Position-2]) && (crc2 == pRxBuff->pData[pRxBuff->Position-1])) |
189 | { // checksum is valid |
203 | { // checksum is valid |
190 | pRxBuff->Locked = TRUE; // lock the rxd buffer |
204 | pRxBuff->Locked = TRUE; // lock the rxd buffer |
191 | pRxBuff->pData[pRxBuff->Position] = '\r'; // set termination character |
205 | pRxBuff->pData[pRxBuff->Position++] = '\r'; // set termination character |
- | 206 | pRxBuff->DataBytes = pRxBuff->Position; |
|
- | 207 | pRxBuff->Position = 0; |
|
192 | // if 2nd byte is an 'R' start bootloader |
208 | // if 2nd byte is an 'R' start bootloader |
193 | if(pRxBuff->pData[2] == 'R') |
209 | if(pRxBuff->pData[2] == 'R') |
194 | { |
210 | { |
195 | PowerOff(); |
211 | PowerOff(); |
196 | VIC_DeInit(); |
212 | VIC_DeInit(); |
197 | Execute_Bootloader(); // Reset-Commando - Bootloader starten |
213 | Execute_Bootloader(); // Reset-Commando - Bootloader starten |
198 | } |
214 | } |
199 | } // eof checksum valid |
215 | } // eof checksum valid |
200 | else |
216 | else |
201 | { // checksum is invalid |
217 | { // checksum is invalid |
202 | pRxBuff->Position = 0; // reset rxd buffer position |
- | |
203 | pRxBuff->Locked = FALSE; // unlock rxd buffer |
218 | Buffer_Clear(pRxBuff); |
204 | } // eof checksum invalid |
219 | } // eof checksum invalid |
205 | } // eof termination character received |
220 | } // eof termination character received |
206 | } // rxd buffer not full |
221 | } // rxd buffer not full |
207 | else // rxd buffer overrun |
222 | else // rxd buffer overrun |
208 | { |
223 | { |
209 | pRxBuff->Position = 0; // reset rxd buffer pointer |
- | |
210 | pRxBuff->Locked = FALSE; // unlock rxd buffer |
224 | Buffer_Clear(pRxBuff); |
211 | } // eof rxd buffer overrrun |
225 | } // eof rxd buffer overrrun |
212 | } |
226 | } |
213 | } |
227 | } |
Line 214... | Line 228... | ||
214 | 228 | ||
Line 219... | Line 233... | ||
219 | { |
233 | { |
220 | u8 a,b,c,d; |
234 | u8 a,b,c,d; |
221 | u8 x,y,z; |
235 | u8 x,y,z; |
222 | u8 ptrIn = 3; // start with first data byte in rx buffer |
236 | u8 ptrIn = 3; // start with first data byte in rx buffer |
223 | u8 ptrOut = 3; |
237 | u8 ptrOut = 3; |
224 | u8 len = pRxBuff->Position - 6; // must be a multiple of 4 (3 bytes at begin and 3 bytes at end are no payload ) |
238 | u8 len = pRxBuff->DataBytes - 6; // must be a multiple of 4 (3 bytes at begin and 3 bytes at end are no payload ) |
225 | while(len) |
239 | while(len) |
226 | { |
240 | { |
227 | a = pRxBuff->pData[ptrIn++] - '='; |
241 | a = pRxBuff->pData[ptrIn++] - '='; |
228 | b = pRxBuff->pData[ptrIn++] - '='; |
242 | b = pRxBuff->pData[ptrIn++] - '='; |
229 | c = pRxBuff->pData[ptrIn++] - '='; |
243 | c = pRxBuff->pData[ptrIn++] - '='; |
Line 240... | Line 254... | ||
240 | } |
254 | } |
241 | pSerialMsg->Address = pRxBuff->pData[1] - 'a'; |
255 | pSerialMsg->Address = pRxBuff->pData[1] - 'a'; |
242 | pSerialMsg->CmdID = pRxBuff->pData[2]; |
256 | pSerialMsg->CmdID = pRxBuff->pData[2]; |
243 | pSerialMsg->pData = &(pRxBuff->pData[3]); |
257 | pSerialMsg->pData = &(pRxBuff->pData[3]); |
244 | pSerialMsg->DataLen = ptrOut - 3; // return number of data in bytes |
258 | pSerialMsg->DataLen = ptrOut - 3; // return number of data in bytes |
- | 259 | pRxBuff->Position = 0; |
|
- | 260 | pRxBuff->DataBytes = ptrOut; |
|
245 | } |
261 | } |