Subversion Repositories NaviCtrl

Rev

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

Rev 349 Rev 360
1
/*#######################################################################################*/
1
/*#######################################################################################*/
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
3
/*#######################################################################################*/
3
/*#######################################################################################*/
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// + Copyright (c) 2008 Ingo Busker, Holger Buss
-
 
6
// + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY
-
 
7
// + FOR NON COMMERCIAL USE ONLY
-
 
8
// + www.MikroKopter.com
5
// + www.MikroKopter.com
9
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
7
// + Software Nutzungsbedingungen (english version: see below)
10
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
8
// + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt -
11
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist.
9
// + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den
12
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
10
// + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool 
13
// + bzgl. der Nutzungsbedingungen aufzunehmen.
11
// + - nachfolgend Software genannt - nur für private Zwecke zu nutzen.
14
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
12
// + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig.
15
// + Verkauf von Luftbildaufnahmen, usw.
-
 
16
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
13
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
17
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
14
// + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im
18
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
15
// + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu.
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
16
// + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie
20
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
17
// + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden.
21
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
18
// + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren
22
// + eindeutig als Ursprung verlinkt werden
19
// + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
20
// + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren
24
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
21
// + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand 
25
// + Benutzung auf eigene Gefahr
22
// + des Mitverschuldens offen.
-
 
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.
26
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
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
27
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
27
// + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt.
28
// + Die Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
28
// + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software.
29
// + mit unserer Zustimmung zulässig
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
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
32
// + Software LICENSING TERMS
32
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
33
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
33
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
34
// + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor -
34
// + this list of conditions and the following disclaimer.
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
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
36
// + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*.
36
// +     from this software without specific prior written permission.
37
// + The Software may only be used with the Licensor's products.
37
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permitted
38
// + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this
38
// +     for non-commercial use (directly or indirectly)
-
 
39
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
39
// + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this
40
// +     with our written permission
40
// + agreement shall be the property of the Licensor.
41
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
41
// + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other
42
// +     clearly linked as origin
-
 
43
// +   * porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed
42
// + features that can be used to identify the program may not be altered or defaced by the customer.
44
//
-
 
45
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
43
// + The customer shall be responsible for taking reasonable precautions
46
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44
// + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the
47
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45
// + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and
48
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
46
// + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product
49
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
47
// + liability. However, the Licensor shall be entitled to the defense of contributory negligence.
50
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
48
// + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test
51
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
49
// + the software for his purpose before any operational usage. The customer will backup his data before using the software.
52
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
50
// + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data
53
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
51
// + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations.
54
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
52
// + *) The territory aspect only refers to the place where the Software is used, not its programmed range.
55
// +  POSSIBILITY OF SUCH DAMAGE.
53
// + #### END OF LICENSING TERMS ####
-
 
54
// + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de.
56
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
55
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
57
#include <stdarg.h>
56
#include <stdarg.h>
58
#include "91x_lib.h"
57
#include "91x_lib.h"
59
#include "mkprotocol.h" 
58
#include "mkprotocol.h" 
60
#include "ramfunc.h"
59
#include "ramfunc.h"
61
#include "usb.h"
60
#include "usb.h"
62
 
61
 
