Subversion Repositories NaviCtrl

Rev

Rev 558 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 558 Rev 596
1
/*#######################################################################################*/
1
/*#######################################################################################*/
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
3
/*#######################################################################################*/
3
/*#######################################################################################*/
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// + www.MikroKopter.com
5
// + www.MikroKopter.com
6
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
7
// + Software Nutzungsbedingungen (english version: see below)
7
// + Software Nutzungsbedingungen (english version: see below)
8
// + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt -
8
// + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt -
9
// + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den
9
// + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den
10
// + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool 
10
// + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool 
11
// + - nachfolgend Software genannt - nur für private Zwecke zu nutzen.
11
// + - nachfolgend Software genannt - nur für private Zwecke zu nutzen.
12
// + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig.
12
// + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig.
13
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
13
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
14
// + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im
14
// + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im
15
// + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu.
15
// + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu.
16
// + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie
16
// + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie
17
// + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden.
17
// + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden.
18
// + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren
18
// + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren
19
// + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
19
// + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
20
// + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren
20
// + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren
21
// + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand 
21
// + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand 
22
// + des Mitverschuldens offen.
22
// + des Mitverschuldens offen.
23
// + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet.
23
// + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet.
24
// + Er wird die Software gründlich auf deren Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
24
// + Er wird die Software gründlich auf deren Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
25
// + Der Kunde wird er seine Daten vor Einsatz der Software nach dem Stand der Technik sichern.
25
// + Der Kunde wird er seine Daten vor Einsatz der Software nach dem Stand der Technik sichern.
26
// + Der Kunde ist darüber unterrichtet, dass der Lizenzgeber seine Daten im zur Vertragsdurchführung erforderlichen Umfang
26
// + Der Kunde ist darüber unterrichtet, dass der Lizenzgeber seine Daten im zur Vertragsdurchführung erforderlichen Umfang
27
// + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt.
27
// + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt.
28
// + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software.
28
// + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software.
29
// + #### ENDE DER NUTZUNGSBEDINGUNGEN ####'
29
// + #### ENDE DER NUTZUNGSBEDINGUNGEN ####'
30
// +  Hinweis: Informationen über erweiterte Nutzungsrechte (wie z.B. Nutzung für nicht-private Zwecke) sind auf Anfrage per Email an info(@)hisystems.de verfügbar.
30
// +  Hinweis: Informationen über erweiterte Nutzungsrechte (wie z.B. Nutzung für nicht-private Zwecke) sind auf Anfrage per Email an info(@)hisystems.de verfügbar.
31
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
32
// + Software LICENSING TERMS
32
// + Software LICENSING TERMS
33
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
33
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
34
// + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor -
34
// + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor -
35
// + The Licensor grants the customer a non-exclusive license to use the microcontroller firmware of the Flight-Ctrl, Navi-Ctrl, BL-Ctrl, and MK3Mag hardware 
35
// + The Licensor grants the customer a non-exclusive license to use the microcontroller firmware of the Flight-Ctrl, Navi-Ctrl, BL-Ctrl, and MK3Mag hardware 
36
// + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*.
36
// + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*.
37
// + The Software may only be used with the Licensor's products.
37
// + The Software may only be used with the Licensor's products.
38
// + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this
38
// + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this
39
// + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this
39
// + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this
40
// + agreement shall be the property of the Licensor.
40
// + agreement shall be the property of the Licensor.
41
// + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other
41
// + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other
42
// + features that can be used to identify the program may not be altered or defaced by the customer.
42
// + features that can be used to identify the program may not be altered or defaced by the customer.
43
// + The customer shall be responsible for taking reasonable precautions
43
// + The customer shall be responsible for taking reasonable precautions
44
// + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the
44
// + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the
45
// + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and
45
// + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and
46
// + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product
46
// + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product
47
// + liability. However, the Licensor shall be entitled to the defense of contributory negligence.
47
// + liability. However, the Licensor shall be entitled to the defense of contributory negligence.
48
// + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test
48
// + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test
49
// + the software for his purpose before any operational usage. The customer will backup his data before using the software.
49
// + the software for his purpose before any operational usage. The customer will backup his data before using the software.
50
// + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data
50
// + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data
51
// + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations.
51
// + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations.
52
// + *) The territory aspect only refers to the place where the Software is used, not its programmed range.
52
// + *) The territory aspect only refers to the place where the Software is used, not its programmed range.
53
// + #### END OF LICENSING TERMS ####
53
// + #### END OF LICENSING TERMS ####
54
// + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de.
54
// + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de.
55
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
55
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
56
#include <stdarg.h>
56
#include <stdarg.h>
57
#include <stdio.h>
57
#include <stdio.h>
58
#include "91x_lib.h"
58
#include "91x_lib.h"
59
#include "mkprotocol.h" 
59
#include "mkprotocol.h" 
60
#include "ramfunc.h"
60
#include "ramfunc.h"
61
#include "uart1.h"
61
#include "uart1.h"
62
#include "main.h"
62
#include "main.h"
63
#include "GPS.h"
63
#include "GPS.h"
64
 
