Subversion Repositories NaviCtrl

Rev

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

Rev 483 Rev 489
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 "91x_lib.h"
58
#include "91x_lib.h"
58
#include "mkprotocol.h" 
59
#include "mkprotocol.h" 
59
#include "ramfunc.h"
60
#include "ramfunc.h"
60
#include "usb.h"
-
 
61
#include "uart1.h"
61
#include "uart1.h"
62
#include "main.h"
62
#include "main.h"
63
 
63
 
64
/**************************************************************/
64
/**************************************************************/
65
/* Create serial output frame                                 */
65
/* Create serial output frame                                 */
66
/**************************************************************/
66
/**************************************************************/
67
u8 AddSerialData(Buffer_t* pTxBuff, u8 *buffer, u16 size) //u8 *data, u8 len, ....
67
u8 AddSerialData(Buffer_t* pTxBuff, u8 *buffer, u16 size) //u8 *data, u8 len, ....
68
{
68
{
69
  unsigned int i;
69
  unsigned int i;
70
        if(pTxBuff->Locked == TRUE) return(0);
70
        if(pTxBuff->Locked == TRUE) return(0);
71
        // tx-buffer is not in use
71
        // tx-buffer is not in use
72
        // lock the buffer
72
        // lock the buffer
73
        pTxBuff->Locked = TRUE;
73
        pTxBuff->Locked = TRUE;
74
        pTxBuff->Position = 0;
74
        pTxBuff->Position = 0;
75
        for(i=0; i<size; i++) pTxBuff->pData[pTxBuff->Position++] = buffer[i];                     
75
        for(i=0; i<size; i++) pTxBuff->pData[pTxBuff->Position++] = buffer[i];                     
76
        pTxBuff->DataBytes = pTxBuff->Position;
76
        pTxBuff->DataBytes = pTxBuff->Position;
77
        pTxBuff->Position = 0;  // reset buffer position for transmision
77
        pTxBuff->Position = 0;  // reset buffer position for transmision
78
        return(pTxBuff->Locked);
78
        return(pTxBuff->Locked);
79
}
79
}
80
 
80
 