63
/**************************************************************/
62
/**************************************************************/
64
/* Create serial output frame                                 */
63
/* Create serial output frame                                 */
65
/**************************************************************/
64
/**************************************************************/
66
u8 MKProtocol_CreateSerialFrame(Buffer_t* pTxBuff, u8 CmdID, u8 Address, u8 numofbuffers , ...) //u8 *data, u8 len, ....
65
u8 MKProtocol_CreateSerialFrame(Buffer_t* pTxBuff, u8 CmdID, u8 Address, u8 numofbuffers , ...) //u8 *data, u8 len, ....
67
{
66
{
68
        va_list ap;
67
        va_list ap;
69
 
68
 
70
        u8 a,b,c;
69
        u8 a,b,c;
71
        u16 ptr = 0;
70
        u16 ptr = 0;
72
        u16 tmpCRC = 0, i;
71
        u16 tmpCRC = 0, i;
73
 
72
 
74
        u8* pdata = NULL;
73
        u8* pdata = NULL;
75
        int len = 0;
74
        int len = 0;
76
       
75
       
77
        if(pTxBuff->Locked == TRUE) return(0);
76
        if(pTxBuff->Locked == TRUE) return(0);
78
 
77
 
79
        // tx-buffer is not in use
78
        // tx-buffer is not in use
80
        // lock the buffer
79
        // lock the buffer
81
        pTxBuff->Locked = TRUE;
80
        pTxBuff->Locked = TRUE;
82
        pTxBuff->Position = 0;
81
        pTxBuff->Position = 0;
83
        pTxBuff->pData[pTxBuff->Position++] = '#';                          // Start character
82
        pTxBuff->pData[pTxBuff->Position++] = '#';                          // Start character
84
        pTxBuff->pData[pTxBuff->Position++] = 'a' + Address;    // Address (a=0; b=1,...)
83
        pTxBuff->pData[pTxBuff->Position++] = 'a' + Address;    // Address (a=0; b=1,...)
85
        pTxBuff->pData[pTxBuff->Position++] = CmdID;                    // Command
84
        pTxBuff->pData[pTxBuff->Position++] = CmdID;                    // Command
86
 
85
 
87
        va_start(ap, numofbuffers);
86
        va_start(ap, numofbuffers);
88
        if(numofbuffers)
87
        if(numofbuffers)
89
        {
88
        {
90
                pdata = va_arg(ap, u8*);
89
                pdata = va_arg(ap, u8*);
91
                len = va_arg(ap, int);
90
                len = va_arg(ap, int);
92
                ptr = 0;
91
                ptr = 0;
93
                numofbuffers--;
92
                numofbuffers--;
94
        }
93
        }
95
        while(len)
94
        while(len)
96
        {
95
        {
97
                if(len)
96
                if(len)
98
                {
97
                {
99
                        a = pdata[ptr++];
98
                        a = pdata[ptr++];
100
                        len--;
99
                        len--;
101
                        if((!len) && numofbuffers) // try to jump to next buffer
100
                        if((!len) && numofbuffers) // try to jump to next buffer
102
                        {
101
                        {
103
                                pdata = va_arg(ap, u8*);
102
                                pdata = va_arg(ap, u8*);
104
                                len = va_arg(ap, int);
103
                                len = va_arg(ap, int);
105
                                ptr = 0;
104
                                ptr = 0;
106
                                numofbuffers--;
105
                                numofbuffers--;
107
                        }
106
                        }
108
                }
107
                }
109
                else a = 0;
108
                else a = 0;
110
                if(len)
109
                if(len)
111
                {
110
                {
112
                        b = pdata[ptr++];
111
                        b = pdata[ptr++];
113
                        len--;
112
                        len--;
114
                        if((!len) && numofbuffers) // try to jump to next buffer
113
                        if((!len) && numofbuffers) // try to jump to next buffer
115
                        {
114
                        {
116
                                pdata = va_arg(ap, u8*);
115
                                pdata = va_arg(ap, u8*);
117
                                len = va_arg(ap, int);
116
                                len = va_arg(ap, int);
118
                                ptr = 0;
117
                                ptr = 0;
119
                                numofbuffers--;
118
                                numofbuffers--;
120
                        }
119
                        }
121
                }
120
                }
122
                else b = 0;
121
                else b = 0;
123
                if(len)
122
                if(len)
124
                {
123
                {
125
                        c = pdata[ptr++];
124
                        c = pdata[ptr++];
126
                        len--;
125
                        len--;
127
                        if((!len) && numofbuffers) // try to jump to next buffer
126
                        if((!len) && numofbuffers) // try to jump to next buffer
128
                        {
127
                        {
129
                                pdata = va_arg(ap, u8*);
128
                                pdata = va_arg(ap, u8*);
130
                                len = va_arg(ap, int);
129
                                len = va_arg(ap, int);
131
                                ptr = 0;
130
                                ptr = 0;
132
                                numofbuffers--;
131
                                numofbuffers--;
133
                        }
132
                        }
134
                }
133
                }
135
                else c = 0;
134
                else c = 0;
136
                pTxBuff->pData[pTxBuff->Position++] = '=' + (a >> 2);
135
                pTxBuff->pData[pTxBuff->Position++] = '=' + (a >> 2);
137
                pTxBuff->pData[pTxBuff->Position++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4));
136
                pTxBuff->pData[pTxBuff->Position++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4));
138
                pTxBuff->pData[pTxBuff->Position++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6));
137
                pTxBuff->pData[pTxBuff->Position++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6));
