Rev 110 | Rev 113 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 110 | Rev 112 | ||
---|---|---|---|
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 |
5 | // + Copyright (c) 2008 Ingo Busker, Holger Buss |
6 | // + Nur für den privaten Gebrauch |
6 | // + Nur für den privaten Gebrauch |
7 | // + FOR NON COMMERCIAL USE ONLY |
7 | // + FOR NON COMMERCIAL USE ONLY |
8 | // + www.MikroKopter.com |
8 | // + www.MikroKopter.com |
9 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
9 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
10 | // + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), |
10 | // + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), |
11 | // + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist. |
11 | // + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist. |
12 | // + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt |
12 | // + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt |
13 | // + bzgl. der Nutzungsbedingungen aufzunehmen. |
13 | // + bzgl. der Nutzungsbedingungen aufzunehmen. |
14 | // + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen, |
14 | // + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen, |
15 | // + Verkauf von Luftbildaufnahmen, usw. |
15 | // + Verkauf von Luftbildaufnahmen, usw. |
16 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
16 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
17 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
17 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
18 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen |
18 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen |
19 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
19 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
20 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
20 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
21 | // + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
21 | // + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
22 | // + eindeutig als Ursprung verlinkt werden |
22 | // + eindeutig als Ursprung verlinkt werden |
23 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
23 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
24 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
24 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
25 | // + Benutzung auf eigene Gefahr |
25 | // + Benutzung auf eigene Gefahr |
26 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
26 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
27 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
27 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
28 | // + Die PORTIERUNG der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
28 | // + Die PORTIERUNG der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
29 | // + mit unserer Zustimmung zulässig |
29 | // + mit unserer Zustimmung zulässig |
30 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
30 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
31 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
31 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
32 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
32 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
33 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
33 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
34 | // + this list of conditions and the following disclaimer. |
34 | // + this list of conditions and the following disclaimer. |
35 | // + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived |
35 | // + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived |
36 | // + from this software without specific prior written permission. |
36 | // + from this software without specific prior written permission. |
37 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permitted |
37 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permitted |
38 | // + for non-commercial use (directly or indirectly) |
38 | // + for non-commercial use (directly or indirectly) |
39 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
39 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
40 | // + with our written permission |
40 | // + with our written permission |
41 | // + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
41 | // + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
42 | // + clearly linked as origin |
42 | // + clearly linked as origin |
43 | // + * PORTING this software (or part of it) to systems (other than hardware from www.mikrokopter.de) is NOT allowed |
43 | // + * PORTING this software (or part of it) to systems (other than hardware from www.mikrokopter.de) is NOT allowed |
44 | // |
44 | // |
45 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
45 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
46 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
46 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
47 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
47 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
48 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
48 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
49 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
49 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
50 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
50 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
51 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
51 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
52 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
52 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
53 | // + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
53 | // + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
54 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
54 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
55 | // + POSSIBILITY OF SUCH DAMAGE. |
55 | // + POSSIBILITY OF SUCH DAMAGE. |
56 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
56 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
57 | #include <string.h> |
57 | #include <string.h> |
58 | #include <stdarg.h> |
58 | #include <stdarg.h> |
59 | #include "91x_lib.h" |
59 | #include "91x_lib.h" |
60 | #include "mkprotocol.h" |
60 | #include "mkprotocol.h" |
61 | #include "ramfunc.h" |
61 | #include "ramfunc.h" |
62 | #include "usb.h" |
62 | #include "usb.h" |
- | 63 | ||
- | 64 | void Buffer_Clear(Buffer_t* pBuffer) |
|
- | 65 | { |
|
- | 66 | pBuffer->DataBytes = 0; |
|
- | 67 | pBuffer->Position = 0; |
|
- | 68 | pBuffer->Locked = FALSE; |
|
- | 69 | } |
|
- | 70 | ||
- | 71 | void Buffer_Init(Buffer_t* pBuffer, u8* pDataBuffer, u16 DataBufferSize) |
|
- | 72 | { |
|
- | 73 | pBuffer->pData = pDataBuffer; |
|
- | 74 | pBuffer->Size = DataBufferSize; |
|
- | 75 | Buffer_Clear(pBuffer); |
|
63 | 76 | } |
|
64 | 77 | ||
65 | /**************************************************************/ |
78 | /**************************************************************/ |
66 | /* Create serial output frame */ |
79 | /* Create serial output frame */ |
67 | /**************************************************************/ |
80 | /**************************************************************/ |
68 | void MKProtocol_CreateSerialFrame(Buffer_t* pTxBuff, u8 CmdID, u8 Address, u8 numofbuffers , ...) //u8 *data, u8 len, .... |
81 | void MKProtocol_CreateSerialFrame(Buffer_t* pTxBuff, u8 CmdID, u8 Address, u8 numofbuffers , ...) //u8 *data, u8 len, .... |
69 | { |
82 | { |
70 | va_list ap; |
83 | va_list ap; |
71 | 84 | ||
72 | u8 a,b,c; |
85 | u8 a,b,c; |
73 | u8 ptr = 0; |
86 | u8 ptr = 0; |
74 | u16 tmpCRC = 0, i; |
87 | u16 tmpCRC = 0, i; |
75 | 88 | ||
76 | u8* pdata = NULL; |
89 | u8* pdata = NULL; |
77 | int len = 0; |
90 | int len = 0; |
78 | 91 | ||
79 | if(pTxBuff->Locked == FALSE) |
92 | if(pTxBuff->Locked == FALSE) |
80 | { // tx-buffer is not in use |
93 | { // tx-buffer is not in use |
81 | pTxBuff->Position = 0; |
94 | pTxBuff->Position = 0; |
82 | pTxBuff->pData[pTxBuff->Position++] = '#'; // Start character |
95 | pTxBuff->pData[pTxBuff->Position++] = '#'; // Start character |
83 | pTxBuff->pData[pTxBuff->Position++] = 'a' + Address; // Address (a=0; b=1,...) |
96 | pTxBuff->pData[pTxBuff->Position++] = 'a' + Address; // Address (a=0; b=1,...) |
84 | pTxBuff->pData[pTxBuff->Position++] = CmdID; // Command |
97 | pTxBuff->pData[pTxBuff->Position++] = CmdID; // Command |
85 | 98 | ||
86 | va_start(ap, numofbuffers); |
99 | va_start(ap, numofbuffers); |
87 | if(numofbuffers) |
100 | if(numofbuffers) |
88 | { |
101 | { |
89 | pdata = va_arg(ap, u8*); |
102 | pdata = va_arg(ap, u8*); |
90 | len = va_arg(ap, int); |
103 | len = va_arg(ap, int); |
91 | ptr = 0; |
104 | ptr = 0; |
92 | numofbuffers--; |
105 | numofbuffers--; |
93 | } |
106 | } |
94 | while(len) |
107 | while(len) |
95 | { |
108 | { |
96 | if(len) |
109 | if(len) |
97 | { |
110 | { |
98 | a = pdata[ptr++]; |
111 | a = pdata[ptr++]; |
99 | len--; |
112 | len--; |
100 | if((!len) && numofbuffers) // try to jump to next buffer |
113 | if((!len) && numofbuffers) // try to jump to next buffer |
101 | { |
114 | { |
102 | pdata = va_arg(ap, u8*); |
115 | pdata = va_arg(ap, u8*); |
103 | len = va_arg(ap, int); |
116 | len = va_arg(ap, int); |
104 | ptr = 0; |
117 | ptr = 0; |
105 | numofbuffers--; |
118 | numofbuffers--; |
106 | } |
119 | } |
107 | } |
120 | } |
108 | else a = 0; |
121 | else a = 0; |
109 | if(len) |
122 | if(len) |
110 | { |
123 | { |
111 | b = pdata[ptr++]; |
124 | b = pdata[ptr++]; |
112 | len--; |
125 | len--; |
113 | if((!len) && numofbuffers) // try to jump to next buffer |
126 | if((!len) && numofbuffers) // try to jump to next buffer |
114 | { |
127 | { |
115 | pdata = va_arg(ap, u8*); |
128 | pdata = va_arg(ap, u8*); |
116 | len = va_arg(ap, int); |
129 | len = va_arg(ap, int); |
117 | ptr = 0; |
130 | ptr = 0; |
118 | numofbuffers--; |
131 | numofbuffers--; |
119 | } |
132 | } |
120 | } |
133 | } |
121 | else b = 0; |
134 | else b = 0; |
122 | if(len) |
135 | if(len) |
123 | { |
136 | { |
124 | c = pdata[ptr++]; |
137 | c = pdata[ptr++]; |
125 | len--; |
138 | len--; |
126 | if((!len) && numofbuffers) // try to jump to next buffer |
139 | if((!len) && numofbuffers) // try to jump to next buffer |
127 | { |
140 | { |
128 | pdata = va_arg(ap, u8*); |
141 | pdata = va_arg(ap, u8*); |
129 | len = va_arg(ap, int); |
142 | len = va_arg(ap, int); |
130 | ptr = 0; |
143 | ptr = 0; |
131 | numofbuffers--; |
144 | numofbuffers--; |
132 | } |
145 | } |
133 | } |
146 | } |
134 | else c = 0; |
147 | else c = 0; |
135 | pTxBuff->pData[pTxBuff->Position++] = '=' + (a >> 2); |
148 | pTxBuff->pData[pTxBuff->Position++] = '=' + (a >> 2); |
136 | pTxBuff->pData[pTxBuff->Position++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4)); |
149 | pTxBuff->pData[pTxBuff->Position++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4)); |
137 | pTxBuff->pData[pTxBuff->Position++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6)); |
150 | pTxBuff->pData[pTxBuff->Position++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6)); |
138 | pTxBuff->pData[pTxBuff->Position++] = '=' + ( c & 0x3f); |
151 | pTxBuff->pData[pTxBuff->Position++] = '=' + ( c & 0x3f); |
139 | } |
152 | } |
140 | va_end(ap); |
153 | va_end(ap); |
141 | // add crc |
154 | // add crc |
142 | for(i = 0; i < pTxBuff->Position; i++) |
155 | for(i = 0; i < pTxBuff->Position; i++) |
143 | { |
156 | { |
144 | tmpCRC += pTxBuff->pData[i]; |
157 | tmpCRC += pTxBuff->pData[i]; |
145 | } |
158 | } |
146 | tmpCRC %= 4096; |
159 | tmpCRC %= 4096; |
147 | pTxBuff->pData[pTxBuff->Position++] = '=' + tmpCRC / 64; |
160 | pTxBuff->pData[pTxBuff->Position++] = '=' + tmpCRC / 64; |
148 | pTxBuff->pData[pTxBuff->Position++] = '=' + tmpCRC % 64; |
161 | pTxBuff->pData[pTxBuff->Position++] = '=' + tmpCRC % 64; |
149 | pTxBuff->pData[pTxBuff->Position++] = '\r'; |
162 | pTxBuff->pData[pTxBuff->Position++] = '\r'; |
- | 163 | pTxBuff->DataBytes = pTxBuff->Position; |
|
150 | pTxBuff->Position = 0; // reset buffer position for transmision |
164 | pTxBuff->Position = 0; // reset buffer position for transmision |
151 | pTxBuff->Locked = TRUE; // mark as used |
165 | pTxBuff->Locked = TRUE; // mark as used |
152 | } // EOF buffer not in use |
166 | } // EOF buffer not in use |
153 | } |
167 | } |
154 | 168 | ||
155 | // typical called in an UART Rx ISR |
169 | // typical called in an UART Rx ISR |
156 | /**************************************************************/ |
170 | /**************************************************************/ |
157 | /* Collect serial frame */ |
171 | /* Collect serial frame */ |
158 | /**************************************************************/ |
172 | /**************************************************************/ |
159 | void MKProtocol_CollectSerialFrame(Buffer_t* pRxBuff, u8 c) |
173 | void MKProtocol_CollectSerialFrame(Buffer_t* pRxBuff, u8 c) |
160 | { |
174 | { |
161 | if(pRxBuff->Locked == FALSE) |
175 | if(pRxBuff->Locked == FALSE) |
162 | { // rx buffer not locked |
176 | { // rx buffer not locked |
163 | if((pRxBuff->Position == 0) && (c == '#')) // if rxd buffer is empty and syncronisation character is received |
177 | if((pRxBuff->Position == 0) && (c == '#')) // if rxd buffer is empty and syncronisation character is received |
164 | { |
178 | { |
165 | pRxBuff->pData[pRxBuff->Position++] = c; // copy 1st byte to buffer |
179 | pRxBuff->pData[pRxBuff->Position++] = c; // copy 1st byte to buffer |
166 | //crc = c; // init crc |
180 | pRxBuff->DataBytes = 1; |
167 | } |
181 | } |
168 | else if (pRxBuff->Position < pRxBuff->Size) // rx buffer not full |
182 | else if (pRxBuff->Position < pRxBuff->Size) // rx buffer not full |
169 | { |
183 | { |
170 | if (c != '\r') // no termination character received |
184 | if (c != '\r') // no termination character received |
171 | { |
185 | { |
172 | pRxBuff->pData[pRxBuff->Position++] = c; // copy byte to rxd buffer |
186 | pRxBuff->pData[pRxBuff->Position++] = c; // copy byte to rxd buffer |
173 | //crc += c; // update crc |
187 | pRxBuff->DataBytes++; |
174 | } |
188 | } |
175 | else // termination character received |
189 | else // termination character received |
176 | { |
190 | { |
177 | // calculate checksum from transmitted data |
191 | // calculate checksum from transmitted data |
178 | u16 crc = 0, i; |
192 | u16 crc = 0, i; |
179 | u8 crc1, crc2; |
193 | u8 crc1, crc2; |
180 | for(i = 0; i < (pRxBuff->Position-2); i++) |
194 | for(i = 0; i < (pRxBuff->Position-2); i++) |
181 | { |
195 | { |
182 | crc += pRxBuff->pData[i]; |
196 | crc += pRxBuff->pData[i]; |
183 | } |
197 | } |
184 | crc %= 4096; |
198 | crc %= 4096; |
185 | crc1 = '=' + crc / 64; |
199 | crc1 = '=' + crc / 64; |
186 | crc2 = '=' + crc % 64; |
200 | crc2 = '=' + crc % 64; |
187 | // compare checksum to transmitted checksum bytes |
201 | // compare checksum to transmitted checksum bytes |
188 | if((crc1 == pRxBuff->pData[pRxBuff->Position-2]) && (crc2 == pRxBuff->pData[pRxBuff->Position-1])) |
202 | if((crc1 == pRxBuff->pData[pRxBuff->Position-2]) && (crc2 == pRxBuff->pData[pRxBuff->Position-1])) |
189 | { // checksum is valid |
203 | { // checksum is valid |
190 | pRxBuff->Locked = TRUE; // lock the rxd buffer |
204 | pRxBuff->Locked = TRUE; // lock the rxd buffer |
191 | pRxBuff->pData[pRxBuff->Position] = '\r'; // set termination character |
205 | pRxBuff->pData[pRxBuff->Position++] = '\r'; // set termination character |
- | 206 | pRxBuff->DataBytes = pRxBuff->Position; |
|
- | 207 | pRxBuff->Position = 0; |
|
192 | // if 2nd byte is an 'R' start bootloader |
208 | // if 2nd byte is an 'R' start bootloader |
193 | if(pRxBuff->pData[2] == 'R') |
209 | if(pRxBuff->pData[2] == 'R') |
194 | { |
210 | { |
195 | PowerOff(); |
211 | PowerOff(); |
196 | VIC_DeInit(); |
212 | VIC_DeInit(); |
197 | Execute_Bootloader(); // Reset-Commando - Bootloader starten |
213 | Execute_Bootloader(); // Reset-Commando - Bootloader starten |
198 | } |
214 | } |
199 | } // eof checksum valid |
215 | } // eof checksum valid |
200 | else |
216 | else |
201 | { // checksum is invalid |
217 | { // checksum is invalid |
202 | pRxBuff->Position = 0; // reset rxd buffer position |
- | |
203 | pRxBuff->Locked = FALSE; // unlock rxd buffer |
218 | Buffer_Clear(pRxBuff); |
204 | } // eof checksum invalid |
219 | } // eof checksum invalid |
205 | } // eof termination character received |
220 | } // eof termination character received |
206 | } // rxd buffer not full |
221 | } // rxd buffer not full |
207 | else // rxd buffer overrun |
222 | else // rxd buffer overrun |
208 | { |
223 | { |
209 | pRxBuff->Position = 0; // reset rxd buffer pointer |
- | |
210 | pRxBuff->Locked = FALSE; // unlock rxd buffer |
224 | Buffer_Clear(pRxBuff); |
211 | } // eof rxd buffer overrrun |
225 | } // eof rxd buffer overrrun |
212 | } |
226 | } |
213 | } |
227 | } |
214 | 228 | ||
215 | /**************************************************************/ |
229 | /**************************************************************/ |
216 | /* Decode data */ |
230 | /* Decode data */ |
217 | /**************************************************************/ |
231 | /**************************************************************/ |
218 | void MKProtocol_DecodeSerialFrame(Buffer_t* pRxBuff, SerialMsg_t* pSerialMsg) |
232 | void MKProtocol_DecodeSerialFrame(Buffer_t* pRxBuff, SerialMsg_t* pSerialMsg) |
219 | { |
233 | { |
220 | u8 a,b,c,d; |
234 | u8 a,b,c,d; |
221 | u8 x,y,z; |
235 | u8 x,y,z; |
222 | u8 ptrIn = 3; // start with first data byte in rx buffer |
236 | u8 ptrIn = 3; // start with first data byte in rx buffer |
223 | u8 ptrOut = 3; |
237 | u8 ptrOut = 3; |
224 | u8 len = pRxBuff->Position - 6; // must be a multiple of 4 (3 bytes at begin and 3 bytes at end are no payload ) |
238 | u8 len = pRxBuff->DataBytes - 6; // must be a multiple of 4 (3 bytes at begin and 3 bytes at end are no payload ) |
225 | while(len) |
239 | while(len) |
226 | { |
240 | { |
227 | a = pRxBuff->pData[ptrIn++] - '='; |
241 | a = pRxBuff->pData[ptrIn++] - '='; |
228 | b = pRxBuff->pData[ptrIn++] - '='; |
242 | b = pRxBuff->pData[ptrIn++] - '='; |
229 | c = pRxBuff->pData[ptrIn++] - '='; |
243 | c = pRxBuff->pData[ptrIn++] - '='; |
230 | d = pRxBuff->pData[ptrIn++] - '='; |
244 | d = pRxBuff->pData[ptrIn++] - '='; |
231 | //if(ptrIn > ReceivedBytes - 3) break; |
245 | //if(ptrIn > ReceivedBytes - 3) break; |
232 | 246 | ||
233 | x = (a << 2) | (b >> 4); |
247 | x = (a << 2) | (b >> 4); |
234 | y = ((b & 0x0f) << 4) | (c >> 2); |
248 | y = ((b & 0x0f) << 4) | (c >> 2); |
235 | z = ((c & 0x03) << 6) | d; |
249 | z = ((c & 0x03) << 6) | d; |
236 | 250 | ||
237 | if(len--) pRxBuff->pData[ptrOut++] = x; else break; |
251 | if(len--) pRxBuff->pData[ptrOut++] = x; else break; |
238 | if(len--) pRxBuff->pData[ptrOut++] = y; else break; |
252 | if(len--) pRxBuff->pData[ptrOut++] = y; else break; |
239 | if(len--) pRxBuff->pData[ptrOut++] = z; else break; |
253 | if(len--) pRxBuff->pData[ptrOut++] = z; else break; |
240 | } |
254 | } |
241 | pSerialMsg->Address = pRxBuff->pData[1] - 'a'; |
255 | pSerialMsg->Address = pRxBuff->pData[1] - 'a'; |
242 | pSerialMsg->CmdID = pRxBuff->pData[2]; |
256 | pSerialMsg->CmdID = pRxBuff->pData[2]; |
243 | pSerialMsg->pData = &(pRxBuff->pData[3]); |
257 | pSerialMsg->pData = &(pRxBuff->pData[3]); |
244 | pSerialMsg->DataLen = ptrOut - 3; // return number of data in bytes |
258 | pSerialMsg->DataLen = ptrOut - 3; // return number of data in bytes |
- | 259 | pRxBuff->Position = 0; |
|
- | 260 | pRxBuff->DataBytes = ptrOut; |
|
245 | } |
261 | } |
246 | 262 |