64
 
65
/**************************************************************/
65
/**************************************************************/
66
/* Create serial output frame                                 */
66
/* Create serial output frame                                 */
67
/**************************************************************/
67
/**************************************************************/
68
u8 AddSerialData(Buffer_t* pTxBuff, u8 *buffer, u16 size) //u8 *data, u8 len, ....
68
u8 AddSerialData(Buffer_t* pTxBuff, u8 *buffer, u16 size) //u8 *data, u8 len, ....
69
{
69
{
70
  unsigned int i;
70
  unsigned int i;
71
        if(pTxBuff->Locked == TRUE) return(0);
71
        if(pTxBuff->Locked == TRUE) return(0);
72
        // tx-buffer is not in use
72
        // tx-buffer is not in use
73
        // lock the buffer
73
        // lock the buffer
74
        pTxBuff->Locked = TRUE;
74
        pTxBuff->Locked = TRUE;
75
        pTxBuff->Position = 0;
75
        pTxBuff->Position = 0;
76
        for(i=0; i<size; i++) pTxBuff->pData[pTxBuff->Position++] = buffer[i];                     
76
        for(i=0; i<size; i++) pTxBuff->pData[pTxBuff->Position++] = buffer[i];                     
77
        pTxBuff->DataBytes = pTxBuff->Position;
77
        pTxBuff->DataBytes = pTxBuff->Position;
78
        pTxBuff->Position = 0;  // reset buffer position for transmision
78
        pTxBuff->Position = 0;  // reset buffer position for transmision
79
        return(pTxBuff->Locked);
79
        return(pTxBuff->Locked);
80
}
80
}
81
 
81
 