81
/**************************************************************/
81
/**************************************************************/
82
/* Create serial output frame                                 */
82
/* Create serial output frame                                 */
83
/**************************************************************/
83
/**************************************************************/
84
u8 MKProtocol_CreateSerialFrame(Buffer_t* pTxBuff, u8 CmdID, u8 Address, u8 numofbuffers , ...) //u8 *data, u8 len, ....
84
u8 MKProtocol_CreateSerialFrame(Buffer_t* pTxBuff, u8 CmdID, u8 Address, u8 numofbuffers , ...) //u8 *data, u8 len, ....
85
{
85
{
86
        va_list ap;
86
        va_list ap;
87
 
87
 
88
        u8 a,b,c;
88
        u8 a,b,c;
89
        u16 ptr = 0;
89
        u16 ptr = 0;
90
        u16 tmpCRC = 0, i;
90
        u16 tmpCRC = 0, i;
91
 
91
 
92
        u8* pdata = NULL;
92
        u8* pdata = NULL;
93
        int len = 0;
93
        int len = 0;
94
       
94
       
95
        if(pTxBuff->Locked == TRUE) return(0);
95
        if(pTxBuff->Locked == TRUE) return(0);
96
 
96
 
97
        // tx-buffer is not in use
97
        // tx-buffer is not in use
98
        // lock the buffer
98
        // lock the buffer
99
        pTxBuff->Locked = TRUE;
99
        pTxBuff->Locked = TRUE;
100
        pTxBuff->Position = 0;
100
        pTxBuff->Position = 0;
101
        pTxBuff->pData[pTxBuff->Position++] = '#';                          // Start character
101
        pTxBuff->pData[pTxBuff->Position++] = '#';                          // Start character
102
        pTxBuff->pData[pTxBuff->Position++] = 'a' + Address;    // Address (a=0; b=1,...)
102
        pTxBuff->pData[pTxBuff->Position++] = 'a' + Address;    // Address (a=0; b=1,...)
103
        pTxBuff->pData[pTxBuff->Position++] = CmdID;                    // Command
103
        pTxBuff->pData[pTxBuff->Position++] = CmdID;                    // Command
104
 
104
 
105
        va_start(ap, numofbuffers);
105
        va_start(ap, numofbuffers);
106
        if(numofbuffers)
106
        if(numofbuffers)
107
        {
107
        {
108
                pdata = va_arg(ap, u8*);
108
                pdata = va_arg(ap, u8*);
109
                len = va_arg(ap, int);
109
                len = va_arg(ap, int);
110
                ptr = 0;
110
                ptr = 0;
111
                numofbuffers--;
111
                numofbuffers--;
112
        }
112
        }
113
        while(len)
113
        while(len)
114
        {
114
        {
115
                if(len)
115
                if(len)
116
                {
116
                {
117
                        a = pdata[ptr++];
117
                        a = pdata[ptr++];
118
                        len--;
118
                        len--;
119
                        if((!len) && numofbuffers) // try to jump to next buffer
119
                        if((!len) && numofbuffers) // try to jump to next buffer
120
                        {
120
                        {
121
                                pdata = va_arg(ap, u8*);
121
                                pdata = va_arg(ap, u8*);
122
                                len = va_arg(ap, int);
122
                                len = va_arg(ap, int);
123
                                ptr = 0;
123
                                ptr = 0;
124
                                numofbuffers--;
124
                                numofbuffers--;
125
                        }
125
                        }
126
                }
126
                }
127
                else a = 0;
127
                else a = 0;
128
                if(len)
128
                if(len)
129
                {
129
                {
130
                        b = pdata[ptr++];
130
                        b = pdata[ptr++];
131
                        len--;
131
                        len--;
132
                        if((!len) && numofbuffers) // try to jump to next buffer
132
                        if((!len) && numofbuffers) // try to jump to next buffer
133
                        {
133
                        {
134
                                pdata = va_arg(ap, u8*);
134
                                pdata = va_arg(ap, u8*);
135
                                len = va_arg(ap, int);
135
                                len = va_arg(ap, int);
136
                                ptr = 0;
136
                                ptr = 0;
137
                                numofbuffers--;
137
                                numofbuffers--;
138
                        }
138
                        }
139
                }
139
                }
140
                else b = 0;
140
                else b = 0;
141
                if(len)
141
                if(len)
142
                {
142
                {
143
                        c = pdata[ptr++];
143
                        c = pdata[ptr++];
144
                        len--;
144
                        len--;
145
                        if((!len) && numofbuffers) // try to jump to next buffer
145
                        if((!len) && numofbuffers) // try to jump to next buffer
146
                        {
146
                        {
147
                                pdata = va_arg(ap, u8*);
147
                                pdata = va_arg(ap, u8*);
148
                                len = va_arg(ap, int);
148
                                len = va_arg(ap, int);
149
                                ptr = 0;
149
                                ptr = 0;
150
                                numofbuffers--;
150
                                numofbuffers--;
151
                        }
151
                        }
152
                }
152
                }
153
                else c = 0;
153
                else c = 0;
154
                pTxBuff->pData[pTxBuff->Position++] = '=' + (a >> 2);
154
                pTxBuff->pData[pTxBuff->Position++] = '=' + (a >> 2);
155
                pTxBuff->pData[pTxBuff->Position++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4));
155
                pTxBuff->pData[pTxBuff->Position++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4));
156
                pTxBuff->pData[pTxBuff->Position++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6));
156
                pTxBuff->pData[pTxBuff->Position++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6));
157
                pTxBuff->pData[pTxBuff->Position++] = '=' + ( c & 0x3f);
157
                pTxBuff->pData[pTxBuff->Position++] = '=' + ( c & 0x3f);
158
        }
158
        }
159
        va_end(ap);
159
        va_end(ap);
160
        // add crc
160
        // add crc
161
        for(i = 0; i < pTxBuff->Position; i++)
161
        for(i = 0; i < pTxBuff->Position; i++)
162
        {
162
        {
163
                tmpCRC += pTxBuff->pData[i];
163
                tmpCRC += pTxBuff->pData[i];
164
        }
164
        }
165
        tmpCRC %= 4096;
165
        tmpCRC %= 4096;
166
        pTxBuff->pData[pTxBuff->Position++] = '=' + tmpCRC / 64;
166
        pTxBuff->pData[pTxBuff->Position++] = '=' + tmpCRC / 64;
167
        pTxBuff->pData[pTxBuff->Position++] = '=' + tmpCRC % 64;
167
        pTxBuff->pData[pTxBuff->Position++] = '=' + tmpCRC % 64;
168
        pTxBuff->pData[pTxBuff->Position++] = '\r';
168
        pTxBuff->pData[pTxBuff->Position++] = '\r';
