Subversion Repositories NaviCtrl

Rev

Rev 171 | Rev 238 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 171 Rev 190
Line 73... Line 73...
73
        pBuffer->pData = pDataBuffer;
73
        pBuffer->pData = pDataBuffer;
74
        pBuffer->Size = DataBufferSize;
74
        pBuffer->Size = DataBufferSize;
75
        Buffer_Clear(pBuffer); 
75
        Buffer_Clear(pBuffer); 
76
}
76
}
Line -... Line 77...
-
 
77
 
-
 
78
u8 Buffer_Copy(Buffer_t* pSrcBuffer, Buffer_t* pDstBuffer)
-
 
79
{
-
 
80
        u8 retval = 0;
-
 
81
 
-
 
82
        if( (pSrcBuffer != NULL) && (pDstBuffer != NULL) )
-
 
83
        {
-
 
84
                if(pSrcBuffer->Locked && !(pDstBuffer->Locked) && (pDstBuffer->Size >= pSrcBuffer->DataBytes))
-
 
85
                {
-
 
86
                        memcpy(pDstBuffer->pData, pSrcBuffer->pData, pSrcBuffer->DataBytes);
-
 
87
                        pDstBuffer->DataBytes = pSrcBuffer->DataBytes;
-
 
88
                        pDstBuffer->Position = 0;
-
 
89
                        pDstBuffer->Locked = TRUE;
-
 
90
                        retval = 1;
-
 
91
                }
-
 
92
        }
-
 
93
        return retval;
-
 
94
}
77
 
95
 
78
/**************************************************************/
96
/**************************************************************/
79
/* Create serial output frame                                 */
97
/* Create serial output frame                                 */
80
/**************************************************************/
98
/**************************************************************/
81
void MKProtocol_CreateSerialFrame(Buffer_t* pTxBuff, u8 CmdID, u8 Address, u8 numofbuffers , ...) //u8 *data, u8 len, ....
99
void MKProtocol_CreateSerialFrame(Buffer_t* pTxBuff, u8 CmdID, u8 Address, u8 numofbuffers , ...) //u8 *data, u8 len, ....
Line 181... Line 199...
181
                        pRxBuff->pData[pRxBuff->Position++] = c;        // copy 1st byte to buffer
199
                        pRxBuff->pData[pRxBuff->Position++] = c;        // copy 1st byte to buffer
182
                        pRxBuff->DataBytes = 1;
200
                        pRxBuff->DataBytes = 1;
183
                }
201
                }
184
                else if (pRxBuff->Position < pRxBuff->Size) // rx buffer not full
202
                else if (pRxBuff->Position < pRxBuff->Size) // rx buffer not full