82
/**************************************************************/
82
/**************************************************************/
83
/* Create serial output frame                                 */
83
/* Create serial output frame                                 */
84
/**************************************************************/
84
/**************************************************************/
85
u8 MKProtocol_CreateSerialFrame(Buffer_t* pTxBuff, u8 CmdID, u8 Address, u8 numofbuffers , ...) //u8 *data, u8 len, ....
85
u16 MKProtocol_CreateSerialFrame(Buffer_t* pTxBuff, u8 CmdID, u8 Address, u8 numofbuffers , ...) //u8 *data, u8 len, ....
86
{
86
{
87
        va_list ap;
87
        va_list ap;
88
 
88
 
89
        u8 a,b,c;
89
        u8 a,b,c;
90
        u16 ptr = 0;
90
        u16 ptr = 0;
91
        u16 tmpCRC = 0, i;
91
        u16 tmpCRC = 0, i;
92
 
92
 
93
        u8* pdata = NULL;
93
        u8* pdata = NULL;
94
        int len = 0;
94
        int len = 0;
95
       
95
       
96
        if(pTxBuff->Locked == TRUE) return(0);
96
        if(pTxBuff->Locked == TRUE) return(0);
97
 
97
 
98
        // tx-buffer is not in use
98
        // tx-buffer is not in use
99
        // lock the buffer
99
        // lock the buffer
100
        pTxBuff->Locked = TRUE;
100
        pTxBuff->Locked = TRUE;
101
        pTxBuff->Position = 0;
101
        pTxBuff->Position = 0;
102
        pTxBuff->pData[pTxBuff->Position++] = '#';                          // Start character
102
        pTxBuff->pData[pTxBuff->Position++] = '#';                          // Start character
103
        pTxBuff->pData[pTxBuff->Position++] = 'a' + Address;    // Address (a=0; b=1,...)
103
        pTxBuff->pData[pTxBuff->Position++] = 'a' + Address;    // Address (a=0; b=1,...)
104
        pTxBuff->pData[pTxBuff->Position++] = CmdID;                    // Command
104
        pTxBuff->pData[pTxBuff->Position++] = CmdID;                    // Command
105
 
105
 
106
        va_start(ap, numofbuffers);
106
        va_start(ap, numofbuffers);
107
        if(numofbuffers)
107
        if(numofbuffers)
108
        {
108
        {
109
                pdata = va_arg(ap, u8*);
109
                pdata = va_arg(ap, u8*);
110
                len = va_arg(ap, int);
110
                len = va_arg(ap, int);
111
                ptr = 0;
111
                ptr = 0;
112
                numofbuffers--;
112
                numofbuffers--;
113
        }
113
        }
114
        while(len)
114
        while(len)
115
        {
115
        {
116
                if(len)
116
                if(len)
117
                {
117
                {
118
                        a = pdata[ptr++];
118
                        a = pdata[ptr++];
119
                        len--;
119
                        len--;
120
                        if((!len) && numofbuffers) // try to jump to next buffer
120
                        if((!len) && numofbuffers) // try to jump to next buffer
121
                        {
121
                        {
122
                                pdata = va_arg(ap, u8*);
122
                                pdata = va_arg(ap, u8*);
123
                                len = va_arg(ap, int);
123
                                len = va_arg(ap, int);
124
                                ptr = 0;
124
                                ptr = 0;
125
                                numofbuffers--;
125
                                numofbuffers--;
126
                        }
126
                        }
127
                }
127
                }
128
                else a = 0;
128
                else a = 0;
129
                if(len)
129
                if(len)
130
                {
130
                {
131
                        b = pdata[ptr++];
131
                        b = pdata[ptr++];
132
                        len--;
132
                        len--;
133
                        if((!len) && numofbuffers) // try to jump to next buffer
133
                        if((!len) && numofbuffers) // try to jump to next buffer
134
                        {
134
                        {
135
                                pdata = va_arg(ap, u8*);
135
                                pdata = va_arg(ap, u8*);
136
                                len = va_arg(ap, int);
136
                                len = va_arg(ap, int);
137
                                ptr = 0;
137
                                ptr = 0;
138
                                numofbuffers--;
138
                                numofbuffers--;
139
                        }
139
                        }
140
                }
140
                }
141
                else b = 0;
141
                else b = 0;
142
                if(len)
142
                if(len)
143
                {
143
                {
144
                        c = pdata[ptr++];
144
                        c = pdata[ptr++];
145
                        len--;
145
                        len--;
146
                        if((!len) && numofbuffers) // try to jump to next buffer
146
                        if((!len) && numofbuffers) // try to jump to next buffer
147
                        {
147
                        {
148
                                pdata = va_arg(ap, u8*);
148
                                pdata = va_arg(ap, u8*);
149
                                len = va_arg(ap, int);
149
                                len = va_arg(ap, int);
150
                                ptr = 0;
150
                                ptr = 0;
151
                                numofbuffers--;
151
                                numofbuffers--;
152
                        }
152
                        }
153
                }
153
                }
154
                else c = 0;
154
                else c = 0;
155
                pTxBuff->pData[pTxBuff->Position++] = '=' + (a >> 2);
155
                pTxBuff->pData[pTxBuff->Position++] = '=' + (a >> 2);
156
                pTxBuff->pData[pTxBuff->Position++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4));
156
                pTxBuff->pData[pTxBuff->Position++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4));
157
                pTxBuff->pData[pTxBuff->Position++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6));
157
                pTxBuff->pData[pTxBuff->Position++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6));
158
                pTxBuff->pData[pTxBuff->Position++] = '=' + ( c & 0x3f);
158
                pTxBuff->pData[pTxBuff->Position++] = '=' + ( c & 0x3f);
159
        }
159
        }
160
        va_end(ap);
160
        va_end(ap);
161
        // add crc
161
        // add crc
162
        for(i = 0; i < pTxBuff->Position; i++)
162
        for(i = 0; i < pTxBuff->Position; i++)
163
        {
163
        {
164
                tmpCRC += pTxBuff->pData[i];
164
                tmpCRC += pTxBuff->pData[i];
165
        }
165
        }
166
        tmpCRC %= 4096;
166
        tmpCRC %= 4096;
167
        pTxBuff->pData[pTxBuff->Position++] = '=' + tmpCRC / 64;
167
        pTxBuff->pData[pTxBuff->Position++] = '=' + tmpCRC / 64;
168
        pTxBuff->pData[pTxBuff->Position++] = '=' + tmpCRC % 64;
168
        pTxBuff->pData[pTxBuff->Position++] = '=' + tmpCRC % 64;
169
        pTxBuff->pData[pTxBuff->Position++] = '\r';
169
        pTxBuff->pData[pTxBuff->Position++] = '\r';