169
        pTxBuff->DataBytes = pTxBuff->Position;
169
        pTxBuff->DataBytes = pTxBuff->Position;
170
        pTxBuff->Position = 0;  // reset buffer position for transmision
170
        pTxBuff->Position = 0;  // reset buffer position for transmision
171
        return(pTxBuff->Locked);
171
        return(pTxBuff->Locked);
172
}
172
}
173
 
173
 
174
// typical called in an UART Rx ISR
174
// typical called in an UART Rx ISR
175
/**************************************************************/
175
/**************************************************************/
176
/* Collect serial frame                                       */
176
/* Collect serial frame                                       */
177
/**************************************************************/
177
/**************************************************************/
178
u8 MKProtocol_CollectSerialFrame(Buffer_t* pRxBuff, u8 c)
178
u8 MKProtocol_CollectSerialFrame(Buffer_t* pRxBuff, u8 c)
179
{
179
{
180
        if(pRxBuff->Locked == FALSE)
180
        if(pRxBuff->Locked == FALSE)
181
        { // rx buffer not locked
181
        { // rx buffer not locked
182
                if(c == '#') // if syncronisation character is received
182
                if(c == '#') // if syncronisation character is received
183
                {
183
                {
184
                        pRxBuff->Position = 0;                                          // reset buffer
184
                        pRxBuff->Position = 0;                                          // reset buffer
185
                        pRxBuff->pData[pRxBuff->Position++] = c;        // copy 1st byte to buffer
185
                        pRxBuff->pData[pRxBuff->Position++] = c;        // copy 1st byte to buffer
186
                        pRxBuff->DataBytes = 1;
186
                        pRxBuff->DataBytes = 1;
187
                }
187
                }
188
                else if (pRxBuff->Position < pRxBuff->Size) // rx buffer not full
188
                else if (pRxBuff->Position < pRxBuff->Size) // rx buffer not full
189
                {
189
                {
190
                        pRxBuff->pData[pRxBuff->Position++] = c; // copy byte to rxd buffer
190
                        pRxBuff->pData[pRxBuff->Position++] = c; // copy byte to rxd buffer
191
                        pRxBuff->DataBytes++;
191
                        pRxBuff->DataBytes++;
192
                        // termination character received and sync has been established
192
                        // termination character received and sync has been established
193
                        if ((c == '\r') && (pRxBuff->pData[0]== '#'))
193
                        if ((c == '\r') && (pRxBuff->pData[0]== '#'))
194
                        {
194
                        {
195
                                // calculate checksum from transmitted data
195
                                // calculate checksum from transmitted data
196
                                u16 crc = 0, i;
196
                                u16 crc = 0, i;
197
                                u8 crc1, crc2;
197
                                u8 crc1, crc2;
198
                                for(i = 0; i < (pRxBuff->Position-3); i++)
198
                                for(i = 0; i < (pRxBuff->Position-3); i++)
199
                                {
199
                                {
200
                                        crc +=  pRxBuff->pData[i];  
200
                                        crc +=  pRxBuff->pData[i];  
201
                                }              
201
                                }              
202
                                crc %= 4096;
202
                                crc %= 4096;
203
                                crc1 = '=' + crc / 64;
203
                                crc1 = '=' + crc / 64;
204
                                crc2 = '=' + crc % 64;
204
                                crc2 = '=' + crc % 64;
205
                                // compare checksum to transmitted checksum bytes
205
                                // compare checksum to transmitted checksum bytes
206
                                if((crc1 == pRxBuff->pData[pRxBuff->Position-3]) && (crc2 == pRxBuff->pData[pRxBuff->Position-2]))
206
                                if((crc1 == pRxBuff->pData[pRxBuff->Position-3]) && (crc2 == pRxBuff->pData[pRxBuff->Position-2]))
207
                                {
207
                                {
208
                                    // checksum is valid
208
                                    // checksum is valid
209
                                        pRxBuff->Position = 0;                                 
209
                                        pRxBuff->Position = 0;                                 
210
                                        pRxBuff->Locked = TRUE;                                     // lock the rxd buffer 
210
                                        pRxBuff->Locked = TRUE;                                     // lock the rxd buffer 
211
                                        // if 2nd byte is an 'R' start bootloader
211
                                        // if 2nd byte is an 'R' start bootloader
212
                                        if(pRxBuff->pData[2] == 'R' && !(FC.StatusFlags & FC_STATUS_MOTOR_RUN)) // not if the motors are running)
212
                                        if(pRxBuff->pData[2] == 'R' && !(FC.StatusFlags & FC_STATUS_MOTOR_RUN)) // not if the motors are running)
213
                                        {
213
                                        {
214
                                                VIC_DeInit();
214
                                                VIC_DeInit();
215
                                                Execute_Bootloader(); // Reset-Commando - Bootloader starten
215
                                                Execute_Bootloader(); // Reset-Commando - Bootloader starten
216
                                        }
216
                                        }
217
                                } // eof checksum valid
217
                                } // eof checksum valid
218
                                else
218
                                else
219
                                {       // checksum is invalid
219
                                {       // checksum is invalid
220
                                        Buffer_Clear(pRxBuff);
220
                                        Buffer_Clear(pRxBuff);
221
                                }  // eof checksum invalid
221
                                }  // eof checksum invalid
222
                        } // eof termination character received
222
                        } // eof termination character received
223
                } // rxd buffer not full
223
                } // rxd buffer not full
224
                else // rxd buffer overrun
224
                else // rxd buffer overrun
225
                {
225
                {
226
                        Buffer_Clear(pRxBuff);
226
                        Buffer_Clear(pRxBuff);
227
                } // eof rxd buffer overrun
227
                } // eof rxd buffer overrun
228
        }
228
        }
229
        return(pRxBuff->Locked);
229
        return(pRxBuff->Locked);
230
}
230
}
231
 