139
                pTxBuff->pData[pTxBuff->Position++] = '=' + ( c & 0x3f);
138
                pTxBuff->pData[pTxBuff->Position++] = '=' + ( c & 0x3f);
140
        }
139
        }
141
        va_end(ap);
140
        va_end(ap);
142
        // add crc
141
        // add crc
143
        for(i = 0; i < pTxBuff->Position; i++)
142
        for(i = 0; i < pTxBuff->Position; i++)
144
        {
143
        {
145
                tmpCRC += pTxBuff->pData[i];
144
                tmpCRC += pTxBuff->pData[i];
146
        }
145
        }
147
        tmpCRC %= 4096;
146
        tmpCRC %= 4096;
148
        pTxBuff->pData[pTxBuff->Position++] = '=' + tmpCRC / 64;
147
        pTxBuff->pData[pTxBuff->Position++] = '=' + tmpCRC / 64;
149
        pTxBuff->pData[pTxBuff->Position++] = '=' + tmpCRC % 64;
148
        pTxBuff->pData[pTxBuff->Position++] = '=' + tmpCRC % 64;
150
        pTxBuff->pData[pTxBuff->Position++] = '\r';
149
        pTxBuff->pData[pTxBuff->Position++] = '\r';
151
        pTxBuff->DataBytes = pTxBuff->Position;
150
        pTxBuff->DataBytes = pTxBuff->Position;
152
        pTxBuff->Position = 0;  // reset buffer position for transmision
151
        pTxBuff->Position = 0;  // reset buffer position for transmision
153
        return(pTxBuff->Locked);
152
        return(pTxBuff->Locked);
154
}
153
}
155
 
154
 