170
        pTxBuff->DataBytes = pTxBuff->Position;
170
        pTxBuff->DataBytes = pTxBuff->Position;
171
        pTxBuff->Position = 0;  // reset buffer position for transmision
171
        pTxBuff->Position = 0;  // reset buffer position for transmision
172
        return(pTxBuff->Locked);
172
//      return(pTxBuff->Locked);
-
 
173
        return(pTxBuff->DataBytes);
173
}
174
}
174
 
175
 
175
// typical called in an UART Rx ISR
176
// typical called in an UART Rx ISR
176
/**************************************************************/
177
/**************************************************************/
177
/* Collect serial frame                                       */
178
/* Collect serial frame                                       */
178
/**************************************************************/
179
/**************************************************************/
179
u8 MKProtocol_CollectSerialFrame(Buffer_t* pRxBuff, u8 c)
180
u8 MKProtocol_CollectSerialFrame(Buffer_t* pRxBuff, u8 c)
180
{
181
{
181
        if(pRxBuff->Locked == FALSE)
182
        if(pRxBuff->Locked == FALSE)
182
        { // rx buffer not locked
183
        { // rx buffer not locked
183
                if(c == '#') // if syncronisation character is received
184
                if(c == '#') // if syncronisation character is received
184
                {
185
                {
185
                        pRxBuff->Position = 0;                                          // reset buffer
186
                        pRxBuff->Position = 0;                                          // reset buffer
186
                        pRxBuff->pData[pRxBuff->Position++] = c;        // copy 1st byte to buffer
187
                        pRxBuff->pData[pRxBuff->Position++] = c;        // copy 1st byte to buffer
187
                        pRxBuff->DataBytes = 1;
188
                        pRxBuff->DataBytes = 1;
188
                }
189
                }
189
                else if (pRxBuff->Position < pRxBuff->Size) // rx buffer not full
190
                else if (pRxBuff->Position < pRxBuff->Size) // rx buffer not full
190
                {
191
                {
191
                        pRxBuff->pData[pRxBuff->Position++] = c; // copy byte to rxd buffer
192
                        pRxBuff->pData[pRxBuff->Position++] = c; // copy byte to rxd buffer
192
                        pRxBuff->DataBytes++;
193
                        pRxBuff->DataBytes++;
193
                        // termination character received and sync has been established
194
                        // termination character received and sync has been established
194
                        if ((c == '\r') && (pRxBuff->pData[0]== '#'))
195
                        if ((c == '\r') && (pRxBuff->pData[0]== '#'))
195
                        {
196
                        {
196
                                // calculate checksum from transmitted data
197
                                // calculate checksum from transmitted data
197
                                u16 crc = 0, i;
198
                                u16 crc = 0, i;
198
                                u8 crc1, crc2;
199
                                u8 crc1, crc2;
199
                                for(i = 0; i < (pRxBuff->Position-3); i++)
200
                                for(i = 0; i < (pRxBuff->Position-3); i++)
200
                                {
201
                                {
201
                                        crc +=  pRxBuff->pData[i];  
202
                                        crc +=  pRxBuff->pData[i];  
202
                                }              
203
                                }              
203
                                crc %= 4096;
204
                                crc %= 4096;
204
                                crc1 = '=' + crc / 64;
205
                                crc1 = '=' + crc / 64;
205
                                crc2 = '=' + crc % 64;
206
                                crc2 = '=' + crc % 64;
206
                                // compare checksum to transmitted checksum bytes
207
                                // compare checksum to transmitted checksum bytes
207
                                if((crc1 == pRxBuff->pData[pRxBuff->Position-3]) && (crc2 == pRxBuff->pData[pRxBuff->Position-2]))
208
                                if((crc1 == pRxBuff->pData[pRxBuff->Position-3]) && (crc2 == pRxBuff->pData[pRxBuff->Position-2]))
208
                                {
209
                                {
209
                                    // checksum is valid
210
                                    // checksum is valid
210
                                        pRxBuff->Position = 0;                                 
211
                                        pRxBuff->Position = 0;                                 
211
                                        pRxBuff->Locked = TRUE;                                     // lock the rxd buffer 
212
                                        pRxBuff->Locked = TRUE;                                     // lock the rxd buffer 
212
                                        // if 2nd byte is an 'R' start bootloader
213
                                        // if 2nd byte is an 'R' start bootloader
213
                                        if(pRxBuff->pData[2] == 'R' && !(FC.RealStatusFlags & FC_STATUS_MOTOR_RUN)) // not if the motors are running)
214
                                        if(pRxBuff->pData[2] == 'R' && !(FC.RealStatusFlags & FC_STATUS_MOTOR_RUN)) // not if the motors are running)
214
                                        {
215
                                        {
215
                                                VIC_DeInit();
216
                                                VIC_DeInit();
216
                                                Execute_Bootloader(); // Reset-Commando - Bootloader starten
217
                                                Execute_Bootloader(); // Reset-Commando - Bootloader starten
217
                                        }
218
                                        }
218
                                } // eof checksum valid
219
                                } // eof checksum valid
219
                                else
220
                                else
220
                                {       // checksum is invalid
221
                                {       // checksum is invalid
221
                                        Buffer_Clear(pRxBuff);
222
                                        Buffer_Clear(pRxBuff);
222
                                }  // eof checksum invalid
223
                                }  // eof checksum invalid
223
                        } // eof termination character received
224
                        } // eof termination character received
224
                } // rxd buffer not full
225
                } // rxd buffer not full
225
                else // rxd buffer overrun
226
                else // rxd buffer overrun
226
                {
227
                {
227
                        Buffer_Clear(pRxBuff);
228
                        Buffer_Clear(pRxBuff);
228
                } // eof rxd buffer overrun
229
                } // eof rxd buffer overrun
229
        }
230
        }
230
        return(pRxBuff->Locked);
231
        return(pRxBuff->Locked);
231
}
232
}
232
 