231
 
232
/**************************************************************/
232
/**************************************************************/
233
/* Decode detination address                                  */
233
/* Decode detination address                                  */
234
/**************************************************************/
234
/**************************************************************/
235
void MKProtocol_DecodeSerialFrameHeader(Buffer_t* pRxBuff, SerialMsg_t* pSerialMsg)
235
void MKProtocol_DecodeSerialFrameHeader(Buffer_t* pRxBuff, SerialMsg_t* pSerialMsg)
236
{
236
{
237
        if(pRxBuff->Locked)
237
        if(pRxBuff->Locked)
238
        {
238
        {
239
                pSerialMsg->Address = pRxBuff->pData[1] - 'a';
239
                pSerialMsg->Address = pRxBuff->pData[1] - 'a';
240
                pSerialMsg->CmdID = pRxBuff->pData[2];
240
                pSerialMsg->CmdID = pRxBuff->pData[2];
241
        }
241
        }
242
        else
242
        else
243
        {
243
        {
244
                pSerialMsg->Address = 0;
244
                pSerialMsg->Address = 0;
245
                pSerialMsg->CmdID = ' ';
245
                pSerialMsg->CmdID = ' ';
246
        }      
246
        }      
247
}
247
}
248
 
248
 
249
/**************************************************************/
249
/**************************************************************/
250
/* Decode data                                                */
250
/* Decode data                                                */
251
/**************************************************************/
251
/**************************************************************/
252
void MKProtocol_DecodeSerialFrameData(Buffer_t* pRxBuff, SerialMsg_t* pSerialMsg)
252
void MKProtocol_DecodeSerialFrameData(Buffer_t* pRxBuff, SerialMsg_t* pSerialMsg)
253
{
253
{
254
        u8 a,b,c,d;
254
        u8 a,b,c,d;
255
        u16 ptrIn = 3; // start with first data byte in rx buffer
255
        u16 ptrIn = 3; // start with first data byte in rx buffer
256
        u16 ptrOut = 3;
256
        u16 ptrOut = 3;
257
        u16 len = pRxBuff->DataBytes - 6;        // must be a multiple of 4 (3 bytes at begin and 3 bytes at end are no payload )
257
        u16 len = pRxBuff->DataBytes - 6;        // must be a multiple of 4 (3 bytes at begin and 3 bytes at end are no payload )
258
 
258
 
259
        len/=4;   // number of 4 byte blocks
259
        len/=4;   // number of 4 byte blocks
260
        while(len--)
260
        while(len--)
261
        {
261
        {
262
                a = pRxBuff->pData[ptrIn++] - '=';
262
                a = pRxBuff->pData[ptrIn++] - '=';
263
                b = pRxBuff->pData[ptrIn++] - '=';
263
                b = pRxBuff->pData[ptrIn++] - '=';
264
                c = pRxBuff->pData[ptrIn++] - '=';
264
                c = pRxBuff->pData[ptrIn++] - '=';
265
                d = pRxBuff->pData[ptrIn++] - '=';
265
                d = pRxBuff->pData[ptrIn++] - '=';
266
 
266
 
267
                pRxBuff->pData[ptrOut++] = (a << 2) | (b >> 4);
267
                pRxBuff->pData[ptrOut++] = (a << 2) | (b >> 4);
268
                pRxBuff->pData[ptrOut++] = ((b & 0x0f) << 4) | (c >> 2);
268
                pRxBuff->pData[ptrOut++] = ((b & 0x0f) << 4) | (c >> 2);
269
                pRxBuff->pData[ptrOut++] = ((c & 0x03) << 6) | d;
269
                pRxBuff->pData[ptrOut++] = ((c & 0x03) << 6) | d;
270
        }
270
        }
271
        pSerialMsg->pData = &(pRxBuff->pData[3]);
271
        pSerialMsg->pData = &(pRxBuff->pData[3]);
272
        pSerialMsg->DataLen = ptrOut - 3;       // return number of data in bytes
272
        pSerialMsg->DataLen = ptrOut - 3;       // return number of data in bytes
273
        pRxBuff->Position = 0;
273
        pRxBuff->Position = 0;
274
        pRxBuff->DataBytes = ptrOut;
274
        pRxBuff->DataBytes = ptrOut;
275
}
275
}
276
 
