Subversion Repositories NaviCtrl

Rev

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

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