233
 
233
/**************************************************************/
234
/**************************************************************/
234
/* Decode detination address                                  */
235
/* Decode detination address                                  */
235
/**************************************************************/
236
/**************************************************************/
236
void MKProtocol_DecodeSerialFrameHeader(Buffer_t* pRxBuff, SerialMsg_t* pSerialMsg)
237
void MKProtocol_DecodeSerialFrameHeader(Buffer_t* pRxBuff, SerialMsg_t* pSerialMsg)
237
{
238
{
238
        if(pRxBuff->Locked)
239
        if(pRxBuff->Locked)
239
        {
240
        {
240
                pSerialMsg->Address = pRxBuff->pData[1] - 'a';
241
                pSerialMsg->Address = pRxBuff->pData[1] - 'a';
241
                pSerialMsg->CmdID = pRxBuff->pData[2];
242
                pSerialMsg->CmdID = pRxBuff->pData[2];
242
        }
243
        }
243
        else
244
        else
244
        {
245
        {
245
                pSerialMsg->Address = 0;
246
                pSerialMsg->Address = 0;
246
                pSerialMsg->CmdID = ' ';
247
                pSerialMsg->CmdID = ' ';
247
        }      
248
        }      
248
}
249
}
249
 
250
 
250
/**************************************************************/
251
/**************************************************************/
251
/* Decode data                                                */
252
/* Decode data                                                */
252
/**************************************************************/
253
/**************************************************************/
253
void MKProtocol_DecodeSerialFrameData(Buffer_t* pRxBuff, SerialMsg_t* pSerialMsg)
254
void MKProtocol_DecodeSerialFrameData(Buffer_t* pRxBuff, SerialMsg_t* pSerialMsg)
254
{
255
{
255
        u8 a,b,c,d;
256
        u8 a,b,c,d;
256
        u16 ptrIn = 3; // start with first data byte in rx buffer
257
        u16 ptrIn = 3; // start with first data byte in rx buffer
257
        u16 ptrOut = 3;
258
        u16 ptrOut = 3;
258
        u16 len = pRxBuff->DataBytes - 6;        // must be a multiple of 4 (3 bytes at begin and 3 bytes at end are no payload )
259
        u16 len = pRxBuff->DataBytes - 6;        // must be a multiple of 4 (3 bytes at begin and 3 bytes at end are no payload )
259
 
260
 
260
        len/=4;   // number of 4 byte blocks
261
        len/=4;   // number of 4 byte blocks
261
        while(len--)
262
        while(len--)
262
        {
263
        {
263
                a = pRxBuff->pData[ptrIn++] - '=';
264
                a = pRxBuff->pData[ptrIn++] - '=';
264
                b = pRxBuff->pData[ptrIn++] - '=';
265
                b = pRxBuff->pData[ptrIn++] - '=';
265
                c = pRxBuff->pData[ptrIn++] - '=';
266
                c = pRxBuff->pData[ptrIn++] - '=';
266
                d = pRxBuff->pData[ptrIn++] - '=';
267
                d = pRxBuff->pData[ptrIn++] - '=';
267
 
268
 
268
                pRxBuff->pData[ptrOut++] = (a << 2) | (b >> 4);
269
                pRxBuff->pData[ptrOut++] = (a << 2) | (b >> 4);
269
                pRxBuff->pData[ptrOut++] = ((b & 0x0f) << 4) | (c >> 2);
270
                pRxBuff->pData[ptrOut++] = ((b & 0x0f) << 4) | (c >> 2);
270
                pRxBuff->pData[ptrOut++] = ((c & 0x03) << 6) | d;
271
                pRxBuff->pData[ptrOut++] = ((c & 0x03) << 6) | d;
271
        }
272
        }
272
        pSerialMsg->pData = &(pRxBuff->pData[3]);
273
        pSerialMsg->pData = &(pRxBuff->pData[3]);
273
        pSerialMsg->DataLen = ptrOut - 3;       // return number of data in bytes
274
        pSerialMsg->DataLen = ptrOut - 3;       // return number of data in bytes
274
        pRxBuff->Position = 0;
275
        pRxBuff->Position = 0;
275
        pRxBuff->DataBytes = ptrOut;
276
        pRxBuff->DataBytes = ptrOut;
276
}
277
}
277
 