276
 
277
/*
277
/*
278
void MKProtocol_DecodeSerialFrameData(Buffer_t* pRxBuff, SerialMsg_t* pSerialMsg)
278
void MKProtocol_DecodeSerialFrameData(Buffer_t* pRxBuff, SerialMsg_t* pSerialMsg)
279
{
279
{
280
        u8 a,b,c,d;
280
        u8 a,b,c,d;
281
        u8 x,y,z;
281
        u8 x,y,z;
282
        u16 ptrIn = 3; // start with first data byte in rx buffer
282
        u16 ptrIn = 3; // start with first data byte in rx buffer
283
        u16 ptrOut = 3;
283
        u16 ptrOut = 3;
284
        u16 len = pRxBuff->DataBytes - 6;        // must be a multiple of 4 (3 bytes at begin and 3 bytes at end are no payload )
284
        u16 len = pRxBuff->DataBytes - 6;        // must be a multiple of 4 (3 bytes at begin and 3 bytes at end are no payload )
285
        while(len)
285
        while(len)
286
        {
286
        {
287
                a = pRxBuff->pData[ptrIn++] - '=';
287
                a = pRxBuff->pData[ptrIn++] - '=';
288
                b = pRxBuff->pData[ptrIn++] - '=';
288
                b = pRxBuff->pData[ptrIn++] - '=';
289
                c = pRxBuff->pData[ptrIn++] - '=';
289
                c = pRxBuff->pData[ptrIn++] - '=';
290
                d = pRxBuff->pData[ptrIn++] - '=';
290
                d = pRxBuff->pData[ptrIn++] - '=';
291
                //if(ptrIn > ReceivedBytes - 3) break;
291
                //if(ptrIn > ReceivedBytes - 3) break;
292
 
292
 
293
                x = (a << 2) | (b >> 4);
293
                x = (a << 2) | (b >> 4);
294
                y = ((b & 0x0f) << 4) | (c >> 2);
294
                y = ((b & 0x0f) << 4) | (c >> 2);
295
                z = ((c & 0x03) << 6) | d;
295
                z = ((c & 0x03) << 6) | d;
296
 
296
 
297
                if(len--) pRxBuff->pData[ptrOut++] = x; else break;
297
                if(len--) pRxBuff->pData[ptrOut++] = x; else break;
298
                if(len--) pRxBuff->pData[ptrOut++] = y; else break;
298
                if(len--) pRxBuff->pData[ptrOut++] = y; else break;
299
                if(len--) pRxBuff->pData[ptrOut++] = z; else break;
299
                if(len--) pRxBuff->pData[ptrOut++] = z; else break;
300
        }
300
        }
301
        pSerialMsg->pData = &(pRxBuff->pData[3]);
301
        pSerialMsg->pData = &(pRxBuff->pData[3]);
302
        pSerialMsg->DataLen = ptrOut - 3;       // return number of data in bytes
302
        pSerialMsg->DataLen = ptrOut - 3;       // return number of data in bytes
303
        pRxBuff->Position = 0;
303
        pRxBuff->Position = 0;
304
        pRxBuff->DataBytes = ptrOut;
304
        pRxBuff->DataBytes = ptrOut;
305
}
305
}
306
*/
306
*/
307
 
307
 
308
 
308