Rev 349 | Rev 378 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
110 | killagreg | 1 | /*#######################################################################################*/ |
2 | /* !!! THIS IS NOT FREE SOFTWARE !!! */ |
||
3 | /*#######################################################################################*/ |
||
4 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
5 | // + www.MikroKopter.com |
||
6 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
360 | holgerb | 7 | // + Software Nutzungsbedingungen (english version: see below) |
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 |
||
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. |
||
12 | // + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig. |
||
110 | killagreg | 13 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
360 | holgerb | 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. |
||
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. |
||
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. |
||
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 |
||
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. |
||
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 | // + 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. |
||
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. |
||
110 | killagreg | 31 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
360 | holgerb | 32 | // + Software LICENSING TERMS |
110 | killagreg | 33 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
360 | holgerb | 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 |
||
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. |
||
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 |
||
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 |
||
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 |
||
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 |
||
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. |
||
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. |
||
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. |
||
52 | // + *) The territory aspect only refers to the place where the Software is used, not its programmed range. |
||
53 | // + #### END OF LICENSING TERMS #### |
||
54 | // + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de. |
||
110 | killagreg | 55 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
56 | #include <stdarg.h> |
||
57 | #include "91x_lib.h" |
||
58 | #include "mkprotocol.h" |
||
59 | #include "ramfunc.h" |
||
60 | #include "usb.h" |
||
61 | |||
62 | /**************************************************************/ |
||
63 | /* Create serial output frame */ |
||
64 | /**************************************************************/ |
||
245 | killagreg | 65 | u8 MKProtocol_CreateSerialFrame(Buffer_t* pTxBuff, u8 CmdID, u8 Address, u8 numofbuffers , ...) //u8 *data, u8 len, .... |
110 | killagreg | 66 | { |
67 | va_list ap; |
||
68 | |||
69 | u8 a,b,c; |
||
349 | ingob | 70 | u16 ptr = 0; |
110 | killagreg | 71 | u16 tmpCRC = 0, i; |
72 | |||
73 | u8* pdata = NULL; |
||
74 | int len = 0; |
||
75 | |||
245 | killagreg | 76 | if(pTxBuff->Locked == TRUE) return(0); |
77 | |||
78 | // tx-buffer is not in use |
||
79 | // lock the buffer |
||
80 | pTxBuff->Locked = TRUE; |
||
81 | pTxBuff->Position = 0; |
||
82 | pTxBuff->pData[pTxBuff->Position++] = '#'; // Start character |
||
83 | pTxBuff->pData[pTxBuff->Position++] = 'a' + Address; // Address (a=0; b=1,...) |
||
84 | pTxBuff->pData[pTxBuff->Position++] = CmdID; // Command |
||
85 | |||
86 | va_start(ap, numofbuffers); |
||
87 | if(numofbuffers) |
||
88 | { |
||
89 | pdata = va_arg(ap, u8*); |
||
90 | len = va_arg(ap, int); |
||
91 | ptr = 0; |
||
92 | numofbuffers--; |
||
93 | } |
||
94 | while(len) |
||
95 | { |
||
96 | if(len) |
||
110 | killagreg | 97 | { |
245 | killagreg | 98 | a = pdata[ptr++]; |
99 | len--; |
||
100 | if((!len) && numofbuffers) // try to jump to next buffer |
||
101 | { |
||
102 | pdata = va_arg(ap, u8*); |
||
103 | len = va_arg(ap, int); |
||
104 | ptr = 0; |
||
105 | numofbuffers--; |
||
106 | } |
||
110 | killagreg | 107 | } |
245 | killagreg | 108 | else a = 0; |
109 | if(len) |
||
110 | killagreg | 110 | { |
245 | killagreg | 111 | b = pdata[ptr++]; |
112 | len--; |
||
113 | if((!len) && numofbuffers) // try to jump to next buffer |
||
110 | killagreg | 114 | { |
245 | killagreg | 115 | pdata = va_arg(ap, u8*); |
116 | len = va_arg(ap, int); |
||
117 | ptr = 0; |
||
118 | numofbuffers--; |
||
110 | killagreg | 119 | } |
245 | killagreg | 120 | } |
121 | else b = 0; |
||
122 | if(len) |
||
123 | { |
||
124 | c = pdata[ptr++]; |
||
125 | len--; |
||
126 | if((!len) && numofbuffers) // try to jump to next buffer |
||
110 | killagreg | 127 | { |
245 | killagreg | 128 | pdata = va_arg(ap, u8*); |
129 | len = va_arg(ap, int); |
||
130 | ptr = 0; |
||
131 | numofbuffers--; |
||
110 | killagreg | 132 | } |
133 | } |
||
245 | killagreg | 134 | else c = 0; |
135 | pTxBuff->pData[pTxBuff->Position++] = '=' + (a >> 2); |
||
136 | pTxBuff->pData[pTxBuff->Position++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4)); |
||
137 | pTxBuff->pData[pTxBuff->Position++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6)); |
||
138 | pTxBuff->pData[pTxBuff->Position++] = '=' + ( c & 0x3f); |
||
139 | } |
||
140 | va_end(ap); |
||
141 | // add crc |
||
142 | for(i = 0; i < pTxBuff->Position; i++) |
||
143 | { |
||
144 | tmpCRC += pTxBuff->pData[i]; |
||
145 | } |
||
146 | tmpCRC %= 4096; |
||
147 | pTxBuff->pData[pTxBuff->Position++] = '=' + tmpCRC / 64; |
||
148 | pTxBuff->pData[pTxBuff->Position++] = '=' + tmpCRC % 64; |
||
149 | pTxBuff->pData[pTxBuff->Position++] = '\r'; |
||
150 | pTxBuff->DataBytes = pTxBuff->Position; |
||
151 | pTxBuff->Position = 0; // reset buffer position for transmision |
||
152 | return(pTxBuff->Locked); |
||
110 | killagreg | 153 | } |
154 | |||
155 | // typical called in an UART Rx ISR |
||
156 | /**************************************************************/ |
||
157 | /* Collect serial frame */ |
||
158 | /**************************************************************/ |
||
153 | killagreg | 159 | u8 MKProtocol_CollectSerialFrame(Buffer_t* pRxBuff, u8 c) |
110 | killagreg | 160 | { |
161 | if(pRxBuff->Locked == FALSE) |
||
162 | { // rx buffer not locked |
||
113 | killagreg | 163 | if(c == '#') // if syncronisation character is received |
164 | { |
||
165 | pRxBuff->Position = 0; // reset buffer |
||
166 | pRxBuff->pData[pRxBuff->Position++] = c; // copy 1st byte to buffer |
||
167 | pRxBuff->DataBytes = 1; |
||
168 | } |
||
169 | else if (pRxBuff->Position < pRxBuff->Size) // rx buffer not full |
||
170 | { |
||
190 | killagreg | 171 | pRxBuff->pData[pRxBuff->Position++] = c; // copy byte to rxd buffer |
172 | pRxBuff->DataBytes++; |
||
244 | killagreg | 173 | // termination character received and sync has been established |
174 | if ((c == '\r') && (pRxBuff->pData[0]== '#')) |
||
113 | killagreg | 175 | { |
176 | // calculate checksum from transmitted data |
||
177 | u16 crc = 0, i; |
||
178 | u8 crc1, crc2; |
||
190 | killagreg | 179 | for(i = 0; i < (pRxBuff->Position-3); i++) |
110 | killagreg | 180 | { |
113 | killagreg | 181 | crc += pRxBuff->pData[i]; |
182 | } |
||
183 | crc %= 4096; |
||
184 | crc1 = '=' + crc / 64; |
||
185 | crc2 = '=' + crc % 64; |
||
186 | // compare checksum to transmitted checksum bytes |
||
190 | killagreg | 187 | if((crc1 == pRxBuff->pData[pRxBuff->Position-3]) && (crc2 == pRxBuff->pData[pRxBuff->Position-2])) |
244 | killagreg | 188 | { |
189 | // checksum is valid |
||
113 | killagreg | 190 | pRxBuff->Position = 0; |
191 | pRxBuff->Locked = TRUE; // lock the rxd buffer |
||
192 | // if 2nd byte is an 'R' start bootloader |
||
193 | if(pRxBuff->pData[2] == 'R') |
||
194 | { |
||
195 | PowerOff(); |
||
196 | VIC_DeInit(); |
||
197 | Execute_Bootloader(); // Reset-Commando - Bootloader starten |
||
198 | } |
||
199 | } // eof checksum valid |
||
200 | else |
||
201 | { // checksum is invalid |
||
202 | Buffer_Clear(pRxBuff); |
||
203 | } // eof checksum invalid |
||
204 | } // eof termination character received |
||
205 | } // rxd buffer not full |
||
206 | else // rxd buffer overrun |
||
207 | { |
||
208 | Buffer_Clear(pRxBuff); |
||
275 | killagreg | 209 | } // eof rxd buffer overrun |
110 | killagreg | 210 | } |
153 | killagreg | 211 | return(pRxBuff->Locked); |
110 | killagreg | 212 | } |
213 | |||
214 | /**************************************************************/ |
||
190 | killagreg | 215 | /* Decode detination address */ |
216 | /**************************************************************/ |
||
217 | void MKProtocol_DecodeSerialFrameHeader(Buffer_t* pRxBuff, SerialMsg_t* pSerialMsg) |
||
218 | { |
||
219 | if(pRxBuff->Locked) |
||
220 | { |
||
221 | pSerialMsg->Address = pRxBuff->pData[1] - 'a'; |
||
222 | pSerialMsg->CmdID = pRxBuff->pData[2]; |
||
223 | } |
||
224 | else |
||
225 | { |
||
226 | pSerialMsg->Address = 0; |
||
227 | pSerialMsg->CmdID = ' '; |
||
228 | } |
||
229 | } |
||
230 | |||
231 | /**************************************************************/ |
||
110 | killagreg | 232 | /* Decode data */ |
233 | /**************************************************************/ |
||
190 | killagreg | 234 | void MKProtocol_DecodeSerialFrameData(Buffer_t* pRxBuff, SerialMsg_t* pSerialMsg) |
110 | killagreg | 235 | { |
236 | u8 a,b,c,d; |
||
237 | u8 x,y,z; |
||
238 | u8 ptrIn = 3; // start with first data byte in rx buffer |
||
239 | u8 ptrOut = 3; |
||
112 | killagreg | 240 | u8 len = pRxBuff->DataBytes - 6; // must be a multiple of 4 (3 bytes at begin and 3 bytes at end are no payload ) |
110 | killagreg | 241 | while(len) |
242 | { |
||
243 | a = pRxBuff->pData[ptrIn++] - '='; |
||
244 | b = pRxBuff->pData[ptrIn++] - '='; |
||
245 | c = pRxBuff->pData[ptrIn++] - '='; |
||
246 | d = pRxBuff->pData[ptrIn++] - '='; |
||
247 | //if(ptrIn > ReceivedBytes - 3) break; |
||
248 | |||
249 | x = (a << 2) | (b >> 4); |
||
250 | y = ((b & 0x0f) << 4) | (c >> 2); |
||
251 | z = ((c & 0x03) << 6) | d; |
||
252 | |||
253 | if(len--) pRxBuff->pData[ptrOut++] = x; else break; |
||
254 | if(len--) pRxBuff->pData[ptrOut++] = y; else break; |
||
255 | if(len--) pRxBuff->pData[ptrOut++] = z; else break; |
||
256 | } |
||
257 | pSerialMsg->pData = &(pRxBuff->pData[3]); |
||
258 | pSerialMsg->DataLen = ptrOut - 3; // return number of data in bytes |
||
112 | killagreg | 259 | pRxBuff->Position = 0; |
260 | pRxBuff->DataBytes = ptrOut; |
||
110 | killagreg | 261 | } |