185
                {
203
                {
186
                        if (c != '\r') // no termination character received
-
 
187
                        {
-
 
188
                                pRxBuff->pData[pRxBuff->Position++] = c; // copy byte to rxd buffer
204
                        pRxBuff->pData[pRxBuff->Position++] = c; // copy byte to rxd buffer
189
                                pRxBuff->DataBytes++;
205
                        pRxBuff->DataBytes++;
190
                        }
-
 
191
                        else // termination character received
206
                        if (c == '\r') // termination character received
192
                        {
207
                        {
193
                                // calculate checksum from transmitted data
208
                                // calculate checksum from transmitted data
194
                                u16 crc = 0, i;
209
                                u16 crc = 0, i;
195
                                u8 crc1, crc2;
210
                                u8 crc1, crc2;
196
                                for(i = 0; i < (pRxBuff->Position-2); i++)
211
                                for(i = 0; i < (pRxBuff->Position-3); i++)
197
                                {
212
                                {
198
                                        crc +=  pRxBuff->pData[i];  
213
                                        crc +=  pRxBuff->pData[i];  
199
                                }              
214
                                }              
200
                                crc %= 4096;
215
                                crc %= 4096;
201
                                crc1 = '=' + crc / 64;
216
                                crc1 = '=' + crc / 64;
202
                                crc2 = '=' + crc % 64;
217
                                crc2 = '=' + crc % 64;
203
                                // compare checksum to transmitted checksum bytes
218
                                // compare checksum to transmitted checksum bytes
204
                                if((crc1 == pRxBuff->pData[pRxBuff->Position-2]) && (crc2 == pRxBuff->pData[pRxBuff->Position-1]))
219
                                if((crc1 == pRxBuff->pData[pRxBuff->Position-3]) && (crc2 == pRxBuff->pData[pRxBuff->Position-2]))
205
                                {   // checksum is valid
220
                                {   // checksum is valid
206
                                        pRxBuff->pData[pRxBuff->Position++] = '\r';     // set termination character
-
 
207
                                        pRxBuff->DataBytes = pRxBuff->Position;
-
 
208
                                        pRxBuff->Position = 0;                                 
221
                                        pRxBuff->Position = 0;                                 
209
                                        pRxBuff->Locked = TRUE;                                     // lock the rxd buffer 
222
                                        pRxBuff->Locked = TRUE;                                     // lock the rxd buffer 
210
                                        // if 2nd byte is an 'R' start bootloader
223
                                        // if 2nd byte is an 'R' start bootloader
211
                                        if(pRxBuff->pData[2] == 'R')
224
                                        if(pRxBuff->pData[2] == 'R')
212
                                        {
225
                                        {
Line 228... Line 241...
228
        }
241
        }
229
        return(pRxBuff->Locked);
242
        return(pRxBuff->Locked);
230
}
243
}
Line 231... Line 244...
231
 
244
 
-
 
245
/**************************************************************/
-
 
246
/* Decode detination address                                  */
-
 
247
/**************************************************************/
-
 
248
void MKProtocol_DecodeSerialFrameHeader(Buffer_t* pRxBuff, SerialMsg_t* pSerialMsg)
-
 
249
{
-
 
250
        if(pRxBuff->Locked)
-
 
251
        {
-
 
252
                pSerialMsg->Address = pRxBuff->pData[1] - 'a';
-
 
253
                pSerialMsg->CmdID = pRxBuff->pData[2];
-
 
254
        }
-
 
255
        else
-
 
256
        {
-
 
257
                pSerialMsg->Address = 0;
-
 
258
                pSerialMsg->CmdID = ' ';
-
 
259
        }      
-
 
260
}
-
 
261
 
232
/**************************************************************/
262
/**************************************************************/
233
/* Decode data                                                */
263
/* Decode data                                                */
234
/**************************************************************/
264
/**************************************************************/
235
void MKProtocol_DecodeSerialFrame(Buffer_t* pRxBuff, SerialMsg_t* pSerialMsg)
265
void MKProtocol_DecodeSerialFrameData(Buffer_t* pRxBuff, SerialMsg_t* pSerialMsg)
236
{
266
{
237
        u8 a,b,c,d;
267
        u8 a,b,c,d;
238
        u8 x,y,z;
268
        u8 x,y,z;
239
        u8 ptrIn = 3; // start with first data byte in rx buffer
269
        u8 ptrIn = 3; // start with first data byte in rx buffer
Line 253... Line 283...
253
 
283
 
254
                if(len--) pRxBuff->pData[ptrOut++] = x; else break;
284
                if(len--) pRxBuff->pData[ptrOut++] = x; else break;
255
                if(len--) pRxBuff->pData[ptrOut++] = y; else break;
285
                if(len--) pRxBuff->pData[ptrOut++] = y; else break;
256
                if(len--) pRxBuff->pData[ptrOut++] = z; else break;
286
                if(len--) pRxBuff->pData[ptrOut++] = z; else break;
257
        }
-
 
258
        pRxBuff->pData[1] -= 'a'; // substract address offset
-
 
259
        pSerialMsg->pAddress = &(pRxBuff->pData[1]);
-
 
260
        pSerialMsg->pCmdID = &(pRxBuff->pData[2]);
287
        }
261
        pSerialMsg->pData = &(pRxBuff->pData[3]);
288
        pSerialMsg->pData = &(pRxBuff->pData[3]);
262
        pSerialMsg->DataLen = ptrOut - 3;       // return number of data in bytes
289
        pSerialMsg->DataLen = ptrOut - 3;       // return number of data in bytes
263
        pRxBuff->Position = 0;
290
        pRxBuff->Position = 0;
264
        pRxBuff->DataBytes = ptrOut;
291
        pRxBuff->DataBytes = ptrOut;