278
 
278
/*
279
/*
279
void MKProtocol_DecodeSerialFrameData(Buffer_t* pRxBuff, SerialMsg_t* pSerialMsg)
280
void MKProtocol_DecodeSerialFrameData(Buffer_t* pRxBuff, SerialMsg_t* pSerialMsg)
280
{
281
{
281
        u8 a,b,c,d;
282
        u8 a,b,c,d;
282
        u8 x,y,z;
283
        u8 x,y,z;
283
        u16 ptrIn = 3; // start with first data byte in rx buffer
284
        u16 ptrIn = 3; // start with first data byte in rx buffer
284
        u16 ptrOut = 3;
285
        u16 ptrOut = 3;
285
        u16 len = pRxBuff->DataBytes - 6;        // must be a multiple of 4 (3 bytes at begin and 3 bytes at end are no payload )
286
        u16 len = pRxBuff->DataBytes - 6;        // must be a multiple of 4 (3 bytes at begin and 3 bytes at end are no payload )
286
        while(len)
287
        while(len)
287
        {
288
        {
288
                a = pRxBuff->pData[ptrIn++] - '=';
289
                a = pRxBuff->pData[ptrIn++] - '=';
289
                b = pRxBuff->pData[ptrIn++] - '=';
290
                b = pRxBuff->pData[ptrIn++] - '=';
290
                c = pRxBuff->pData[ptrIn++] - '=';
291
                c = pRxBuff->pData[ptrIn++] - '=';
291
                d = pRxBuff->pData[ptrIn++] - '=';
292
                d = pRxBuff->pData[ptrIn++] - '=';
292
                //if(ptrIn > ReceivedBytes - 3) break;
293
                //if(ptrIn > ReceivedBytes - 3) break;
293
 
294
 
294
                x = (a << 2) | (b >> 4);
295
                x = (a << 2) | (b >> 4);
295
                y = ((b & 0x0f) << 4) | (c >> 2);
296
                y = ((b & 0x0f) << 4) | (c >> 2);
296
                z = ((c & 0x03) << 6) | d;
297
                z = ((c & 0x03) << 6) | d;
297
 
298
 
298
                if(len--) pRxBuff->pData[ptrOut++] = x; else break;
299
                if(len--) pRxBuff->pData[ptrOut++] = x; else break;
299
                if(len--) pRxBuff->pData[ptrOut++] = y; else break;
300
                if(len--) pRxBuff->pData[ptrOut++] = y; else break;
300
                if(len--) pRxBuff->pData[ptrOut++] = z; else break;
301
                if(len--) pRxBuff->pData[ptrOut++] = z; else break;
301
        }
302
        }
302
        pSerialMsg->pData = &(pRxBuff->pData[3]);
303
        pSerialMsg->pData = &(pRxBuff->pData[3]);
303
        pSerialMsg->DataLen = ptrOut - 3;       // return number of data in bytes
304
        pSerialMsg->DataLen = ptrOut - 3;       // return number of data in bytes
304
        pRxBuff->Position = 0;
305
        pRxBuff->Position = 0;
305
        pRxBuff->DataBytes = ptrOut;
306
        pRxBuff->DataBytes = ptrOut;
306
}
307
}
307
*/
308
*/
308
 
309
 
309
 
310