156
// typical called in an UART Rx ISR
155
// typical called in an UART Rx ISR
157
/**************************************************************/
156
/**************************************************************/
158
/* Collect serial frame                                       */
157
/* Collect serial frame                                       */
159
/**************************************************************/
158
/**************************************************************/
160
u8 MKProtocol_CollectSerialFrame(Buffer_t* pRxBuff, u8 c)
159
u8 MKProtocol_CollectSerialFrame(Buffer_t* pRxBuff, u8 c)
161
{
160
{
162
        if(pRxBuff->Locked == FALSE)
161
        if(pRxBuff->Locked == FALSE)
163
        { // rx buffer not locked
162
        { // rx buffer not locked
164
                if(c == '#') // if syncronisation character is received
163
                if(c == '#') // if syncronisation character is received
165
                {
164
                {
166
                        pRxBuff->Position = 0;                                          // reset buffer
165
                        pRxBuff->Position = 0;                                          // reset buffer
167
                        pRxBuff->pData[pRxBuff->Position++] = c;        // copy 1st byte to buffer
166
                        pRxBuff->pData[pRxBuff->Position++] = c;        // copy 1st byte to buffer
168
                        pRxBuff->DataBytes = 1;
167
                        pRxBuff->DataBytes = 1;
169
                }
168
                }
170
                else if (pRxBuff->Position < pRxBuff->Size) // rx buffer not full
169
                else if (pRxBuff->Position < pRxBuff->Size) // rx buffer not full
171
                {
170
                {
172
                        pRxBuff->pData[pRxBuff->Position++] = c; // copy byte to rxd buffer
171
                        pRxBuff->pData[pRxBuff->Position++] = c; // copy byte to rxd buffer
173
                        pRxBuff->DataBytes++;
172
                        pRxBuff->DataBytes++;
174
                        // termination character received and sync has been established
173
                        // termination character received and sync has been established
175
                        if ((c == '\r') && (pRxBuff->pData[0]== '#'))
174
                        if ((c == '\r') && (pRxBuff->pData[0]== '#'))
176
                        {
175
                        {
177
                                // calculate checksum from transmitted data
176
                                // calculate checksum from transmitted data
178
                                u16 crc = 0, i;
177
                                u16 crc = 0, i;
179
                                u8 crc1, crc2;
178
                                u8 crc1, crc2;
180
                                for(i = 0; i < (pRxBuff->Position-3); i++)
179
                                for(i = 0; i < (pRxBuff->Position-3); i++)
181
                                {
180
                                {
182
                                        crc +=  pRxBuff->pData[i];  
181
                                        crc +=  pRxBuff->pData[i];  
183
                                }              
182
                                }              
184
                                crc %= 4096;
183
                                crc %= 4096;
185
                                crc1 = '=' + crc / 64;
184
                                crc1 = '=' + crc / 64;
186
                                crc2 = '=' + crc % 64;
185
                                crc2 = '=' + crc % 64;
187
                                // compare checksum to transmitted checksum bytes
186
                                // compare checksum to transmitted checksum bytes
188
                                if((crc1 == pRxBuff->pData[pRxBuff->Position-3]) && (crc2 == pRxBuff->pData[pRxBuff->Position-2]))
187
                                if((crc1 == pRxBuff->pData[pRxBuff->Position-3]) && (crc2 == pRxBuff->pData[pRxBuff->Position-2]))
189
                                {
188
                                {
190
                                    // checksum is valid
189
                                    // checksum is valid
191
                                        pRxBuff->Position = 0;                                 
190
                                        pRxBuff->Position = 0;                                 
192
                                        pRxBuff->Locked = TRUE;                                     // lock the rxd buffer 
191
                                        pRxBuff->Locked = TRUE;                                     // lock the rxd buffer 
193
                                        // if 2nd byte is an 'R' start bootloader
192
                                        // if 2nd byte is an 'R' start bootloader
194
                                        if(pRxBuff->pData[2] == 'R')
193
                                        if(pRxBuff->pData[2] == 'R')
195
                                        {
194
                                        {
196
                                                PowerOff();
195
                                                PowerOff();
197
                                                VIC_DeInit();
196
                                                VIC_DeInit();
198
                                                Execute_Bootloader(); // Reset-Commando - Bootloader starten
197
                                                Execute_Bootloader(); // Reset-Commando - Bootloader starten
199
                                        }
198
                                        }
200
                                } // eof checksum valid
199
                                } // eof checksum valid
201
                                else
200
                                else
202
                                {       // checksum is invalid
201
                                {       // checksum is invalid
203
                                        Buffer_Clear(pRxBuff);
202
                                        Buffer_Clear(pRxBuff);
204
                                }  // eof checksum invalid
203
                                }  // eof checksum invalid
205
                        } // eof termination character received
204
                        } // eof termination character received
206
                } // rxd buffer not full
205
                } // rxd buffer not full
207
                else // rxd buffer overrun
206
                else // rxd buffer overrun
208
                {
207
                {
209
                        Buffer_Clear(pRxBuff);
208
                        Buffer_Clear(pRxBuff);
210
                } // eof rxd buffer overrun
209
                } // eof rxd buffer overrun
211
        }
210
        }
212
        return(pRxBuff->Locked);
211
        return(pRxBuff->Locked);
213
}
212
}
214
 
213
 
215
/**************************************************************/
214
/**************************************************************/
216
/* Decode detination address                                  */
215
/* Decode detination address                                  */
217
/**************************************************************/
216
/**************************************************************/
218
void MKProtocol_DecodeSerialFrameHeader(Buffer_t* pRxBuff, SerialMsg_t* pSerialMsg)
217
void MKProtocol_DecodeSerialFrameHeader(Buffer_t* pRxBuff, SerialMsg_t* pSerialMsg)
219
{
218
{
220
        if(pRxBuff->Locked)
219
        if(pRxBuff->Locked)
221
        {
220
        {
222
                pSerialMsg->Address = pRxBuff->pData[1] - 'a';
221
                pSerialMsg->Address = pRxBuff->pData[1] - 'a';
223
                pSerialMsg->CmdID = pRxBuff->pData[2];
222
                pSerialMsg->CmdID = pRxBuff->pData[2];
224
        }
223
        }
225
        else
224
        else
226
        {
225
        {
227
                pSerialMsg->Address = 0;
226
                pSerialMsg->Address = 0;
228
                pSerialMsg->CmdID = ' ';
227
                pSerialMsg->CmdID = ' ';
229
        }      
228
        }      
230
}
229
}
231
 
230
 
232
/**************************************************************/
231
/**************************************************************/
233
/* Decode data                                                */
232
/* Decode data                                                */
234
/**************************************************************/
233
/**************************************************************/
235
void MKProtocol_DecodeSerialFrameData(Buffer_t* pRxBuff, SerialMsg_t* pSerialMsg)
234
void MKProtocol_DecodeSerialFrameData(Buffer_t* pRxBuff, SerialMsg_t* pSerialMsg)
236
{
235
{
237
        u8 a,b,c,d;
236
        u8 a,b,c,d;
238
        u8 x,y,z;
237
        u8 x,y,z;
239
        u8 ptrIn = 3; // start with first data byte in rx buffer
238
        u8 ptrIn = 3; // start with first data byte in rx buffer
240
        u8 ptrOut = 3;
239
        u8 ptrOut = 3;
241
        u8 len = pRxBuff->DataBytes - 6;         // must be a multiple of 4 (3 bytes at begin and 3 bytes at end are no payload )
240
        u8 len = pRxBuff->DataBytes - 6;         // must be a multiple of 4 (3 bytes at begin and 3 bytes at end are no payload )
242
        while(len)
241
        while(len)
243
        {
242
        {
244
                a = pRxBuff->pData[ptrIn++] - '=';
243
                a = pRxBuff->pData[ptrIn++] - '=';
245
                b = pRxBuff->pData[ptrIn++] - '=';
244
                b = pRxBuff->pData[ptrIn++] - '=';
246
                c = pRxBuff->pData[ptrIn++] - '=';
245
                c = pRxBuff->pData[ptrIn++] - '=';
247
                d = pRxBuff->pData[ptrIn++] - '=';
246
                d = pRxBuff->pData[ptrIn++] - '=';
248
                //if(ptrIn > ReceivedBytes - 3) break;
247
                //if(ptrIn > ReceivedBytes - 3) break;
249
 
248
 
250
                x = (a << 2) | (b >> 4);
249
                x = (a << 2) | (b >> 4);
251
                y = ((b & 0x0f) << 4) | (c >> 2);
250
                y = ((b & 0x0f) << 4) | (c >> 2);
252
                z = ((c & 0x03) << 6) | d;
251
                z = ((c & 0x03) << 6) | d;
253
 
252
 
254
                if(len--) pRxBuff->pData[ptrOut++] = x; else break;
253
                if(len--) pRxBuff->pData[ptrOut++] = x; else break;
255
                if(len--) pRxBuff->pData[ptrOut++] = y; else break;
254
                if(len--) pRxBuff->pData[ptrOut++] = y; else break;
256
                if(len--) pRxBuff->pData[ptrOut++] = z; else break;
255
                if(len--) pRxBuff->pData[ptrOut++] = z; else break;
257
        }
256
        }
258
        pSerialMsg->pData = &(pRxBuff->pData[3]);
257
        pSerialMsg->pData = &(pRxBuff->pData[3]);
259
        pSerialMsg->DataLen = ptrOut - 3;       // return number of data in bytes
258
        pSerialMsg->DataLen = ptrOut - 3;       // return number of data in bytes
260
        pRxBuff->Position = 0;
259
        pRxBuff->Position = 0;
261
        pRxBuff->DataBytes = ptrOut;
260
        pRxBuff->DataBytes = ptrOut;
262
}
261
}
263
 
262